Search in sources :

Example 1 with Partition

use of com.twitter.distributedlog.service.streamset.Partition in project distributedlog by twitter.

the class StreamManagerImpl method getOrCreateStream.

@Override
public Stream getOrCreateStream(String streamName) throws IOException {
    Stream stream = streams.get(streamName);
    if (null == stream) {
        closeLock.readLock().lock();
        try {
            if (closed) {
                return null;
            }
            DynamicDistributedLogConfiguration dynConf = getDynConf(streamName);
            int maxCachedPartitions = dynConf.getMaxCachedPartitionsPerProxy();
            // get partition from the stream name
            Partition partition = partitionConverter.convert(streamName);
            // add partition to cached map
            if (!cachedPartitions.addPartition(partition, maxCachedPartitions)) {
                throw new StreamUnavailableException("Stream " + streamName + " is not allowed to cache more than " + maxCachedPartitions + " partitions");
            }
            stream = newStream(streamName, dynConf);
            Stream oldWriter = streams.putIfAbsent(streamName, stream);
            if (null != oldWriter) {
                stream = oldWriter;
            } else {
                numCached.getAndIncrement();
                logger.info("Inserted mapping stream name {} -> stream {}", streamName, stream);
                stream.initialize();
                stream.start();
            }
        } finally {
            closeLock.readLock().unlock();
        }
    }
    return stream;
}
Also used : StreamUnavailableException(com.twitter.distributedlog.exceptions.StreamUnavailableException) Partition(com.twitter.distributedlog.service.streamset.Partition) DynamicDistributedLogConfiguration(com.twitter.distributedlog.config.DynamicDistributedLogConfiguration)

Example 2 with Partition

use of com.twitter.distributedlog.service.streamset.Partition in project distributedlog by twitter.

the class TestStreamManager method testCollectionMethods.

@Test(timeout = 60000)
public void testCollectionMethods() throws Exception {
    Stream mockStream = mock(Stream.class);
    when(mockStream.getStreamName()).thenReturn("stream1");
    when(mockStream.getPartition()).thenReturn(new Partition("stream1", 0));
    StreamFactory mockStreamFactory = mock(StreamFactory.class);
    StreamPartitionConverter mockPartitionConverter = mock(StreamPartitionConverter.class);
    StreamConfigProvider mockStreamConfigProvider = mock(StreamConfigProvider.class);
    when(mockStreamFactory.create((String) any(), (DynamicDistributedLogConfiguration) any(), (StreamManager) any())).thenReturn(mockStream);
    StreamManager streamManager = new StreamManagerImpl("", new DistributedLogConfiguration(), mockExecutorService, mockStreamFactory, mockPartitionConverter, mockStreamConfigProvider, mock(DistributedLogNamespace.class));
    assertFalse(streamManager.isAcquired("stream1"));
    assertEquals(0, streamManager.numAcquired());
    assertEquals(0, streamManager.numCached());
    streamManager.notifyAcquired(mockStream);
    assertTrue(streamManager.isAcquired("stream1"));
    assertEquals(1, streamManager.numAcquired());
    assertEquals(0, streamManager.numCached());
    streamManager.notifyReleased(mockStream);
    assertFalse(streamManager.isAcquired("stream1"));
    assertEquals(0, streamManager.numAcquired());
    assertEquals(0, streamManager.numCached());
    streamManager.notifyAcquired(mockStream);
    assertTrue(streamManager.isAcquired("stream1"));
    assertEquals(1, streamManager.numAcquired());
    assertEquals(0, streamManager.numCached());
    streamManager.notifyAcquired(mockStream);
    assertTrue(streamManager.isAcquired("stream1"));
    assertEquals(1, streamManager.numAcquired());
    assertEquals(0, streamManager.numCached());
    streamManager.notifyReleased(mockStream);
    assertFalse(streamManager.isAcquired("stream1"));
    assertEquals(0, streamManager.numAcquired());
    assertEquals(0, streamManager.numCached());
    streamManager.notifyReleased(mockStream);
    assertFalse(streamManager.isAcquired("stream1"));
    assertEquals(0, streamManager.numAcquired());
    assertEquals(0, streamManager.numCached());
}
Also used : Partition(com.twitter.distributedlog.service.streamset.Partition) DistributedLogConfiguration(com.twitter.distributedlog.DistributedLogConfiguration) DynamicDistributedLogConfiguration(com.twitter.distributedlog.config.DynamicDistributedLogConfiguration) DistributedLogNamespace(com.twitter.distributedlog.namespace.DistributedLogNamespace) StreamConfigProvider(com.twitter.distributedlog.service.config.StreamConfigProvider) StreamPartitionConverter(com.twitter.distributedlog.service.streamset.StreamPartitionConverter) Test(org.junit.Test)

Aggregations

DynamicDistributedLogConfiguration (com.twitter.distributedlog.config.DynamicDistributedLogConfiguration)2 Partition (com.twitter.distributedlog.service.streamset.Partition)2 DistributedLogConfiguration (com.twitter.distributedlog.DistributedLogConfiguration)1 StreamUnavailableException (com.twitter.distributedlog.exceptions.StreamUnavailableException)1 DistributedLogNamespace (com.twitter.distributedlog.namespace.DistributedLogNamespace)1 StreamConfigProvider (com.twitter.distributedlog.service.config.StreamConfigProvider)1 StreamPartitionConverter (com.twitter.distributedlog.service.streamset.StreamPartitionConverter)1 Test (org.junit.Test)1