Search in sources :

Example 11 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse 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 12 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.

the class TestDistributedLogService method testStreamOpNoChecksum.

@Test(timeout = 60000)
public void testStreamOpNoChecksum() throws Exception {
    DistributedLogServiceImpl localService = createConfiguredLocalService();
    WriteContext ctx = new WriteContext();
    Future<WriteResponse> result = localService.release("test", ctx);
    WriteResponse resp = Await.result(result);
    assertEquals(StatusCode.SUCCESS, resp.getHeader().getCode());
    result = localService.delete("test", ctx);
    resp = Await.result(result);
    assertEquals(StatusCode.SUCCESS, resp.getHeader().getCode());
    result = localService.heartbeat("test", ctx);
    resp = Await.result(result);
    assertEquals(StatusCode.SUCCESS, resp.getHeader().getCode());
    localService.shutdown();
}
Also used : WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) WriteContext(com.twitter.distributedlog.thrift.service.WriteContext) Test(org.junit.Test)

Example 13 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.

the class TestDistributedLogService method testTruncateOpChecksumBadChecksum.

@Test(timeout = 60000)
public void testTruncateOpChecksumBadChecksum() throws Exception {
    DistributedLogServiceImpl localService = createConfiguredLocalService();
    WriteContext ctx = new WriteContext().setCrc32(999);
    Future<WriteResponse> result = localService.truncate("test", new DLSN(1, 2, 3).serialize(), ctx);
    WriteResponse resp = Await.result(result);
    assertEquals(StatusCode.CHECKSUM_FAILED, resp.getHeader().getCode());
    localService.shutdown();
}
Also used : DLSN(com.twitter.distributedlog.DLSN) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) WriteContext(com.twitter.distributedlog.thrift.service.WriteContext) Test(org.junit.Test)

Example 14 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.

the class TestStreamOp method testResponseFailedTwice.

@Test(timeout = 60000)
public void testResponseFailedTwice() throws Exception {
    WriteOp writeOp = getWriteOp();
    writeOp.fail(new InternalServerException("test1"));
    writeOp.fail(new InternalServerException("test2"));
    WriteResponse response = Await.result(writeOp.result());
    assertEquals(StatusCode.INTERNAL_SERVER_ERROR, response.getHeader().getCode());
    assertEquals(ResponseUtils.exceptionToHeader(new InternalServerException("test1")), response.getHeader());
}
Also used : WriteOp(com.twitter.distributedlog.service.stream.WriteOp) InternalServerException(com.twitter.distributedlog.exceptions.InternalServerException) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) Test(org.junit.Test)

Example 15 with WriteResponse

use of com.twitter.distributedlog.thrift.service.WriteResponse in project distributedlog by twitter.

the class TestStreamOp method testResponseSucceededThenFailed.

@Test(timeout = 60000)
public void testResponseSucceededThenFailed() throws Exception {
    AsyncLogWriter writer = mock(AsyncLogWriter.class);
    when(writer.write((LogRecord) any())).thenReturn(Future.value(new DLSN(1, 2, 3)));
    when(writer.getStreamName()).thenReturn("test");
    WriteOp writeOp = getWriteOp();
    writeOp.execute(writer, new Sequencer() {

        public long nextId() {
            return 0;
        }
    }, new Object());
    writeOp.fail(new InternalServerException("test2"));
    WriteResponse response = Await.result(writeOp.result());
    assertEquals(StatusCode.SUCCESS, response.getHeader().getCode());
}
Also used : Sequencer(com.twitter.distributedlog.util.Sequencer) DLSN(com.twitter.distributedlog.DLSN) WriteOp(com.twitter.distributedlog.service.stream.WriteOp) InternalServerException(com.twitter.distributedlog.exceptions.InternalServerException) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) AsyncLogWriter(com.twitter.distributedlog.AsyncLogWriter) Test(org.junit.Test)

Aggregations

WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)23 Test (org.junit.Test)21 WriteContext (com.twitter.distributedlog.thrift.service.WriteContext)10 WriteOp (com.twitter.distributedlog.service.stream.WriteOp)8 Future (com.twitter.util.Future)7 ArrayList (java.util.ArrayList)7 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)6 StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)6 DLSN (com.twitter.distributedlog.DLSN)5 StreamManagerImpl (com.twitter.distributedlog.service.stream.StreamManagerImpl)5 ServerConfiguration (com.twitter.distributedlog.service.config.ServerConfiguration)4 Stream (com.twitter.distributedlog.service.stream.Stream)3 List (java.util.List)3 LogRecord (com.twitter.distributedlog.LogRecord)2 InternalServerException (com.twitter.distributedlog.exceptions.InternalServerException)2 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)2 HeartbeatOptions (com.twitter.distributedlog.thrift.service.HeartbeatOptions)2 AsyncLogWriter (com.twitter.distributedlog.AsyncLogWriter)1 AlreadyClosedException (com.twitter.distributedlog.exceptions.AlreadyClosedException)1 DLException (com.twitter.distributedlog.exceptions.DLException)1