use of java.util.concurrent.ConcurrentSkipListMap in project ignite by apache.
the class CacheContinuousQueryEventBufferTest method testBuffer.
/**
* @param rnd Random.
* @param b Buffer.
* @param cnt Entries count.
* @param cntr Current counter.
* @param filterRatio Filtered events ratio.
* @param threads Threads number.
* @throws Exception If failed.
*/
private void testBuffer(Random rnd, final CacheContinuousQueryEventBuffer b, int cnt, long cntr, float filterRatio, int threads) throws Exception {
List<CacheContinuousQueryEntry> expEntries = new ArrayList<>();
List<CacheContinuousQueryEntry> entries = new ArrayList<>();
long filtered = b.currentFiltered();
for (int i = 0; i < cnt; i++) {
CacheContinuousQueryEntry entry = new CacheContinuousQueryEntry(0, EventType.CREATED, null, null, null, false, 0, cntr, null, (byte) 0);
entries.add(entry);
if (rnd.nextFloat() < filterRatio) {
entry.markFiltered();
filtered++;
} else {
CacheContinuousQueryEntry expEntry = new CacheContinuousQueryEntry(0, EventType.CREATED, null, null, null, false, 0, cntr, null, (byte) 0);
expEntry.filteredCount(filtered);
expEntries.add(expEntry);
filtered = 0;
}
cntr++;
}
Collections.shuffle(entries, rnd);
List<CacheContinuousQueryEntry> actualEntries = new ArrayList<>(expEntries.size());
if (threads == 1) {
for (int i = 0; i < entries.size(); i++) {
Object o = entries.get(i);
Object res = b.processEntry((CacheContinuousQueryEntry) o, false);
if (res != null) {
if (res instanceof CacheContinuousQueryEntry)
actualEntries.add((CacheContinuousQueryEntry) res);
else
actualEntries.addAll((List<CacheContinuousQueryEntry>) res);
}
}
} else {
final CyclicBarrier barrier = new CyclicBarrier(threads);
final ConcurrentLinkedQueue<CacheContinuousQueryEntry> q = new ConcurrentLinkedQueue<>(entries);
final ConcurrentSkipListMap<Long, CacheContinuousQueryEntry> act0 = new ConcurrentSkipListMap<>();
GridTestUtils.runMultiThreaded(new Callable<Object>() {
@Override
public Object call() throws Exception {
barrier.await();
Object o;
while ((o = q.poll()) != null) {
Object res = b.processEntry((CacheContinuousQueryEntry) o, false);
if (res != null) {
if (res instanceof CacheContinuousQueryEntry)
act0.put(((CacheContinuousQueryEntry) res).updateCounter(), (CacheContinuousQueryEntry) res);
else {
for (CacheContinuousQueryEntry e : ((List<CacheContinuousQueryEntry>) res)) act0.put(e.updateCounter(), e);
}
}
}
return null;
}
}, threads, "test");
actualEntries.addAll(act0.values());
}
assertEquals(expEntries.size(), actualEntries.size());
for (int i = 0; i < expEntries.size(); i++) {
CacheContinuousQueryEntry expEvt = expEntries.get(i);
CacheContinuousQueryEntry actualEvt = actualEntries.get(i);
assertEquals(expEvt.updateCounter(), actualEvt.updateCounter());
assertEquals(expEvt.filteredCount(), actualEvt.filteredCount());
}
}
use of java.util.concurrent.ConcurrentSkipListMap in project angel by Tencent.
the class MatrixFilesMeta method write.
/*
public void write(DataOutputStream output) throws IOException {
output.writeInt(matrixId);
output.writeUTF(matrixName);
output.writeUTF(formatClassName);
output.writeInt(rowType);
output.writeInt(row);
output.writeLong(col);
output.writeInt(blockRow);
output.writeLong(blockCol);
if (options == null || options.isEmpty()) {
output.writeInt(0);
} else {
output.writeInt(options.size());
for (Map.Entry<String, String> opEntry : options.entrySet()) {
output.writeUTF(opEntry.getKey());
output.writeUTF(opEntry.getValue());
}
}
if (partMetas == null || partMetas.isEmpty()) {
output.writeInt(0);
} else {
output.writeInt(partMetas.size());
for (Map.Entry<Integer, MatrixPartitionMeta> partEntry : partMetas.entrySet()) {
partEntry.getValue().write(output);
}
}
}
public void read(DataInputStream input) throws IOException {
matrixId = input.readInt();
matrixName = input.readUTF();
formatClassName = input.readUTF();
rowType = input.readInt();
row = input.readInt();
col = input.readLong();
blockRow = input.readInt();
blockCol = input.readLong();
int optionNum = input.readInt();
options = new HashMap<>();
for (int i = 0; i < optionNum; i++) {
options.put(input.readUTF(), input.readUTF());
}
int partNum = input.readInt();
partMetas = new TreeMap<>();
for (int i = 0; i < partNum; i++) {
MatrixPartitionMeta partMeta = new MatrixPartitionMeta();
partMeta.read(input);
partMetas.put(partMeta.getPartId(), partMeta);
}
}*/
public void write(DataOutputStream output) throws IOException {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("matrixId", matrixId);
jsonObject.put("matrixName", matrixName);
jsonObject.put("formatClassName", formatClassName);
jsonObject.put("rowType", rowType);
jsonObject.put("row", row);
jsonObject.put("col", col);
jsonObject.put("blockRow", blockRow);
jsonObject.put("blockCol", blockCol);
jsonObject.put("options", options);
Map<Integer, JSONObject> jsonMap = new ConcurrentSkipListMap<>();
for (Map.Entry<Integer, MatrixPartitionMeta> partEntry : partMetas.entrySet()) {
JSONObject parJsonOnbect = new JSONObject();
partEntry.getValue().write(parJsonOnbect);
jsonMap.put(partEntry.getKey(), parJsonOnbect);
}
jsonObject.put("partMetas", jsonMap);
byte[] b = jsonObject.toString().getBytes("utf-8");
output.writeInt(b.length);
output.write(b);
} catch (Throwable e) {
throw new IOException(e);
}
}
use of java.util.concurrent.ConcurrentSkipListMap in project bazel by bazelbuild.
the class SkyframeActionExecutor method constructActionGraphAndPathMap.
/**
* Simultaneously construct an action graph for all the actions in Skyframe and a map from
* {@link PathFragment}s to their respective {@link Artifact}s. We do this in a threadpool to save
* around 1.5 seconds on a mid-sized build versus a single-threaded operation.
*/
private static Pair<ActionGraph, SortedMap<PathFragment, Artifact>> constructActionGraphAndPathMap(Iterable<ActionLookupValue> values, ConcurrentMap<ActionAnalysisMetadata, ConflictException> badActionMap) throws InterruptedException {
MutableActionGraph actionGraph = new MapBasedActionGraph();
ConcurrentNavigableMap<PathFragment, Artifact> artifactPathMap = new ConcurrentSkipListMap<>();
// Action graph construction is CPU-bound.
int numJobs = Runtime.getRuntime().availableProcessors();
// No great reason for expecting 5000 action lookup values, but not worth counting size of
// values.
Sharder<ActionLookupValue> actionShards = new Sharder<>(numJobs, 5000);
for (ActionLookupValue value : values) {
actionShards.add(value);
}
ThrowableRecordingRunnableWrapper wrapper = new ThrowableRecordingRunnableWrapper("SkyframeActionExecutor#constructActionGraphAndPathMap");
ExecutorService executor = Executors.newFixedThreadPool(numJobs, new ThreadFactoryBuilder().setNameFormat("ActionLookupValue Processor %d").build());
for (List<ActionLookupValue> shard : actionShards) {
executor.execute(wrapper.wrap(actionRegistration(shard, actionGraph, artifactPathMap, badActionMap)));
}
boolean interrupted = ExecutorUtil.interruptibleShutdown(executor);
Throwables.propagateIfPossible(wrapper.getFirstThrownError());
if (interrupted) {
throw new InterruptedException();
}
return Pair.<ActionGraph, SortedMap<PathFragment, Artifact>>of(actionGraph, artifactPathMap);
}
use of java.util.concurrent.ConcurrentSkipListMap in project hbase by apache.
the class TestClientNoCluster method makeMeta.
/**
* Create up a map that is keyed by meta row name and whose value is the HRegionInfo and
* ServerName to return for this row.
* @return Map with faked hbase:meta content in it.
*/
static SortedMap<byte[], Pair<HRegionInfo, ServerName>> makeMeta(final byte[] tableName, final int regionCount, final long namespaceSpan, final int serverCount) {
// I need a comparator for meta rows so we sort properly.
SortedMap<byte[], Pair<HRegionInfo, ServerName>> meta = new ConcurrentSkipListMap<>(new MetaRowsComparator());
HRegionInfo[] hris = makeHRegionInfos(tableName, regionCount, namespaceSpan);
ServerName[] serverNames = makeServerNames(serverCount);
int per = regionCount / serverCount;
int count = 0;
for (HRegionInfo hri : hris) {
Pair<HRegionInfo, ServerName> p = new Pair<>(hri, serverNames[count++ / per]);
meta.put(hri.getRegionName(), p);
}
return meta;
}
use of java.util.concurrent.ConcurrentSkipListMap in project azure-iot-sdk-java by Azure.
the class DeviceTwinTest method subscribeToDesiredDoesNotSubscribeIfAlreadySubscribed.
@Test
public void subscribeToDesiredDoesNotSubscribeIfAlreadySubscribed(@Mocked final TwinParser mockedTwinParserObject, @Mocked final DeviceTwinMessage mockedDeviceTwinMessage, @Mocked final PropertyCallBack<String, Object> mockedDesiredCB) throws IOException {
new NonStrictExpectations() {
{
new TwinParser(withAny(new TwinChangedCallback() {
@Override
public void execute(Map<String, Object> map) {
}
}), withAny(new TwinChangedCallback() {
@Override
public void execute(Map<String, Object> map) {
}
}));
result = mockedTwinParserObject;
new DeviceTwinMessage(withAny(new byte[0]));
result = mockedDeviceTwinMessage;
}
};
DeviceTwin testTwin = new DeviceTwin(mockedDeviceIO, mockedConfig, mockedStatusCB, null, mockedGenericPropertyCB, null);
Map<Property, Pair<PropertyCallBack<String, Object>, Object>> desiredMap = new HashMap<>();
desiredMap.put(new Property("DesiredProp1", "DesiredValue1"), new Pair<>(mockedDesiredCB, null));
testTwin.subscribeDesiredPropertiesNotification(desiredMap);
Deencapsulation.setField(testTwin, "isSubscribed", true);
desiredMap.put(new Property("DesiredProp2", "DesiredValue2"), new Pair<>(mockedDesiredCB, null));
testTwin.subscribeDesiredPropertiesNotification(desiredMap);
final ConcurrentSkipListMap<String, Pair<PropertyCallBack<String, Object>, Object>> actualMap = Deencapsulation.getField(testTwin, "onDesiredPropertyChangeMap");
assertNotNull(actualMap);
assertFalse(actualMap.isEmpty());
assertTrue(actualMap.containsKey("DesiredProp1"));
assertTrue(actualMap.containsKey("DesiredProp2"));
assertEquals(actualMap.get("DesiredProp2").getKey(), mockedDesiredCB);
new Verifications() {
{
mockedDeviceTwinMessage.setDeviceOperationType(DeviceOperations.DEVICE_OPERATION_TWIN_SUBSCRIBE_DESIRED_PROPERTIES_REQUEST);
times = 1;
mockedDeviceIO.sendEventAsync(mockedDeviceTwinMessage, (IotHubEventCallback) any, null);
times = 1;
}
};
}
Aggregations