Example usage for com.google.common.collect Maps newConcurrentMap

List of usage examples for com.google.common.collect Maps newConcurrentMap

Introduction

In this page you can find the example usage for com.google.common.collect Maps newConcurrentMap.

Prototype

public static <K, V> ConcurrentMap<K, V> newConcurrentMap() 

Source Link

Document

Returns a general-purpose instance of ConcurrentMap , which supports all optional operations of the ConcurrentMap interface.

Usage

From source file:org.apache.drill.exec.store.StoragePluginRegistry.java

@SuppressWarnings("unchecked")
public void init() throws DrillbitStartupException {
    final DrillConfig config = context.getConfig();
    final Collection<Class<? extends StoragePlugin>> pluginClasses = PathScanner.scanForImplementations(
            StoragePlugin.class, config.getStringList(ExecConstants.STORAGE_ENGINE_SCAN_PACKAGES));
    final String lineBrokenList = pluginClasses.size() == 0 ? ""
            : "\n\t- " + Joiner.on("\n\t- ").join(pluginClasses);
    logger.debug("Found {} storage plugin configuration classes: {}.", pluginClasses.size(), lineBrokenList);
    for (Class<? extends StoragePlugin> plugin : pluginClasses) {
        int i = 0;
        for (Constructor<?> c : plugin.getConstructors()) {
            Class<?>[] params = c.getParameterTypes();
            if (params.length != 3 || params[1] != DrillbitContext.class
                    || !StoragePluginConfig.class.isAssignableFrom(params[0]) || params[2] != String.class) {
                logger.info(//from w w w.j  a  v a 2s.  c o  m
                        "Skipping StoragePlugin constructor {} for plugin class {} since it doesn't implement a [constructor(StoragePluginConfig, DrillbitContext, String)]",
                        c, plugin);
                continue;
            }
            availablePlugins.put(params[0], (Constructor<? extends StoragePlugin>) c);
            i++;
        }
        if (i == 0) {
            logger.debug(
                    "Skipping registration of StoragePlugin {} as it doesn't have a constructor with the parameters of (StorangePluginConfig, Config)",
                    plugin.getCanonicalName());
        }
    }

    // create registered plugins defined in "storage-plugins.json"
    this.plugins = Maps.newConcurrentMap();
    this.plugins.putAll(createPlugins());

}

From source file:org.apache.apex.engine.util.CascadeStorageAgent.java

private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
    input.defaultReadObject();/*from   w  ww  . j a v  a 2 s.com*/
    oldOperatorToWindowIdsMap = Maps.newConcurrentMap();
}

From source file:co.cask.cdap.data.stream.service.DistributedStreamService.java

@Inject
public DistributedStreamService(CConfiguration cConf, StreamAdmin streamAdmin,
        StreamCoordinatorClient streamCoordinatorClient, StreamFileJanitorService janitorService,
        ZKClient zkClient, DiscoveryServiceClient discoveryServiceClient, StreamMetaStore streamMetaStore,
        Supplier<Discoverable> discoverableSupplier, StreamWriterSizeCollector streamWriterSizeCollector,
        HeartbeatPublisher heartbeatPublisher, NotificationFeedManager feedManager,
        NotificationService notificationService, MetricStore metricStore) {
    super(streamCoordinatorClient, janitorService, streamWriterSizeCollector, metricStore);
    this.zkClient = zkClient;
    this.streamAdmin = streamAdmin;
    this.notificationService = notificationService;
    this.discoveryServiceClient = discoveryServiceClient;
    this.streamMetaStore = streamMetaStore;
    this.discoverableSupplier = discoverableSupplier;
    this.feedManager = feedManager;
    this.streamWriterSizeCollector = streamWriterSizeCollector;
    this.heartbeatPublisher = heartbeatPublisher;
    this.resourceCoordinatorClient = new ResourceCoordinatorClient(getCoordinatorZKClient());
    this.leaderListeners = Sets.newHashSet();
    this.instanceId = cConf.getInt(Constants.Stream.CONTAINER_INSTANCE_ID);
    this.aggregators = Maps.newConcurrentMap();
}

From source file:org.onosproject.net.flowobjective.impl.composition.FlowObjectiveCompositionManager.java

@Activate
protected void activate() {
    executorService = newFixedThreadPool(4, groupedThreads("onos/objective-installer", "%d"));
    flowObjectiveStore.setDelegate(delegate);
    mastershipService.addListener(mastershipListener);
    deviceService.addListener(deviceListener);
    deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id()));
    deviceCompositionTreeMap = Maps.newConcurrentMap();
    log.info("Started");
}

From source file:org.onosproject.cpman.impl.ControlPlaneMonitor.java

@Override
public void updateMetric(ControlMetric cm, int updateIntervalInMinutes, Optional<DeviceId> deviceId) {
    if (deviceId.isPresent()) {

        // insert a new device entry if we cannot find any
        ctrlMsgBuf.putIfAbsent(deviceId.get(), Maps.newConcurrentMap());

        // update control message metrics
        if (CONTROL_MESSAGE_METRICS.contains(cm.metricType())) {

            if (!availableDeviceIdSet.contains(deviceId.get())) {
                availableDeviceIdSet.add(deviceId.get());
            }/*from w  w w .  ja v a 2  s .  c o  m*/

            // we will accumulate the metric value into buffer first
            ctrlMsgBuf.get(deviceId.get()).putIfAbsent(cm.metricType(), (double) cm.metricValue().getLoad());

            // if buffer contains all control message metrics,
            // we simply set and update the values into MetricsDatabase.
            if (ctrlMsgBuf.get(deviceId.get()).keySet().containsAll(CONTROL_MESSAGE_METRICS)) {
                updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get());
                ctrlMsgBuf.clear();
            }
        }
    } else {

        // update cpu metrics
        if (CPU_METRICS.contains(cm.metricType())) {
            cpuBuf.putIfAbsent(cm.metricType(), (double) cm.metricValue().getLoad());
            if (cpuBuf.keySet().containsAll(CPU_METRICS)) {
                cpuMetrics.updateMetrics(convertMap(cpuBuf));
                cpuBuf.clear();
            }
        }

        // update memory metrics
        if (MEMORY_METRICS.contains(cm.metricType())) {
            memoryBuf.putIfAbsent(cm.metricType(), (double) cm.metricValue().getLoad());
            if (memoryBuf.keySet().containsAll(MEMORY_METRICS)) {
                memoryMetrics.updateMetrics(convertMap(memoryBuf));
                memoryBuf.clear();
            }
        }
    }
}

From source file:io.atomix.protocols.gossip.map.AntiEntropyMapDelegate.java

public AntiEntropyMapDelegate(String name, Serializer entrySerializer, AntiEntropyProtocolConfig config,
        PrimitiveManagementService managementService) {
    this.localMemberId = managementService.getMembershipService().getLocalMember().id();
    this.mapName = name;
    this.entrySerializer = entrySerializer;
    this.serializer = Serializer.using(Namespace.builder().nextId(Namespaces.BEGIN_USER_CUSTOM_ID + 100)
            .register(Namespaces.BASIC).register(LogicalTimestamp.class).register(WallClockTimestamp.class)
            .register(AntiEntropyAdvertisement.class).register(AntiEntropyResponse.class)
            .register(UpdateEntry.class).register(MapValue.class).register(MapValue.Digest.class)
            .register(UpdateRequest.class).register(MemberId.class).build(name + "-anti-entropy-map"));
    this.items = Maps.newConcurrentMap();
    senderPending = Maps.newConcurrentMap();
    destroyedMessage = mapName + ERROR_DESTROYED;

    this.clusterCommunicator = managementService.getCommunicationService();
    this.membershipService = managementService.getMembershipService();

    this.timestampProvider = config.getTimestampProvider();

    List<MemberId> peers = config.getPeers() != null
            ? config.getPeers().stream().map(MemberId::from).collect(Collectors.toList())
            : null;/*from   w ww  .  j  av  a 2 s . c  o  m*/
    this.peersSupplier = () -> peers != null ? peers
            : managementService.getMembershipService().getMembers().stream().map(Member::id).sorted()
                    .collect(Collectors.toList());
    this.bootstrapPeersSupplier = peersSupplier;

    PeerSelector<Map.Entry<K, V>> peerSelector = config.getPeerSelector();
    this.peerUpdateFunction = (entry, m) -> {
        Collection<MemberId> selected = peerSelector.select(entry, m);
        return peersSupplier.get().stream().filter(selected::contains).collect(Collectors.toList());
    };

    this.executor = newFixedThreadPool(8, namedThreads("atomix-anti-entropy-map-" + mapName + "-fg-%d", log));
    this.communicationExecutor = newFixedThreadPool(8,
            namedThreads("atomix-anti-entropy-map-" + mapName + "-publish-%d", log));
    this.backgroundExecutor = newSingleThreadScheduledExecutor(
            namedThreads("atomix-anti-entropy-map-" + mapName + "-bg-%d", log));

    // start anti-entropy thread
    this.backgroundExecutor.scheduleAtFixedRate(this::sendAdvertisement, initialDelaySec,
            config.getAntiEntropyInterval().toMillis(), TimeUnit.MILLISECONDS);

    bootstrapMessageSubject = "atomix-gossip-map-" + mapName + "-bootstrap";
    clusterCommunicator.subscribe(bootstrapMessageSubject, serializer::decode,
            (Function<MemberId, CompletableFuture<Void>>) this::handleBootstrap, serializer::encode);

    initializeMessageSubject = "atomix-gossip-map-" + mapName + "-initialize";
    clusterCommunicator.subscribe(initializeMessageSubject, serializer::decode,
            (Function<Collection<UpdateEntry>, Void>) u -> {
                processUpdates(u);
                return null;
            }, serializer::encode, this.executor);

    updateMessageSubject = "atomix-gossip-map-" + mapName + "-update";
    clusterCommunicator.subscribe(updateMessageSubject, serializer::decode, this::processUpdates,
            this.executor);

    antiEntropyAdvertisementSubject = "atomix-gossip-map-" + mapName + "-anti-entropy";
    clusterCommunicator.subscribe(antiEntropyAdvertisementSubject, serializer::decode,
            this::handleAntiEntropyAdvertisement, serializer::encode, this.backgroundExecutor);

    updateRequestSubject = "atomix-gossip-map-" + mapName + "-update-request";
    clusterCommunicator.subscribe(updateRequestSubject, serializer::decode, this::handleUpdateRequests,
            this.backgroundExecutor);

    if (!config.isTombstonesDisabled()) {
        previousTombstonePurgeTime = 0;
        this.backgroundExecutor.scheduleWithFixedDelay(this::purgeTombstones, initialDelaySec,
                config.getAntiEntropyInterval().toMillis(), TimeUnit.MILLISECONDS);
    }

    this.tombstonesDisabled = config.isTombstonesDisabled();

    // Initiate first round of Gossip
    this.bootstrap();
}

From source file:alluxio.job.move.MoveDefinition.java

/**
 * {@inheritDoc}/*  w w w. j ava 2s  .  c  om*/
 *
 * Assigns each worker to move whichever files it has the most blocks for. If no worker has blocks
 * for a file, a random worker is chosen.
 */
@Override
public Map<WorkerInfo, ArrayList<MoveCommand>> selectExecutors(MoveConfig config,
        List<WorkerInfo> jobWorkerInfoList, JobMasterContext jobMasterContext) throws Exception {
    AlluxioURI source = new AlluxioURI(config.getSource());
    AlluxioURI destination = new AlluxioURI(config.getDestination());
    if (source.equals(destination)) {
        return new HashMap<WorkerInfo, ArrayList<MoveCommand>>();
    }
    checkMoveValid(config);

    List<BlockWorkerInfo> alluxioWorkerInfoList = AlluxioBlockStore.create(mFileSystemContext).getAllWorkers();
    Preconditions.checkState(!jobWorkerInfoList.isEmpty(), "No workers are available");

    List<URIStatus> allPathStatuses = getPathStatuses(source);
    ConcurrentMap<WorkerInfo, ArrayList<MoveCommand>> assignments = Maps.newConcurrentMap();
    ConcurrentMap<String, WorkerInfo> hostnameToWorker = Maps.newConcurrentMap();
    for (WorkerInfo workerInfo : jobWorkerInfoList) {
        hostnameToWorker.put(workerInfo.getAddress().getHost(), workerInfo);
    }
    List<String> keys = new ArrayList<>();
    keys.addAll(hostnameToWorker.keySet());
    // Assign each file to the worker with the most block locality.
    for (URIStatus status : allPathStatuses) {
        if (status.isFolder()) {
            moveDirectory(status.getPath(), source.getPath(), destination.getPath());
        } else {
            WorkerInfo bestJobWorker = getBestJobWorker(status, alluxioWorkerInfoList, jobWorkerInfoList,
                    hostnameToWorker);
            String destinationPath = computeTargetPath(status.getPath(), source.getPath(),
                    destination.getPath());
            assignments.putIfAbsent(bestJobWorker, Lists.<MoveCommand>newArrayList());
            assignments.get(bestJobWorker).add(new MoveCommand(status.getPath(), destinationPath));
        }
    }
    return assignments;
}

From source file:ch.epfl.eagle.daemon.scheduler.Scheduler.java

public void initialize(Configuration conf, InetSocketAddress socket) throws IOException {
    address = Network.socketAddressToThrift(socket);
    String mode = conf.getString(EagleConf.DEPLOYMENT_MODE, "unspecified");
    this.conf = conf;
    if (mode.equals("standalone")) {
        state = new StandaloneSchedulerState();
    } else if (mode.equals("configbased")) {
        state = new ConfigSchedulerState();
    } else {//from ww  w. j a  v  a 2 s. c  om
        throw new RuntimeException("Unsupported deployment mode: " + mode);
    }

    state.initialize(conf);

    defaultProbeRatioUnconstrained = conf.getDouble(EagleConf.SAMPLE_RATIO, EagleConf.DEFAULT_SAMPLE_RATIO);
    defaultProbeRatioConstrained = conf.getDouble(EagleConf.SAMPLE_RATIO_CONSTRAINED,
            EagleConf.DEFAULT_SAMPLE_RATIO_CONSTRAINED);

    requestTaskPlacers = Maps.newConcurrentMap();

    useCancellation = conf.getBoolean(EagleConf.CANCELLATION, EagleConf.DEFAULT_CANCELLATION);

    // [[FLORIN
    this.hostnameCentralizedScheduler = conf.getString("scheduler.centralized", "none");
    this.smallPartition = conf.getInt(EagleConf.SMALL_PARTITION, EagleConf.DEFAULT_SMALL_PARTITION);
    this.bigPartition = conf.getInt(EagleConf.BIG_PARTITION, EagleConf.DEFAULT_BIG_PARTITION);

    // if(address.getHost().contains(this.hostnameCentralizedScheduler)){
    if (address.getHost().matches(this.hostnameCentralizedScheduler)) {
        amIMaster = true;
        LOG.info("I am master: " + this.hostnameCentralizedScheduler + "--" + address.getHost() + "--");
    }
    // LOG.info("I am NOT master: "+this.hostnameCentralizedScheduler+"--"+address.getHost()+"--");

    // EAGLE
    this.piggybacking = conf.getBoolean(EagleConf.PIGGYBACKING, EagleConf.DEFAULT_PIGGYBACKING);
    LOG.info("Piggybacking: " + this.piggybacking);
    distributedLongStatusTimestamp = -1;
    distributedNotExecutingLong = new ArrayList<String>();
    this.retry_rounds = conf.getInt(EagleConf.RETRY_ROUNDS, EagleConf.DEFAULT_RETRY_ROUNDS);
    LOG.info("retry_rounds: " + this.retry_rounds);
    this.last_round_short_partition = conf.getBoolean(EagleConf.LAST_ROUND_SHORT_PARTITION,
            EagleConf.DEFAULT_LAST_ROUND_SHORT_PARTITION);

    if (useCancellation) {
        LOG.debug("Initializing cancellation service");
        cancellationService = new CancellationService(nodeMonitorClientPool);
        new Thread(cancellationService).start();
    } else {
        LOG.debug("Not using cancellation");
    }

    spreadEvenlyTaskSetSize = conf.getInt(EagleConf.SPREAD_EVENLY_TASK_SET_SIZE,
            EagleConf.DEFAULT_SPREAD_EVENLY_TASK_SET_SIZE);
}

From source file:io.joynr.dispatching.subscription.PublicationManagerImpl.java

@Inject
public PublicationManagerImpl(AttributePollInterpreter attributePollInterpreter, Dispatcher dispatcher,
        ProviderDirectory providerDirectory,
        @Named(JOYNR_SCHEDULER_CLEANUP) ScheduledExecutorService cleanupScheduler) {
    super();//from w w  w  .j a  va 2  s  .c  om
    this.dispatcher = dispatcher;
    this.providerDirectory = providerDirectory;
    this.cleanupScheduler = cleanupScheduler;
    this.queuedSubscriptionRequests = HashMultimap.create();
    this.subscriptionId2PublicationInformation = Maps.newConcurrentMap();
    this.publicationTimers = Maps.newConcurrentMap();
    this.subscriptionEndFutures = Maps.newConcurrentMap();
    this.unregisterAttributeListeners = Maps.newConcurrentMap();
    this.unregisterBroadcastListeners = Maps.newConcurrentMap();
    this.attributePollInterpreter = attributePollInterpreter;
    providerDirectory.addListener(this);

}

From source file:org.onosproject.store.ecmap.EventuallyConsistentMapImpl.java

/**
 * Creates a new eventually consistent map shared amongst multiple instances.
 * <p>/*from  w  w  w  .  ja v a2s  . c  o  m*/
 * See {@link org.onosproject.store.service.EventuallyConsistentMapBuilder}
 * for more description of the parameters expected by the map.
 * </p>
 *
 * @param mapName               a String identifier for the map.
 * @param clusterService        the cluster service
 * @param clusterCommunicator   the cluster communications service
 * @param serializerBuilder     a Kryo namespace builder that can serialize
 *                              both K and V
 * @param timestampProvider     provider of timestamps for K and V
 * @param peerUpdateFunction    function that provides a set of nodes to immediately
 *                              update to when there writes to the map
 * @param eventExecutor         executor to use for processing incoming
 *                              events from peers
 * @param communicationExecutor executor to use for sending events to peers
 * @param backgroundExecutor    executor to use for background anti-entropy
 *                              tasks
 * @param tombstonesDisabled    true if this map should not maintain
 *                              tombstones
 * @param antiEntropyPeriod     period that the anti-entropy task should run
 * @param antiEntropyTimeUnit   time unit for anti-entropy period
 * @param convergeFaster        make anti-entropy try to converge faster
 * @param persistent            persist data to disk
 */
EventuallyConsistentMapImpl(String mapName, ClusterService clusterService,
        ClusterCommunicationService clusterCommunicator, KryoNamespace.Builder serializerBuilder,
        BiFunction<K, V, Timestamp> timestampProvider, BiFunction<K, V, Collection<NodeId>> peerUpdateFunction,
        ExecutorService eventExecutor, ExecutorService communicationExecutor,
        ScheduledExecutorService backgroundExecutor, boolean tombstonesDisabled, long antiEntropyPeriod,
        TimeUnit antiEntropyTimeUnit, boolean convergeFaster, boolean persistent) {
    this.mapName = mapName;
    items = Maps.newConcurrentMap();
    senderPending = Maps.newConcurrentMap();
    destroyedMessage = mapName + ERROR_DESTROYED;

    this.clusterService = clusterService;
    this.clusterCommunicator = clusterCommunicator;
    this.localNodeId = clusterService.getLocalNode().id();

    this.serializer = createSerializer(serializerBuilder);

    this.timestampProvider = timestampProvider;

    if (peerUpdateFunction != null) {
        this.peerUpdateFunction = peerUpdateFunction;
    } else {
        this.peerUpdateFunction = (key, value) -> clusterService.getNodes().stream().map(ControllerNode::id)
                .filter(nodeId -> !nodeId.equals(localNodeId)).collect(Collectors.toList());
    }

    if (eventExecutor != null) {
        this.executor = eventExecutor;
    } else {
        // should be a normal executor; it's used for receiving messages
        this.executor = Executors.newFixedThreadPool(8, groupedThreads("onos/ecm", mapName + "-fg-%d"));
    }

    if (communicationExecutor != null) {
        this.communicationExecutor = communicationExecutor;
    } else {
        // sending executor; should be capped
        //TODO this probably doesn't need to be bounded anymore
        this.communicationExecutor = newFixedThreadPool(8, groupedThreads("onos/ecm", mapName + "-publish-%d"));
    }

    this.persistent = persistent;

    if (this.persistent) {
        String dataDirectory = System.getProperty("karaf.data", "./data");
        String filename = dataDirectory + "/" + "mapdb-ecm-" + mapName;

        ExecutorService dbExecutor = newFixedThreadPool(1, groupedThreads("onos/ecm", mapName + "-dbwriter"));

        persistentStore = new MapDbPersistentStore<>(filename, dbExecutor, serializer);
        persistentStore.readInto(items);
    } else {
        this.persistentStore = null;
    }

    if (backgroundExecutor != null) {
        this.backgroundExecutor = backgroundExecutor;
    } else {
        this.backgroundExecutor = newSingleThreadScheduledExecutor(
                groupedThreads("onos/ecm", mapName + "-bg-%d"));
    }

    // start anti-entropy thread
    this.backgroundExecutor.scheduleAtFixedRate(this::sendAdvertisement, initialDelaySec, antiEntropyPeriod,
            antiEntropyTimeUnit);

    updateMessageSubject = new MessageSubject("ecm-" + mapName + "-update");
    clusterCommunicator.addSubscriber(updateMessageSubject, serializer::decode, this::processUpdates,
            this.executor);

    antiEntropyAdvertisementSubject = new MessageSubject("ecm-" + mapName + "-anti-entropy");
    clusterCommunicator.addSubscriber(antiEntropyAdvertisementSubject, serializer::decode,
            this::handleAntiEntropyAdvertisement, this.backgroundExecutor);

    this.tombstonesDisabled = tombstonesDisabled;
    this.lightweightAntiEntropy = !convergeFaster;
}