Search in sources :

Example 1 with StreamImpl

use of com.twitter.distributedlog.service.stream.StreamImpl 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 2 with StreamImpl

use of com.twitter.distributedlog.service.stream.StreamImpl in project distributedlog by twitter.

the class TestDistributedLogService method testCloseStreamsShouldAbort.

@Test(timeout = 60000)
public void testCloseStreamsShouldAbort() throws Exception {
    DistributedLogConfiguration confLocal = newLocalConf();
    confLocal.setOutputBufferSize(Integer.MAX_VALUE).setImmediateFlushEnabled(false).setPeriodicFlushFrequencyMilliSeconds(0);
    String streamNamePrefix = testName.getMethodName();
    DistributedLogServiceImpl localService = createService(serverConf, confLocal);
    StreamManagerImpl streamManager = (StreamManagerImpl) localService.getStreamManager();
    int numStreams = 10;
    int numWrites = 10;
    List<Future<WriteResponse>> futureList = Lists.newArrayListWithExpectedSize(numStreams * numWrites);
    for (int i = 0; i < numStreams; i++) {
        String streamName = streamNamePrefix + "-" + i;
        HeartbeatOptions hbOptions = new HeartbeatOptions();
        hbOptions.setSendHeartBeatToReader(true);
        // make sure the first log segment of each stream created
        FutureUtils.result(localService.heartbeatWithOptions(streamName, new WriteContext(), hbOptions));
        for (int j = 0; j < numWrites; j++) {
            futureList.add(localService.write(streamName, createRecord(i * numWrites + j)));
        }
    }
    assertEquals("There should be " + numStreams + " streams in cache", numStreams, streamManager.getCachedStreams().size());
    while (streamManager.getAcquiredStreams().size() < numStreams) {
        TimeUnit.MILLISECONDS.sleep(20);
    }
    for (Stream s : streamManager.getAcquiredStreams().values()) {
        StreamImpl stream = (StreamImpl) s;
        stream.setStatus(StreamStatus.FAILED);
    }
    Future<List<Void>> closeResult = localService.closeStreams();
    List<Void> closedStreams = Await.result(closeResult);
    assertEquals("There should be " + numStreams + " streams closed", numStreams, closedStreams.size());
    // all writes should be flushed
    for (Future<WriteResponse> future : futureList) {
        WriteResponse response = Await.result(future);
        assertTrue("Op should fail with " + StatusCode.BK_TRANSMIT_ERROR + " or be rejected : " + response.getHeader().getCode(), StatusCode.BK_TRANSMIT_ERROR == response.getHeader().getCode() || StatusCode.WRITE_EXCEPTION == response.getHeader().getCode() || StatusCode.WRITE_CANCELLED_EXCEPTION == response.getHeader().getCode());
    }
    // acquired streams should all been removed after we close them
    assertTrue("There should be no streams in the acquired cache", streamManager.getAcquiredStreams().isEmpty());
    localService.shutdown();
    // cached streams wouldn't be removed immediately after streams are closed
    // but they should be removed after we shutdown the service
    assertTrue("There should be no streams in the cache after shutting down the service", streamManager.getCachedStreams().isEmpty());
}
Also used : WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl) WriteContext(com.twitter.distributedlog.thrift.service.WriteContext) DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) HeartbeatOptions(com.twitter.distributedlog.thrift.service.HeartbeatOptions) StreamImpl(com.twitter.distributedlog.service.stream.StreamImpl) Future(com.twitter.util.Future) Stream(com.twitter.distributedlog.service.stream.Stream) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with StreamImpl

use of com.twitter.distributedlog.service.stream.StreamImpl in project distributedlog by twitter.

the class TestDistributedLogService method testAcquireStreams.

@Test(timeout = 60000)
public void testAcquireStreams() throws Exception {
    String streamName = testName.getMethodName();
    StreamImpl s0 = createUnstartedStream(service, streamName);
    s0.suspendAcquiring();
    DistributedLogServiceImpl service1 = createService(serverConf, dlConf);
    StreamImpl s1 = createUnstartedStream(service1, streamName);
    s1.suspendAcquiring();
    // create write ops
    WriteOp op0 = createWriteOp(service, streamName, 0L);
    s0.submit(op0);
    WriteOp op1 = createWriteOp(service1, streamName, 1L);
    s1.submit(op1);
    // check pending size
    assertEquals("Write Op 0 should be pending in service 0", 1, s0.numPendingOps());
    assertEquals("Write Op 1 should be pending in service 1", 1, s1.numPendingOps());
    // start acquiring s0
    s0.resumeAcquiring().start();
    WriteResponse wr0 = Await.result(op0.result());
    assertEquals("Op 0 should succeed", StatusCode.SUCCESS, wr0.getHeader().getCode());
    assertEquals("Service 0 should acquire stream", StreamStatus.INITIALIZED, s0.getStatus());
    assertNotNull(s0.getManager());
    assertNotNull(s0.getWriter());
    assertNull(s0.getLastException());
    // start acquiring s1
    s1.resumeAcquiring().start();
    WriteResponse wr1 = Await.result(op1.result());
    assertEquals("Op 1 should fail", StatusCode.FOUND, wr1.getHeader().getCode());
    assertEquals("Service 1 should be in BACKOFF state", StreamStatus.BACKOFF, s1.getStatus());
    assertNotNull(s1.getManager());
    assertNull(s1.getWriter());
    assertNotNull(s1.getLastException());
    assertTrue(s1.getLastException() instanceof OwnershipAcquireFailedException);
    service1.shutdown();
}
Also used : OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) WriteOp(com.twitter.distributedlog.service.stream.WriteOp) StreamImpl(com.twitter.distributedlog.service.stream.StreamImpl) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) Test(org.junit.Test)

Example 4 with StreamImpl

use of com.twitter.distributedlog.service.stream.StreamImpl in project distributedlog by twitter.

the class TestDistributedLogService method createUnstartedStream.

private StreamImpl createUnstartedStream(DistributedLogServiceImpl service, String name) throws Exception {
    StreamImpl stream = (StreamImpl) service.newStream(name);
    stream.initialize();
    return stream;
}
Also used : StreamImpl(com.twitter.distributedlog.service.stream.StreamImpl)

Example 5 with StreamImpl

use of com.twitter.distributedlog.service.stream.StreamImpl 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)

Aggregations

StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)7 WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)6 Test (org.junit.Test)6 WriteOp (com.twitter.distributedlog.service.stream.WriteOp)4 Future (com.twitter.util.Future)4 ArrayList (java.util.ArrayList)4 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)2 StreamManagerImpl (com.twitter.distributedlog.service.stream.StreamManagerImpl)2 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)1 ServerConfiguration (com.twitter.distributedlog.service.config.ServerConfiguration)1 Stream (com.twitter.distributedlog.service.stream.Stream)1 HeartbeatOptions (com.twitter.distributedlog.thrift.service.HeartbeatOptions)1 WriteContext (com.twitter.distributedlog.thrift.service.WriteContext)1 List (java.util.List)1