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;
}
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());
}
Aggregations