Search in sources :

Example 36 with SegmentProperties

use of io.pravega.segmentstore.contracts.SegmentProperties in project pravega by pravega.

the class GetInfoOperation method call.

@Override
public SegmentProperties call() throws IOException {
    String segmentName = getTarget();
    long traceId = LoggerHelpers.traceEnter(log, "getStreamSegmentInfo", segmentName);
    List<FileDescriptor> allFiles = findAll(segmentName, true);
    SegmentProperties result = null;
    int attemptCount = 0;
    do {
        val last = allFiles.get(allFiles.size() - 1);
        long length = last.getOffset() + last.getLength();
        boolean isSealed;
        try {
            isSealed = isSealed(last);
        } catch (FileNotFoundException fnf) {
            // be deleted in that case so we need to try our luck again (in which case we need to refresh the file list).
            if (++attemptCount < MAX_ATTEMPT_COUNT) {
                allFiles = findAll(segmentName, true);
                continue;
            }
            throw fnf;
        }
        result = StreamSegmentInformation.builder().name(segmentName).length(length).sealed(isSealed).build();
    } while (result == null);
    LoggerHelpers.traceLeave(log, "getStreamSegmentInfo", traceId, segmentName, result);
    return result;
}
Also used : lombok.val(lombok.val) FileNotFoundException(java.io.FileNotFoundException) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties)

Example 37 with SegmentProperties

use of io.pravega.segmentstore.contracts.SegmentProperties in project pravega by pravega.

the class AppendProcessorTest method testEventNumbers.

@Test
public void testEventNumbers() {
    String streamSegmentName = "testAppendSegment";
    UUID clientId = UUID.randomUUID();
    byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 };
    StreamSegmentStore store = mock(StreamSegmentStore.class);
    ServerConnection connection = mock(ServerConnection.class);
    AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null);
    CompletableFuture<SegmentProperties> propsFuture = CompletableFuture.completedFuture(StreamSegmentInformation.builder().name(streamSegmentName).build());
    when(store.getStreamSegmentInfo(streamSegmentName, true, AppendProcessor.TIMEOUT)).thenReturn(propsFuture);
    processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, ""));
    verify(store).getStreamSegmentInfo(streamSegmentName, true, AppendProcessor.TIMEOUT);
    CompletableFuture<Void> result = CompletableFuture.completedFuture(null);
    int eventCount = 100;
    when(store.append(streamSegmentName, data, updateEventNumber(clientId, 100, SegmentMetadata.NULL_ATTRIBUTE_VALUE, eventCount), AppendProcessor.TIMEOUT)).thenReturn(result);
    processor.append(new Append(streamSegmentName, clientId, 100, eventCount, Unpooled.wrappedBuffer(data), null));
    verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 100, SegmentMetadata.NULL_ATTRIBUTE_VALUE, eventCount), AppendProcessor.TIMEOUT);
    Map<UUID, Long> map = new HashMap<>();
    map.put(clientId, 100L);
    map.put(EVENT_COUNT, 100L);
    propsFuture = CompletableFuture.completedFuture(StreamSegmentInformation.builder().name(streamSegmentName).attributes(map).build());
    when(store.append(streamSegmentName, data, updateEventNumber(clientId, 200, 100, eventCount), AppendProcessor.TIMEOUT)).thenReturn(result);
    processor.append(new Append(streamSegmentName, clientId, 200, eventCount, Unpooled.wrappedBuffer(data), null));
    verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 200, 100, eventCount), AppendProcessor.TIMEOUT);
    verifyNoMoreInteractions(store);
}
Also used : FailingRequestProcessor(io.pravega.shared.protocol.netty.FailingRequestProcessor) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) UUID(java.util.UUID) Test(org.junit.Test)

Example 38 with SegmentProperties

use of io.pravega.segmentstore.contracts.SegmentProperties in project pravega by pravega.

the class AppendProcessorTest method testEventNumbersOldClient.

@Test
public void testEventNumbersOldClient() {
    String streamSegmentName = "testAppendSegment";
    UUID clientId = UUID.randomUUID();
    byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 };
    StreamSegmentStore store = mock(StreamSegmentStore.class);
    ServerConnection connection = mock(ServerConnection.class);
    AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null);
    CompletableFuture<SegmentProperties> propsFuture = CompletableFuture.completedFuture(StreamSegmentInformation.builder().name(streamSegmentName).attributes(Collections.singletonMap(clientId, 100L)).build());
    when(store.getStreamSegmentInfo(streamSegmentName, true, AppendProcessor.TIMEOUT)).thenReturn(propsFuture);
    processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, ""));
    verify(store).getStreamSegmentInfo(streamSegmentName, true, AppendProcessor.TIMEOUT);
    int eventCount = 10;
    CompletableFuture<Void> result = CompletableFuture.completedFuture(null);
    when(store.append(streamSegmentName, data, updateEventNumber(clientId, 200, 100, eventCount), AppendProcessor.TIMEOUT)).thenReturn(result);
    processor.append(new Append(streamSegmentName, clientId, 200, eventCount, Unpooled.wrappedBuffer(data), null));
    verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 200, 100, eventCount), AppendProcessor.TIMEOUT);
    when(store.append(streamSegmentName, data, updateEventNumber(clientId, 300, 200, eventCount), AppendProcessor.TIMEOUT)).thenReturn(result);
    processor.append(new Append(streamSegmentName, clientId, 300, eventCount, Unpooled.wrappedBuffer(data), null));
    verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 300, 200, eventCount), AppendProcessor.TIMEOUT);
    verifyNoMoreInteractions(store);
}
Also used : FailingRequestProcessor(io.pravega.shared.protocol.netty.FailingRequestProcessor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) UUID(java.util.UUID) Test(org.junit.Test)

Example 39 with SegmentProperties

use of io.pravega.segmentstore.contracts.SegmentProperties in project pravega by pravega.

the class AppendProcessorTest method testDelayedDataAppended.

/**
 * Test to ensure newer appends are processed only after successfully sending the DataAppended acknowledgement
 * back to client. This test tests the following:
 * - If sending first DataAppended is blocked, ensure future appends are not written to store.
 * - Once the first DataAppended is sent ensure the remaining appends are written to store and DataAppended ack'ed
 * back.
 */
@Test(timeout = 15 * 1000)
public void testDelayedDataAppended() throws Exception {
    ReusableLatch firstStoreAppendInvoked = new ReusableLatch();
    ReusableLatch completeFirstDataAppendedAck = new ReusableLatch();
    ReusableLatch secondStoreAppendInvoked = new ReusableLatch();
    @Cleanup("shutdownNow") ScheduledExecutorService nettyExecutor = ExecutorServiceHelpers.newScheduledThreadPool(1, "Netty-threadPool");
    String streamSegmentName = "testDelayedAppend";
    UUID clientId = UUID.randomUUID();
    byte[] data = new byte[] { 1, 2, 3, 4, 6, 7, 8, 9 };
    StreamSegmentStore store = mock(StreamSegmentStore.class);
    ServerConnection connection = mock(ServerConnection.class);
    // Ensure the first DataAppended is hung/delayed.
    doAnswer(invocation -> {
        firstStoreAppendInvoked.release();
        // wait, simulating a hung/delayed dataAppended acknowledgement.
        completeFirstDataAppendedAck.await();
        return null;
    }).doAnswer(invocation -> {
        secondStoreAppendInvoked.release();
        return null;
    }).when(connection).send(any(DataAppended.class));
    AppendProcessor processor = new AppendProcessor(store, connection, new FailingRequestProcessor(), null);
    CompletableFuture<SegmentProperties> propsFuture = CompletableFuture.completedFuture(StreamSegmentInformation.builder().name(streamSegmentName).build());
    when(store.getStreamSegmentInfo(streamSegmentName, true, AppendProcessor.TIMEOUT)).thenReturn(propsFuture);
    processor.setupAppend(new SetupAppend(1, clientId, streamSegmentName, ""));
    verify(store).getStreamSegmentInfo(streamSegmentName, true, AppendProcessor.TIMEOUT);
    CompletableFuture<Void> result = CompletableFuture.completedFuture(null);
    int eventCount = 100;
    when(store.append(streamSegmentName, data, updateEventNumber(clientId, 100, SegmentMetadata.NULL_ATTRIBUTE_VALUE, eventCount), AppendProcessor.TIMEOUT)).thenReturn(result);
    // Trigger the first append, here the sending of DataAppended ack will be delayed/hung.
    nettyExecutor.submit(() -> processor.append(new Append(streamSegmentName, clientId, 100, eventCount, Unpooled.wrappedBuffer(data), null)));
    firstStoreAppendInvoked.await();
    verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 100, SegmentMetadata.NULL_ATTRIBUTE_VALUE, eventCount), AppendProcessor.TIMEOUT);
    /* Trigger the next append. This should be completed immediately and should not cause a store.append to be
        invoked as the previous DataAppended ack is still not sent. */
    processor.append(new Append(streamSegmentName, clientId, 200, eventCount, Unpooled.wrappedBuffer(data), null));
    // Since the first Ack was never sent the next append should not be written to the store.
    verifyNoMoreInteractions(store);
    // Setup mock for check behaviour after the delayed/hung dataAppended completes.
    when(store.append(streamSegmentName, data, updateEventNumber(clientId, 200, 100, eventCount), AppendProcessor.TIMEOUT)).thenReturn(result);
    // Now ensure the dataAppended sent
    completeFirstDataAppendedAck.release();
    // wait until the next store append is invoked.
    secondStoreAppendInvoked.await();
    // Verify that the next store append invoked.
    verify(store).append(streamSegmentName, data, updateEventNumber(clientId, 200, 100, eventCount), AppendProcessor.TIMEOUT);
    // Verify two DataAppended acks are sent out.
    verify(connection, times(2)).send(any(DataAppended.class));
    verify(connection).send(new DataAppended(clientId, 100, Long.MIN_VALUE));
    verify(connection).send(new DataAppended(clientId, 200, 100));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) StreamSegmentInformation(io.pravega.segmentstore.contracts.StreamSegmentInformation) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ConditionalCheckFailed(io.pravega.shared.protocol.netty.WireCommands.ConditionalCheckFailed) Cleanup(lombok.Cleanup) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Append(io.pravega.shared.protocol.netty.Append) Unpooled(io.netty.buffer.Unpooled) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) AttributeUpdate(io.pravega.segmentstore.contracts.AttributeUpdate) SegmentMetadata(io.pravega.segmentstore.server.SegmentMetadata) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) FailingRequestProcessor(io.pravega.shared.protocol.netty.FailingRequestProcessor) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Mockito.atLeast(org.mockito.Mockito.atLeast) Assert.fail(org.junit.Assert.fail) ReusableLatch(io.pravega.common.util.ReusableLatch) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) InOrder(org.mockito.InOrder) EVENT_COUNT(io.pravega.segmentstore.contracts.Attributes.EVENT_COUNT) AppendSetup(io.pravega.shared.protocol.netty.WireCommands.AppendSetup) Collection(java.util.Collection) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Ignore(org.junit.Ignore) BadOffsetException(io.pravega.segmentstore.contracts.BadOffsetException) OperationUnsupported(io.pravega.shared.protocol.netty.WireCommands.OperationUnsupported) Mockito.atMost(org.mockito.Mockito.atMost) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) AttributeUpdateType(io.pravega.segmentstore.contracts.AttributeUpdateType) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) FailingRequestProcessor(io.pravega.shared.protocol.netty.FailingRequestProcessor) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Cleanup(lombok.Cleanup) StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ReusableLatch(io.pravega.common.util.ReusableLatch) Append(io.pravega.shared.protocol.netty.Append) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) DataAppended(io.pravega.shared.protocol.netty.WireCommands.DataAppended) SetupAppend(io.pravega.shared.protocol.netty.WireCommands.SetupAppend) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) UUID(java.util.UUID) Test(org.junit.Test)

Example 40 with SegmentProperties

use of io.pravega.segmentstore.contracts.SegmentProperties in project pravega by pravega.

the class SegmentStatsRecorderTest method setup.

@Before
public void setup() {
    AutoScaleProcessor processor = mock(AutoScaleProcessor.class);
    StreamSegmentStore store = mock(StreamSegmentStore.class);
    CompletableFuture<SegmentProperties> toBeReturned = CompletableFuture.completedFuture(new SegmentProperties() {

        @Override
        public String getName() {
            return STREAM_SEGMENT_NAME;
        }

        @Override
        public boolean isSealed() {
            return false;
        }

        @Override
        public boolean isDeleted() {
            return false;
        }

        @Override
        public ImmutableDate getLastModified() {
            return null;
        }

        @Override
        public long getStartOffset() {
            return 0;
        }

        @Override
        public long getLength() {
            return 0;
        }

        @Override
        public Map<UUID, Long> getAttributes() {
            Map<UUID, Long> map = new HashMap<>();
            map.put(Attributes.SCALE_POLICY_TYPE, 0L);
            map.put(Attributes.SCALE_POLICY_RATE, 10L);
            latch.complete(null);
            return map;
        }
    });
    when(store.getStreamSegmentInfo(STREAM_SEGMENT_NAME, false, Duration.ofMinutes(1))).thenReturn(toBeReturned);
    statsRecorder = new SegmentStatsRecorderImpl(processor, store, 10000, 2, TimeUnit.SECONDS, executor, maintenanceExecutor);
}
Also used : StreamSegmentStore(io.pravega.segmentstore.contracts.StreamSegmentStore) ImmutableDate(io.pravega.common.util.ImmutableDate) SegmentProperties(io.pravega.segmentstore.contracts.SegmentProperties) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Aggregations

SegmentProperties (io.pravega.segmentstore.contracts.SegmentProperties)43 Test (org.junit.Test)24 Cleanup (lombok.Cleanup)22 AtomicLong (java.util.concurrent.atomic.AtomicLong)19 StreamSegmentNotExistsException (io.pravega.segmentstore.contracts.StreamSegmentNotExistsException)18 lombok.val (lombok.val)18 HashMap (java.util.HashMap)17 CompletableFuture (java.util.concurrent.CompletableFuture)17 ArrayList (java.util.ArrayList)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 SegmentMetadata (io.pravega.segmentstore.server.SegmentMetadata)13 BadOffsetException (io.pravega.segmentstore.contracts.BadOffsetException)11 UUID (java.util.UUID)11 Exceptions (io.pravega.common.Exceptions)10 Duration (java.time.Duration)10 Map (java.util.Map)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 StorageOperation (io.pravega.segmentstore.server.logs.operations.StorageOperation)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Futures (io.pravega.common.concurrent.Futures)8