Search in sources :

Example 86 with ConcurrentSkipListMap

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());
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ArrayList(java.util.ArrayList) CyclicBarrier(java.util.concurrent.CyclicBarrier) List(java.util.List) ArrayList(java.util.ArrayList) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 87 with ConcurrentSkipListMap

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);
    }
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) JSONObject(org.codehaus.jettison.json.JSONObject) IOException(java.io.IOException) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 88 with ConcurrentSkipListMap

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);
}
Also used : Sharder(com.google.devtools.build.lib.concurrent.Sharder) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) MutableActionGraph(com.google.devtools.build.lib.actions.MutableActionGraph) MapBasedActionGraph(com.google.devtools.build.lib.actions.MapBasedActionGraph) ActionGraph(com.google.devtools.build.lib.actions.ActionGraph) MapBasedActionGraph(com.google.devtools.build.lib.actions.MapBasedActionGraph) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) MutableActionGraph(com.google.devtools.build.lib.actions.MutableActionGraph) Artifact(com.google.devtools.build.lib.actions.Artifact) SortedMap(java.util.SortedMap) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) ThrowableRecordingRunnableWrapper(com.google.devtools.build.lib.concurrent.ThrowableRecordingRunnableWrapper)

Example 89 with ConcurrentSkipListMap

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;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ServerName(org.apache.hadoop.hbase.ServerName) Pair(org.apache.hadoop.hbase.util.Pair)

Example 90 with ConcurrentSkipListMap

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;
        }
    };
}
Also used : HashMap(java.util.HashMap) TwinParser(com.microsoft.azure.sdk.iot.deps.serializer.TwinParser) TwinChangedCallback(com.microsoft.azure.sdk.iot.deps.serializer.TwinChangedCallback) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Map(java.util.Map) HashMap(java.util.HashMap) DeviceTwin(com.microsoft.azure.sdk.iot.device.DeviceTwin) Test(org.junit.Test)

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