use of java.util.concurrent.FutureTask in project hive by apache.
the class TestLowLevelCacheImpl method testMTTWithCleanup.
@Test
public void testMTTWithCleanup() {
final LowLevelCacheImpl cache = new LowLevelCacheImpl(LlapDaemonCacheMetrics.create("test", "1"), new DummyCachePolicy(), new DummyAllocator(), true, 1);
final long fn1 = 1, fn2 = 2;
final int offsetsToUse = 8;
final CountDownLatch cdlIn = new CountDownLatch(4), cdlOut = new CountDownLatch(1);
final AtomicInteger rdmsDone = new AtomicInteger(0);
Callable<Long> rdmCall = new Callable<Long>() {
public Long call() {
int gets = 0, puts = 0;
try {
Random rdm = new Random(1234 + Thread.currentThread().getId());
syncThreadStart(cdlIn, cdlOut);
for (int i = 0; i < 20000; ++i) {
boolean isGet = rdm.nextBoolean(), isFn1 = rdm.nextBoolean();
long fileName = isFn1 ? fn1 : fn2;
int fileIndex = isFn1 ? 1 : 2;
int count = rdm.nextInt(offsetsToUse);
if (isGet) {
int[] offsets = new int[count];
count = generateOffsets(offsetsToUse, rdm, offsets);
CreateHelper list = new CreateHelper();
for (int j = 0; i < count; ++i) {
list.addOrMerge(offsets[j], offsets[j] + 1, true, false);
}
DiskRangeList iter = cache.getFileData(fileName, list.get(), 0, testFactory, null, null);
int j = -1;
while (iter != null) {
++j;
if (!(iter instanceof CacheChunk)) {
iter = iter.next;
continue;
}
++gets;
LlapDataBuffer result = (LlapDataBuffer) ((CacheChunk) iter).getBuffer();
assertEquals(makeFakeArenaIndex(fileIndex, offsets[j]), result.arenaIndex);
cache.decRefBuffer(result);
iter = iter.next;
}
} else {
DiskRange[] ranges = new DiskRange[count];
int[] offsets = new int[count];
for (int j = 0; j < count; ++j) {
int next = rdm.nextInt(offsetsToUse);
ranges[j] = dr(next, next + 1);
offsets[j] = next;
}
MemoryBuffer[] buffers = new MemoryBuffer[count];
for (int j = 0; j < offsets.length; ++j) {
LlapDataBuffer buf = LowLevelCacheImpl.allocateFake();
buf.arenaIndex = makeFakeArenaIndex(fileIndex, offsets[j]);
buffers[j] = buf;
}
long[] mask = cache.putFileData(fileName, ranges, buffers, 0, Priority.NORMAL, null);
puts += buffers.length;
long maskVal = 0;
if (mask != null) {
assertEquals(1, mask.length);
maskVal = mask[0];
}
for (int j = 0; j < offsets.length; ++j) {
LlapDataBuffer buf = (LlapDataBuffer) (buffers[j]);
if ((maskVal & 1) == 1) {
assertEquals(makeFakeArenaIndex(fileIndex, offsets[j]), buf.arenaIndex);
}
maskVal >>= 1;
cache.decRefBuffer(buf);
}
}
}
} finally {
rdmsDone.incrementAndGet();
}
return (((long) gets) << 32) | puts;
}
private int makeFakeArenaIndex(int fileIndex, long offset) {
return (int) ((fileIndex << 16) + offset);
}
};
FutureTask<Integer> evictionTask = new FutureTask<Integer>(new Callable<Integer>() {
public Integer call() {
boolean isFirstFile = false;
Random rdm = new Random(1234 + Thread.currentThread().getId());
int evictions = 0;
syncThreadStart(cdlIn, cdlOut);
while (rdmsDone.get() < 3) {
DiskRangeList head = new DiskRangeList(0, offsetsToUse + 1);
isFirstFile = !isFirstFile;
long fileId = isFirstFile ? fn1 : fn2;
head = cache.getFileData(fileId, head, 0, testFactory, null, null);
DiskRange[] results = head.listToArray();
int startIndex = rdm.nextInt(results.length), index = startIndex;
LlapDataBuffer victim = null;
do {
DiskRange r = results[index];
if (r instanceof CacheChunk) {
LlapDataBuffer result = (LlapDataBuffer) ((CacheChunk) r).getBuffer();
cache.decRefBuffer(result);
if (victim == null && result.invalidate()) {
++evictions;
victim = result;
}
}
++index;
if (index == results.length)
index = 0;
} while (index != startIndex);
if (victim == null)
continue;
cache.notifyEvicted(victim);
}
return evictions;
}
});
FutureTask<Long> rdmTask1 = new FutureTask<Long>(rdmCall), rdmTask2 = new FutureTask<Long>(rdmCall), rdmTask3 = new FutureTask<Long>(rdmCall);
Executor threadPool = Executors.newFixedThreadPool(4);
threadPool.execute(rdmTask1);
threadPool.execute(rdmTask2);
threadPool.execute(rdmTask3);
threadPool.execute(evictionTask);
try {
cdlIn.await();
cdlOut.countDown();
long result1 = rdmTask1.get(), result2 = rdmTask2.get(), result3 = rdmTask3.get();
int evictions = evictionTask.get();
LOG.info("MTT test: task 1: " + descRdmTask(result1) + ", task 2: " + descRdmTask(result2) + ", task 3: " + descRdmTask(result3) + "; " + evictions + " evictions");
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
use of java.util.concurrent.FutureTask in project hive by apache.
the class TestSlotZnode method concurrencyTest.
private void concurrencyTest(final int nodeCount, boolean isFallback) throws Exception {
final CuratorFramework curator = newCurator();
ExecutorService executor = Executors.newFixedThreadPool(nodeCount);
final CountDownLatch cdlIn = new CountDownLatch(nodeCount), cdlOut = new CountDownLatch(1);
@SuppressWarnings("unchecked") FutureTask<SlotZnode>[] startTasks = new FutureTask[nodeCount];
for (int i = 0; i < nodeCount; ++i) {
if (isFallback) {
curator.create().creatingParentsIfNeeded().forPath(PATH + "/worker-" + i);
}
final int ix = i;
startTasks[i] = new FutureTask<SlotZnode>(new Callable<SlotZnode>() {
SlotZnode node = createZnode(curator);
public SlotZnode call() throws Exception {
syncThreadStart(cdlIn, cdlOut);
int id = System.identityHashCode(node);
LOG.info("Starting the node " + id + " from task #" + ix);
boolean result = node.start(30, TimeUnit.SECONDS);
LOG.info((result ? "Started" : "Failed to start") + " the node from task #" + ix);
return result ? node : null;
}
});
}
for (int i = 0; i < startTasks.length; ++i) {
executor.execute(startTasks[i]);
}
cdlIn.await();
cdlOut.countDown();
boolean[] found = new boolean[nodeCount];
int totalFallbackCount = 0;
for (int i = 0; i < startTasks.length; ++i) {
SlotZnode node = startTasks[i].get();
assertNotNull("failed to start the node from task #" + i, node);
totalFallbackCount += node.getFallbackCount();
int slot = node.getCurrentSlot();
assertTrue(slot < found.length);
// Given these 2 lines we don't need to double check later.
assertFalse(found[slot]);
found[slot] = true;
}
if (isFallback) {
LOG.info("Total fallback count " + totalFallbackCount);
assertTrue(totalFallbackCount > 0);
}
}
use of java.util.concurrent.FutureTask in project che by eclipse.
the class WSocketEventBusClient method connect.
private void connect(final URI wsUri, final Collection<String> channels) throws IOException, DeploymentException {
Future<WSClient> clientFuture = connections.get(wsUri);
if (clientFuture == null) {
FutureTask<WSClient> newFuture = new FutureTask<>(() -> {
WSClient wsClient = new WSClient(wsUri, new WSocketListener(wsUri, channels));
wsClient.connect((int) WS_CONNECTION_TIMEOUT);
return wsClient;
});
clientFuture = connections.putIfAbsent(wsUri, newFuture);
if (clientFuture == null) {
clientFuture = newFuture;
newFuture.run();
}
}
boolean connected = false;
try {
// wait for connection
clientFuture.get();
connected = true;
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
if (cause instanceof Error) {
throw (Error) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof DeploymentException)
throw (DeploymentException) cause;
throw new RuntimeException(e);
} catch (InterruptedException e) {
LOG.info("Client interrupted " + e.getLocalizedMessage());
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} finally {
if (!connected) {
connections.remove(wsUri);
}
}
}
use of java.util.concurrent.FutureTask in project android_frameworks_base by ParanoidAndroid.
the class ViewDebug method callMethodOnAppropriateTheadBlocking.
private static Object callMethodOnAppropriateTheadBlocking(final Method method, final Object object) throws IllegalAccessException, InvocationTargetException, TimeoutException {
if (!(object instanceof View)) {
return method.invoke(object, (Object[]) null);
}
final View view = (View) object;
Callable<Object> callable = new Callable<Object>() {
@Override
public Object call() throws IllegalAccessException, InvocationTargetException {
return method.invoke(view, (Object[]) null);
}
};
FutureTask<Object> future = new FutureTask<Object>(callable);
// Try to use the handler provided by the view
Handler handler = view.getHandler();
// Fall back on using the main thread
if (handler == null) {
handler = new Handler(android.os.Looper.getMainLooper());
}
handler.post(future);
while (true) {
try {
return future.get(CAPTURE_TIMEOUT, java.util.concurrent.TimeUnit.MILLISECONDS);
} catch (ExecutionException e) {
Throwable t = e.getCause();
if (t instanceof IllegalAccessException) {
throw (IllegalAccessException) t;
}
if (t instanceof InvocationTargetException) {
throw (InvocationTargetException) t;
}
throw new RuntimeException("Unexpected exception", t);
} catch (InterruptedException e) {
// Call get again
} catch (CancellationException e) {
throw new RuntimeException("Unexpected cancellation exception", e);
}
}
}
use of java.util.concurrent.FutureTask in project RxJava by ReactiveX.
the class ScheduledRunnableTest method setFutureRunRace.
@Test
public void setFutureRunRace() {
for (int i = 0; i < 500; i++) {
CompositeDisposable set = new CompositeDisposable();
final ScheduledRunnable run = new ScheduledRunnable(Functions.EMPTY_RUNNABLE, set);
set.add(run);
final FutureTask<Object> ft = new FutureTask<Object>(Functions.EMPTY_RUNNABLE, 0);
Runnable r1 = new Runnable() {
@Override
public void run() {
run.setFuture(ft);
}
};
Runnable r2 = new Runnable() {
@Override
public void run() {
run.run();
}
};
TestHelper.race(r1, r2);
assertEquals(0, set.size());
}
}
Aggregations