use of io.pravega.client.ClientFactory in project pravega by pravega.
the class RevisionedStreamClientTest method testMark.
@Test
public void testMark() {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
client.writeUnconditionally("a");
Revision ra = client.fetchLatestRevision();
client.writeUnconditionally("b");
Revision rb = client.fetchLatestRevision();
client.writeUnconditionally("c");
Revision rc = client.fetchLatestRevision();
assertTrue(client.compareAndSetMark(null, ra));
assertEquals(ra, client.getMark());
assertTrue(client.compareAndSetMark(ra, rb));
assertEquals(rb, client.getMark());
assertFalse(client.compareAndSetMark(ra, rc));
assertEquals(rb, client.getMark());
assertTrue(client.compareAndSetMark(rb, rc));
assertEquals(rc, client.getMark());
assertTrue(client.compareAndSetMark(rc, ra));
assertEquals(ra, client.getMark());
assertTrue(client.compareAndSetMark(ra, null));
assertEquals(null, client.getMark());
}
use of io.pravega.client.ClientFactory in project pravega by pravega.
the class RevisionedStreamClientTest method testWriteWhileReading.
@Test
public void testWriteWhileReading() {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
RevisionedStreamClient<String> client = clientFactory.createRevisionedStreamClient(stream, new JavaSerializer<>(), config);
Revision initialRevision = client.fetchLatestRevision();
client.writeUnconditionally("a");
client.writeUnconditionally("b");
client.writeUnconditionally("c");
Iterator<Entry<Revision, String>> iter = client.readFrom(initialRevision);
assertTrue(iter.hasNext());
assertEquals("a", iter.next().getValue());
client.writeUnconditionally("d");
assertTrue(iter.hasNext());
assertEquals("b", iter.next().getValue());
assertTrue(iter.hasNext());
Entry<Revision, String> entry = iter.next();
assertEquals("c", entry.getValue());
assertFalse(iter.hasNext());
iter = client.readFrom(entry.getKey());
assertTrue(iter.hasNext());
assertEquals("d", iter.next().getValue());
assertFalse(iter.hasNext());
}
use of io.pravega.client.ClientFactory in project pravega by pravega.
the class EventProcessorTest method createMockSystem.
@SuppressWarnings("unchecked")
private EventProcessorSystemImpl createMockSystem(final String name, final String processId, final String scope, final SequenceAnswer<EventStreamReader<TestEvent>> readers, final EventStreamWriter<TestEvent> writer, final String readerGroupName) {
ClientFactory clientFactory = Mockito.mock(ClientFactory.class);
Mockito.when(clientFactory.createReader(anyString(), anyString(), any(), any())).thenAnswer(readers);
Mockito.when(clientFactory.<TestEvent>createEventWriter(anyString(), any(), any())).thenReturn(writer);
ReaderGroup readerGroup = Mockito.mock(ReaderGroup.class);
Mockito.when(readerGroup.getGroupName()).thenReturn(readerGroupName);
ReaderGroupManager readerGroupManager = Mockito.mock(ReaderGroupManager.class);
Mockito.when(readerGroupManager.createReaderGroup(anyString(), any(ReaderGroupConfig.class))).then(invocation -> readerGroup);
return new EventProcessorSystemImpl(name, processId, scope, clientFactory, readerGroupManager);
}
use of io.pravega.client.ClientFactory in project pravega by pravega.
the class ReaderGroupStateManagerTest method testSegmentsCannotBeReleasedWithoutCheckpoint.
@Test(timeout = 10000)
public void testSegmentsCannotBeReleasedWithoutCheckpoint() throws ReinitializationRequiredException {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
Segment segment0 = new Segment(scope, stream, 0);
Segment segment1 = new Segment(scope, stream, 1);
Segment segment2 = new Segment(scope, stream, 2);
MockController controller = new MockControllerWithSuccessors(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, new StreamSegmentsWithPredecessors(ImmutableMap.of(), ""));
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
AtomicLong clock = new AtomicLong();
Map<Segment, Long> segments = ImmutableMap.of(segment0, 0L, segment1, 1L, segment2, 2L);
ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
val readerState1 = new ReaderGroupStateManager("reader1", stateSynchronizer, controller, clock::get);
readerState1.initializeReader(0);
val readerState2 = new ReaderGroupStateManager("reader2", stateSynchronizer, controller, clock::get);
readerState2.initializeReader(0);
assertEquals(segments, stateSynchronizer.getState().getUnassignedSegments());
stateSynchronizer.updateStateUnconditionally(new CreateCheckpoint("CP1"));
stateSynchronizer.fetchUpdates();
assertEquals("CP1", readerState1.getCheckpoint());
assertEquals(Collections.emptyMap(), readerState1.acquireNewSegmentsIfNeeded(1));
assertEquals(Collections.emptyMap(), readerState2.acquireNewSegmentsIfNeeded(2));
assertEquals("CP1", readerState2.getCheckpoint());
readerState1.checkpoint("CP1", new PositionImpl(Collections.emptyMap()));
readerState2.checkpoint("CP1", new PositionImpl(Collections.emptyMap()));
assertEquals(segments, stateSynchronizer.getState().getPositionsForCompletedCheckpoint("CP1"));
Map<Segment, Long> segments1 = readerState1.acquireNewSegmentsIfNeeded(1);
Map<Segment, Long> segments2 = readerState2.acquireNewSegmentsIfNeeded(2);
assertFalse(segments1.isEmpty());
assertFalse(segments2.isEmpty());
assertEquals(0, stateSynchronizer.getState().getNumberOfUnassignedSegments());
// Induce imbalance
for (Entry<Segment, Long> entry : segments1.entrySet()) {
stateSynchronizer.updateStateUnconditionally(new ReaderGroupState.ReleaseSegment("reader1", entry.getKey(), entry.getValue()));
stateSynchronizer.updateStateUnconditionally(new ReaderGroupState.AcquireSegment("reader2", entry.getKey()));
}
stateSynchronizer.updateStateUnconditionally(new CreateCheckpoint("CP2"));
stateSynchronizer.fetchUpdates();
clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
assertNull(readerState1.findSegmentToReleaseIfRequired());
assertNull(readerState2.findSegmentToReleaseIfRequired());
clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
assertFalse(readerState2.releaseSegment(segments2.keySet().iterator().next(), 20, 2));
clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
readerState1.checkpoint("CP2", new PositionImpl(Collections.emptyMap()));
readerState2.checkpoint("CP2", new PositionImpl(segments));
assertEquals(segments, stateSynchronizer.getState().getPositionsForCompletedCheckpoint("CP2"));
Segment toRelease = readerState2.findSegmentToReleaseIfRequired();
assertNotNull(toRelease);
assertTrue(readerState2.releaseSegment(toRelease, 10, 1));
assertEquals(1, stateSynchronizer.getState().getNumberOfUnassignedSegments());
}
use of io.pravega.client.ClientFactory in project pravega by pravega.
the class ReaderGroupStateManagerTest method testSegmentsAssigned.
@Test(timeout = 10000)
public void testSegmentsAssigned() throws ReinitializationRequiredException {
String scope = "scope";
String stream = "stream";
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory);
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
@Cleanup ClientFactory clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory, streamFactory, streamFactory, streamFactory);
SynchronizerConfig config = SynchronizerConfig.builder().build();
@Cleanup StateSynchronizer<ReaderGroupState> stateSynchronizer = createState(stream, clientFactory, config);
AtomicLong clock = new AtomicLong();
Map<Segment, Long> segments = new HashMap<>();
segments.put(new Segment(scope, stream, 0), 0L);
segments.put(new Segment(scope, stream, 1), 1L);
segments.put(new Segment(scope, stream, 2), 2L);
segments.put(new Segment(scope, stream, 3), 3L);
ReaderGroupStateManager.initializeReaderGroup(stateSynchronizer, ReaderGroupConfig.builder().stream(Stream.of(scope, stream)).build(), segments);
ReaderGroupStateManager reader1 = new ReaderGroupStateManager("reader1", stateSynchronizer, controller, clock::get);
reader1.initializeReader(0);
ReaderGroupStateManager reader2 = new ReaderGroupStateManager("reader2", stateSynchronizer, controller, clock::get);
reader2.initializeReader(0);
Map<Segment, Long> segments1 = reader1.acquireNewSegmentsIfNeeded(0);
assertFalse(segments1.isEmpty());
assertEquals(2, segments1.size());
assertTrue(reader1.acquireNewSegmentsIfNeeded(0).isEmpty());
assertNull(reader1.findSegmentToReleaseIfRequired());
Map<Segment, Long> segments2 = reader2.acquireNewSegmentsIfNeeded(0);
assertFalse(segments2.isEmpty());
assertEquals(2, segments2.size());
assertTrue(reader2.acquireNewSegmentsIfNeeded(0).isEmpty());
assertNull(reader2.findSegmentToReleaseIfRequired());
assertTrue(Sets.intersection(segments1.keySet(), segments2.keySet()).isEmpty());
clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
assertFalse(reader1.releaseSegment(new Segment(scope, stream, 0), 0, 0));
clock.addAndGet(ReaderGroupStateManager.UPDATE_WINDOW.toNanos());
assertTrue(reader1.acquireNewSegmentsIfNeeded(0).isEmpty());
assertNull(reader1.findSegmentToReleaseIfRequired());
assertTrue(reader2.acquireNewSegmentsIfNeeded(0).isEmpty());
assertNull(reader2.findSegmentToReleaseIfRequired());
reader1.readerShutdown(new PositionImpl(segments1));
Map<Segment, Long> segmentsRecovered = reader2.acquireNewSegmentsIfNeeded(0);
assertFalse(segmentsRecovered.isEmpty());
assertEquals(2, segmentsRecovered.size());
assertEquals(segments1, segmentsRecovered);
assertTrue(reader2.acquireNewSegmentsIfNeeded(0).isEmpty());
assertNull(reader2.findSegmentToReleaseIfRequired());
segments2.putAll(segmentsRecovered);
reader2.readerShutdown(new PositionImpl(segments2));
reader1.initializeReader(0);
segments1 = reader1.acquireNewSegmentsIfNeeded(0);
assertEquals(4, segments1.size());
assertEquals(segments2, segments1);
}
Aggregations