use of java.util.concurrent.atomic.AtomicReferenceArray in project elasticsearch by elastic.
the class TransportGetFieldMappingsAction method doExecute.
@Override
protected void doExecute(GetFieldMappingsRequest request, final ActionListener<GetFieldMappingsResponse> listener) {
ClusterState clusterState = clusterService.state();
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request);
final AtomicInteger indexCounter = new AtomicInteger();
final AtomicInteger completionCounter = new AtomicInteger(concreteIndices.length);
final AtomicReferenceArray<Object> indexResponses = new AtomicReferenceArray<>(concreteIndices.length);
if (concreteIndices.length == 0) {
listener.onResponse(new GetFieldMappingsResponse());
} else {
boolean probablySingleFieldRequest = concreteIndices.length == 1 && request.types().length == 1 && request.fields().length == 1;
for (final String index : concreteIndices) {
GetFieldMappingsIndexRequest shardRequest = new GetFieldMappingsIndexRequest(request, index, probablySingleFieldRequest);
shardAction.execute(shardRequest, new ActionListener<GetFieldMappingsResponse>() {
@Override
public void onResponse(GetFieldMappingsResponse result) {
indexResponses.set(indexCounter.getAndIncrement(), result);
if (completionCounter.decrementAndGet() == 0) {
listener.onResponse(merge(indexResponses));
}
}
@Override
public void onFailure(Exception e) {
int index = indexCounter.getAndIncrement();
indexResponses.set(index, e);
if (completionCounter.decrementAndGet() == 0) {
listener.onResponse(merge(indexResponses));
}
}
});
}
}
}
use of java.util.concurrent.atomic.AtomicReferenceArray in project caffeine by ben-manes.
the class ConcurrentTestHarness method timeTasks.
/**
* Executes a task, on N threads, all starting at the same time.
*
* @param nThreads the number of threads to execute
* @param task the task to execute in each thread
* @return the result of each task and the full execution time, in nanoseconds
*/
public static <T> TestResult<T> timeTasks(int nThreads, Callable<T> task) {
CountDownLatch startGate = new CountDownLatch(1);
CountDownLatch endGate = new CountDownLatch(nThreads);
AtomicReferenceArray<T> results = new AtomicReferenceArray<T>(nThreads);
for (int i = 0; i < nThreads; i++) {
final int index = i;
executor.execute(() -> {
try {
startGate.await();
try {
results.set(index, task.call());
} finally {
endGate.countDown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
long start = System.nanoTime();
startGate.countDown();
Uninterruptibles.awaitUninterruptibly(endGate);
long end = System.nanoTime();
return new TestResult<T>(end - start, toList(results));
}
use of java.util.concurrent.atomic.AtomicReferenceArray in project caffeine by ben-manes.
the class CacheLoadingTest method doConcurrentGet.
/**
* Test-helper method that performs {@code nThreads} concurrent calls to {@code cache.get(key)}
* or {@code cache.getUnchecked(key)}, and returns a List containing each of the results. The
* result for any given call to {@code cache.get} or {@code cache.getUnchecked} is the value
* returned, or the exception thrown.
*
* <p>As we iterate from {@code 0} to {@code nThreads}, threads with an even index will call
* {@code getUnchecked}, and threads with an odd index will call {@code get}. If the cache throws
* exceptions, this difference may be visible in the returned List.
*/
private static <K> List<Object> doConcurrentGet(final LoadingCache<K, ?> cache, final K key, int nThreads, final CountDownLatch gettersStartedSignal) throws InterruptedException {
final AtomicReferenceArray<Object> result = new AtomicReferenceArray<Object>(nThreads);
final CountDownLatch gettersComplete = new CountDownLatch(nThreads);
AtomicBoolean ready = new AtomicBoolean();
for (int i = 0; i < nThreads; i++) {
final int index = i;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
gettersStartedSignal.countDown();
Object value = null;
try {
int mod = index % 2;
ready.set(true);
if (mod == 0) {
value = cache.get(key);
} else if (mod == 1) {
value = cache.getUnchecked(key);
}
result.set(index, value);
} catch (Throwable t) {
result.set(index, t);
}
gettersComplete.countDown();
}
});
thread.start();
// (in startSignal.await()), and the others waiting for that thread's result.
while (thread.isAlive() && !ready.get()) {
Thread.yield();
}
}
gettersStartedSignal.countDown();
gettersComplete.await();
List<Object> resultList = Lists.newArrayListWithExpectedSize(nThreads);
for (int i = 0; i < nThreads; i++) {
resultList.add(result.get(i));
}
return resultList;
}
use of java.util.concurrent.atomic.AtomicReferenceArray in project guava by hceylan.
the class CacheLoadingTest method doConcurrentGet.
/**
* Test-helper method that performs {@code nThreads} concurrent calls to {@code cache.get(key)}
* or {@code cache.getUnchecked(key)}, and returns a List containing each of the results. The
* result for any given call to {@code cache.get} or {@code cache.getUnchecked} is the value
* returned, or the exception thrown.
*
* <p>As we iterate from {@code 0} to {@code nThreads}, threads with an even index will call
* {@code getUnchecked}, and threads with an odd index will call {@code get}. If the cache throws
* exceptions, this difference may be visible in the returned List.
*/
private static <K> List<Object> doConcurrentGet(final LoadingCache<K, ?> cache, final K key, int nThreads, final CountDownLatch gettersStartedSignal) throws InterruptedException {
final AtomicReferenceArray<Object> result = new AtomicReferenceArray<Object>(nThreads);
final CountDownLatch gettersComplete = new CountDownLatch(nThreads);
for (int i = 0; i < nThreads; i++) {
final int index = i;
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
gettersStartedSignal.countDown();
Object value = null;
try {
int mod = index % 3;
if (mod == 0) {
value = cache.get(key);
} else if (mod == 1) {
value = cache.getUnchecked(key);
} else {
cache.refresh(key);
value = cache.get(key);
}
result.set(index, value);
} catch (Throwable t) {
result.set(index, t);
}
gettersComplete.countDown();
}
});
thread.start();
// (in startSignal.await()), and the others waiting for that thread's result.
while (thread.isAlive() && thread.getState() != Thread.State.WAITING) {
Thread.yield();
}
}
gettersStartedSignal.countDown();
gettersComplete.await();
List<Object> resultList = Lists.newArrayListWithExpectedSize(nThreads);
for (int i = 0; i < nThreads; i++) {
resultList.add(result.get(i));
}
return resultList;
}
use of java.util.concurrent.atomic.AtomicReferenceArray in project guava by google.
the class CacheLoadingTest method ignoreTestExpandDuringRefresh.
// Test ignored because it is extremely flaky in CI builds
public void ignoreTestExpandDuringRefresh() throws InterruptedException, ExecutionException {
final AtomicInteger callCount = new AtomicInteger();
// tells the computing thread when to start computing
final CountDownLatch computeSignal = new CountDownLatch(1);
// tells the main thread when computation is pending
final CountDownLatch secondSignal = new CountDownLatch(1);
// tells the main thread when the second get has started
final CountDownLatch thirdSignal = new CountDownLatch(1);
// tells the main thread when the third get has started
final CountDownLatch fourthSignal = new CountDownLatch(1);
// tells the test when all gets have returned
final CountDownLatch doneSignal = new CountDownLatch(3);
final String suffix = "Suffix";
CacheLoader<String, String> computeFunction = new CacheLoader<String, String>() {
@Override
public String load(String key) throws InterruptedException {
callCount.incrementAndGet();
secondSignal.countDown();
computeSignal.await();
return key + suffix;
}
};
final AtomicReferenceArray<String> result = new AtomicReferenceArray<String>(2);
final LoadingCache<String, String> cache = CacheBuilder.newBuilder().build(computeFunction);
final String key = "bar";
cache.asMap().put(key, key);
// start computing thread
new Thread() {
@Override
public void run() {
cache.refresh(key);
doneSignal.countDown();
}
}.start();
// wait for computation to start
secondSignal.await();
checkNothingLogged();
// start waiting thread
new Thread() {
@Override
public void run() {
thirdSignal.countDown();
result.set(0, cache.getUnchecked(key));
doneSignal.countDown();
}
}.start();
// give the second get a chance to run; it is okay for this to be racy
// as the end result should be the same either way
thirdSignal.await();
Thread.yield();
// Expand!
CacheTesting.forceExpandSegment(cache, key);
// start another waiting thread
new Thread() {
@Override
public void run() {
fourthSignal.countDown();
result.set(1, cache.getUnchecked(key));
doneSignal.countDown();
}
}.start();
// give the third get a chance to run; it is okay for this to be racy
// as the end result should be the same either way
fourthSignal.await();
Thread.yield();
// let computation finish
computeSignal.countDown();
doneSignal.await();
assertTrue(callCount.get() == 1);
assertEquals(key, result.get(0));
assertEquals(key, result.get(1));
assertEquals(key + suffix, cache.getUnchecked(key));
}
Aggregations