Search in sources :

Example 36 with ConcurrentSkipListMap

use of java.util.concurrent.ConcurrentSkipListMap in project jackrabbit-oak by apache.

the class DocumentNodeStoreTest method docChildCacheWithIncompatiblDocStoreSort.

@Test
public void docChildCacheWithIncompatiblDocStoreSort() throws CommitFailedException {
    final Set<String> reads = Sets.newHashSet();
    final ConcurrentSkipListMap<String, NodeDocument> nodes = new ConcurrentSkipListMap<String, NodeDocument>(new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            int ret = o1.compareTo(o2);
            if (o1.indexOf("child") > 0 && o2.indexOf("child") > 0) {
                ret = (-ret);
            }
            return ret;
        }
    });
    MemoryDocumentStore docStore = new MemoryDocumentStore() {

        @Override
        @SuppressWarnings("unchecked")
        protected <T extends Document> ConcurrentSkipListMap<String, T> getMap(Collection<T> collection) {
            if (collection == Collection.NODES) {
                return (ConcurrentSkipListMap<String, T>) nodes;
            } else {
                return super.getMap(collection);
            }
        }

        @Override
        public <T extends Document> T find(Collection<T> collection, String key) {
            reads.add(key);
            return super.find(collection, key);
        }
    };
    DocumentNodeStore store = builderProvider.newBuilder().setUseSimpleRevision(true).setClusterId(1).setAsyncDelay(0).setDocumentStore(docStore).getNodeStore();
    NodeBuilder builder = store.getRoot().builder();
    // create < INITIAL_FETCH_SIZE children to have complete child cache entries
    NodeBuilder parentBuilder = builder.child("parent");
    int numChildren = DocumentNodeState.INITIAL_FETCH_SIZE - 2;
    for (int i = 0; i < numChildren; i++) {
        parentBuilder.child("child" + (i + 1));
    }
    merge(store, builder);
    store.invalidateNodeChildrenCache();
    // Force fill child node cache
    NodeState parentNodeState = store.getRoot().getChildNode("parent");
    Iterables.size(parentNodeState.getChildNodeEntries());
    reads.clear();
    NodeState nonExistingChild = parentNodeState.getChildNode("child501-non-existing-child");
    assertEquals("Fully cached entry in doc child cache should be able to find non existing children" + " even if doc store sort order is incompatible to that of Java", 0, reads.size());
    assertFalse("Non existing children should be reported as such", nonExistingChild.exists());
    store.invalidateNodeCache("/parent/child25", store.getHeadRevision());
    reads.clear();
    NodeState existingChild = parentNodeState.getChildNode("child25");
    assertTrue("Fully cached entry in doc child cache should be able to find existing children" + " even if doc store sort order is incompatible to that of Java", reads.size() > 0);
    assertTrue("Existing children should be reported as such", existingChild.exists());
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) TRANSIENT(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException.Type.TRANSIENT) CONSTRAINT(org.apache.jackrabbit.oak.api.CommitFailedException.CONSTRAINT) Test(org.junit.Test)

Example 37 with ConcurrentSkipListMap

use of java.util.concurrent.ConcurrentSkipListMap in project photon-model by vmware.

the class AzureComputeHostStatsGatherer method aggregateComputeStatsResponses.

/**
 * Aggregates stats from all the compute VMs to make up compute Host stats.
 */
private ComputeStats aggregateComputeStatsResponses(AzureStatsDataHolder statsData, List<QueryTask> items) {
    int numberOfComputeResponse = items.size();
    ComputeStats computeStats = new ComputeStats();
    computeStats.computeLink = statsData.computeHost.documentSelfLink;
    Map<String, ServiceStat> statMap = new HashMap<>();
    // Gather all the stats in a single response.
    for (QueryTask queryResult : items) {
        if (queryResult.results.documents != null) {
            for (String key : queryResult.results.documents.keySet()) {
                ResourceMetrics metric = Utils.fromJson(queryResult.results.documents.get(key), ResourceMetrics.class);
                for (Map.Entry<String, Double> entry : metric.entries.entrySet()) {
                    String metricName = entry.getKey();
                    if (statMap.containsKey(metricName)) {
                        statMap.get(metricName).latestValue += entry.getValue();
                    } else {
                        ServiceStat stat = new ServiceStat();
                        stat.latestValue = entry.getValue();
                        statMap.put(metricName, stat);
                    }
                }
            }
        }
    }
    computeStats.statValues = new ConcurrentSkipListMap<>();
    // Divide each metric value by the number of computes to get an average value.
    for (String key : statMap.keySet()) {
        ServiceStat serviceStatValue = statMap.get(key);
        serviceStatValue.unit = PhotonModelConstants.getUnitForMetric(key);
        serviceStatValue.sourceTimeMicrosUtc = Utils.getNowMicrosUtc();
        serviceStatValue.latestValue = serviceStatValue.latestValue / numberOfComputeResponse;
        computeStats.statValues.put(key, Collections.singletonList(serviceStatValue));
    }
    return computeStats;
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 38 with ConcurrentSkipListMap

use of java.util.concurrent.ConcurrentSkipListMap in project photon-model by vmware.

the class AzureCostStatsService method createAzureSubscriptionStats.

// Create Azure account stats
private void createAzureSubscriptionStats(Context context, AzureSubscription subscription) {
    // convert the subscription daily costs to cumulative
    AtomicDouble cumulativeValue = new AtomicDouble(0.0);
    subscription.cost = subscription.cost.entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).collect(Collectors.toMap(Entry::getKey, e -> cumulativeValue.addAndGet(e.getValue())));
    Consumer<List<ComputeState>> subscriptionStatsProcessor = (subscriptionComputeStates) -> subscriptionComputeStates.forEach(subscriptionComputeState -> {
        String statName = AzureStatsNormalizer.getNormalizedStatKeyValue(AzureCostConstants.COST);
        String costUnit = AzureStatsNormalizer.getNormalizedUnitValue(AzureCostConstants.DEFAULT_CURRENCY_VALUE);
        ComputeStats subscriptionStats = new ComputeStats();
        subscriptionStats.computeLink = subscriptionComputeState.documentSelfLink;
        subscriptionStats.statValues = new ConcurrentSkipListMap<>();
        List<ServiceStat> costStats = new ArrayList<>();
        for (Entry<Long, Double> cost : subscription.cost.entrySet()) {
            ServiceStat azureAccountStat = AzureCostHelper.createServiceStat(statName, cost.getValue(), costUnit, cost.getKey());
            costStats.add(azureAccountStat);
        }
        subscriptionStats.statValues.put(statName, costStats);
        context.statsResponse.statsList.add(subscriptionStats);
    });
    processSubscriptionStats(context, subscription, subscriptionStatsProcessor);
}
Also used : AzureCostConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants) AzureStatsNormalizer(com.vmware.photon.controller.model.adapters.azure.utils.AzureStatsNormalizer) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) StringUtils(org.apache.commons.lang3.StringUtils) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Utils(com.vmware.xenon.common.Utils) BufferedSink(okio.BufferedSink) Map(java.util.Map) AzureService(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureService) URI(java.net.URI) AzureSubscriptionsEnumerationRequest(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService.AzureSubscriptionsEnumerationRequest) Builder(com.vmware.xenon.services.common.QueryTask.Builder) Path(java.nio.file.Path) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Interceptor(okhttp3.Interceptor) ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) AzureDetailedBillHandler(com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureDetailedBillHandler) Request(okhttp3.Request) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OldEaSummarizedBillElement(com.vmware.photon.controller.model.adapters.azure.model.cost.OldEaSummarizedBillElement) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AzureSubscriptionsEnumerationService(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService) AzureCostHelper(com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureCostHelper) List(java.util.List) Stream(java.util.stream.Stream) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) INTERNAL_REQUEST_TIMEOUT_SECONDS(com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants.INTERNAL_REQUEST_TIMEOUT_SECONDS) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) SingleResourceTaskCollectionStage(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceTaskCollectionStage) Okio(okio.Okio) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) OperationContext(com.vmware.xenon.common.OperationContext) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) HashMap(java.util.HashMap) Function(java.util.function.Function) AzureResource(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) CollectionUtils(org.apache.commons.collections.CollectionUtils) EaBillLinkElement(com.vmware.photon.controller.model.adapters.azure.model.cost.EaBillLinkElement) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AUTO_DISCOVERED_ENTITY(com.vmware.photon.controller.model.constants.PhotonModelConstants.AUTO_DISCOVERED_ENTITY) EaBillLinks(com.vmware.photon.controller.model.adapters.azure.model.cost.EaBillLinks) BiConsumer(java.util.function.BiConsumer) Response(okhttp3.Response) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) Call(okhttp3.Call) Callback(okhttp3.Callback) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) ExecutorService(java.util.concurrent.ExecutorService) OldApi(com.vmware.photon.controller.model.adapters.azure.model.cost.OldApi) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Files(java.nio.file.Files) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) TypeName(com.vmware.xenon.common.ServiceDocumentDescription.TypeName) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) File(java.io.File) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) BillParsingStatus(com.vmware.photon.controller.model.adapters.azure.model.cost.BillParsingStatus) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) LocalDate(org.joda.time.LocalDate) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) OkHttpClient(okhttp3.OkHttpClient) Paths(java.nio.file.Paths) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) Comparator(java.util.Comparator) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) OperationJoin(com.vmware.xenon.common.OperationJoin) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ArrayList(java.util.ArrayList) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) Entry(java.util.Map.Entry) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) List(java.util.List) ArrayList(java.util.ArrayList)

Example 39 with ConcurrentSkipListMap

use of java.util.concurrent.ConcurrentSkipListMap in project atlasdb by palantir.

the class InMemoryKeyValueService method get.

@Override
public Map<Cell, Value> get(TableReference tableRef, Map<Cell, Long> timestampByCell) {
    ConcurrentSkipListMap<Key, byte[]> table = getTableMap(tableRef).entries;
    Map<Cell, Value> result = Maps.newHashMap();
    for (Map.Entry<Cell, Long> e : timestampByCell.entrySet()) {
        Cell cell = e.getKey();
        Entry<Key, byte[]> lastEntry = table.lowerEntry(new Key(cell, e.getValue()));
        if (lastEntry != null) {
            Key key = lastEntry.getKey();
            if (key.matchesCell(cell)) {
                long ts = lastEntry.getKey().ts;
                result.put(cell, Value.createWithCopyOfData(lastEntry.getValue(), ts));
            }
        }
    }
    return result;
}
Also used : Value(com.palantir.atlasdb.keyvalue.api.Value) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) SortedMap(java.util.SortedMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 40 with ConcurrentSkipListMap

use of java.util.concurrent.ConcurrentSkipListMap in project cdap by caskdata.

the class LeaderElectionInfoService method fetchCurrentParticipants.

/**
 * Fetches the latest participants from ZK. This method will block until it fetched all participants information.
 * Note that the map returned is only a snapshot of the leader election information in ZK, which only reflects
 * the states in ZK at the time when the snapshot was taken.
 *
 * @return An immutable {@link SortedMap} ordered by the participant ID with the smallest key in the map
 *         as the current leader
 * @throws InterruptedException if the caller thread is interrupted while waiting for the participants information
 *                              to be available
 * @throws Exception if failed to fetch information from ZK
 */
public SortedMap<Integer, Participant> fetchCurrentParticipants() throws Exception {
    try {
        NodeChildren nodeChildren = zkClient.getChildren(leaderElectionPath).get();
        ConcurrentNavigableMap<Integer, Participant> result = new ConcurrentSkipListMap<>();
        SettableFuture<CountDownLatch> completion = SettableFuture.create();
        childrenUpdated(nodeChildren, result, completion);
        completion.get().await();
        return Collections.unmodifiableSortedMap(result);
    } catch (ExecutionException e) {
        // If the election path doesn't exists, that means there is no participant
        Throwable cause = e.getCause();
        if (cause instanceof KeeperException.NoNodeException) {
            return ImmutableSortedMap.of();
        }
        Throwables.propagateIfPossible(cause, Exception.class);
        // Shouldn't reach here as we propagate any Exception/RuntimeException/Error already
        return ImmutableSortedMap.of();
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) KeeperException(org.apache.zookeeper.KeeperException) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) ExecutionException(java.util.concurrent.ExecutionException) NodeChildren(org.apache.twill.zookeeper.NodeChildren)

Aggregations

ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)183 Test (org.junit.Test)66 PollStatus (org.opennms.netmgt.poller.PollStatus)32 MonitoredService (org.opennms.netmgt.poller.MonitoredService)31 Map (java.util.Map)30 ServiceMonitor (org.opennms.netmgt.poller.ServiceMonitor)25 MqttDeviceTwin (com.microsoft.azure.sdk.iot.device.transport.mqtt.MqttDeviceTwin)23 HashMap (java.util.HashMap)21 DeviceTwinMessage (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceTwinMessage)17 NavigableMap (java.util.NavigableMap)14 Set (java.util.Set)12 Iterator (java.util.Iterator)11 NavigableSet (java.util.NavigableSet)11 MockMonitoredService (org.opennms.netmgt.poller.mock.MockMonitoredService)11 IOException (java.io.IOException)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 ArrayList (java.util.ArrayList)9 BitSet (java.util.BitSet)9 DeviceOperations (com.microsoft.azure.sdk.iot.device.DeviceTwin.DeviceOperations)8 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)8