Search in sources :

Example 1 with ContainerNotFoundException

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

the class StreamSegmentService method invoke.

// endregion
// region Helpers
/**
 * Executes the given Function on the SegmentContainer that the given Segment maps to.
 *
 * @param streamSegmentName The name of the StreamSegment to fetch the Container for.
 * @param toInvoke          A Function that will be invoked on the Container.
 * @param methodName        The name of the calling method (for logging purposes).
 * @param logArgs           (Optional) A vararg array of items to be logged.
 * @param <T>               Resulting type.
 * @return Either the result of toInvoke or a CompletableFuture completed exceptionally with a ContainerNotFoundException
 * in case the SegmentContainer that the Segment maps to does not exist in this StreamSegmentService.
 */
private <T> CompletableFuture<T> invoke(String streamSegmentName, Function<SegmentContainer, CompletableFuture<T>> toInvoke, String methodName, Object... logArgs) {
    long traceId = LoggerHelpers.traceEnter(log, methodName, logArgs);
    SegmentContainer container;
    try {
        int containerId = this.segmentToContainerMapper.getContainerId(streamSegmentName);
        container = this.segmentContainerRegistry.getContainer(containerId);
    } catch (ContainerNotFoundException ex) {
        return Futures.failedFuture(ex);
    }
    CompletableFuture<T> resultFuture = toInvoke.apply(container);
    if (log.isTraceEnabled()) {
        resultFuture.thenAccept(r -> LoggerHelpers.traceLeave(log, methodName, traceId, r));
    }
    return resultFuture;
}
Also used : SegmentContainer(io.pravega.segmentstore.server.SegmentContainer) ContainerNotFoundException(io.pravega.segmentstore.contracts.ContainerNotFoundException)

Example 2 with ContainerNotFoundException

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

the class AppendProcessor method handleException.

private Void handleException(UUID writerId, long requestId, String segment, long eventNumber, String doingWhat, Throwable u) {
    if (u == null) {
        IllegalStateException exception = new IllegalStateException("No exception to handle.");
        log.error(requestId, "Append processor: Error {} on segment = '{}'", doingWhat, segment, exception);
        throw exception;
    }
    u = Exceptions.unwrap(u);
    String clientReplyStackTrace = replyWithStackTraceOnError ? Throwables.getStackTraceAsString(u) : EMPTY_STACK_TRACE;
    if (u instanceof StreamSegmentExistsException) {
        log.warn(requestId, "Segment '{}' already exists and {} cannot perform operation '{}'.", segment, writerId, doingWhat);
        connection.send(new SegmentAlreadyExists(requestId, segment, clientReplyStackTrace));
    } else if (u instanceof StreamSegmentNotExistsException) {
        log.warn(requestId, "Segment '{}' does not exist and {} cannot perform operation '{}'.", segment, writerId, doingWhat);
        connection.send(new NoSuchSegment(requestId, segment, clientReplyStackTrace, -1L));
    } else if (u instanceof StreamSegmentSealedException) {
        log.info("Segment '{}' is sealed and {} cannot perform operation '{}'.", segment, writerId, doingWhat);
        connection.send(new SegmentIsSealed(requestId, segment, clientReplyStackTrace, eventNumber));
    } else if (u instanceof ContainerNotFoundException) {
        int containerId = ((ContainerNotFoundException) u).getContainerId();
        log.warn(requestId, "Wrong host. Segment '{}' (Container {}) is not owned and {} cannot perform operation '{}'.", segment, containerId, writerId, doingWhat);
        connection.send(new WrongHost(requestId, segment, "", clientReplyStackTrace));
    } else if (u instanceof BadAttributeUpdateException) {
        log.warn(requestId, "Bad attribute update by {} on segment {}.", writerId, segment, u);
        connection.send(new InvalidEventNumber(writerId, requestId, clientReplyStackTrace));
        close();
    } else if (u instanceof TokenExpiredException) {
        log.warn(requestId, "Token expired for writer {} on segment {}.", writerId, segment, u);
        close();
    } else if (u instanceof TokenException) {
        log.warn(requestId, "Token check failed or writer {} on segment {}.", writerId, segment, u);
        connection.send(new WireCommands.AuthTokenCheckFailed(requestId, clientReplyStackTrace, WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED));
    } else if (u instanceof UnsupportedOperationException) {
        log.warn(requestId, "Unsupported Operation '{}'.", doingWhat, u);
        connection.send(new OperationUnsupported(requestId, doingWhat, clientReplyStackTrace));
    } else if (u instanceof CancellationException) {
        // Cancellation exception is thrown when the Operation processor is shutting down.
        log.info("Closing connection '{}' while performing append on Segment '{}' due to {}.", connection, segment, u.toString());
        close();
    } else {
        logError(segment, u);
        // Closing connection should reinitialize things, and hopefully fix the problem
        close();
    }
    return null;
}
Also used : OperationUnsupported(io.pravega.shared.protocol.netty.WireCommands.OperationUnsupported) WrongHost(io.pravega.shared.protocol.netty.WireCommands.WrongHost) StreamSegmentNotExistsException(io.pravega.segmentstore.contracts.StreamSegmentNotExistsException) StreamSegmentExistsException(io.pravega.segmentstore.contracts.StreamSegmentExistsException) SegmentAlreadyExists(io.pravega.shared.protocol.netty.WireCommands.SegmentAlreadyExists) TokenExpiredException(io.pravega.auth.TokenExpiredException) StreamSegmentSealedException(io.pravega.segmentstore.contracts.StreamSegmentSealedException) SegmentIsSealed(io.pravega.shared.protocol.netty.WireCommands.SegmentIsSealed) BadAttributeUpdateException(io.pravega.segmentstore.contracts.BadAttributeUpdateException) InvalidEventNumber(io.pravega.shared.protocol.netty.WireCommands.InvalidEventNumber) CancellationException(java.util.concurrent.CancellationException) TokenException(io.pravega.auth.TokenException) NoSuchSegment(io.pravega.shared.protocol.netty.WireCommands.NoSuchSegment) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ContainerNotFoundException(io.pravega.segmentstore.contracts.ContainerNotFoundException)

Example 3 with ContainerNotFoundException

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

the class StreamSegmentContainerRegistryTests method testStopContainer.

/**
 * Tests the ability to stop the container via the stopContainer() method.
 */
@Test
public void testStopContainer() throws Exception {
    final int containerId = 123;
    TestContainerFactory factory = new TestContainerFactory();
    @Cleanup StreamSegmentContainerRegistry registry = new StreamSegmentContainerRegistry(factory, executorService());
    ContainerHandle handle = registry.startContainer(containerId, TIMEOUT).join();
    // Register a Listener for the Container.Stop event. Make this a Future since these callbacks are invoked async
    // so they may finish executing after stop() finished.
    CompletableFuture<Integer> stopListenerCallback = new CompletableFuture<>();
    handle.setContainerStoppedListener(stopListenerCallback::complete);
    TestContainer container = (TestContainer) registry.getContainer(handle.getContainerId());
    Assert.assertFalse("Container is closed before being shut down.", container.isClosed());
    registry.stopContainer(handle, TIMEOUT).join();
    Assert.assertEquals("Unexpected value passed to Handle.stopListenerCallback or callback was not invoked.", containerId, (int) stopListenerCallback.join());
    Assert.assertTrue("Container is not closed after being shut down.", container.isClosed());
    AssertExtensions.assertThrows("Container is still registered after being shut down.", () -> registry.getContainer(handle.getContainerId()), ex -> ex instanceof ContainerNotFoundException);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Cleanup(lombok.Cleanup) ContainerNotFoundException(io.pravega.segmentstore.contracts.ContainerNotFoundException) ContainerHandle(io.pravega.segmentstore.server.ContainerHandle) Test(org.junit.Test)

Example 4 with ContainerNotFoundException

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

the class StreamSegmentContainerRegistryTests method testContainerFailureWhileRunning.

/**
 * Tests the ability to detect a container failure and unregister the container in case the container fails while running.
 */
@Test
public void testContainerFailureWhileRunning() throws Exception {
    final int containerId = 123;
    TestContainerFactory factory = new TestContainerFactory();
    @Cleanup StreamSegmentContainerRegistry registry = new StreamSegmentContainerRegistry(factory, executorService());
    ContainerHandle handle = registry.startContainer(containerId, TIMEOUT).join();
    // Register a Listener for the Container.Stop event. Make this a Future since these callbacks are invoked async
    // so they may finish executing after stop() finished.
    CompletableFuture<Integer> stopListenerCallback = new CompletableFuture<>();
    handle.setContainerStoppedListener(stopListenerCallback::complete);
    TestContainer container = (TestContainer) registry.getContainer(handle.getContainerId());
    // Fail the container and wait for it to properly terminate.
    container.fail(new IntentionalException());
    ServiceListeners.awaitShutdown(container, false);
    Assert.assertEquals("Unexpected value passed to Handle.stopListenerCallback or callback was not invoked.", containerId, (int) stopListenerCallback.join());
    AssertExtensions.assertThrows("Container is still registered after failure.", () -> registry.getContainer(containerId), ex -> ex instanceof ContainerNotFoundException);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Cleanup(lombok.Cleanup) ContainerNotFoundException(io.pravega.segmentstore.contracts.ContainerNotFoundException) IntentionalException(io.pravega.test.common.IntentionalException) ContainerHandle(io.pravega.segmentstore.server.ContainerHandle) Test(org.junit.Test)

Example 5 with ContainerNotFoundException

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

the class StreamSegmentContainerRegistryTests method testGetContainers.

/**
 * Tests the getContainers method for registered and unregistered containers.
 */
@Test
public void testGetContainers() throws Exception {
    final int containerCount = 1000;
    TestContainerFactory factory = new TestContainerFactory();
    @Cleanup StreamSegmentContainerRegistry registry = new StreamSegmentContainerRegistry(factory, executorService());
    HashSet<Integer> expectedContainerIds = new HashSet<>();
    for (int containerId = 0; containerId < containerCount; containerId++) {
        registry.startContainer(containerId, TIMEOUT);
        expectedContainerIds.add(containerId);
    }
    HashSet<Integer> actualHandleIds = new HashSet<>();
    for (SegmentContainer segmentContainer : registry.getContainers()) {
        actualHandleIds.add(segmentContainer.getId());
        Assert.assertTrue("Wrong container Java type.", segmentContainer instanceof TestContainer);
        segmentContainer.close();
    }
    AssertExtensions.assertContainsSameElements("Unexpected container ids registered.", expectedContainerIds, actualHandleIds);
    AssertExtensions.assertThrows("getContainer did not throw when passed an invalid container id.", () -> registry.getContainer(containerCount + 1), ex -> ex instanceof ContainerNotFoundException);
}
Also used : DebugSegmentContainer(io.pravega.segmentstore.server.DebugSegmentContainer) SegmentContainer(io.pravega.segmentstore.server.SegmentContainer) Cleanup(lombok.Cleanup) ContainerNotFoundException(io.pravega.segmentstore.contracts.ContainerNotFoundException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ContainerNotFoundException (io.pravega.segmentstore.contracts.ContainerNotFoundException)9 Cleanup (lombok.Cleanup)5 Test (org.junit.Test)5 SegmentContainer (io.pravega.segmentstore.server.SegmentContainer)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 TokenException (io.pravega.auth.TokenException)2 TokenExpiredException (io.pravega.auth.TokenExpiredException)2 BadAttributeUpdateException (io.pravega.segmentstore.contracts.BadAttributeUpdateException)2 ContainerHandle (io.pravega.segmentstore.server.ContainerHandle)2 ArrayList (java.util.ArrayList)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 Throwables (com.google.common.base.Throwables)1 Iterators (com.google.common.collect.Iterators)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 EMPTY_BUFFER (io.netty.buffer.Unpooled.EMPTY_BUFFER)1 READ (io.pravega.auth.AuthHandler.Permissions.READ)1 READ_UPDATE (io.pravega.auth.AuthHandler.Permissions.READ_UPDATE)1 Exceptions (io.pravega.common.Exceptions)1