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());
}
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;
}
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);
}
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;
}
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();
}
}
Aggregations