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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations