Search in sources :

Example 6 with StreamManagerImpl

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

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

the class DistributedLogServerTestCase method checkStream.

protected static void checkStream(String name, DLClient dlClient, DLServer dlServer, int expectedNumProxiesInClient, int expectedClientCacheSize, int expectedServerCacheSize, boolean existedInServer, boolean existedInClient) {
    Map<SocketAddress, Set<String>> distribution = dlClient.dlClient.getStreamOwnershipDistribution();
    assertEquals(expectedNumProxiesInClient, distribution.size());
    if (expectedNumProxiesInClient > 0) {
        Map.Entry<SocketAddress, Set<String>> localEntry = distribution.entrySet().iterator().next();
        assertEquals(dlServer.getAddress(), localEntry.getKey());
        assertEquals(expectedClientCacheSize, localEntry.getValue().size());
        assertEquals(existedInClient, localEntry.getValue().contains(name));
    }
    StreamManagerImpl streamManager = (StreamManagerImpl) dlServer.dlServer.getKey().getStreamManager();
    Set<String> cachedStreams = streamManager.getCachedStreams().keySet();
    Set<String> acquiredStreams = streamManager.getCachedStreams().keySet();
    assertEquals(expectedServerCacheSize, cachedStreams.size());
    assertEquals(existedInServer, cachedStreams.contains(name));
    assertEquals(expectedServerCacheSize, acquiredStreams.size());
    assertEquals(existedInServer, acquiredStreams.contains(name));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl) SocketAddress(java.net.SocketAddress) Map(java.util.Map)

Example 8 with StreamManagerImpl

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

the class TestDistributedLogServer method checkStream.

private void checkStream(int expectedNumProxiesInClient, int expectedClientCacheSize, int expectedServerCacheSize, String name, SocketAddress owner, boolean existedInServer, boolean existedInClient) {
    Map<SocketAddress, Set<String>> distribution = dlClient.dlClient.getStreamOwnershipDistribution();
    assertEquals(expectedNumProxiesInClient, distribution.size());
    if (expectedNumProxiesInClient > 0) {
        Map.Entry<SocketAddress, Set<String>> localEntry = distribution.entrySet().iterator().next();
        assertEquals(owner, localEntry.getKey());
        assertEquals(expectedClientCacheSize, localEntry.getValue().size());
        assertEquals(existedInClient, localEntry.getValue().contains(name));
    }
    StreamManagerImpl streamManager = (StreamManagerImpl) dlServer.dlServer.getKey().getStreamManager();
    Set<String> cachedStreams = streamManager.getCachedStreams().keySet();
    Set<String> acquiredStreams = streamManager.getCachedStreams().keySet();
    assertEquals(expectedServerCacheSize, cachedStreams.size());
    assertEquals(existedInServer, cachedStreams.contains(name));
    assertEquals(expectedServerCacheSize, acquiredStreams.size());
    assertEquals(existedInServer, acquiredStreams.contains(name));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) StreamManagerImpl(com.twitter.distributedlog.service.stream.StreamManagerImpl) SocketAddress(java.net.SocketAddress) Map(java.util.Map) HashMap(java.util.HashMap)

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