Search in sources :

Example 1 with WriteResponse

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

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

the class TestDistributedLogService method testNonDurableWrite.

@Test(timeout = 60000)
public void testNonDurableWrite() throws Exception {
    DistributedLogConfiguration confLocal = newLocalConf();
    confLocal.setOutputBufferSize(Integer.MAX_VALUE).setImmediateFlushEnabled(false).setPeriodicFlushFrequencyMilliSeconds(0).setDurableWriteEnabled(false);
    ServerConfiguration serverConfLocal = new ServerConfiguration();
    serverConfLocal.addConfiguration(serverConf);
    serverConfLocal.enableDurableWrite(false);
    serverConfLocal.setServiceTimeoutMs(Integer.MAX_VALUE).setStreamProbationTimeoutMs(Integer.MAX_VALUE);
    String streamName = testName.getMethodName();
    DistributedLogServiceImpl localService = createService(serverConfLocal, confLocal);
    StreamManagerImpl streamManager = (StreamManagerImpl) localService.getStreamManager();
    int numWrites = 10;
    List<Future<WriteResponse>> futureList = new ArrayList<Future<WriteResponse>>();
    for (int i = 0; i < numWrites; i++) {
        futureList.add(localService.write(streamName, createRecord(i)));
    }
    assertTrue("Stream " + streamName + " should be cached", streamManager.getCachedStreams().containsKey(streamName));
    List<WriteResponse> resultList = FutureUtils.result(Future.collect(futureList));
    for (WriteResponse wr : resultList) {
        assertEquals(DLSN.InvalidDLSN, DLSN.deserialize(wr.getDlsn()));
    }
    localService.shutdown();
}
Also used : DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) ServerConfiguration(com.twitter.distributedlog.service.config.ServerConfiguration) ArrayList(java.util.ArrayList) WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) Future(com.twitter.util.Future) StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl) Test(org.junit.Test)

Example 3 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 4 with WriteResponse

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

the class TestDistributedLogService method testWriteOpChecksumBadData.

@Test(timeout = 60000)
public void testWriteOpChecksumBadData() throws Exception {
    DistributedLogServiceImpl localService = createConfiguredLocalService();
    ByteBuffer buffer = getTestDataBuffer();
    WriteContext ctx = new WriteContext().setCrc32(ProtocolUtils.writeOpCRC32("test", buffer.array()));
    // Overwrite 1 byte to corrupt data.
    buffer.put(1, (byte) 0xab);
    Future<WriteResponse> result = localService.writeWithContext("test", buffer, ctx);
    WriteResponse resp = Await.result(result);
    assertEquals(StatusCode.CHECKSUM_FAILED, resp.getHeader().getCode());
    localService.shutdown();
}
Also used : WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) ByteBuffer(java.nio.ByteBuffer) WriteContext(com.twitter.distributedlog.thrift.service.WriteContext) Test(org.junit.Test)

Example 5 with WriteResponse

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

the class TestDistributedLogService method testShutdown.

@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());
}
Also used : WriteResponse(com.twitter.distributedlog.thrift.service.WriteResponse) StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl) 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