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