Search in sources :

Example 1 with StreamManagerImpl

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

Example 2 with StreamManagerImpl

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

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

the class DistributedLogServerTestCase method checkStreams.

protected static void checkStreams(Set<String> streams, DLServer dlServer) {
    StreamManagerImpl streamManager = (StreamManagerImpl) dlServer.dlServer.getKey().getStreamManager();
    Set<String> cachedStreams = streamManager.getCachedStreams().keySet();
    Set<String> acquiredStreams = streamManager.getAcquiredStreams().keySet();
    assertEquals(streams.size(), cachedStreams.size());
    assertEquals(streams.size(), acquiredStreams.size());
    assertTrue(Sets.difference(streams, cachedStreams).isEmpty());
    assertTrue(Sets.difference(streams, acquiredStreams).isEmpty());
}
Also used : StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl)

Example 4 with StreamManagerImpl

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

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

Aggregations

StreamManagerImpl (com.twitter.distributedlog.service.stream.StreamManagerImpl)8 WriteResponse (com.twitter.distributedlog.thrift.service.WriteResponse)5 Test (org.junit.Test)5 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)4 Future (com.twitter.util.Future)4 ArrayList (java.util.ArrayList)4 ServerConfiguration (com.twitter.distributedlog.service.config.ServerConfiguration)2 StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)2 HeartbeatOptions (com.twitter.distributedlog.thrift.service.HeartbeatOptions)2 WriteContext (com.twitter.distributedlog.thrift.service.WriteContext)2 SocketAddress (java.net.SocketAddress)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Stream (com.twitter.distributedlog.service.stream.Stream)1 HashMap (java.util.HashMap)1