Search in sources :

Example 1 with HeartbeatOptions

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

the class TestDistributedLogServer method testBasicWrite.

/**
     * {@link https://issues.apache.org/jira/browse/DL-27}
     */
@DistributedLogAnnotations.FlakyTest
@Ignore
@Test(timeout = 60000)
public void testBasicWrite() throws Exception {
    String name = "dlserver-basic-write";
    dlClient.routingService.addHost(name, dlServer.getAddress());
    for (long i = 1; i <= 10; i++) {
        logger.debug("Write entry {} to stream {}.", i, name);
        Await.result(dlClient.dlClient.write(name, ByteBuffer.wrap(("" + i).getBytes())));
    }
    HeartbeatOptions hbOptions = new HeartbeatOptions();
    hbOptions.setSendHeartBeatToReader(true);
    // make sure the first log segment of each stream created
    FutureUtils.result(dlClient.dlClient.heartbeat(name));
    DistributedLogManager dlm = DLMTestUtil.createNewDLM(name, conf, getUri());
    LogReader reader = dlm.getInputStream(1);
    int numRead = 0;
    LogRecord r = reader.readNext(false);
    while (null != r) {
        int i = Integer.parseInt(new String(r.getPayload()));
        assertEquals(numRead + 1, i);
        ++numRead;
        r = reader.readNext(false);
    }
    assertEquals(10, numRead);
    reader.close();
    dlm.close();
}
Also used : LogRecord(com.twitter.distributedlog.LogRecord) HeartbeatOptions(com.twitter.distributedlog.thrift.service.HeartbeatOptions) DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) AsyncLogReader(com.twitter.distributedlog.AsyncLogReader) LogReader(com.twitter.distributedlog.LogReader) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with HeartbeatOptions

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

the class TestDistributedLogService method testCloseStreamsShouldFlush.

@Test(timeout = 60000)
public void testCloseStreamsShouldFlush() 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);
    }
    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 succeed or be rejected : " + response.getHeader().getCode(), StatusCode.SUCCESS == response.getHeader().getCode() || StatusCode.WRITE_EXCEPTION == response.getHeader().getCode() || StatusCode.STREAM_UNAVAILABLE == response.getHeader().getCode());
    }
    assertTrue("There should be no streams in the cache", streamManager.getCachedStreams().isEmpty());
    assertTrue("There should be no streams in the acquired cache", streamManager.getAcquiredStreams().isEmpty());
    localService.shutdown();
}
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) Future(com.twitter.util.Future) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with HeartbeatOptions

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

Aggregations

HeartbeatOptions (com.twitter.distributedlog.thrift.service.HeartbeatOptions)3 Test (org.junit.Test)3 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)2 StreamManagerImpl (com.twitter.distributedlog.service.stream.StreamManagerImpl)2 WriteContext (com.twitter.distributedlog.thrift.service.WriteContext)2 WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)2 Future (com.twitter.util.Future)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AsyncLogReader (com.twitter.distributedlog.AsyncLogReader)1 DistributedLogManager (com.twitter.distributedlog.DistributedLogManager)1 LogReader (com.twitter.distributedlog.LogReader)1 LogRecord (com.twitter.distributedlog.LogRecord)1 Stream (com.twitter.distributedlog.service.stream.Stream)1 StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)1 Ignore (org.junit.Ignore)1