use of io.pravega.controller.eventProcessor.EventSerializer in project pravega by pravega.
the class EventProcessorTest method testEventProcessorGroup.
@Test(timeout = 10000)
public void testEventProcessorGroup() throws CheckpointStoreException, ReinitializationRequiredException {
int count = 4;
int initialCount = count / 2;
String systemName = "testSystem";
String readerGroupName = "testReaderGroup";
int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int expectedSum = input.length * (input.length + 1) / 2;
CheckpointStore checkpointStore = CheckpointStoreFactory.createInMemoryStore();
checkpointStore.addReaderGroup(PROCESS, readerGroupName);
EventProcessorGroupConfig config = createEventProcessorGroupConfig(initialCount);
EventProcessorSystemImpl system = createMockSystem(systemName, PROCESS, SCOPE, createEventReaders(count, input), new EventStreamWriterMock<>(), readerGroupName);
EventProcessorConfig<TestEvent> eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(() -> new TestEventProcessor(false)).serializer(new EventSerializer<>()).decider((Throwable e) -> ExceptionHandler.Directive.Stop).config(config).build();
// Create EventProcessorGroup.
@Cleanup EventProcessorGroupImpl<TestEvent> group = (EventProcessorGroupImpl<TestEvent>) system.createEventProcessorGroup(eventProcessorConfig, checkpointStore, executor);
group.awaitRunning();
// Add a few event processors to the group.
group.changeEventProcessorCount(count - initialCount);
long actualSum = 0;
for (EventProcessorCell<TestEvent> cell : group.getEventProcessorMap().values()) {
cell.awaitTerminated();
TestEventProcessor actor = (TestEventProcessor) cell.getActor();
actualSum += actor.sum;
}
assertEquals(count * expectedSum, actualSum);
// Stop the group, and await its termination.
group.stopAsync();
group.awaitTerminated();
}
use of io.pravega.controller.eventProcessor.EventSerializer in project pravega by pravega.
the class EventProcessorTest method testEventProcessorCell.
@Test(timeout = 10000)
@SuppressWarnings("unchecked")
public void testEventProcessorCell() throws CheckpointStoreException, ReinitializationRequiredException {
CheckpointStore checkpointStore = CheckpointStoreFactory.createInMemoryStore();
CheckpointConfig.CheckpointPeriod period = CheckpointConfig.CheckpointPeriod.builder().numEvents(1).numSeconds(1).build();
CheckpointConfig checkpointConfig = CheckpointConfig.builder().type(CheckpointConfig.Type.Periodic).checkpointPeriod(period).build();
EventProcessorGroupConfig config = EventProcessorGroupConfigImpl.builder().eventProcessorCount(1).readerGroupName(READER_GROUP).streamName(STREAM_NAME).checkpointConfig(checkpointConfig).build();
int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int expectedSum = input.length * (input.length + 1) / 2;
List<MockEventRead<TestEvent>> inputEvents = new ArrayList<>(input.length);
for (int i = 0; i < input.length; i++) {
inputEvents.add(new MockEventRead<>(i, new TestEvent(input[i])));
}
inputEvents.add(new MockEventRead<>(input.length, new TestEvent(-1)));
EventProcessorSystem system = Mockito.mock(EventProcessorSystem.class);
Mockito.when(system.getProcess()).thenReturn(PROCESS);
EventStreamReader<TestEvent> reader = Mockito.mock(EventStreamReader.class);
checkpointStore.addReaderGroup(PROCESS, READER_GROUP);
// Test case 1. Actor does not throw any exception during normal operation.
Mockito.when(reader.readNextEvent(anyLong())).thenAnswer(new SequenceAnswer<>(inputEvents));
EventProcessorConfig<TestEvent> eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(() -> new TestEventProcessor(false)).serializer(new EventSerializer<>()).decider((Throwable e) -> ExceptionHandler.Directive.Stop).config(config).build();
testEventProcessor(system, eventProcessorConfig, reader, READER_ID, checkpointStore, expectedSum);
// Test case 2. Actor throws an error during normal operation, and Directive is to Resume on error.
Mockito.when(reader.readNextEvent(anyLong())).thenAnswer(new SequenceAnswer<>(inputEvents));
eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(() -> new TestEventProcessor(true)).serializer(new EventSerializer<>()).decider((Throwable e) -> (e instanceof IllegalArgumentException) ? ExceptionHandler.Directive.Resume : ExceptionHandler.Directive.Stop).config(config).build();
testEventProcessor(system, eventProcessorConfig, reader, READER_ID, checkpointStore, expectedSum);
// Test case 3. Actor throws an error during normal operation, and Directive is to Restart on error.
Mockito.when(reader.readNextEvent(anyLong())).thenAnswer(new SequenceAnswer<>(inputEvents));
eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(() -> new TestEventProcessor(true)).serializer(new EventSerializer<>()).decider((Throwable e) -> (e instanceof IllegalArgumentException) ? ExceptionHandler.Directive.Restart : ExceptionHandler.Directive.Stop).config(config).build();
testEventProcessor(system, eventProcessorConfig, reader, READER_ID, checkpointStore, 0);
// Test case 3. Actor throws an error during normal operation, and Directive is to Restart on error.
Mockito.when(reader.readNextEvent(anyLong())).thenAnswer(new SequenceAnswer<>(inputEvents));
eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(() -> new RestartFailingEventProcessor(true)).serializer(new EventSerializer<>()).decider((Throwable e) -> (e instanceof IllegalArgumentException) ? ExceptionHandler.Directive.Restart : ExceptionHandler.Directive.Stop).config(config).build();
testEventProcessor(system, eventProcessorConfig, reader, READER_ID, checkpointStore, 3);
// Test case 5. startup fails for an event processor
eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(StartFailingEventProcessor::new).serializer(new EventSerializer<>()).decider((Throwable e) -> ExceptionHandler.Directive.Stop).config(config).build();
checkpointStore.addReader(PROCESS, READER_GROUP, READER_ID);
EventProcessorCell<TestEvent> cell = new EventProcessorCell<>(eventProcessorConfig, reader, new EventStreamWriterMock<>(), system.getProcess(), READER_ID, 0, checkpointStore);
cell.startAsync();
cell.awaitTerminated();
checkpointStore.removeReader(PROCESS, READER_GROUP, READER_ID);
// Test case 6. Close event processor cell when reader/checkpoint store throw exceptions.
Mockito.doThrow(new IllegalArgumentException("Failing reader")).when(reader).closeAt(any());
checkpointStore = spy(checkpointStore);
Mockito.doThrow(new IllegalArgumentException("Failing checkpointStore")).when(checkpointStore).removeReader(anyString(), anyString(), anyString());
eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(StartFailingEventProcessor::new).serializer(new EventSerializer<>()).decider((Throwable e) -> ExceptionHandler.Directive.Stop).config(config).build();
checkpointStore.addReader(PROCESS, READER_GROUP, READER_ID);
cell = new EventProcessorCell<>(eventProcessorConfig, reader, new EventStreamWriterMock<>(), system.getProcess(), READER_ID, 0, checkpointStore);
cell.startAsync();
cell.awaitTerminated();
}
use of io.pravega.controller.eventProcessor.EventSerializer in project pravega by pravega.
the class EventProcessorTest method testInitialize.
@Test(timeout = 10000)
public void testInitialize() throws ReinitializationRequiredException, CheckpointStoreException {
String systemName = "testSystem";
String readerGroupName = "testReaderGroup";
EventStreamWriterMock<TestEvent> writer = new EventStreamWriterMock<>();
int[] input = { 1, 2, 3, 4, 5 };
CheckpointStore checkpointStore = CheckpointStoreFactory.createInMemoryStore();
checkpointStore.addReaderGroup(PROCESS, readerGroupName);
CheckpointConfig checkpointConfig = CheckpointConfig.builder().type(CheckpointConfig.Type.None).build();
EventProcessorGroupConfig config = EventProcessorGroupConfigImpl.builder().eventProcessorCount(3).readerGroupName(READER_GROUP).streamName(STREAM_NAME).checkpointConfig(checkpointConfig).build();
createEventProcessorGroupConfig(3);
EventProcessorSystemImpl system = createMockSystem(systemName, PROCESS, SCOPE, createEventReaders(3, input), writer, readerGroupName);
EventProcessorConfig<TestEvent> eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(() -> new StartWritingEventProcessor(false, input)).serializer(new EventSerializer<>()).decider((Throwable e) -> ExceptionHandler.Directive.Stop).config(config).build();
// Create EventProcessorGroup.
@Cleanup EventProcessorGroupImpl<TestEvent> group = (EventProcessorGroupImpl<TestEvent>) system.createEventProcessorGroup(eventProcessorConfig, checkpointStore, executor);
// test idempotent initialize
group.initialize();
group.initialize();
// Await until it is ready.
group.awaitRunning();
group.initialize();
assertEquals(3, group.getEventProcessorMap().values().size());
}
use of io.pravega.controller.eventProcessor.EventSerializer in project pravega by pravega.
the class EventProcessorTest method testEventProcessorWriter.
@Test(timeout = 10000)
public void testEventProcessorWriter() throws ReinitializationRequiredException, CheckpointStoreException {
int initialCount = 1;
String systemName = "testSystem";
String readerGroupName = "testReaderGroup";
int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
EventStreamWriterMock<TestEvent> writer = new EventStreamWriterMock<>();
CheckpointStore checkpointStore = CheckpointStoreFactory.createInMemoryStore();
checkpointStore.addReaderGroup(PROCESS, readerGroupName);
CheckpointConfig checkpointConfig = CheckpointConfig.builder().type(CheckpointConfig.Type.None).build();
EventProcessorGroupConfig config = EventProcessorGroupConfigImpl.builder().eventProcessorCount(1).readerGroupName(READER_GROUP).streamName(STREAM_NAME).checkpointConfig(checkpointConfig).build();
createEventProcessorGroupConfig(initialCount);
EventProcessorSystemImpl system = createMockSystem(systemName, PROCESS, SCOPE, createEventReaders(1, input), writer, readerGroupName);
EventProcessorConfig<TestEvent> eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(() -> new StartWritingEventProcessor(false, input)).serializer(new EventSerializer<>()).decider((Throwable e) -> ExceptionHandler.Directive.Stop).config(config).build();
// Create EventProcessorGroup.
@Cleanup EventProcessorGroupImpl<TestEvent> group = (EventProcessorGroupImpl<TestEvent>) system.createEventProcessorGroup(eventProcessorConfig, checkpointStore, executor);
// Await until it is ready.
group.awaitRunning();
// By now, the events have been written to the Mock EventStreamWriter.
Integer[] writerList = writer.getEventList().stream().map(TestEvent::getNumber).collect(Collectors.toList()).toArray(new Integer[input.length]);
// Validate that events are correctly written.
Assert.assertArrayEquals(input, ArrayUtils.toPrimitive(writerList));
}
use of io.pravega.controller.eventProcessor.EventSerializer in project pravega by pravega.
the class EventProcessorTest method testFailingEventProcessorInGroup.
@Test(timeout = 10000)
public void testFailingEventProcessorInGroup() throws ReinitializationRequiredException, CheckpointStoreException {
String systemName = "testSystem";
String readerGroupName = "testReaderGroup";
int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
CheckpointStore checkpointStore = CheckpointStoreFactory.createInMemoryStore();
EventProcessorGroupConfig config = createEventProcessorGroupConfig(1);
EventProcessorSystemImpl system = createMockSystem(systemName, PROCESS, SCOPE, createEventReaders(1, input), new EventStreamWriterMock<>(), readerGroupName);
EventProcessorConfig<TestEvent> eventProcessorConfig = EventProcessorConfig.<TestEvent>builder().supplier(StartFailingEventProcessor::new).serializer(new EventSerializer<>()).decider((Throwable e) -> ExceptionHandler.Directive.Stop).config(config).build();
// Create EventProcessorGroup.
@Cleanup EventProcessorGroupImpl<TestEvent> group = (EventProcessorGroupImpl<TestEvent>) system.createEventProcessorGroup(eventProcessorConfig, checkpointStore, executor);
// awaitRunning should succeed.
group.awaitRunning();
Assert.assertTrue(true);
}
Aggregations