use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testRead.
@Test(timeout = 10000)
public void testRead() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testRead", 1);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
ClientConnection c = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, c);
WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.EMPTY_BUFFER, in.getRequestId());
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(0, dataAvailable.availablePermits());
AssertExtensions.assertBlocks(() -> readFuture.get(), () -> {
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.process(segmentRead);
});
assertEquals(1, dataAvailable.availablePermits());
verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
assertTrue(Futures.isSuccessful(readFuture));
assertEquals(segmentRead, readFuture.join());
verifyNoMoreInteractions(c);
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testWrongOffsetReturned.
@Test(timeout = 10000)
public void testWrongOffsetReturned() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testWrongOffsetReturned", 0);
byte[] good = new byte[] { 0, 1, 2, 3, 4 };
byte[] bad = new byte[] { 9, 8, 7, 6 };
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
ClientConnection c = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, c);
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(0, dataAvailable.availablePermits());
AssertExtensions.assertBlocks(() -> readFuture.get(), () -> {
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.process(new WireCommands.SegmentRead(segment.getScopedName(), 1235, false, false, Unpooled.wrappedBuffer(bad), in.getRequestId()));
processor.process(new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.wrappedBuffer(good), in.getRequestId()));
});
assertEquals(2, dataAvailable.availablePermits());
verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
assertTrue(Futures.isSuccessful(readFuture));
assertEquals(Unpooled.wrappedBuffer(good), readFuture.join().getData());
verifyNoMoreInteractions(c);
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testSegmentTruncated.
@Test(timeout = 10000)
public void testSegmentTruncated() throws ConnectionFailedException {
String mockClientReplyStackTrace = "SomeException";
Segment segment = new Segment("scope", "testRead", 1);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, DelegationTokenProviderFactory.createWithEmptyToken(), dataAvailable);
ClientConnection c = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, c);
// segment truncated response from Segment store.
WireCommands.SegmentIsTruncated segmentIsTruncated = new WireCommands.SegmentIsTruncated(in.getRequestId(), segment.getScopedName(), 1234, mockClientReplyStackTrace, 1234L);
// Trigger read.
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(0, dataAvailable.availablePermits());
// verify that a response from Segment store completes the readFuture and the future completes with SegmentTruncatedException.
AssertExtensions.assertBlocks(() -> assertThrows(SegmentTruncatedException.class, () -> readFuture.get()), () -> {
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.process(segmentIsTruncated);
});
verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
// verify read future completedExceptionally
assertTrue(!Futures.isSuccessful(readFuture));
assertThrows(SegmentTruncatedException.class, () -> readFuture.get());
verifyNoMoreInteractions(c);
// Ensure that reads at a different offset can still happen on the same instance.
WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 5656, false, false, Unpooled.EMPTY_BUFFER, in.getRequestId());
CompletableFuture<SegmentRead> readFuture2 = in.read(5656, 5678);
AssertExtensions.assertBlocks(() -> readFuture2.get(), () -> {
ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
processor.segmentRead(segmentRead);
});
verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 5656, 5678, "", in.getRequestId())));
assertTrue(Futures.isSuccessful(readFuture2));
assertEquals(segmentRead, readFuture2.join());
verifyNoMoreInteractions(c);
assertEquals(1, dataAvailable.availablePermits());
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testRetry.
@Test(timeout = 10000)
public void testRetry() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
DelegationTokenProvider tokenProvider = mock(DelegationTokenProvider.class);
// return empty token
when(tokenProvider.retrieveToken()).thenReturn(CompletableFuture.completedFuture(""));
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, tokenProvider, dataAvailable);
ClientConnection c = mock(ClientConnection.class);
InOrder inOrder = Mockito.inOrder(c);
connectionFactory.provideConnection(endpoint, c);
WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.EMPTY_BUFFER, in.getRequestId());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
connectionFactory.getProcessor(endpoint).connectionDropped();
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
connectionFactory.getProcessor(endpoint).authTokenCheckFailed(new WireCommands.AuthTokenCheckFailed(in.getRequestId(), "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED));
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
connectionFactory.getProcessor(endpoint).process(segmentRead);
return null;
}
}).when(c).send(any(ReadSegment.class));
assertEquals(0, dataAvailable.availablePermits());
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(segmentRead, readFuture.join());
assertTrue(Futures.isSuccessful(readFuture));
assertEquals(1, dataAvailable.availablePermits());
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
inOrder.verify(c).close();
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
inOrder.verify(c).close();
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
verifyNoMoreInteractions(c);
// ensure retrieve Token is invoked for every retry.
verify(tokenProvider, times(3)).retrieveToken();
}
use of io.pravega.shared.protocol.netty.WireCommands.SegmentRead in project pravega by pravega.
the class PravegaRequestProcessor method handleException.
private Void handleException(long requestId, String segment, long offset, String operation, Throwable u) {
if (u == null) {
IllegalStateException exception = new IllegalStateException("No exception to handle.");
log.error(requestId, "Error (Segment = '{}', Operation = '{}')", segment, operation, exception);
throw exception;
}
u = Exceptions.unwrap(u);
String clientReplyStackTrace = replyWithStackTraceOnError ? Throwables.getStackTraceAsString(u) : EMPTY_STACK_TRACE;
final Consumer<Throwable> failureHandler = t -> {
log.error(requestId, "Error (Segment = '{}', Operation = '{}')", segment, "handling result of " + operation, t);
connection.close();
};
if (u instanceof StreamSegmentExistsException) {
log.info(requestId, "Segment '{}' already exists and cannot perform operation '{}'.", segment, operation);
invokeSafely(connection::send, new SegmentAlreadyExists(requestId, segment, clientReplyStackTrace), failureHandler);
} else if (u instanceof StreamSegmentNotExistsException) {
log.warn(requestId, "Segment '{}' does not exist and cannot perform operation '{}'.", segment, operation);
invokeSafely(connection::send, new NoSuchSegment(requestId, segment, clientReplyStackTrace, offset), failureHandler);
} else if (u instanceof StreamSegmentSealedException) {
log.info(requestId, "Segment '{}' is sealed and cannot perform operation '{}'.", segment, operation);
invokeSafely(connection::send, new SegmentIsSealed(requestId, segment, clientReplyStackTrace, offset), failureHandler);
} else if (u instanceof ContainerNotFoundException) {
int containerId = ((ContainerNotFoundException) u).getContainerId();
log.warn(requestId, "Wrong host. Segment = '{}' (Container {}) is not owned. Operation = '{}').", segment, containerId, operation);
invokeSafely(connection::send, new WrongHost(requestId, segment, "", clientReplyStackTrace), failureHandler);
} else if (u instanceof ReadCancellationException) {
log.info(requestId, "Sending empty response on connection {} while reading segment {} due to CancellationException.", connection, segment);
invokeSafely(connection::send, new SegmentRead(segment, offset, true, false, EMPTY_BUFFER, requestId), failureHandler);
} else if (u instanceof CancellationException) {
log.info(requestId, "Closing connection {} while performing {} due to {}.", connection, operation, u.toString());
connection.close();
} else if (u instanceof TokenExpiredException) {
log.warn(requestId, "Expired token during operation {}", operation);
invokeSafely(connection::send, new AuthTokenCheckFailed(requestId, clientReplyStackTrace, AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED), failureHandler);
} else if (u instanceof TokenException) {
log.warn(requestId, "Token exception encountered during operation {}.", operation, u);
invokeSafely(connection::send, new AuthTokenCheckFailed(requestId, clientReplyStackTrace, AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED), failureHandler);
} else if (u instanceof UnsupportedOperationException) {
log.warn(requestId, "Unsupported Operation '{}'.", operation, u);
invokeSafely(connection::send, new OperationUnsupported(requestId, operation, clientReplyStackTrace), failureHandler);
} else if (u instanceof BadOffsetException) {
BadOffsetException badOffset = (BadOffsetException) u;
log.info(requestId, "Segment '{}' is truncated and cannot perform operation '{}' at offset '{}'", segment, operation, offset);
invokeSafely(connection::send, new SegmentIsTruncated(requestId, segment, badOffset.getExpectedOffset(), clientReplyStackTrace, offset), failureHandler);
} else if (u instanceof TableSegmentNotEmptyException) {
log.warn(requestId, "Table segment '{}' is not empty to perform '{}'.", segment, operation);
invokeSafely(connection::send, new TableSegmentNotEmpty(requestId, segment, clientReplyStackTrace), failureHandler);
} else if (u instanceof KeyNotExistsException) {
log.warn(requestId, "Conditional update on Table segment '{}' failed as the key does not exist.", segment);
invokeSafely(connection::send, new WireCommands.TableKeyDoesNotExist(requestId, segment, clientReplyStackTrace), failureHandler);
} else if (u instanceof BadKeyVersionException) {
log.warn(requestId, "Conditional update on Table segment '{}' failed due to bad key version.", segment);
invokeSafely(connection::send, new WireCommands.TableKeyBadVersion(requestId, segment, clientReplyStackTrace), failureHandler);
} else if (errorCodeExists(u)) {
log.warn(requestId, "Operation on segment '{}' failed due to a {}.", segment, u.getClass());
invokeSafely(connection::send, new WireCommands.ErrorMessage(requestId, segment, u.getMessage(), WireCommands.ErrorMessage.ErrorCode.valueOf(u.getClass())), failureHandler);
} else {
logError(requestId, segment, operation, u);
// Closing connection should reinitialize things, and hopefully fix the problem
connection.close();
throw new IllegalStateException("Unknown exception.", u);
}
return null;
}
Aggregations