Search in sources :

Example 36 with Future

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();
}
Also used : DLSN(com.twitter.distributedlog.DLSN) LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) LogRecord(com.twitter.distributedlog.LogRecord) DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) AsyncLogReader(com.twitter.distributedlog.AsyncLogReader) LogReader(com.twitter.distributedlog.LogReader) Future(com.twitter.util.Future)

Example 37 with Future

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());
    }
}
Also used : WriteOp(com.twitter.distributedlog.service.stream.WriteOp) StreamImpl(com.twitter.distributedlog.service.stream.StreamImpl) ArrayList(java.util.ArrayList) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) Future(com.twitter.util.Future) Test(org.junit.Test)

Example 38 with Future

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());
    }
}
Also used : WriteOp(com.twitter.distributedlog.service.stream.WriteOp) StreamImpl(com.twitter.distributedlog.service.stream.StreamImpl) ArrayList(java.util.ArrayList) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) Future(com.twitter.util.Future) Test(org.junit.Test)

Example 39 with Future

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));
    }
}
Also used : LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) Future(com.twitter.util.Future) Test(org.junit.Test)

Example 40 with Future

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;
        }
    });
}
Also used : DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) ZKLogMetadataForWriter(com.twitter.distributedlog.impl.metadata.ZKLogMetadataForWriter) Promise(com.twitter.util.Promise) ZooKeeper(org.apache.zookeeper.ZooKeeper) Future(com.twitter.util.Future) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException)

Aggregations

Future (com.twitter.util.Future)40 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)24 ByteBuffer (java.nio.ByteBuffer)9 Promise (com.twitter.util.Promise)8 ZKDistributedLock (com.twitter.distributedlog.lock.ZKDistributedLock)7 WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)7 List (java.util.List)6 DLSN (com.twitter.distributedlog.DLSN)5 FutureEventListener (com.twitter.util.FutureEventListener)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)5 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)4 StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)4 StreamManagerImpl (com.twitter.distributedlog.service.stream.StreamManagerImpl)4 IOException (java.io.IOException)4 LogRecordWithDLSN (com.twitter.distributedlog.LogRecordWithDLSN)3 DynamicDistributedLogConfiguration (com.twitter.distributedlog.config.DynamicDistributedLogConfiguration)3 BKTransmitException (com.twitter.distributedlog.exceptions.BKTransmitException)3 WriteCancelledException (com.twitter.distributedlog.exceptions.WriteCancelledException)3