use of com.twitter.util.Future in project distributedlog by twitter.
the class TestDistributedLogServer method runSimpleBulkWriteTest.
private void runSimpleBulkWriteTest(int writeCount) throws Exception {
String name = String.format("dlserver-bulk-write-%d", writeCount);
dlClient.routingService.addHost(name, dlServer.getAddress());
List<ByteBuffer> writes = new ArrayList<ByteBuffer>(writeCount);
for (long i = 1; i <= writeCount; i++) {
writes.add(ByteBuffer.wrap(("" + i).getBytes()));
}
logger.debug("Write {} entries to stream {}.", writeCount, name);
List<Future<DLSN>> futures = dlClient.dlClient.writeBulk(name, writes);
assertEquals(futures.size(), writeCount);
for (Future<DLSN> future : futures) {
// No throw == pass.
DLSN dlsn = Await.result(future, Duration.fromSeconds(10));
}
DistributedLogManager dlm = DLMTestUtil.createNewDLM(name, conf, getUri());
LogReader reader = dlm.getInputStream(1);
int numRead = 0;
LogRecord r = reader.readNext(false);
while (null != r) {
int i = Integer.parseInt(new String(r.getPayload()));
assertEquals(numRead + 1, i);
++numRead;
r = reader.readNext(false);
}
assertEquals(writeCount, numRead);
reader.close();
dlm.close();
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class TestDistributedLogService method testCloseShouldErrorOutPendingOps.
@Test(timeout = 60000)
public void testCloseShouldErrorOutPendingOps() throws Exception {
String streamName = testName.getMethodName();
StreamImpl s = createUnstartedStream(service, streamName);
int numWrites = 10;
List<Future<WriteResponse>> futureList = new ArrayList<Future<WriteResponse>>(numWrites);
for (int i = 0; i < numWrites; i++) {
WriteOp op = createWriteOp(service, streamName, i);
s.submit(op);
futureList.add(op.result());
}
assertEquals(numWrites, s.numPendingOps());
Await.result(s.requestClose("close stream"));
assertEquals("Stream " + streamName + " is set to " + StreamStatus.CLOSED, StreamStatus.CLOSED, s.getStatus());
for (int i = 0; i < numWrites; i++) {
Future<WriteResponse> future = futureList.get(i);
WriteResponse wr = Await.result(future);
assertEquals("Pending op should fail with " + StatusCode.STREAM_UNAVAILABLE, StatusCode.STREAM_UNAVAILABLE, wr.getHeader().getCode());
}
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class TestDistributedLogService method testCloseTwice.
@Test(timeout = 60000)
public void testCloseTwice() throws Exception {
String streamName = testName.getMethodName();
StreamImpl s = createUnstartedStream(service, streamName);
int numWrites = 10;
List<Future<WriteResponse>> futureList = new ArrayList<Future<WriteResponse>>(numWrites);
for (int i = 0; i < numWrites; i++) {
WriteOp op = createWriteOp(service, streamName, i);
s.submit(op);
futureList.add(op.result());
}
assertEquals(numWrites, s.numPendingOps());
Future<Void> closeFuture0 = s.requestClose("close 0");
assertTrue("Stream " + streamName + " should be set to " + StreamStatus.CLOSING, StreamStatus.CLOSING == s.getStatus() || StreamStatus.CLOSED == s.getStatus());
Future<Void> closeFuture1 = s.requestClose("close 1");
assertTrue("Stream " + streamName + " should be set to " + StreamStatus.CLOSING, StreamStatus.CLOSING == s.getStatus() || StreamStatus.CLOSED == s.getStatus());
Await.result(closeFuture0);
assertEquals("Stream " + streamName + " should be set to " + StreamStatus.CLOSED, StreamStatus.CLOSED, s.getStatus());
Await.result(closeFuture1);
assertEquals("Stream " + streamName + " should be set to " + StreamStatus.CLOSED, StreamStatus.CLOSED, s.getStatus());
for (int i = 0; i < numWrites; i++) {
Future<WriteResponse> future = futureList.get(i);
WriteResponse wr = Await.result(future);
assertEquals("Pending op should fail with " + StatusCode.STREAM_UNAVAILABLE, StatusCode.STREAM_UNAVAILABLE, wr.getHeader().getCode());
}
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class TestZKLogSegmentMetadataStore method testGetLogSegmentNames.
@Test(timeout = 60000)
public void testGetLogSegmentNames() throws Exception {
Transaction<Object> createTxn = lsmStore.transaction();
List<LogSegmentMetadata> createdSegments = Lists.newArrayListWithExpectedSize(10);
for (int i = 0; i < 10; i++) {
LogSegmentMetadata segment = createLogSegment(i);
createdSegments.add(segment);
lsmStore.createLogSegment(createTxn, segment);
}
FutureUtils.result(createTxn.execute());
String rootPath = "/" + runtime.getMethodName();
List<String> children = zkc.get().getChildren(rootPath, false);
Collections.sort(children);
assertEquals("Should find 10 log segments", 10, children.size());
List<String> logSegmentNames = FutureUtils.result(lsmStore.getLogSegmentNames(rootPath));
Collections.sort(logSegmentNames);
assertEquals("Should find 10 log segments", 10, logSegmentNames.size());
assertEquals(children, logSegmentNames);
List<Future<LogSegmentMetadata>> getFutures = Lists.newArrayListWithExpectedSize(10);
for (int i = 0; i < 10; i++) {
getFutures.add(lsmStore.getLogSegment(rootPath + "/" + logSegmentNames.get(i)));
}
List<LogSegmentMetadata> segments = FutureUtils.result(Future.collect(getFutures));
for (int i = 0; i < 10; i++) {
assertEquals(createdSegments.get(i), segments.get(i));
}
}
use of com.twitter.util.Future in project distributedlog by twitter.
the class BKDistributedLogManager method asyncCreateWriteHandler.
Future<BKLogWriteHandler> asyncCreateWriteHandler(final boolean lockHandler) {
final ZooKeeper zk;
try {
zk = writerZKC.get();
} catch (InterruptedException e) {
LOG.error("Failed to initialize zookeeper client : ", e);
return Future.exception(new DLInterruptedException("Failed to initialize zookeeper client", e));
} catch (ZooKeeperClient.ZooKeeperConnectionException e) {
return Future.exception(FutureUtils.zkException(e, uri.getPath()));
}
boolean ownAllocator = null == ledgerAllocator;
// Fetching Log Metadata
Future<ZKLogMetadataForWriter> metadataFuture = ZKLogMetadataForWriter.of(uri, name, streamIdentifier, zk, writerZKC.getDefaultACL(), ownAllocator, conf.getCreateStreamIfNotExists() || ownAllocator);
return metadataFuture.flatMap(new AbstractFunction1<ZKLogMetadataForWriter, Future<BKLogWriteHandler>>() {
@Override
public Future<BKLogWriteHandler> apply(ZKLogMetadataForWriter logMetadata) {
Promise<BKLogWriteHandler> createPromise = new Promise<BKLogWriteHandler>();
createWriteHandler(logMetadata, lockHandler, createPromise);
return createPromise;
}
});
}
Aggregations