Search in sources :

Example 11 with ContainerEventProcessor

use of io.pravega.segmentstore.server.ContainerEventProcessor in project pravega by pravega.

the class ContainerEventProcessorTests method testEventProcessorClose.

/**
 * Test closing the EventProcessor.
 *
 * @throws Exception
 */
@Test(timeout = 30000)
public void testEventProcessorClose() throws Exception {
    @Cleanup ContainerEventProcessor eventProcessorService = new ContainerEventProcessorImpl(0, mockSegmentSupplier(), ITERATION_DELAY, CONTAINER_OPERATION_TIMEOUT, this.executorService());
    int maxItemsProcessed = 10;
    int maxOutstandingBytes = 4 * 1024 * 1024;
    int truncationDataSize = 500;
    ContainerEventProcessor.EventProcessorConfig config = new ContainerEventProcessor.EventProcessorConfig(maxItemsProcessed, maxOutstandingBytes, truncationDataSize);
    AtomicLong processorResults = new AtomicLong(0);
    Function<List<BufferView>, CompletableFuture<Void>> handler = l -> {
        processorResults.addAndGet(l.size());
        return CompletableFuture.completedFuture(null);
    };
    @Cleanup ContainerEventProcessor.EventProcessor processor = eventProcessorService.forConsumer("testClose", handler, config).get(TIMEOUT_FUTURE.toSeconds(), TimeUnit.SECONDS);
    // Assert that the existing object in the ContainerEventProcessorImpl map is the same as the one we just instantiated.
    Assert.assertEquals(processor, eventProcessorService.forConsumer("testClose", handler, config).get(TIMEOUT_FUTURE.toSeconds(), TimeUnit.SECONDS));
    // Now, close the EventProcessor object, which should auto-unregister from the map.
    processor.close();
    // After that, we should see a new object being instantiated in ContainerEventProcessorImpl for the same name.
    Assert.assertNotEquals(processor, eventProcessorService.forConsumer("testClose", handler, config).get(TIMEOUT_FUTURE.toSeconds(), TimeUnit.SECONDS));
}
Also used : ContainerEventProcessor(io.pravega.segmentstore.server.ContainerEventProcessor) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ContainerEventProcessor(io.pravega.segmentstore.server.ContainerEventProcessor) AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) Cleanup(lombok.Cleanup) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Timeout(org.junit.rules.Timeout) SerializationException(io.pravega.common.io.SerializationException) Mockito.anyLong(org.mockito.Mockito.anyLong) ReusableLatch(io.pravega.common.util.ReusableLatch) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) SegmentMock(io.pravega.segmentstore.server.SegmentMock) AtomicLong(java.util.concurrent.atomic.AtomicLong) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) List(java.util.List) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Mockito.anyInt(org.mockito.Mockito.anyInt) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 12 with ContainerEventProcessor

use of io.pravega.segmentstore.server.ContainerEventProcessor in project pravega by pravega.

the class ContainerEventProcessorTests method testEventProcessorWithSerializationError.

public static void testEventProcessorWithSerializationError(ContainerEventProcessor containerEventProcessor) throws Exception {
    int maxItemsPerBatch = 10;
    int maxOutstandingBytes = 4 * 1024 * 1024;
    int truncationDataSize = 500;
    AtomicLong readEvents = new AtomicLong(0);
    Function<List<BufferView>, CompletableFuture<Void>> handler = l -> {
        readEvents.addAndGet(l.size());
        return CompletableFuture.completedFuture(null);
    };
    ContainerEventProcessor.EventProcessorConfig eventProcessorConfig = spy(new ContainerEventProcessor.EventProcessorConfig(maxItemsPerBatch, maxOutstandingBytes, truncationDataSize));
    @Cleanup ContainerEventProcessor.EventProcessor processor = containerEventProcessor.forConsumer("testConsumer", handler, eventProcessorConfig).get(TIMEOUT_FUTURE.toSeconds(), TimeUnit.SECONDS);
    // Simulate an BufferView.Reader.OutOfBoundsException within the deserializeEvents() method and then behave normally.
    when(eventProcessorConfig.getMaxItemsAtOnce()).thenThrow(new BufferView.Reader.OutOfBoundsException()).thenThrow(new RuntimeException(new SerializationException("Intentional exception"))).thenCallRealMethod();
    // Write an event and wait for the event to be processed.
    BufferView event = new ByteArraySegment("event".getBytes());
    processor.add(event, TIMEOUT_FUTURE).join();
    // Wait until the processor reads the event.
    AssertExtensions.assertEventuallyEquals(true, () -> readEvents.get() == 1, 10000);
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ContainerEventProcessor(io.pravega.segmentstore.server.ContainerEventProcessor) AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) Cleanup(lombok.Cleanup) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Timeout(org.junit.rules.Timeout) SerializationException(io.pravega.common.io.SerializationException) Mockito.anyLong(org.mockito.Mockito.anyLong) ReusableLatch(io.pravega.common.util.ReusableLatch) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) SegmentMock(io.pravega.segmentstore.server.SegmentMock) AtomicLong(java.util.concurrent.atomic.AtomicLong) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) List(java.util.List) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Mockito.anyInt(org.mockito.Mockito.anyInt) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) SerializationException(io.pravega.common.io.SerializationException) ByteArraySegment(io.pravega.common.util.ByteArraySegment) Cleanup(lombok.Cleanup) ContainerEventProcessor(io.pravega.segmentstore.server.ContainerEventProcessor) AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFuture(java.util.concurrent.CompletableFuture) BufferView(io.pravega.common.util.BufferView) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with ContainerEventProcessor

use of io.pravega.segmentstore.server.ContainerEventProcessor in project pravega by pravega.

the class ContainerEventProcessorTests method testMultipleProcessors.

public static void testMultipleProcessors(ContainerEventProcessor eventProcessorService) throws Exception {
    int allEventsToProcess = 100;
    int maxItemsPerBatch = 10;
    int maxOutstandingBytes = 4 * 1024 * 1024;
    int truncationDataSize = 500;
    List<Integer> processorResults1 = new ArrayList<>();
    Function<List<BufferView>, CompletableFuture<Void>> handler1 = getNumberSequenceHandler(processorResults1, maxItemsPerBatch);
    List<Integer> processorResults2 = new ArrayList<>();
    Function<List<BufferView>, CompletableFuture<Void>> handler2 = getNumberSequenceHandler(processorResults2, maxItemsPerBatch);
    AtomicLong processorResults3 = new AtomicLong(0);
    Function<List<BufferView>, CompletableFuture<Void>> handler3 = l -> {
        Object o = null;
        // We should expect NPE here, so the results counter would not be incremented.
        o.toString();
        processorResults3.addAndGet(1);
        return CompletableFuture.completedFuture(null);
    };
    ContainerEventProcessor.EventProcessorConfig config = new ContainerEventProcessor.EventProcessorConfig(maxItemsPerBatch, maxOutstandingBytes, truncationDataSize);
    @Cleanup ContainerEventProcessor.EventProcessor processor1 = eventProcessorService.forConsumer("testSegment1", handler1, config).get(TIMEOUT_FUTURE.toSeconds(), TimeUnit.SECONDS);
    @Cleanup ContainerEventProcessor.EventProcessor processor2 = eventProcessorService.forConsumer("testSegment2", handler2, config).get(TIMEOUT_FUTURE.toSeconds(), TimeUnit.SECONDS);
    @Cleanup ContainerEventProcessor.EventProcessor processor3 = eventProcessorService.forConsumer("testSegment3", handler3, config).get(TIMEOUT_FUTURE.toSeconds(), TimeUnit.SECONDS);
    for (int i = 0; i < allEventsToProcess; i++) {
        BufferView event = new ByteArraySegment(ByteBuffer.allocate(Integer.BYTES).putInt(i).array());
        processor1.add(event, TIMEOUT_FUTURE).join();
        processor2.add(event, TIMEOUT_FUTURE).join();
        processor3.add(event, TIMEOUT_FUTURE).join();
    }
    // Wait for all items to be processed.
    validateProcessorResults(processor1, processorResults1, allEventsToProcess);
    validateProcessorResults(processor2, processorResults2, allEventsToProcess);
    AssertExtensions.assertEventuallyEquals(true, () -> processorResults3.get() == 0, 10000);
    AssertExtensions.assertEventuallyEquals(true, () -> ((ContainerEventProcessorImpl.EventProcessorImpl) processor3).getOutstandingBytes() > 0, 10000);
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ContainerEventProcessor(io.pravega.segmentstore.server.ContainerEventProcessor) AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) Cleanup(lombok.Cleanup) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) BufferView(io.pravega.common.util.BufferView) Duration(java.time.Duration) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Timeout(org.junit.rules.Timeout) SerializationException(io.pravega.common.io.SerializationException) Mockito.anyLong(org.mockito.Mockito.anyLong) ReusableLatch(io.pravega.common.util.ReusableLatch) IntentionalException(io.pravega.test.common.IntentionalException) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) SegmentMock(io.pravega.segmentstore.server.SegmentMock) AtomicLong(java.util.concurrent.atomic.AtomicLong) DirectSegmentAccess(io.pravega.segmentstore.server.DirectSegmentAccess) List(java.util.List) Rule(org.junit.Rule) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) Mockito.anyInt(org.mockito.Mockito.anyInt) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ArrayList(java.util.ArrayList) Cleanup(lombok.Cleanup) ContainerEventProcessor(io.pravega.segmentstore.server.ContainerEventProcessor) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) BufferView(io.pravega.common.util.BufferView) ArrayList(java.util.ArrayList) List(java.util.List)

Example 14 with ContainerEventProcessor

use of io.pravega.segmentstore.server.ContainerEventProcessor in project pravega by pravega.

the class StreamSegmentContainerTests method testEventProcessorEventRejectionOnMaxOutstanding.

/**
 * Check that an EventProcessor does not accept any new event once the maximum outstanding bytes has been reached.
 *
 * @throws Exception
 */
@Test(timeout = 30000)
public void testEventProcessorEventRejectionOnMaxOutstanding() throws Exception {
    @Cleanup TestContext context = createContext();
    val container = (StreamSegmentContainer) context.container;
    container.startAsync().awaitRunning();
    @Cleanup ContainerEventProcessor containerEventProcessor = new ContainerEventProcessorImpl(container, container.metadataStore, TIMEOUT_EVENT_PROCESSOR_ITERATION, TIMEOUT_EVENT_PROCESSOR_ITERATION, this.executorService());
    ContainerEventProcessorTests.testEventRejectionOnMaxOutstanding(containerEventProcessor);
}
Also used : lombok.val(lombok.val) ContainerEventProcessor(io.pravega.segmentstore.server.ContainerEventProcessor) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 15 with ContainerEventProcessor

use of io.pravega.segmentstore.server.ContainerEventProcessor in project pravega by pravega.

the class StreamSegmentContainerTests method testEventProcessorFaultyHandler.

/**
 * Verify that when a faulty handler is passed to an EventProcessor, the Segment is not truncated and the retries
 * continue indefinitely.
 *
 * @throws Exception
 */
@Test(timeout = 10000)
public void testEventProcessorFaultyHandler() throws Exception {
    @Cleanup TestContext context = createContext();
    val container = (StreamSegmentContainer) context.container;
    container.startAsync().awaitRunning();
    @Cleanup ContainerEventProcessor containerEventProcessor = new ContainerEventProcessorImpl(container, container.metadataStore, TIMEOUT_EVENT_PROCESSOR_ITERATION, TIMEOUT_EVENT_PROCESSOR_ITERATION, this.executorService());
    ContainerEventProcessorTests.testFaultyHandler(containerEventProcessor);
}
Also used : lombok.val(lombok.val) ContainerEventProcessor(io.pravega.segmentstore.server.ContainerEventProcessor) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

ContainerEventProcessor (io.pravega.segmentstore.server.ContainerEventProcessor)24 Cleanup (lombok.Cleanup)24 Test (org.junit.Test)23 BufferView (io.pravega.common.util.BufferView)12 ByteArraySegment (io.pravega.common.util.ByteArraySegment)12 ArrayList (java.util.ArrayList)12 List (java.util.List)12 CompletableFuture (java.util.concurrent.CompletableFuture)12 Exceptions (io.pravega.common.Exceptions)10 DirectSegmentAccess (io.pravega.segmentstore.server.DirectSegmentAccess)10 SegmentMetadata (io.pravega.segmentstore.server.SegmentMetadata)10 AssertExtensions (io.pravega.test.common.AssertExtensions)10 IntentionalException (io.pravega.test.common.IntentionalException)10 ThreadPooledTestSuite (io.pravega.test.common.ThreadPooledTestSuite)10 IOException (java.io.IOException)10 ByteBuffer (java.nio.ByteBuffer)10 Duration (java.time.Duration)10 CompletionException (java.util.concurrent.CompletionException)10 TimeUnit (java.util.concurrent.TimeUnit)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10