Search in sources :

Example 46 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project ignite by apache.

the class CacheContinuousQueryRandomOperationsTest method batchOperation.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void batchOperation(CacheConfiguration ccfg) throws Exception {
    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
    try {
        ContinuousQuery<QueryTestKey, QueryTestValue> qry = new ContinuousQuery<>();
        final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
        if (noOpFilterFactory() != null)
            qry.setRemoteFilterFactory(noOpFilterFactory());
        qry.setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
            }
        });
        Map<QueryTestKey, QueryTestValue> map = new TreeMap<>();
        for (int i = 0; i < KEYS; i++) map.put(new QueryTestKey(i), new QueryTestValue(i));
        try (QueryCursor qryCur = cache.query(qry)) {
            for (int i = 0; i < ITERATION_CNT / 2; i++) {
                log.info("Start iteration: " + i);
                // Not events.
                cache.removeAll(map.keySet());
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.putAll(map);
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == KEYS;
                    }
                }, 5_000);
                checkEvents(evts, CREATED);
                evts.clear();
                // Not events.
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                U.sleep(100);
                assertEquals(0, evts.size());
                // Get events.
                cache.invokeAll(map.keySet(), (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                // Not events.
                cache.removeAll(map.keySet());
                cache.removeAll(map.keySet());
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == KEYS;
                    }
                }, 5_000);
                checkEvents(evts, REMOVED);
                evts.clear();
                log.info("Finish iteration: " + i);
            }
        }
    } finally {
        grid(getClientIndex()).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) TreeMap(java.util.TreeMap) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 47 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project ignite by apache.

the class CacheContinuousQueryRandomOperationsTest method singleOperation.

/**
     * @param ccfg Cache configuration.
     * @throws Exception If failed.
     */
private void singleOperation(CacheConfiguration ccfg) throws Exception {
    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(getClientIndex()).createCache(ccfg);
    try {
        ContinuousQuery<QueryTestKey, QueryTestValue> qry = new ContinuousQuery<>();
        final List<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> evts = new CopyOnWriteArrayList<>();
        if (noOpFilterFactory() != null)
            qry.setRemoteFilterFactory(noOpFilterFactory());
        qry.setLocalListener(new CacheEntryUpdatedListener<QueryTestKey, QueryTestValue>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> events) throws CacheEntryListenerException {
                for (CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e : events) evts.add(e);
            }
        });
        QueryTestKey key = new QueryTestKey(1);
        try (QueryCursor qryCur = cache.query(qry)) {
            for (int i = 0; i < ITERATION_CNT; i++) {
                log.info("Start iteration: " + i);
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.put(key, new QueryTestValue(1));
                cache.remove(key);
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.remove(key);
                // Get events.
                cache.put(key, new QueryTestValue(2));
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                // Get events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(null, false));
                // Not events.
                cache.remove(key);
                // Get events.
                cache.put(key, new QueryTestValue(3));
                cache.put(key, new QueryTestValue(4));
                // Not events.
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.putIfAbsent(key, new QueryTestValue(5));
                cache.invoke(key, (EntryProcessor<QueryTestKey, QueryTestValue, ? extends Object>) (Object) new EntrySetValueProcessor(true));
                cache.remove(key, new QueryTestValue(5));
                // Get events.
                cache.remove(key, new QueryTestValue(4));
                cache.putIfAbsent(key, new QueryTestValue(5));
                // Not events.
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                cache.replace(key, new QueryTestValue(3), new QueryTestValue(2));
                // Get events.
                cache.replace(key, new QueryTestValue(5), new QueryTestValue(6));
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == 9;
                    }
                }, 5_000);
                checkSingleEvent(evts.get(0), CREATED, new QueryTestValue(1), null);
                checkSingleEvent(evts.get(1), REMOVED, null, new QueryTestValue(1));
                checkSingleEvent(evts.get(2), CREATED, new QueryTestValue(2), null);
                checkSingleEvent(evts.get(3), REMOVED, null, new QueryTestValue(2));
                checkSingleEvent(evts.get(4), CREATED, new QueryTestValue(3), null);
                checkSingleEvent(evts.get(5), EventType.UPDATED, new QueryTestValue(4), new QueryTestValue(3));
                checkSingleEvent(evts.get(6), REMOVED, null, new QueryTestValue(4));
                checkSingleEvent(evts.get(7), CREATED, new QueryTestValue(5), null);
                checkSingleEvent(evts.get(8), EventType.UPDATED, new QueryTestValue(6), new QueryTestValue(5));
                evts.clear();
                cache.remove(key);
                cache.remove(key);
                assert GridTestUtils.waitForCondition(new PA() {

                    @Override
                    public boolean apply() {
                        return evts.size() == 1;
                    }
                }, 5_000);
                evts.clear();
                log.info("Finish iteration: " + i);
            }
        }
    } finally {
        grid(getClientIndex()).destroyCache(ccfg.getName());
    }
}
Also used : CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) CacheEntryEvent(javax.cache.event.CacheEntryEvent) PA(org.apache.ignite.internal.util.typedef.PA) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 48 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project lucene-solr by apache.

the class TestLockTree method testLocks.

public void testLocks() throws Exception {
    LockTree lockTree = new LockTree();
    Lock coll1Lock = lockTree.getSession().lock(CollectionAction.CREATE, Arrays.asList("coll1"));
    assertNotNull(coll1Lock);
    assertNull("Should not be able to lock coll1/shard1", lockTree.getSession().lock(CollectionAction.BALANCESHARDUNIQUE, Arrays.asList("coll1", "shard1")));
    assertNull(lockTree.getSession().lock(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2")));
    coll1Lock.unlock();
    Lock shard1Lock = lockTree.getSession().lock(CollectionAction.BALANCESHARDUNIQUE, Arrays.asList("coll1", "shard1"));
    assertNotNull(shard1Lock);
    shard1Lock.unlock();
    Lock replica1Lock = lockTree.getSession().lock(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2"));
    assertNotNull(replica1Lock);
    List<Pair<CollectionAction, List<String>>> operations = new ArrayList<>();
    operations.add(new Pair<>(ADDREPLICAPROP, Arrays.asList("coll1", "shard1", "core_node2")));
    operations.add(new Pair<>(MODIFYCOLLECTION, Arrays.asList("coll1")));
    operations.add(new Pair<>(SPLITSHARD, Arrays.asList("coll1", "shard1")));
    operations.add(new Pair<>(SPLITSHARD, Arrays.asList("coll2", "shard2")));
    operations.add(new Pair<>(MODIFYCOLLECTION, Arrays.asList("coll2")));
    operations.add(new Pair<>(DELETEREPLICA, Arrays.asList("coll2", "shard1")));
    List<Set<String>> orderOfExecution = Arrays.asList(ImmutableSet.of("coll1/shard1/core_node2", "coll2/shard2"), ImmutableSet.of("coll1", "coll2"), ImmutableSet.of("coll1/shard1", "coll2/shard1"));
    lockTree = new LockTree();
    for (int counter = 0; counter < orderOfExecution.size(); counter++) {
        LockTree.Session session = lockTree.getSession();
        List<Pair<CollectionAction, List<String>>> completedOps = new CopyOnWriteArrayList<>();
        List<Lock> locks = new CopyOnWriteArrayList<>();
        List<Thread> threads = new ArrayList<>();
        for (int i = 0; i < operations.size(); i++) {
            Pair<CollectionAction, List<String>> operation = operations.get(i);
            final Lock lock = session.lock(operation.first(), operation.second());
            if (lock != null) {
                Thread thread = new Thread(getRunnable(completedOps, operation, locks, lock));
                threads.add(thread);
                thread.start();
            }
        }
        for (Thread thread : threads) thread.join();
        if (locks.isEmpty())
            throw new RuntimeException("Could not attain lock for anything " + operations);
        Set<String> expectedOps = orderOfExecution.get(counter);
        log.info("counter : {} , expected : {}, actual : {}", counter, expectedOps, locks);
        assertEquals(expectedOps.size(), locks.size());
        for (Lock lock : locks) assertTrue("locks : " + locks + " expectedOps : " + expectedOps, expectedOps.contains(lock.toString()));
        locks.clear();
        for (Pair<CollectionAction, List<String>> completedOp : completedOps) {
            operations.remove(completedOp);
        }
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Lock(org.apache.solr.cloud.OverseerMessageHandler.Lock) CollectionAction(org.apache.solr.common.params.CollectionParams.CollectionAction) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Pair(org.apache.solr.common.util.Pair) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 49 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project lucene-solr by apache.

the class FastLRUCache method initializeMetrics.

@Override
public void initializeMetrics(SolrMetricManager manager, String registryName, String scope) {
    registry = manager.registry(registryName);
    cacheMap = new MetricsMap((detailed, map) -> {
        if (cache != null) {
            ConcurrentLRUCache.Stats stats = cache.getStats();
            long lookups = stats.getCumulativeLookups();
            long hits = stats.getCumulativeHits();
            long inserts = stats.getCumulativePuts();
            long evictions = stats.getCumulativeEvictions();
            long size = stats.getCurrentSize();
            long clookups = 0;
            long chits = 0;
            long cinserts = 0;
            long cevictions = 0;
            // NOTE: It is safe to iterate on a CopyOnWriteArrayList
            for (ConcurrentLRUCache.Stats statistiscs : statsList) {
                clookups += statistiscs.getCumulativeLookups();
                chits += statistiscs.getCumulativeHits();
                cinserts += statistiscs.getCumulativePuts();
                cevictions += statistiscs.getCumulativeEvictions();
            }
            map.put("lookups", lookups);
            map.put("hits", hits);
            map.put("hitratio", calcHitRatio(lookups, hits));
            map.put("inserts", inserts);
            map.put("evictions", evictions);
            map.put("size", size);
            map.put("warmupTime", warmupTime);
            map.put("cumulative_lookups", clookups);
            map.put("cumulative_hits", chits);
            map.put("cumulative_hitratio", calcHitRatio(clookups, chits));
            map.put("cumulative_inserts", cinserts);
            map.put("cumulative_evictions", cevictions);
            if (detailed && showItems != 0) {
                Map items = cache.getLatestAccessedItems(showItems == -1 ? Integer.MAX_VALUE : showItems);
                for (Map.Entry e : (Set<Map.Entry>) items.entrySet()) {
                    Object k = e.getKey();
                    Object v = e.getValue();
                    String ks = "item_" + k;
                    String vs = v.toString();
                    map.put(ks, vs);
                }
            }
        }
    });
    manager.registerGauge(this, registryName, cacheMap, true, scope, getCategory().toString());
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) MethodHandles(java.lang.invoke.MethodHandles) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) MetricsMap(org.apache.solr.metrics.MetricsMap) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) SolrException(org.apache.solr.common.SolrException) List(java.util.List) ConcurrentLRUCache(org.apache.solr.util.ConcurrentLRUCache) Map(java.util.Map) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) MetricsMap(org.apache.solr.metrics.MetricsMap) MetricsMap(org.apache.solr.metrics.MetricsMap) Map(java.util.Map)

Example 50 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project undertow by undertow-io.

the class HttpClientTestCase method testSsl.

@Test
public void testSsl() throws Exception {
    //
    DefaultServer.setRootHandler(SIMPLE_MESSAGE_HANDLER);
    final UndertowClient client = createClient();
    final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
    final CountDownLatch latch = new CountDownLatch(10);
    DefaultServer.startSSLServer();
    SSLContext context = DefaultServer.getClientSSLContext();
    XnioSsl ssl = new UndertowXnioSsl(DefaultServer.getWorker().getXnio(), OptionMap.EMPTY, DefaultServer.SSL_BUFFER_POOL, context);
    final ClientConnection connection = client.connect(new URI(DefaultServer.getDefaultServerSSLAddress()), worker, ssl, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
    try {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath("/");
                    request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
                    connection.sendRequest(request, createClientCallback(responses, latch));
                }
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(10, responses.size());
        for (final ClientResponse response : responses) {
            Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
        }
    } finally {
        connection.getIoThread().execute(new Runnable() {

            @Override
            public void run() {
                IoUtils.safeClose(connection);
            }
        });
        DefaultServer.stopSSLServer();
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) XnioSsl(org.xnio.ssl.XnioSsl) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) UndertowClient(io.undertow.client.UndertowClient) SSLContext(javax.net.ssl.SSLContext) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientConnection(io.undertow.client.ClientConnection) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) ClientRequest(io.undertow.client.ClientRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Aggregations

CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)97 CountDownLatch (java.util.concurrent.CountDownLatch)38 IOException (java.io.IOException)23 ArrayList (java.util.ArrayList)23 List (java.util.List)23 Test (org.junit.Test)20 ExecutionException (java.util.concurrent.ExecutionException)15 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)13 CyclicBarrier (java.util.concurrent.CyclicBarrier)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)10 MockTransportService (org.elasticsearch.test.transport.MockTransportService)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 HashMap (java.util.HashMap)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 HashSet (java.util.HashSet)6 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 Settings (org.elasticsearch.common.settings.Settings)6 Config (com.hazelcast.config.Config)5