Example usage for com.google.common.collect Sets newConcurrentHashSet

List of usage examples for com.google.common.collect Sets newConcurrentHashSet

Introduction

In this page you can find the example usage for com.google.common.collect Sets newConcurrentHashSet.

Prototype

public static <E> Set<E> newConcurrentHashSet() 

Source Link

Document

Creates a thread-safe set backed by a hash map.

Usage

From source file:org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler.java

/**
 * Installs static ARP rules used in ARP BROAD_CAST mode.
 *
 * @param gateway gateway node//from  w w w . j  a v a  2 s.  c  o m
 * @param install flow rule installation flag
 */
private void setFloatingIpArpRuleForGateway(OpenstackNode gateway, boolean install) {
    if (arpMode.equals(ARP_BROADCAST_MODE)) {

        Set<OpenstackNode> completedGws = osNodeService.completeNodes(GATEWAY);
        Set<OpenstackNode> finalGws = Sets.newConcurrentHashSet();
        finalGws.addAll(ImmutableSet.copyOf(completedGws));

        if (install) {
            if (completedGws.contains(gateway)) {
                if (completedGws.size() > 1) {
                    finalGws.remove(gateway);
                    osRouterService.floatingIps().forEach(fip -> {
                        if (fip.getPortId() != null) {
                            setFloatingIpArpRule(fip, finalGws, false);
                            finalGws.add(gateway);
                        }
                    });
                }
                osRouterService.floatingIps().forEach(fip -> {
                    if (fip.getPortId() != null) {
                        setFloatingIpArpRule(fip, finalGws, true);
                    }
                });
            } else {
                log.warn("Detected node should be included in completed gateway set");
            }
        } else {
            if (!completedGws.contains(gateway)) {
                finalGws.add(gateway);
                osRouterService.floatingIps().forEach(fip -> {
                    if (fip.getPortId() != null) {
                        setFloatingIpArpRule(fip, finalGws, false);
                    }
                });
                finalGws.remove(gateway);
                if (completedGws.size() >= 1) {
                    osRouterService.floatingIps().forEach(fip -> {
                        if (fip.getPortId() != null) {
                            setFloatingIpArpRule(fip, finalGws, true);
                        }
                    });
                }
            } else {
                log.warn("Detected node should NOT be included in completed gateway set");
            }
        }
    }
}

From source file:com.vmware.photon.controller.scheduler.xenon.task.PlacementTaskService.java

/**
 * Asks the host candidates to score the resource request by sending a "place" request via thrift.
 *
 * This is asynchronous: the response is returned via an a completion routine. That response will
 * provide two things://from   www. jav  a2s.  c o m
 * - The set of "okResponses", from hosts that could accept the resource. This will include the score.
 * - All responses. These are used when there's an error, to summarize what went wrong
 *
 * @param resource
 * @param candidates
 * @param completion
 */
private void queryHostsForScores(Resource resource, Map<String, ServerAddress> candidates,
        ScoreResultsCompletion completion) {

    final int numCandidates = candidates.size();
    final Set<PlaceResponse> okResponses = Sets.newConcurrentHashSet();
    final Set<PlaceResponse> allResponses = Sets.newConcurrentHashSet();
    final AtomicInteger resultCount = new AtomicInteger(0);

    final String requestId = LoggingUtils.getRequestId();
    for (Map.Entry<String, ServerAddress> entry : candidates.entrySet()) {
        ServerAddress address = entry.getValue();
        try {
            // The thrift "place" request is a request to get a score from the host indicating how good
            // of a match the host is for the resource request.
            // Note that the place() call has an embedded timeout (currently 60 seconds): we'll get a
            // timeout exception when it fails.
            HostClient hostClient = ((HostClientProvider) getHost()).getHostClient();
            hostClient.setIpAndPort(address.getHost(), address.getPort());
            hostClient.place(resource, new AsyncMethodCallback<Host.AsyncSSLClient.place_call>() {
                @Override
                public void onComplete(Host.AsyncSSLClient.place_call call) {
                    if (requestId != null) {
                        // We have to do more work here than normal: the PlaceResponse
                        // doesn't have the request ID and we're in a new thread, so we
                        // need to set it correctly for both Xenon (ServiceUtils.log*) and regular
                        // logging.
                        LoggingUtils.setRequestId(requestId);
                        UtilsHelper.setThreadContextId(requestId);
                    }
                    PlaceResponse response;
                    try {
                        response = call.getResult();
                    } catch (TException ex) {
                        onError(ex);
                        return;
                    }
                    ServiceUtils.logInfo(PlacementTaskService.this, "Received a place response from %s: %s",
                            entry, response);
                    if (response.getAddress() == null) {
                        response.setAddress(address);
                    }
                    allResponses.add(response);
                    if (response.getResult() == PlaceResultCode.OK) {
                        okResponses.add(response);
                    }
                    if (resultCount.addAndGet(1) == numCandidates) {
                        completion.handle(okResponses, allResponses);
                    }
                }

                @Override
                public void onError(Exception ex) {
                    if (requestId != null) {
                        // See comments above in onComplete()
                        LoggingUtils.setRequestId(requestId);
                        UtilsHelper.setThreadContextId(requestId);
                    }
                    ServiceUtils.logWarning(PlacementTaskService.this,
                            "Failed to get a placement response from %s: %s", entry, ex);
                    PlaceResponse errorResponse = new PlaceResponse();
                    errorResponse.setResult(PlaceResultCode.SYSTEM_ERROR);
                    errorResponse.setError(String.format("Failed to get a placement response from %s: %s",
                            entry, ex.getMessage()));
                    if (errorResponse.getAddress() == null) {
                        errorResponse.setAddress(address);
                    }
                    allResponses.add(errorResponse);
                    if (resultCount.addAndGet(1) == numCandidates) {
                        completion.handle(okResponses, allResponses);
                    }
                }
            });
        } catch (RpcException ex) {
            ServiceUtils.logWarning(PlacementTaskService.this, "Failed to send placement request to %s: %s",
                    entry, ex);
            PlaceResponse errorResponse = new PlaceResponse();
            errorResponse.setAddress(entry.getValue());
            errorResponse.setResult(PlaceResultCode.SYSTEM_ERROR);
            errorResponse.setError(
                    String.format("Failed to send placement request to %s: %s", entry, ex.getMessage()));
            allResponses.add(errorResponse);
            if (resultCount.addAndGet(1) == numCandidates) {
                completion.handle(okResponses, allResponses);
            }
        }
    }
}

From source file:com.vmware.photon.controller.rootscheduler.xenon.task.PlacementTaskService.java

/**
 * Asks the host candidates to score the resource request by sending a "place" request via thrift.
 *
 * This is asynchronous: the response is returned via an a completion routine. That response will
 * provide two things://www  .j  a va2s . c om
 * - The set of "okResponses", from hosts that could accept the resource. This will include the score.
 * - All responses. These are used when there's an error, to summarize what went wrong
 *
 * @param resource
 * @param candidates
 * @param completion
 */
private void queryHostsForScores(Resource resource, Map<String, ServerAddress> candidates,
        ScoreResultsCompletion completion) {

    final int numCandidates = candidates.size();
    final Set<PlaceResponse> okResponses = Sets.newConcurrentHashSet();
    final Set<PlaceResponse> allResponses = Sets.newConcurrentHashSet();
    final AtomicInteger resultCount = new AtomicInteger(0);

    final String requestId = LoggingUtils.getRequestId();
    for (Map.Entry<String, ServerAddress> entry : candidates.entrySet()) {
        ServerAddress address = entry.getValue();
        try {
            // The thrift "place" request is a request to get a score from the host indicating how good
            // of a match the host is for the resource request.
            // Note that the place() call has an embedded timeout (currently 60 seconds): we'll get a
            // timeout exception when it fails.
            HostClient hostClient = ((HostClientProvider) getHost()).getHostClient();
            hostClient.setIpAndPort(address.getHost(), address.getPort());
            hostClient.place(resource, new AsyncMethodCallback<Host.AsyncClient.place_call>() {
                @Override
                public void onComplete(Host.AsyncClient.place_call call) {
                    if (requestId != null) {
                        // We have to do more work here than normal: the PlaceResponse
                        // doesn't have the request ID and we're in a new thread, so we
                        // need to set it correctly for both Xenon (ServiceUtils.log*) and regular
                        // logging.
                        LoggingUtils.setRequestId(requestId);
                        UtilsHelper.setThreadContextId(requestId);
                    }
                    PlaceResponse response;
                    try {
                        response = call.getResult();
                    } catch (TException ex) {
                        onError(ex);
                        return;
                    }
                    ServiceUtils.logInfo(PlacementTaskService.this, "Received a place response from %s: %s",
                            entry, response);
                    if (response.getAddress() == null) {
                        response.setAddress(address);
                    }
                    allResponses.add(response);
                    if (response.getResult() == PlaceResultCode.OK) {
                        okResponses.add(response);
                    }
                    if (resultCount.addAndGet(1) == numCandidates) {
                        completion.handle(okResponses, allResponses);
                    }
                }

                @Override
                public void onError(Exception ex) {
                    if (requestId != null) {
                        // See comments above in onComplete()
                        LoggingUtils.setRequestId(requestId);
                        UtilsHelper.setThreadContextId(requestId);
                    }
                    ServiceUtils.logWarning(PlacementTaskService.this,
                            "Failed to get a placement response from %s: %s", entry, ex);
                    PlaceResponse errorResponse = new PlaceResponse();
                    errorResponse.setResult(PlaceResultCode.SYSTEM_ERROR);
                    errorResponse.setError(String.format("Failed to get a placement response from %s: %s",
                            entry, ex.getMessage()));
                    if (errorResponse.getAddress() == null) {
                        errorResponse.setAddress(address);
                    }
                    allResponses.add(errorResponse);
                    if (resultCount.addAndGet(1) == numCandidates) {
                        completion.handle(okResponses, allResponses);
                    }
                }
            });
        } catch (RpcException ex) {
            ServiceUtils.logWarning(PlacementTaskService.this, "Failed to send placement request to %s: %s",
                    entry, ex);
            PlaceResponse errorResponse = new PlaceResponse();
            errorResponse.setAddress(entry.getValue());
            errorResponse.setResult(PlaceResultCode.SYSTEM_ERROR);
            errorResponse.setError(
                    String.format("Failed to send placement request to %s: %s", entry, ex.getMessage()));
            allResponses.add(errorResponse);
            if (resultCount.addAndGet(1) == numCandidates) {
                completion.handle(okResponses, allResponses);
            }
        }
    }
}

From source file:oims.dataBase.MYSQL.Db_dataBase_MYSQL.java

@Override
public Boolean copyTable(Db_dataBase cpyTo, Db_table copyTable) {
    Boolean result = Boolean.FALSE;
    if (cpyTo.getType() != db_type.MYSQL || this.curConnection_ == null) {
        return result;
    }/*from w  ww . j a v  a  2s. co m*/
    Db_dataBase_MYSQL mysqlDataBase = (Db_dataBase_MYSQL) cpyTo;

    // Make a direct copy from this DataBase to dst dataBase
    String query = "select * from " + copyTable.getName();
    try (Statement sm = curConnection_.createStatement()) {
        ResultSet resultSet = sm.executeQuery(query);
        if (resultSet != null) {
            if (mysqlDataBase.truncateTable(copyTable)) {
                ResultSetMetaData data = resultSet.getMetaData();
                int colCnt = data.getColumnCount();
                String coln = " (";
                for (int i = 1; i <= colCnt; i++) {
                    coln += " " + data.getColumnName(i) + (i == colCnt ? "" : ", ");
                }
                coln += ") ";
                resultSet.first();
                Set<String> querySet = Sets.newConcurrentHashSet();
                do {
                    String buildSql = " (";
                    for (int i = 1; i <= colCnt; i++) {
                        buildSql += " '" + resultSet.getString(i) + (i == colCnt ? "'" : "', ");
                    }
                    buildSql += " )";
                    query = "insert into " + copyTable.getName() + coln + " values " + buildSql + " ;";
                    querySet.add(query);
                } while (resultSet.next());
                if (mysqlDataBase.batchExecute(querySet)) {
                    result = Boolean.TRUE;
                }
            }
        }
    } catch (SQLException ex) {
        Logger.getLogger(Db_dataBase_MYSQL.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}

From source file:com.facebook.buck.core.build.engine.impl.CachingBuildEngine.java

@Override
public int getNumRulesToBuild(Iterable<BuildRule> rules) {
    Set<BuildRule> seen = Sets.newConcurrentHashSet();
    ImmutableList.Builder<ListenableFuture<?>> results = ImmutableList.builder();
    for (BuildRule rule : rules) {
        if (seen.add(rule)) {
            results.add(walkRule(rule, seen));
        }/* ww  w  .  j  a v a  2  s  .c  om*/
    }
    Futures.getUnchecked(Futures.allAsList(results.build()));
    return seen.size();
}

From source file:org.onosproject.store.link.impl.GossipLinkStore.java

/**
 * Creates concurrent readable, synchronized HashMultimap.
 *
 * @return SetMultimap/*from www . ja  v a 2  s.  co  m*/
 */
private static <K, V> SetMultimap<K, V> createSynchronizedHashMultiMap() {
    return synchronizedSetMultimap(
            Multimaps.newSetMultimap(new ConcurrentHashMap<>(), () -> Sets.newConcurrentHashSet()));
}

From source file:com.linkedin.pinot.routing.HelixExternalViewBasedRouting.java

private void buildRoutingTable(String tableName, ExternalView externalView,
        List<InstanceConfig> instanceConfigs) {
    // Save the current version number of the external view to avoid unnecessary routing table updates
    int externalViewRecordVersion = externalView.getRecord().getVersion();
    _lastKnownExternalViewVersionMap.put(tableName, externalViewRecordVersion);

    RoutingTableBuilder routingTableBuilder;
    CommonConstants.Helix.TableType tableType = TableNameBuilder.getTableTypeFromTableName(tableName);

    // Pick the appropriate routing table builder based on the table type
    if (CommonConstants.Helix.TableType.REALTIME.equals(tableType)) {
        routingTableBuilder = _realtimeHLCRoutingTableBuilder;
    } else {/*w w  w. j a v a2  s  .  com*/
        if (isLargeCluster(externalView)) {
            routingTableBuilder = _largeClusterRoutingTableBuilder;
        } else {
            routingTableBuilder = _smallClusterRoutingTableBuilder;
        }
    }

    LOGGER.info("Trying to compute routing table for table {} using {}", tableName, routingTableBuilder);
    long startTimeMillis = System.currentTimeMillis();

    try {
        Map<String, InstanceConfig> relevantInstanceConfigs = new HashMap<>();

        // Build a list of routing tables
        List<ServerToSegmentSetMap> serverToSegmentSetMap = routingTableBuilder
                .computeRoutingTableFromExternalView(tableName, externalView, instanceConfigs);

        // Keep track of the instance configs that are used in that routing table
        updateInstanceConfigsMapFromExternalView(relevantInstanceConfigs, instanceConfigs, externalView);

        _brokerRoutingTable.put(tableName, serverToSegmentSetMap);

        // If this is a realtime table, also build a LLC routing table
        if (CommonConstants.Helix.TableType.REALTIME.equals(tableType)) {
            _routingTableSelector.registerTable(tableName);

            try {
                // Build the routing table
                List<ServerToSegmentSetMap> llcserverToSegmentSetMap = _realtimeLLCRoutingTableBuilder
                        .computeRoutingTableFromExternalView(tableName, externalView, instanceConfigs);

                // Keep track of the instance configs that are used in that routing table
                updateInstanceConfigsMapFromExternalView(relevantInstanceConfigs, instanceConfigs,
                        externalView);

                _llcBrokerRoutingTable.put(tableName, llcserverToSegmentSetMap);
            } catch (Exception e) {
                LOGGER.error("Failed to compute LLC routing table for {}. Ignoring", tableName, e);
            }
        }

        // Save the instance configs used so that we can avoid unnecessary routing table updates later
        _lastKnownInstanceConfigsForTable.put(tableName, relevantInstanceConfigs);
        for (InstanceConfig instanceConfig : relevantInstanceConfigs.values()) {
            _lastKnownInstanceConfigs.put(instanceConfig.getInstanceName(), instanceConfig);
        }

        // Ensure this table is registered with all relevant instances
        for (String instanceName : relevantInstanceConfigs.keySet()) {
            Set<String> tablesForCurrentInstance = _tablesForInstance.get(instanceName);

            // Ensure there is a table set for this instance
            if (tablesForCurrentInstance == null) {
                synchronized (_tablesForInstance) {
                    if (!_tablesForInstance.containsKey(instanceName)) {
                        tablesForCurrentInstance = Sets.newConcurrentHashSet();
                        _tablesForInstance.put(instanceName, tablesForCurrentInstance);
                    } else {
                        // Another thread has created a table set for this instance, use it
                        tablesForCurrentInstance = _tablesForInstance.get(instanceName);
                    }
                }
            }

            // Add the table to the set of tables for this instance
            tablesForCurrentInstance.add(tableName);
        }
    } catch (Exception e) {
        _brokerMetrics.addMeteredTableValue(tableName, BrokerMeter.ROUTING_TABLE_REBUILD_FAILURES, 1L);
        LOGGER.error("Failed to compute/update the routing table", e);

        // Mark the routing table as needing a rebuild
        _lastKnownExternalViewVersionMap.put(tableName, INVALID_EXTERNAL_VIEW_VERSION);
    }

    try {
        // We need to compute the time boundary only in two situations:
        // 1) We're adding/updating an offline table and there's a realtime table that we're serving
        // 2) We're adding a new realtime table and there's already an offline table, in which case we need to update the
        //    time boundary for the existing offline table
        String tableForTimeBoundaryUpdate = null;
        ExternalView externalViewForTimeBoundaryUpdate = null;

        if (tableType == CommonConstants.Helix.TableType.OFFLINE) {
            // Does a realtime table exist?
            String realtimeTableName = TableNameBuilder.REALTIME_TABLE_NAME_BUILDER
                    .forTable(TableNameBuilder.extractRawTableName(tableName));
            if (_brokerRoutingTable.containsKey(realtimeTableName)) {
                tableForTimeBoundaryUpdate = tableName;
                externalViewForTimeBoundaryUpdate = externalView;
            }
        }

        if (tableType == CommonConstants.Helix.TableType.REALTIME) {
            // Does an offline table exist?
            String offlineTableName = TableNameBuilder.OFFLINE_TABLE_NAME_BUILDER
                    .forTable(TableNameBuilder.extractRawTableName(tableName));
            if (_brokerRoutingTable.containsKey(offlineTableName)) {
                // Is there no time boundary?
                if (_timeBoundaryService.getTimeBoundaryInfoFor(offlineTableName) == null) {
                    tableForTimeBoundaryUpdate = offlineTableName;
                    externalViewForTimeBoundaryUpdate = fetchExternalView(offlineTableName);
                }
            }
        }

        if (tableForTimeBoundaryUpdate != null) {
            updateTimeBoundary(tableForTimeBoundaryUpdate, externalViewForTimeBoundaryUpdate);
        } else {
            LOGGER.info("No need to update time boundary for table {}", tableName);
        }
    } catch (Exception e) {
        LOGGER.error("Failed to update the TimeBoundaryService", e);
    }

    long updateTime = System.currentTimeMillis() - startTimeMillis;

    if (_brokerMetrics != null) {
        _brokerMetrics.addTimedValue(BrokerTimer.ROUTING_TABLE_UPDATE_TIME, updateTime, TimeUnit.MILLISECONDS);
    }

    LOGGER.info("Routing table update for table {} completed in {} ms", tableName, updateTime);
}

From source file:com.taobao.android.PatchMethodTool.java

public static void modifyMethod(String srcDexFile, String outDexFile, boolean isAndFix) throws IOException {

    DexFile dexFile = DexFileFactory.loadDexFile(srcDexFile, 15, true);

    final Set<ClassDef> classes = Sets.newConcurrentHashSet();

    for (ClassDef classDef : dexFile.getClasses()) {
        Set<Method> methods = Sets.newConcurrentHashSet();
        boolean modifiedMethod = false;
        for (Method method : classDef.getMethods()) {
            MethodImplementation implementation = method.getImplementation();
            if (implementation != null && (methodNeedsModification(classDef, method, isAndFix))) {
                modifiedMethod = true;/*w ww.  j a v a2 s  .  c o  m*/
                methods.add(new ImmutableMethod(method.getDefiningClass(), method.getName(),
                        method.getParameters(), method.getReturnType(), method.getAccessFlags(),
                        method.getAnnotations(), isAndFix ? modifyMethodAndFix(implementation, method)
                                : modifyMethodTpatch(implementation, method)));
            } else {
                methods.add(method);
            }
        }
        if (!modifiedMethod) {
            classes.add(classDef);
        } else {
            classes.add(new ImmutableClassDef(classDef.getType(), classDef.getAccessFlags(),
                    classDef.getSuperclass(), classDef.getInterfaces(), classDef.getSourceFile(),
                    classDef.getAnnotations(), classDef.getFields(), methods));
        }

    }

    DexFileFactory.writeDexFile(outDexFile, new DexFile() {
        @Nonnull
        @Override
        public Set<? extends ClassDef> getClasses() {
            return new AbstractSet<ClassDef>() {
                @Nonnull
                @Override
                public Iterator<ClassDef> iterator() {
                    return classes.iterator();
                }

                @Override
                public int size() {
                    return classes.size();
                }
            };
        }
    });
}

From source file:com.taobao.android.tools.PatchMethodTool.java

public static void modifyMethod(String srcDexFile, String outDexFile, boolean isAndFix) throws IOException {

    DexFile dexFile = DexFileFactory.loadDexFile(srcDexFile, Opcodes.getDefault());

    final Set<ClassDef> classes = Sets.newConcurrentHashSet();

    for (ClassDef classDef : dexFile.getClasses()) {
        Set<Method> methods = Sets.newConcurrentHashSet();
        boolean modifiedMethod = false;
        for (Method method : classDef.getMethods()) {
            MethodImplementation implementation = method.getImplementation();
            if (implementation != null && (methodNeedsModification(classDef, method, isAndFix))) {
                modifiedMethod = true;/*from  w  w  w .  ja va  2 s  .  c  om*/
                methods.add(new ImmutableMethod(method.getDefiningClass(), method.getName(),
                        method.getParameters(), method.getReturnType(), method.getAccessFlags(),
                        method.getAnnotations(), isAndFix ? modifyMethodAndFix(implementation, method)
                                : modifyMethodTpatch(implementation, method)));
            } else {
                methods.add(method);
            }
        }
        if (!modifiedMethod) {
            classes.add(classDef);
        } else {
            classes.add(new ImmutableClassDef(classDef.getType(), classDef.getAccessFlags(),
                    classDef.getSuperclass(), classDef.getInterfaces(), classDef.getSourceFile(),
                    classDef.getAnnotations(), classDef.getFields(), methods));
        }

    }

    DexFileFactory.writeDexFile(outDexFile, new DexFile() {
        @Nonnull
        @Override
        public Set<? extends ClassDef> getClasses() {
            return new AbstractSet<ClassDef>() {
                @Nonnull
                @Override
                public Iterator<ClassDef> iterator() {
                    return classes.iterator();
                }

                @Override
                public int size() {
                    return classes.size();
                }
            };
        }

        @Nonnull
        @Override
        public Opcodes getOpcodes() {
            return Opcodes.getDefault();
        }
    });
}

From source file:com.google.devtools.build.lib.skyframe.SkyframeBuildView.java

/** Clear the invalidated configured targets detected during loading and analysis phases. */
public void clearInvalidatedConfiguredTargets() {
    dirtiedConfiguredTargetKeys = Sets.newConcurrentHashSet();
    anyConfiguredTargetDeleted = false;/*from w w  w.  j  a  v a  2s  . c  o  m*/
}