com.twitter.util.Await Java Examples

The following examples show how to use com.twitter.util.Await. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestReadUtils.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testGetLastRecordControlRecord() throws Exception {
    String streamName = runtime.getMethodName();
    BKDistributedLogManager bkdlm = (BKDistributedLogManager) createNewDLM(conf, streamName);

    AsyncLogWriter out = bkdlm.startAsyncLogSegmentNonPartitioned();
    int txid = 1;
    Await.result(out.write(DLMTestUtil.getLargeLogRecordInstance(txid++, false)));
    Await.result(out.write(DLMTestUtil.getLargeLogRecordInstance(txid++, false)));
    Await.result(out.write(DLMTestUtil.getLargeLogRecordInstance(txid++, false)));
    Await.result(out.write(DLMTestUtil.getLargeLogRecordInstance(txid++, true)));
    Await.result(out.write(DLMTestUtil.getLargeLogRecordInstance(txid++, true)));
    Utils.close(out);

    Future<LogRecordWithDLSN> futureLogrec = getLastUserRecord(bkdlm, 0);
    LogRecordWithDLSN logrec = Await.result(futureLogrec);
    assertEquals(new DLSN(1,2,0), logrec.getDlsn());
    bkdlm.close();
}
 
Example #2
Source File: TestLeastLoadPlacementPolicy.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10000)
public void testCalculateBalances() throws Exception {
    int numSevers = new Random().nextInt(20) + 1;
    int numStreams = new Random().nextInt(200) + 1;
    RoutingService mockRoutingService = mock(RoutingService.class);
    Namespace mockNamespace = mock(Namespace.class);
    LeastLoadPlacementPolicy leastLoadPlacementPolicy = new LeastLoadPlacementPolicy(
        new EqualLoadAppraiser(),
        mockRoutingService,
        mockNamespace,
        null,
        Duration.fromSeconds(600),
        new NullStatsLogger());
    TreeSet<ServerLoad> serverLoads =
        Await.result(leastLoadPlacementPolicy.calculate(generateServers(numSevers), generateStreams(numStreams)));
    long lowLoadPerServer = numStreams / numSevers;
    long highLoadPerServer = lowLoadPerServer + 1;
    for (ServerLoad serverLoad : serverLoads) {
        long load = serverLoad.getLoad();
        assertEquals(load, serverLoad.getStreamLoads().size());
        assertTrue(String.format("Load %d is not between %d and %d",
            load, lowLoadPerServer, highLoadPerServer), load == lowLoadPerServer || load == highLoadPerServer);
    }
}
 
Example #3
Source File: TestAsyncReaderWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testGetLastTxId() throws Exception {
    String name = runtime.getMethodName();
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.addConfiguration(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(true);

    DistributedLogManager dlm = createNewDLM(confLocal, name);
    AsyncLogWriter writer = dlm.startAsyncLogSegmentNonPartitioned();

    int numRecords = 10;
    for (int i = 0; i < numRecords; i++) {
        Await.result(writer.write(DLMTestUtil.getLogRecordInstance(i)));
        assertEquals("last tx id should become " + i,
                i, writer.getLastTxId());
    }
    // open a writer to recover the inprogress log segment
    AsyncLogWriter recoverWriter = dlm.startAsyncLogSegmentNonPartitioned();
    assertEquals("recovered last tx id should be " + (numRecords - 1),
            numRecords - 1, recoverWriter.getLastTxId());
}
 
Example #4
Source File: TestDistributedLogService.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testFailRequestsDuringClosing() throws Exception {
    String streamName = testName.getMethodName();
    StreamImpl s = createUnstartedStream(service, streamName);

    Future<Void> closeFuture = s.requestClose("close");
    assertTrue("Stream " + streamName + " should be set to " + StreamStatus.CLOSING,
            StreamStatus.CLOSING == s.getStatus()
                || StreamStatus.CLOSED == s.getStatus());
    WriteOp op1 = createWriteOp(service, streamName, 0L);
    s.submit(op1);
    WriteResponse response1 = Await.result(op1.result());
    assertEquals("Op should fail with " + StatusCode.STREAM_UNAVAILABLE + " if it is closing",
            StatusCode.STREAM_UNAVAILABLE, response1.getHeader().getCode());

    Await.result(closeFuture);
    assertEquals("Stream " + streamName + " should be set to " + StreamStatus.CLOSED,
            StreamStatus.CLOSED, s.getStatus());
    WriteOp op2 = createWriteOp(service, streamName, 1L);
    s.submit(op2);
    WriteResponse response2 = Await.result(op2.result());
    assertEquals("Op should fail with " + StatusCode.STREAM_UNAVAILABLE + " if it is closed",
            StatusCode.STREAM_UNAVAILABLE, response2.getHeader().getCode());
}
 
Example #5
Source File: BalancerTool.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
protected void balanceFromSource(DistributedLogClientBuilder clientBuilder,
                                 ClusterBalancer balancer,
                                 String source,
                                 Optional<RateLimiter> rateLimiter)
        throws Exception {
    InetSocketAddress sourceAddr = DLSocketAddress.parseSocketAddress(source);
    DistributedLogClientBuilder sourceClientBuilder =
            DistributedLogClientBuilder.newBuilder(clientBuilder)
                    .host(sourceAddr);

    Pair<DistributedLogClient, MonitorServiceClient> clientPair =
            ClientUtils.buildClient(sourceClientBuilder);
    try {
        Await.result(clientPair.getRight().setAcceptNewStream(false));
        logger.info("Disable accepting new stream on proxy {}.", source);
        balancer.balanceAll(source, rebalanceConcurrency, rateLimiter);
    } finally {
        clientPair.getLeft().close();
    }
}
 
Example #6
Source File: TestStreamMover.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testMoveStream() throws Exception {
    String name = "dlserver-move-stream";

    // src client
    dlClient.routingService.addHost(name, dlServer.getAddress());
    // target client
    targetClient.routingService.addHost(name, targetServer.getAddress());

    // src client write a record to that stream
    Await.result(((DistributedLogClient) dlClient.dlClient).write(name, ByteBuffer.wrap("1".getBytes(UTF_8))));
    checkStream(name, dlClient, dlServer, 1, 1, 1, true, true);
    checkStream(name, targetClient, targetServer, 0, 0, 0, false, false);

    StreamMover streamMover = new StreamMoverImpl("source", dlClient.dlClient, dlClient.dlClient,
                                                  "target", targetClient.dlClient, targetClient.dlClient);
    assertTrue(streamMover.moveStream(name));
    checkStream(name, dlClient, dlServer, 0, 0, 0, false, false);
    checkStream(name, targetClient, targetServer, 1, 1, 1, true, true);
}
 
Example #7
Source File: TestDistributedLogServer.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Sanity check to make sure both checksum flag values work.
 */
@Test(timeout = 60000)
public void testChecksumFlag() throws Exception {
    String name = "dlserver-basic-write";
    LocalRoutingService routingService = LocalRoutingService.newBuilder().build();
    routingService.addHost(name, dlServer.getAddress());
    DistributedLogClientBuilder dlClientBuilder = DistributedLogClientBuilder.newBuilder()
        .name(name)
        .clientId(ClientId$.MODULE$.apply("test"))
        .routingService(routingService)
        .handshakeWithClientInfo(true)
        .clientBuilder(ClientBuilder.get()
            .hostConnectionLimit(1)
            .connectionTimeout(Duration.fromSeconds(1))
            .requestTimeout(Duration.fromSeconds(60)))
        .checksum(false);
    DistributedLogClient dlClient = (DistributedLogClientImpl) dlClientBuilder.build();
    Await.result(dlClient.write(name, ByteBuffer.wrap(("1").getBytes())));
    dlClient.close();

    dlClient = dlClientBuilder.checksum(true).build();
    Await.result(dlClient.write(name, ByteBuffer.wrap(("2").getBytes())));
    dlClient.close();
}
 
Example #8
Source File: BalancerTool.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
protected void balanceFromSource(DistributedLogClientBuilder clientBuilder,
                                 ClusterBalancer balancer,
                                 String source,
                                 Optional<RateLimiter> rateLimiter)
        throws Exception {
    InetSocketAddress sourceAddr = DLSocketAddress.parseSocketAddress(source);
    DistributedLogClientBuilder sourceClientBuilder =
            DistributedLogClientBuilder.newBuilder(clientBuilder)
                    .host(sourceAddr);

    Pair<DistributedLogClient, MonitorServiceClient> clientPair =
            ClientUtils.buildClient(sourceClientBuilder);
    try {
        Await.result(clientPair.getRight().setAcceptNewStream(false));
        logger.info("Disable accepting new stream on proxy {}.", source);
        balancer.balanceAll(source, rebalanceConcurrency, rateLimiter);
    } finally {
        clientPair.getLeft().close();
    }
}
 
Example #9
Source File: TestBKLogSegmentWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
/**
 * Non durable write should fail if writer is closed.
 *
 * @throws Exception
 */
@Test(timeout = 60000)
public void testNondurableWriteAfterWriterIsClosed() throws Exception {
    DistributedLogConfiguration confLocal = newLocalConf();
    confLocal.setImmediateFlushEnabled(false);
    confLocal.setOutputBufferSize(Integer.MAX_VALUE);
    confLocal.setPeriodicFlushFrequencyMilliSeconds(0);
    confLocal.setDurableWriteEnabled(false);
    ZKDistributedLock lock = createLock("/test/lock-" + runtime.getMethodName(), zkc, true);
    BKLogSegmentWriter writer =
            createLogSegmentWriter(confLocal, 0L, -1L, lock);

    // close the writer
    closeWriterAndLock(writer, lock);
    FutureUtils.result(writer.asyncClose());

    try {
        Await.result(writer.asyncWrite(DLMTestUtil.getLogRecordInstance(1)));
        fail("Should fail the write if the writer is closed");
    } catch (WriteException we) {
        // expected
    }
}
 
Example #10
Source File: TestDistributedLogServer.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testCreateStream() throws Exception {
    try {
        setupNoAdHocCluster();
        final String name = "dlserver-create-stream";

        noAdHocClient.routingService.addHost("dlserver-create-stream", noAdHocServer.getAddress());
        assertFalse(noAdHocServer.dlServer.getKey().getStreamManager().isAcquired(name));
        assertTrue(Await.ready(noAdHocClient.dlClient.create(name)).isReturn());

        long txid = 101;
        for (long i = 1; i <= 10; i++) {
            long curTxId = txid++;
            logger.debug("Write entry {} to stream {}.", curTxId, name);
            noAdHocClient.dlClient.write(name,
                ByteBuffer.wrap(("" + curTxId).getBytes())).get();
        }

        assertTrue(noAdHocServer.dlServer.getKey().getStreamManager().isAcquired(name));
    } finally {
        tearDownNoAdHocCluster();
    }
}
 
Example #11
Source File: TestBKDistributedLogManager.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
@Deprecated
public void testSubscriptionStateStore() throws Exception {
    String name = "distrlog-subscription-state";
    String subscriberId = "defaultSubscriber";
    DLSN commitPosition0 = new DLSN(4, 33, 5);
    DLSN commitPosition1 = new DLSN(4, 34, 5);
    DLSN commitPosition2 = new DLSN(5, 34, 5);

    DistributedLogManager dlm = createNewDLM(conf, name);
    SubscriptionStateStore store = dlm.getSubscriptionStateStore(subscriberId);
    assertEquals(Await.result(store.getLastCommitPosition()), DLSN.NonInclusiveLowerBound);
    Await.result(store.advanceCommitPosition(commitPosition1));
    assertEquals(Await.result(store.getLastCommitPosition()), commitPosition1);
    Await.result(store.advanceCommitPosition(commitPosition0));
    assertEquals(Await.result(store.getLastCommitPosition()), commitPosition1);
    Await.result(store.advanceCommitPosition(commitPosition2));
    assertEquals(Await.result(store.getLastCommitPosition()), commitPosition2);
    SubscriptionStateStore store1 = dlm.getSubscriptionStateStore(subscriberId);
    assertEquals(Await.result(store1.getLastCommitPosition()), commitPosition2);
}
 
Example #12
Source File: FinagleIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenServerAndClient_whenRequestSent_thenClientShouldReceiveResponseFromServer() throws Exception {
    // given
    Service serverService = new LogFilter().andThen(new GreetingService());
    Http.serve(":8080", serverService);

    Service<Request, Response> clientService = new LogFilter().andThen(Http.newService(":8080"));

    // when
    Request request = Request.apply(Method.Get(), "/?name=John");
    request.host("localhost");
    Future<Response> response = clientService.apply(request);

    // then
    Await.result(response
            .onSuccess(r -> {
                assertEquals("Hello John", r.getContentString());
                return BoxedUnit.UNIT;
            })
            .onFailure(r -> {
                throw new RuntimeException(r);
            })
    );
}
 
Example #13
Source File: TestAsyncReaderLock.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testReaderLockFutureCancelledWhileLocked() throws Exception {
    String name = runtime.getMethodName();
    DistributedLogManager dlm0 = createNewDLM(conf, name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter)(dlm0.startAsyncLogSegmentNonPartitioned());
    writer.write(DLMTestUtil.getLogRecordInstance(1L));
    writer.write(DLMTestUtil.getLogRecordInstance(2L));
    writer.closeAndComplete();

    DistributedLogManager dlm1 = createNewDLM(conf, name);
    Future<AsyncLogReader> futureReader1 = dlm1.getAsyncLogReaderWithLock(DLSN.InitialDLSN);

    // Must not throw or cancel or do anything bad, future already completed.
    Await.result(futureReader1);
    FutureUtils.cancel(futureReader1);
    AsyncLogReader reader1 = Await.result(futureReader1);
    Await.result(reader1.readNext());

    dlm0.close();
    dlm1.close();
}
 
Example #14
Source File: TestDistributedLock.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testAsyncAcquireCloseAfterAcquire() throws Exception {
    TestLockFactory locks = new TestLockFactory(runtime.getMethodName(), zkc, lockStateExecutor);
    final ZKDistributedLock lock0 = locks.createLock(0, zkc);

    Future<ZKDistributedLock> result = lock0.asyncAcquire();
    Await.result(result);
    FutureUtils.result(lock0.asyncClose());

    // Already have this, stays satisfied.
    Await.result(result);

    // But we no longer have the lock.
    assertEquals(false, lock0.haveLock());
    assertEquals(false, lock0.getInternalLock().isLockHeld());
}
 
Example #15
Source File: TestDistributedLogMultiStreamWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 20000)
public void testFailRequestAfterRetriedAllStreams() throws Exception {
    DistributedLogClient client = mock(DistributedLogClient.class);
    when(client.writeRecordSet((String) any(), (LogRecordSetBuffer) any()))
            .thenReturn(new Promise<DLSN>());
    DistributedLogMultiStreamWriter writer = DistributedLogMultiStreamWriter.newBuilder()
            .streams(Lists.newArrayList("stream1", "stream2"))
            .client(client)
            .compressionCodec(CompressionCodec.Type.LZ4)
            .firstSpeculativeTimeoutMs(10)
            .maxSpeculativeTimeoutMs(20)
            .speculativeBackoffMultiplier(2)
            .requestTimeoutMs(5000000)
            .flushIntervalMs(10)
            .bufferSize(Integer.MAX_VALUE)
            .build();

    byte[] data = "test-test".getBytes(UTF_8);
    ByteBuffer buffer = ByteBuffer.wrap(data);
    Future<DLSN> writeFuture = writer.write(buffer);
    try {
        Await.result(writeFuture);
        fail("Should fail the request after retries all streams");
    } catch (IndividualRequestTimeoutException e) {
        long timeoutMs = e.timeout().inMilliseconds();
        assertTrue(timeoutMs >= (10 + 20) && timeoutMs < 5000000);
    }
    writer.close();
}
 
Example #16
Source File: TestDistributedLogMultiStreamWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 20000)
public void testPeriodicalFlush() throws Exception {
    DistributedLogClient client = mock(DistributedLogClient.class);
    DistributedLogMultiStreamWriter writer = DistributedLogMultiStreamWriter.newBuilder()
            .streams(Lists.newArrayList("stream1", "stream2"))
            .client(client)
            .compressionCodec(CompressionCodec.Type.LZ4)
            .firstSpeculativeTimeoutMs(10)
            .maxSpeculativeTimeoutMs(20)
            .speculativeBackoffMultiplier(2)
            .requestTimeoutMs(5000000)
            .flushIntervalMs(10)
            .bufferSize(Integer.MAX_VALUE)
            .build();

    final DLSN dlsn = new DLSN(99L, 88L, 0L);

    Mockito.doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return Future.value(dlsn);
        }
    }).when(client).writeRecordSet((String) any(), (LogRecordSetBuffer) any());

    byte[] data = "test-test".getBytes(UTF_8);
    ByteBuffer buffer = ByteBuffer.wrap(data);
    Future<DLSN> writeFuture = writer.write(buffer);
    DLSN writeDLSN = Await.result(writeFuture);
    assertEquals(dlsn, writeDLSN);
    writer.close();
}
 
Example #17
Source File: TestDistributedLogService.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testWriteOpChecksumBadChecksum() throws Exception {
    DistributedLogServiceImpl localService = createConfiguredLocalService();
    WriteContext ctx = new WriteContext().setCrc32(999);
    Future<WriteResponse> result = localService.writeWithContext("test", getTestDataBuffer(), ctx);
    WriteResponse resp = Await.result(result);
    assertEquals(StatusCode.CHECKSUM_FAILED, resp.getHeader().getCode());
    localService.shutdown();
}
 
Example #18
Source File: TestBKLogReadHandler.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testGetLogRecordCountLastRecord() throws Exception {
    String dlName = runtime.getMethodName();
    prepareLogSegmentsNonPartitioned(dlName, 11, 3);
    DistributedLogManager dlm = createNewDLM(conf, dlName);
    BKLogReadHandler readHandler = ((BKDistributedLogManager) dlm).createReadHandler();
    Future<Long> count = null;
    count = readHandler.asyncGetLogRecordCount(new DLSN(11, 2, 0));
    assertEquals(1, Await.result(count).longValue());
}
 
Example #19
Source File: TestDistributedLogService.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testTruncateOpNoChecksum() throws Exception {
    DistributedLogServiceImpl localService = createConfiguredLocalService();
    WriteContext ctx = new WriteContext();
    Future<WriteResponse> result = localService.truncate("test", new DLSN(1, 2, 3).serialize(), ctx);
    WriteResponse resp = Await.result(result);
    assertEquals(StatusCode.SUCCESS, resp.getHeader().getCode());
    localService.shutdown();
}
 
Example #20
Source File: TestDistributedLogService.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testWriteOpChecksumBadStream() throws Exception {
    DistributedLogServiceImpl localService = createConfiguredLocalService();
    WriteContext ctx = new WriteContext().setCrc32(
        ProtocolUtils.writeOpCRC32("test", getTestDataBuffer()));
    Future<WriteResponse> result = localService.writeWithContext("test1", getTestDataBuffer(), ctx);
    WriteResponse resp = Await.result(result);
    assertEquals(StatusCode.CHECKSUM_FAILED, resp.getHeader().getCode());
    localService.shutdown();
}
 
Example #21
Source File: TestAsyncReaderWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testCloseAndCompleteLogSegmentWhenCloseFailed() throws Exception {
    String name = "distrlog-close-and-complete-logsegment-when-close-failed";
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.loadConf(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setImmediateFlushEnabled(true);

    BKDistributedLogManager dlm = (BKDistributedLogManager) createNewDLM(confLocal, name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter)(dlm.startAsyncLogSegmentNonPartitioned());

    long txId = 1L;
    for (int i = 0; i < 5; i++) {
        Await.result(writer.write(DLMTestUtil.getLogRecordInstance(txId++)));
    }

    BKLogSegmentWriter logWriter = writer.getCachedLogWriter();

    // fence the ledger
    dlm.getWriterBKC().get().openLedger(logWriter.getLogSegmentId(),
            BookKeeper.DigestType.CRC32, confLocal.getBKDigestPW().getBytes(UTF_8));

    try {
        // insert a write to detect the fencing state, to make test more robust.
        writer.write(DLMTestUtil.getLogRecordInstance(txId++));
        writer.closeAndComplete();
        fail("Should fail to complete a log segment when its ledger is fenced");
    } catch (IOException ioe) {
        // expected
        LOG.error("Failed to close and complete log segment {} : ", logWriter.getFullyQualifiedLogSegment(), ioe);
    }

    List<LogSegmentMetadata> segments = dlm.getLogSegments();
    assertEquals(1, segments.size());
    assertTrue(segments.get(0).isInProgress());

    dlm.close();
}
 
Example #22
Source File: TestBKLogReadHandler.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testLockStreamSameSubscriber() throws Exception {
    String streamName = runtime.getMethodName();
    BKDistributedLogManager bkdlm = createNewDLM(conf, streamName);
    DLMTestUtil.generateLogSegmentNonPartitioned(bkdlm, 0, 5, 1);
    BKLogReadHandler readHandler = bkdlm.createReadHandler();
    Await.result(readHandler.lockStream());

    // same subscrbiers couldn't lock stream in parallel
    BKDistributedLogManager bkdlm10 = createNewDLM(conf, streamName);
    BKLogReadHandler s10Handler =
            bkdlm10.createReadHandler(Optional.of("s1"));
    Await.result(s10Handler.lockStream());

    BKDistributedLogManager bkdlm11 = createNewDLM(conf, streamName);
    BKLogReadHandler s11Handler =
            bkdlm11.createReadHandler(Optional.of("s1"));
    try {
        Await.result(s11Handler.lockStream(), Duration.apply(10000, TimeUnit.MILLISECONDS));
        fail("Should fail lock stream using same subscriber id");
    } catch (OwnershipAcquireFailedException oafe) {
        // expected
    } catch (TimeoutException te) {
        // expected.
    }

    readHandler.asyncClose();
    bkdlm.close();
    s10Handler.asyncClose();
    bkdlm10.close();
    s11Handler.asyncClose();
    bkdlm11.close();
}
 
Example #23
Source File: TestDistributedLogServer.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testRequestDenied() throws Exception {
    String name = "request-denied";

    dlClient.routingService.addHost(name, dlServer.getAddress());

    AccessControlEntry ace = new AccessControlEntry();
    ace.setDenyWrite(true);
    ZooKeeperClient zkc = TestZooKeeperClientBuilder
            .newBuilder()
            .uri(getUri())
            .connectionTimeoutMs(60000)
            .sessionTimeoutMs(60000)
            .build();
    DistributedLogNamespace dlNamespace = dlServer.dlServer.getLeft().getDistributedLogNamespace();
    BKDLConfig bkdlConfig = BKDLConfig.resolveDLConfig(zkc, getUri());
    String zkPath = getUri().getPath() + "/" + bkdlConfig.getACLRootPath() + "/" + name;
    ZKAccessControl accessControl = new ZKAccessControl(ace, zkPath);
    accessControl.create(zkc);

    AccessControlManager acm = dlNamespace.createAccessControlManager();
    while (acm.allowWrite(name)) {
        Thread.sleep(100);
    }

    try {
        Await.result(dlClient.dlClient.write(name, ByteBuffer.wrap("1".getBytes(UTF_8))));
        fail("Should fail with request denied exception");
    } catch (DLException dle) {
        assertEquals(StatusCode.REQUEST_DENIED, dle.getCode());
    }
}
 
Example #24
Source File: TestDistributedLock.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testAsyncAcquireSyncThenAsyncOnSameLock() throws Exception {
    TestLockFactory locks = new TestLockFactory(runtime.getMethodName(), zkc, lockStateExecutor);
    final ZKDistributedLock lock0 = locks.createLock(0, zkc);
    final ZKDistributedLock lock1 = locks.createLock(1, zkc0);

    FutureUtils.result(lock0.asyncAcquire());

    // Initial state.
    assertLockState(lock0, true, true, lock1, false, false, 1, locks.getLockPath());

    Thread lock1Thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                FutureUtils.result(lock1.asyncAcquire());
            } catch (IOException e) {
                fail("shouldn't fail to acquire");
            }
        }
    }, "lock1-thread");
    lock1Thread.start();

    // Wait for lock count to increase, indicating background acquire has succeeded.
    while (getLockWaiters(zkc, locks.getLockPath()).size() < 2) {
        Thread.sleep(1);
    }
    assertLockState(lock0, true, true, lock1, false, false, 2, locks.getLockPath());

    FutureUtils.result(lock0.asyncClose());
    Await.result(lock1.getLockAcquireFuture());

    assertLockState(lock0, false, false, lock1, true, true, 1, locks.getLockPath());

    // Release lock1
    FutureUtils.result(lock1.asyncClose());
    assertLockState(lock0, false, false, lock1, false, false, 0, locks.getLockPath());
}
 
Example #25
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String setValueN() throws Exception {
  Promise<String> p = new Promise<String>();
  Future<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.map(mapF);
  p.setValue(string);
  return Await.result(f);
}
 
Example #26
Source File: TestAsyncReaderWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testAsyncReadEmptyRecords() throws Exception {
    String name = "distrlog-simpleasyncreadempty";
    DistributedLogConfiguration confLocal = new DistributedLogConfiguration();
    confLocal.loadConf(testConf);
    confLocal.setOutputBufferSize(0);
    confLocal.setReadAheadWaitTime(10);
    confLocal.setReadAheadBatchSize(10);
    DistributedLogManager dlm = createNewDLM(confLocal, name);

    int numLogSegments = 3;
    int numRecordsPerLogSegment = 10;

    long txid = 1L;
    // write 3 log segments, 10 records per log segment
    txid = writeRecords(dlm, numLogSegments, numRecordsPerLogSegment, txid, true);
    // write another log segment with 5 records and flush every 2 records
    txid = writeLogSegment(dlm, 5, txid, 2, true);

    AsyncLogReader asyncReader = dlm.getAsyncLogReader(DLSN.InvalidDLSN);
    assertEquals("Expected stream name = " + name + " but " + asyncReader.getStreamName() + " found",
            name, asyncReader.getStreamName());
    long numTrans = 0;
    DLSN lastDLSN = DLSN.InvalidDLSN;
    LogRecordWithDLSN record = Await.result(asyncReader.readNext());
    while (null != record) {
        DLMTestUtil.verifyEmptyLogRecord(record);
        assertEquals(0, record.getDlsn().getSlotId());
        assertTrue(record.getDlsn().compareTo(lastDLSN) > 0);
        lastDLSN = record.getDlsn();
        numTrans++;
        if (numTrans >= (txid - 1)) {
            break;
        }
        record = Await.result(asyncReader.readNext());
    }
    assertEquals((txid - 1), numTrans);
    Utils.close(asyncReader);
    dlm.close();
}
 
Example #27
Source File: TestStreamManager.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testCreateStream() throws Exception {
    Stream mockStream = mock(Stream.class);
    final String streamName = "stream1";
    when(mockStream.getStreamName()).thenReturn(streamName);
    StreamFactory mockStreamFactory = mock(StreamFactory.class);
    StreamPartitionConverter mockPartitionConverter = mock(StreamPartitionConverter.class);
    StreamConfigProvider mockStreamConfigProvider = mock(StreamConfigProvider.class);
    when(mockStreamFactory.create(
        (String) any(),
        (DynamicDistributedLogConfiguration) any(),
        (StreamManager) any())
    ).thenReturn(mockStream);
    Namespace dlNamespace = mock(Namespace.class);
    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1);

    StreamManager streamManager = new StreamManagerImpl(
            "",
            new DistributedLogConfiguration(),
            executorService,
            mockStreamFactory,
            mockPartitionConverter,
            mockStreamConfigProvider,
            dlNamespace);

    assertTrue(Await.ready(streamManager.createStreamAsync(streamName)).isReturn());
    verify(dlNamespace).createLog(streamName);
}
 
Example #28
Source File: TestBKLogReadHandler.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testGetLogRecordCountWithAllControlRecords() throws Exception {
    DistributedLogManager dlm = createNewDLM(conf, runtime.getMethodName());
    long txid = 1;
    txid += DLMTestUtil.generateLogSegmentNonPartitioned(dlm, 5, 0, txid);
    txid += DLMTestUtil.generateLogSegmentNonPartitioned(dlm, 10, 0, txid);
    BKLogReadHandler readHandler = ((BKDistributedLogManager) dlm).createReadHandler();
    Future<Long> count = null;
    count = readHandler.asyncGetLogRecordCount(new DLSN(1, 0, 0));
    assertEquals(0, Await.result(count).longValue());
}
 
Example #29
Source File: TestDistributedLogService.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testShutdown() throws Exception {
    service.shutdown();
    StreamManagerImpl streamManager = (StreamManagerImpl) service.getStreamManager();
    WriteResponse response =
            Await.result(service.write(testName.getMethodName(), createRecord(0L)));
    assertEquals("Write should fail with " + StatusCode.SERVICE_UNAVAILABLE,
            StatusCode.SERVICE_UNAVAILABLE, response.getHeader().getCode());
    assertTrue("There should be no streams created after shutdown",
            streamManager.getCachedStreams().isEmpty());
    assertTrue("There should be no streams acquired after shutdown",
            streamManager.getAcquiredStreams().isEmpty());
}
 
Example #30
Source File: TestDistributedLogServerBase.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testRequestDenied() throws Exception {
    String name = "request-denied";

    dlClient.routingService.addHost(name, dlServer.getAddress());

    AccessControlEntry ace = new AccessControlEntry();
    ace.setDenyWrite(true);
    ZooKeeperClient zkc = TestZooKeeperClientBuilder
            .newBuilder()
            .uri(getUri())
            .connectionTimeoutMs(60000)
            .sessionTimeoutMs(60000)
            .build();
    Namespace dlNamespace = dlServer.dlServer.getLeft().getDistributedLogNamespace();
    BKDLConfig bkdlConfig = BKDLConfig.resolveDLConfig(zkc, getUri());
    String zkPath = getUri().getPath() + "/" + bkdlConfig.getACLRootPath() + "/" + name;
    ZKAccessControl accessControl = new ZKAccessControl(ace, zkPath);
    accessControl.create(zkc);

    AccessControlManager acm = dlNamespace.createAccessControlManager();
    while (acm.allowWrite(name)) {
        Thread.sleep(100);
    }

    try {
        Await.result(dlClient.dlClient.write(name, ByteBuffer.wrap("1".getBytes(UTF_8))));
        fail("Should fail with request denied exception");
    } catch (DLException dle) {
        assertEquals(StatusCode.REQUEST_DENIED.getValue(), dle.getCode());
    }
}