use of com.google.common.util.concurrent.ListeningExecutorService in project jackrabbit-oak by apache.
the class UploadStagingCacheTest method testConcurrentGetDelete.
/**
* Concurrently retrieve after stage but before upload.
* @throws Exception
*/
@Test
public void testConcurrentGetDelete() throws Exception {
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
closer.register(new ExecutorCloser(executorService));
// Add load
List<ListenableFuture<Integer>> futures = put(folder);
// Get a handle to the file and open stream
File file = stagingCache.getIfPresent(ID_PREFIX + 0);
final InputStream fStream = Files.asByteSource(file).openStream();
// task to copy the steam to a file simulating read from the stream
File temp = folder.newFile();
CountDownLatch copyThreadLatch = new CountDownLatch(1);
SettableFuture<File> future1 = copyStreamThread(executorService, fStream, temp, copyThreadLatch);
// start
taskLatch.countDown();
callbackLatch.countDown();
waitFinish(futures);
// trying copying now
copyThreadLatch.countDown();
future1.get();
assertTrue(Files.equal(temp, uploader.read(ID_PREFIX + 0)));
}
use of com.google.common.util.concurrent.ListeningExecutorService in project jackrabbit-oak by apache.
the class CompositeDataStoreCacheTest method concurrentGetCached.
/**
* Concurrently retrieves 2 different files from cache.
* @throws Exception
*/
@Test
public void concurrentGetCached() throws Exception {
LOG.info("Starting concurrentGetCached");
// Add 2 files to backend
// Concurrently get both
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
closer.register(new ExecutorCloser(executorService, 5, TimeUnit.MILLISECONDS));
File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
loader.write(ID_PREFIX + 0, f);
assertTrue(f.exists());
File f2 = copyToFile(randomStream(1, 4 * 1024), folder.newFile());
loader.write(ID_PREFIX + 1, f2);
assertTrue(f2.exists());
CountDownLatch thread1Start = new CountDownLatch(1);
SettableFuture<File> future1 = retrieveThread(executorService, ID_PREFIX + 0, cache, thread1Start);
CountDownLatch thread2Start = new CountDownLatch(1);
SettableFuture<File> future2 = retrieveThread(executorService, ID_PREFIX + 1, cache, thread2Start);
thread1Start.countDown();
thread2Start.countDown();
File cached = future1.get();
File cached2 = future2.get();
LOG.info("Async tasks finished");
assertTrue(Files.equal(f, cached));
assertTrue(Files.equal(f2, cached2));
assertCacheStats(cache.getStagingCacheStats(), 0, 0, 0, 0);
assertEquals(2, cache.getStagingCacheStats().getLoadCount());
assertEquals(0, cache.getStagingCacheStats().getLoadSuccessCount());
assertCacheStats(cache.getCacheStats(), 2, 8 * 1024, 0, 4);
assertEquals(2, cache.getCacheStats().getLoadCount());
assertEquals(2, cache.getCacheStats().getLoadSuccessCount());
LOG.info("Finished concurrentGetCached");
}
use of com.google.common.util.concurrent.ListeningExecutorService in project jackrabbit-oak by apache.
the class TarRevisionsTest method concurrentSetHeadFromFunction.
@Test
public void concurrentSetHeadFromFunction() throws InterruptedException, ExecutionException, TimeoutException {
ListeningExecutorService executor = listeningDecorator(newFixedThreadPool(2));
try {
ListenableFuture<Boolean> t1 = executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return null != revisions.setHead(new Function<RecordId, RecordId>() {
@Nullable
@Override
public RecordId apply(RecordId headId) {
return addChild(reader.readNode(headId), "a").getRecordId();
}
});
}
});
ListenableFuture<Boolean> t2 = executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return null != revisions.setHead(new Function<RecordId, RecordId>() {
@Nullable
@Override
public RecordId apply(RecordId headId) {
return addChild(reader.readNode(headId), "b").getRecordId();
}
});
}
});
assertTrue(t1.get(500, MILLISECONDS));
assertTrue(t2.get(500, MILLISECONDS));
SegmentNodeState root = reader.readNode(revisions.getHead());
assertTrue(root.hasChildNode("a"));
assertTrue(root.hasChildNode("b"));
} finally {
executor.shutdown();
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project cdap by caskdata.
the class MessageTableTest method testConcurrentWrites.
@Test
public void testConcurrentWrites() throws Exception {
// Create two threads, each of them writes to a different topic with two events in one store call.
// The iterators in the two threads would alternate to produce payload. This is for testing CDAP-12013
ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch storeCompletion = new CountDownLatch(2);
for (int i = 0; i < 2; i++) {
final TopicId topicId = NamespaceId.DEFAULT.topic("testConcurrentWrites" + i);
TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
try (MetadataTable metadataTable = getMetadataTable()) {
metadataTable.createTopic(metadata);
}
final int threadId = i;
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try (MessageTable messageTable = getMessageTable()) {
messageTable.store(new AbstractIterator<MessageTable.Entry>() {
int messageCount = 0;
@Override
protected MessageTable.Entry computeNext() {
if (messageCount >= 2) {
return endOfData();
}
try {
barrier.await();
} catch (Exception e) {
throw Throwables.propagate(e);
}
return new TestMessageEntry(topicId, GENERATION, System.currentTimeMillis(), messageCount, null, Bytes.toBytes("message " + threadId + " " + messageCount++));
}
});
storeCompletion.countDown();
} catch (Exception e) {
LOG.error("Failed to store to MessageTable", e);
}
return null;
}
});
}
executor.shutdown();
Assert.assertTrue(storeCompletion.await(30, TimeUnit.SECONDS));
// Read from each topic. Each topic should have two messages
for (int i = 0; i < 2; i++) {
TopicId topicId = NamespaceId.DEFAULT.topic("testConcurrentWrites" + i);
TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
try (MessageTable messageTable = getMessageTable();
CloseableIterator<MessageTable.Entry> iterator = messageTable.fetch(metadata, 0, 10, null)) {
List<MessageTable.Entry> entries = Lists.newArrayList(iterator);
Assert.assertEquals(2, entries.size());
int count = 0;
for (MessageTable.Entry entry : entries) {
Assert.assertEquals("message " + i + " " + count++, Bytes.toString(entry.getPayload()));
}
}
}
}
use of com.google.common.util.concurrent.ListeningExecutorService in project cdap by caskdata.
the class PayloadTableTest method testConcurrentWrites.
@Test
public void testConcurrentWrites() throws Exception {
// Create two threads, each of them writes to a different topic with two events in one store call.
// The iterators in the two threads would alternate to produce payload. This is for testing CDAP-12013
ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch storeCompletion = new CountDownLatch(2);
for (int i = 0; i < 2; i++) {
final TopicId topicId = NamespaceId.DEFAULT.topic("testConcurrentWrites" + i);
TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
try (MetadataTable metadataTable = getMetadataTable()) {
metadataTable.createTopic(metadata);
}
final int threadId = i;
executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try (PayloadTable payloadTable = getPayloadTable()) {
payloadTable.store(new AbstractIterator<PayloadTable.Entry>() {
short messageCount = 0;
@Override
protected PayloadTable.Entry computeNext() {
if (messageCount >= 2) {
return endOfData();
}
try {
barrier.await();
} catch (Exception e) {
throw Throwables.propagate(e);
}
return new TestPayloadEntry(topicId, GENERATION, threadId, 0, messageCount, Bytes.toBytes("message " + threadId + " " + messageCount++));
}
});
storeCompletion.countDown();
} catch (Exception e) {
LOG.error("Failed to store to MessageTable", e);
}
return null;
}
});
}
executor.shutdown();
Assert.assertTrue(storeCompletion.await(5, TimeUnit.SECONDS));
// Read from each topic. Each topic should have two messages
for (int i = 0; i < 2; i++) {
TopicId topicId = NamespaceId.DEFAULT.topic("testConcurrentWrites" + i);
TopicMetadata metadata = new TopicMetadata(topicId, DEFAULT_PROPERTY);
byte[] rawId = new byte[MessageId.RAW_ID_SIZE];
MessageId.putRawId(0L, (short) 0, 0, (short) 0, rawId, 0);
MessageId messageId = new MessageId(rawId);
try (PayloadTable payloadTable = getPayloadTable();
CloseableIterator<PayloadTable.Entry> iterator = payloadTable.fetch(metadata, i, messageId, true, 10)) {
List<PayloadTable.Entry> entries = Lists.newArrayList(iterator);
Assert.assertEquals(2, entries.size());
int count = 0;
for (PayloadTable.Entry entry : entries) {
Assert.assertEquals("message " + i + " " + count++, Bytes.toString(entry.getPayload()));
}
}
}
}
Aggregations