Search in sources :

Example 16 with Future

use of com.twitter.util.Future in project distributedlog by twitter.

the class TestDistributedLogServer method testBulkWriteEmptyBuffer.

@Test(timeout = 60000)
public void testBulkWriteEmptyBuffer() throws Exception {
    String name = String.format("dlserver-bulk-write-%s", "empty");
    dlClient.routingService.addHost(name, dlServer.getAddress());
    List<ByteBuffer> writes = new ArrayList<ByteBuffer>();
    writes.add(ByteBuffer.wrap(("").getBytes()));
    writes.add(ByteBuffer.wrap(("").getBytes()));
    List<Future<DLSN>> futures = dlClient.dlClient.writeBulk(name, writes);
    assertEquals(2, futures.size());
    for (Future<DLSN> future : futures) {
        // No throw == pass
        DLSN dlsn = Await.result(future, Duration.fromSeconds(10));
    }
}
Also used : DLSN(com.twitter.distributedlog.DLSN) LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) ArrayList(java.util.ArrayList) Future(com.twitter.util.Future) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 17 with Future

use of com.twitter.util.Future in project distributedlog by twitter.

the class TestDistributedLogService method testServiceTimeout.

@Test(timeout = 60000)
public void testServiceTimeout() throws Exception {
    DistributedLogConfiguration confLocal = newLocalConf();
    confLocal.setOutputBufferSize(Integer.MAX_VALUE).setImmediateFlushEnabled(false).setPeriodicFlushFrequencyMilliSeconds(0);
    ServerConfiguration serverConfLocal = newLocalServerConf();
    serverConfLocal.addConfiguration(serverConf);
    serverConfLocal.setServiceTimeoutMs(200).setStreamProbationTimeoutMs(100);
    String streamName = testName.getMethodName();
    // create a new service with 200ms timeout
    DistributedLogServiceImpl localService = createService(serverConfLocal, confLocal);
    StreamManagerImpl streamManager = (StreamManagerImpl) localService.getStreamManager();
    int numWrites = 10;
    List<Future<WriteResponse>> futureList = new ArrayList<Future<WriteResponse>>(numWrites);
    for (int i = 0; i < numWrites; i++) {
        futureList.add(localService.write(streamName, createRecord(i)));
    }
    assertTrue("Stream " + streamName + " should be cached", streamManager.getCachedStreams().containsKey(streamName));
    StreamImpl s = (StreamImpl) streamManager.getCachedStreams().get(streamName);
    // the stream should be set CLOSING
    while (StreamStatus.CLOSING != s.getStatus() && StreamStatus.CLOSED != s.getStatus()) {
        TimeUnit.MILLISECONDS.sleep(20);
    }
    assertNotNull("Writer should be initialized", s.getWriter());
    assertNull("No exception should be thrown", s.getLastException());
    Future<Void> closeFuture = s.getCloseFuture();
    Await.result(closeFuture);
    for (int i = 0; i < numWrites; i++) {
        assertTrue("Write should not fail before closing", futureList.get(i).isDefined());
        WriteResponse response = Await.result(futureList.get(i));
        assertTrue("Op should fail with " + StatusCode.WRITE_CANCELLED_EXCEPTION, StatusCode.BK_TRANSMIT_ERROR == response.getHeader().getCode() || StatusCode.WRITE_EXCEPTION == response.getHeader().getCode() || StatusCode.WRITE_CANCELLED_EXCEPTION == response.getHeader().getCode());
    }
    while (streamManager.getCachedStreams().containsKey(streamName)) {
        TimeUnit.MILLISECONDS.sleep(20);
    }
    assertFalse("Stream should be removed from cache", streamManager.getCachedStreams().containsKey(streamName));
    assertFalse("Stream should be removed from acquired cache", streamManager.getAcquiredStreams().containsKey(streamName));
    localService.shutdown();
}
Also used : ServerConfiguration(com.twitter.distributedlog.service.config.ServerConfiguration) ArrayList(java.util.ArrayList) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl) DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) StreamImpl(com.twitter.distributedlog.service.stream.StreamImpl) Future(com.twitter.util.Future) Test(org.junit.Test)

Example 18 with Future

use of com.twitter.util.Future in project distributedlog by twitter.

the class ZKLogMetadataForWriter method checkLogMetadataPaths.

static Future<List<Versioned<byte[]>>> checkLogMetadataPaths(ZooKeeper zk, String logRootPath, boolean ownAllocator) {
    // Note re. persistent lock state initialization: the read lock persistent state (path) is
    // initialized here but only used in the read handler. The reason is its more convenient and
    // less error prone to manage all stream structure in one place.
    final String logRootParentPath = new File(logRootPath).getParent();
    final String logSegmentsPath = logRootPath + LOGSEGMENTS_PATH;
    final String maxTxIdPath = logRootPath + MAX_TXID_PATH;
    final String lockPath = logRootPath + LOCK_PATH;
    final String readLockPath = logRootPath + READ_LOCK_PATH;
    final String versionPath = logRootPath + VERSION_PATH;
    final String allocationPath = logRootPath + ALLOCATION_PATH;
    int numPaths = ownAllocator ? MetadataIndex.ALLOCATION + 1 : MetadataIndex.LOGSEGMENTS + 1;
    List<Future<Versioned<byte[]>>> checkFutures = Lists.newArrayListWithExpectedSize(numPaths);
    checkFutures.add(Utils.zkGetData(zk, logRootParentPath, false));
    checkFutures.add(Utils.zkGetData(zk, logRootPath, false));
    checkFutures.add(Utils.zkGetData(zk, maxTxIdPath, false));
    checkFutures.add(Utils.zkGetData(zk, versionPath, false));
    checkFutures.add(Utils.zkGetData(zk, lockPath, false));
    checkFutures.add(Utils.zkGetData(zk, readLockPath, false));
    checkFutures.add(Utils.zkGetData(zk, logSegmentsPath, false));
    if (ownAllocator) {
        checkFutures.add(Utils.zkGetData(zk, allocationPath, false));
    }
    return Future.collect(checkFutures);
}
Also used : Future(com.twitter.util.Future) File(java.io.File)

Example 19 with Future

use of com.twitter.util.Future in project distributedlog by twitter.

the class TestDistributedLock method testAsyncAcquireBasics.

@Test(timeout = 60000)
public void testAsyncAcquireBasics() throws Exception {
    TestLockFactory locks = new TestLockFactory(runtime.getMethodName(), zkc, lockStateExecutor);
    int count = 3;
    ArrayList<Future<ZKDistributedLock>> results = new ArrayList<Future<ZKDistributedLock>>(count);
    ZKDistributedLock[] lockArray = new ZKDistributedLock[count];
    final CountDownLatch[] latches = new CountDownLatch[count];
    // the future.
    for (int i = 0; i < count; i++) {
        latches[i] = new CountDownLatch(1);
        lockArray[i] = locks.createLock(i, zkc);
        final int index = i;
        results.add(lockArray[i].asyncAcquire().addEventListener(new FutureEventListener<ZKDistributedLock>() {

            @Override
            public void onSuccess(ZKDistributedLock lock) {
                latches[index].countDown();
            }

            @Override
            public void onFailure(Throwable cause) {
                fail("unexpected failure " + cause);
            }
        }));
    }
    // acquired).
    for (int i = 0; i < count; i++) {
        latches[i].await();
        assertLatchesSet(latches, i + 1);
        Await.result(results.get(i));
        FutureUtils.result(lockArray[i].asyncClose());
    }
}
Also used : ArrayList(java.util.ArrayList) Future(com.twitter.util.Future) FutureEventListener(com.twitter.util.FutureEventListener) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 20 with Future

use of com.twitter.util.Future in project distributedlog by twitter.

the class DistributedLogAdmin method checkStream.

private static StreamCandidate checkStream(final com.twitter.distributedlog.DistributedLogManagerFactory factory, final String streamName, final ExecutorService executorService, final BookKeeperClient bkc, String digestpw) throws IOException {
    DistributedLogManager dlm = factory.createDistributedLogManagerWithSharedClients(streamName);
    try {
        List<LogSegmentMetadata> segments = dlm.getLogSegments();
        if (segments.isEmpty()) {
            return null;
        }
        List<Future<LogSegmentCandidate>> futures = new ArrayList<Future<LogSegmentCandidate>>(segments.size());
        for (LogSegmentMetadata segment : segments) {
            futures.add(checkLogSegment(streamName, segment, executorService, bkc, digestpw));
        }
        List<LogSegmentCandidate> segmentCandidates;
        try {
            segmentCandidates = Await.result(Future.collect(futures));
        } catch (Exception e) {
            throw new IOException("Failed on checking stream " + streamName, e);
        }
        StreamCandidate streamCandidate = new StreamCandidate(streamName);
        for (LogSegmentCandidate segmentCandidate : segmentCandidates) {
            if (null != segmentCandidate) {
                streamCandidate.addLogSegmentCandidate(segmentCandidate);
            }
        }
        if (streamCandidate.segmentCandidates.isEmpty()) {
            return null;
        }
        return streamCandidate;
    } finally {
        dlm.close();
    }
}
Also used : DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) LogSegmentMetadata(com.twitter.distributedlog.LogSegmentMetadata) ArrayList(java.util.ArrayList) Future(com.twitter.util.Future) IOException(java.io.IOException) DLIllegalStateException(com.twitter.distributedlog.exceptions.DLIllegalStateException) ParseException(org.apache.commons.cli.ParseException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

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