use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class PravegaRequestProcessorTest method testReadTableEntriesDeltaEmpty.
@Test
public void testReadTableEntriesDeltaEmpty() throws Exception {
// Set up PravegaRequestProcessor instance to execute requests against
String tableSegmentName = "testReadTableEntriesDelta";
@Cleanup ServiceBuilder serviceBuilder = newInlineExecutionInMemoryBuilder(getBuilderConfig());
serviceBuilder.initialize();
StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
TableStore tableStore = serviceBuilder.createTableStoreService();
ServerConnection connection = mock(ServerConnection.class);
InOrder order = inOrder(connection);
val recorderMock = mock(TableSegmentStatsRecorder.class);
PravegaRequestProcessor processor = new PravegaRequestProcessor(store, tableStore, new TrackedConnection(connection), SegmentStatsRecorder.noOp(), recorderMock, new PassingTokenVerifier(), false);
processor.createTableSegment(new WireCommands.CreateTableSegment(1, tableSegmentName, false, 0, "", 0));
order.verify(connection).send(new WireCommands.SegmentCreated(1, tableSegmentName));
verify(recorderMock).createTableSegment(eq(tableSegmentName), any());
processor.readTableEntriesDelta(new WireCommands.ReadTableEntriesDelta(1, tableSegmentName, "", 0, 3));
ArgumentCaptor<WireCommand> wireCommandsCaptor = ArgumentCaptor.forClass(WireCommand.class);
verify(recorderMock).iterateEntries(eq(tableSegmentName), eq(0), any());
}
use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class PravegaRequestProcessorTest method testGetTableEntries.
@Test
public void testGetTableEntries() throws Exception {
// Set up PravegaRequestProcessor instance to execute requests against
val rnd = new Random(0);
String tableSegmentName = "testGetTableEntries";
@Cleanup ServiceBuilder serviceBuilder = newInlineExecutionInMemoryBuilder(getBuilderConfig());
serviceBuilder.initialize();
StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
TableStore tableStore = serviceBuilder.createTableStoreService();
ServerConnection connection = mock(ServerConnection.class);
InOrder order = inOrder(connection);
val recorderMock = mock(TableSegmentStatsRecorder.class);
PravegaRequestProcessor processor = new PravegaRequestProcessor(store, tableStore, new TrackedConnection(connection), SegmentStatsRecorder.noOp(), recorderMock, new PassingTokenVerifier(), false);
// Generate keys.
ArrayList<ArrayView> keys = generateKeys(3, rnd);
ArrayView testValue = generateValue(rnd);
TableEntry e1 = TableEntry.unversioned(keys.get(0), testValue);
TableEntry e2 = TableEntry.unversioned(keys.get(1), testValue);
TableEntry e3 = TableEntry.unversioned(keys.get(2), testValue);
// Create a table segment and add data.
processor.createTableSegment(new WireCommands.CreateTableSegment(1, tableSegmentName, false, 0, "", 0));
order.verify(connection).send(new WireCommands.SegmentCreated(1, tableSegmentName));
verify(recorderMock).createTableSegment(eq(tableSegmentName), any());
processor.updateTableEntries(new WireCommands.UpdateTableEntries(2, tableSegmentName, "", getTableEntries(asList(e1, e2, e3)), WireCommands.NULL_TABLE_SEGMENT_OFFSET));
verify(recorderMock).updateEntries(eq(tableSegmentName), eq(3), eq(false), any());
// 1. Now read the table entries where suggestedEntryCount is equal to number of entries in the Table Store.
WireCommands.TableIteratorArgs args = new WireCommands.TableIteratorArgs(Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
processor.readTableEntries(new WireCommands.ReadTableEntries(3, tableSegmentName, "", 3, args));
// Capture the WireCommands sent.
ArgumentCaptor<WireCommand> wireCommandsCaptor = ArgumentCaptor.forClass(WireCommand.class);
order.verify(connection, times(2)).send(wireCommandsCaptor.capture());
verify(recorderMock).iterateEntries(eq(tableSegmentName), eq(3), any());
// Verify the WireCommands.
List<Long> keyVersions = ((WireCommands.TableEntriesUpdated) wireCommandsCaptor.getAllValues().get(0)).getUpdatedVersions();
WireCommands.TableEntriesRead getTableEntriesIteratorsResp = (WireCommands.TableEntriesRead) wireCommandsCaptor.getAllValues().get(1);
assertTrue(getTableEntriesIteratorsResp.getEntries().getEntries().stream().map(e -> e.getKey().getKeyVersion()).collect(Collectors.toList()).containsAll(keyVersions));
// Verify if the value is correct.
assertTrue(getTableEntriesIteratorsResp.getEntries().getEntries().stream().allMatch(e -> {
ByteBuf buf = e.getValue().getData();
byte[] bytes = new byte[buf.readableBytes()];
buf.getBytes(buf.readerIndex(), bytes);
return testValue.equals(new ByteArraySegment(bytes));
}));
// 2. Now read the table keys where suggestedEntryCount is less than the number of entries in the Table Store.
processor.readTableEntries(new WireCommands.ReadTableEntries(3, tableSegmentName, "", 1, args));
// Capture the WireCommands sent.
ArgumentCaptor<WireCommands.TableEntriesRead> tableEntriesCaptor = ArgumentCaptor.forClass(WireCommands.TableEntriesRead.class);
order.verify(connection, times(1)).send(tableEntriesCaptor.capture());
// Verify the WireCommands.
getTableEntriesIteratorsResp = tableEntriesCaptor.getAllValues().get(0);
assertEquals(1, getTableEntriesIteratorsResp.getEntries().getEntries().size());
assertTrue(keyVersions.contains(getTableEntriesIteratorsResp.getEntries().getEntries().get(0).getKey().getKeyVersion()));
// Get the last state.
ByteBuf state = getTableEntriesIteratorsResp.getContinuationToken();
args = new WireCommands.TableIteratorArgs(state, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
// 3. Now read the remaining table entries by providing a higher suggestedKeyCount and the state to the iterator.
processor.readTableEntries(new WireCommands.ReadTableEntries(3, tableSegmentName, "", 3, args));
// Capture the WireCommands sent.
tableEntriesCaptor = ArgumentCaptor.forClass(WireCommands.TableEntriesRead.class);
order.verify(connection, times(1)).send(tableEntriesCaptor.capture());
verify(recorderMock).iterateEntries(eq(tableSegmentName), eq(1), any());
// Verify the WireCommands.
getTableEntriesIteratorsResp = tableEntriesCaptor.getAllValues().get(0);
assertEquals(2, getTableEntriesIteratorsResp.getEntries().getEntries().size());
assertTrue(keyVersions.containsAll(getTableEntriesIteratorsResp.getEntries().getEntries().stream().map(e -> e.getKey().getKeyVersion()).collect(Collectors.toList())));
}
use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class AppendProcessorTest method sendRequest.
private Reply sendRequest(EmbeddedChannel channel, Request request) throws Exception {
channel.writeInbound(request);
Object encodedReply = channel.readOutbound();
for (int i = 0; encodedReply == null && i < 50; i++) {
channel.runPendingTasks();
Thread.sleep(10);
encodedReply = channel.readOutbound();
}
if (encodedReply == null) {
return null;
}
WireCommand decoded = CommandDecoder.parseCommand((ByteBuf) encodedReply);
((ByteBuf) encodedReply).release();
assertNotNull(decoded);
return (Reply) decoded;
}
use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class PravegaRequestProcessorTest method testGetTableKeys.
@Test
public void testGetTableKeys() throws Exception {
// Set up PravegaRequestProcessor instance to execute requests against
val rnd = new Random(0);
String tableSegmentName = "testGetTableKeys";
@Cleanup ServiceBuilder serviceBuilder = newInlineExecutionInMemoryBuilder(getBuilderConfig());
serviceBuilder.initialize();
StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
TableStore tableStore = serviceBuilder.createTableStoreService();
ServerConnection connection = mock(ServerConnection.class);
InOrder order = inOrder(connection);
val recorderMock = mock(TableSegmentStatsRecorder.class);
PravegaRequestProcessor processor = new PravegaRequestProcessor(store, tableStore, new TrackedConnection(connection), SegmentStatsRecorder.noOp(), recorderMock, new PassingTokenVerifier(), false);
// Generate keys.
ArrayList<ArrayView> keys = generateKeys(3, rnd);
TableEntry e1 = TableEntry.unversioned(keys.get(0), generateValue(rnd));
TableEntry e2 = TableEntry.unversioned(keys.get(1), generateValue(rnd));
TableEntry e3 = TableEntry.unversioned(keys.get(2), generateValue(rnd));
// Create a table segment and add data.
processor.createTableSegment(new WireCommands.CreateTableSegment(1, tableSegmentName, false, 0, "", 0));
order.verify(connection).send(new WireCommands.SegmentCreated(1, tableSegmentName));
verify(recorderMock).createTableSegment(eq(tableSegmentName), any());
processor.updateTableEntries(new WireCommands.UpdateTableEntries(2, tableSegmentName, "", getTableEntries(asList(e1, e2, e3)), WireCommands.NULL_TABLE_SEGMENT_OFFSET));
verify(recorderMock).updateEntries(eq(tableSegmentName), eq(3), eq(false), any());
// 1. Now read the table keys where suggestedKeyCount is equal to number of entries in the Table Store.
WireCommands.TableIteratorArgs args = new WireCommands.TableIteratorArgs(Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
processor.readTableKeys(new WireCommands.ReadTableKeys(3, tableSegmentName, "", 3, args));
// Capture the WireCommands sent.
ArgumentCaptor<WireCommand> wireCommandsCaptor = ArgumentCaptor.forClass(WireCommand.class);
order.verify(connection, times(2)).send(wireCommandsCaptor.capture());
verify(recorderMock).iterateKeys(eq(tableSegmentName), eq(3), any());
// Verify the WireCommands.
List<Long> keyVersions = ((WireCommands.TableEntriesUpdated) wireCommandsCaptor.getAllValues().get(0)).getUpdatedVersions();
WireCommands.TableKeysRead getTableKeysReadResponse = (WireCommands.TableKeysRead) wireCommandsCaptor.getAllValues().get(1);
assertTrue(getTableKeysReadResponse.getKeys().stream().map(WireCommands.TableKey::getKeyVersion).collect(Collectors.toList()).containsAll(keyVersions));
// 2. Now read the table keys where suggestedKeyCount is less than the number of keys in the Table Store.
processor.readTableKeys(new WireCommands.ReadTableKeys(3, tableSegmentName, "", 1, args));
// Capture the WireCommands sent.
ArgumentCaptor<WireCommands.TableKeysRead> tableKeysCaptor = ArgumentCaptor.forClass(WireCommands.TableKeysRead.class);
order.verify(connection, times(1)).send(tableKeysCaptor.capture());
verify(recorderMock).iterateKeys(eq(tableSegmentName), eq(1), any());
// Verify the WireCommands.
getTableKeysReadResponse = tableKeysCaptor.getAllValues().get(0);
assertEquals(1, getTableKeysReadResponse.getKeys().size());
assertTrue(keyVersions.contains(getTableKeysReadResponse.getKeys().get(0).getKeyVersion()));
// Get the last state.
ByteBuf state = getTableKeysReadResponse.getContinuationToken();
args = new WireCommands.TableIteratorArgs(state, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
// 3. Now read the remaining table keys by providing a higher suggestedKeyCount and the state to the iterator.
processor.readTableKeys(new WireCommands.ReadTableKeys(3, tableSegmentName, "", 3, args));
// Capture the WireCommands sent.
tableKeysCaptor = ArgumentCaptor.forClass(WireCommands.TableKeysRead.class);
order.verify(connection, times(1)).send(tableKeysCaptor.capture());
verify(recorderMock).iterateKeys(eq(tableSegmentName), eq(1), any());
// Verify the WireCommands.
getTableKeysReadResponse = tableKeysCaptor.getAllValues().get(0);
assertEquals(2, getTableKeysReadResponse.getKeys().size());
assertTrue(keyVersions.containsAll(getTableKeysReadResponse.getKeys().stream().map(WireCommands.TableKey::getKeyVersion).collect(Collectors.toList())));
}
use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class SegmentHelper method sendRequestAsync.
private <ResultT> void sendRequestAsync(final WireCommand request, final ReplyProcessor replyProcessor, final CompletableFuture<ResultT> resultFuture, final ConnectionFactory connectionFactory, final PravegaNodeUri uri) {
CompletableFuture<ClientConnection> connectionFuture = connectionFactory.establishConnection(uri, replyProcessor);
connectionFuture.whenComplete((connection, e) -> {
if (connection == null) {
resultFuture.completeExceptionally(new WireCommandFailedException(new ConnectionFailedException(e), request.getType(), WireCommandFailedException.Reason.ConnectionFailed));
} else {
try {
connection.send(request);
} catch (ConnectionFailedException cfe) {
throw new WireCommandFailedException(cfe, request.getType(), WireCommandFailedException.Reason.ConnectionFailed);
} catch (Exception e2) {
throw new RuntimeException(e2);
}
}
}).exceptionally(e -> {
Throwable cause = Exceptions.unwrap(e);
if (cause instanceof WireCommandFailedException) {
resultFuture.completeExceptionally(cause);
} else if (cause instanceof ConnectionFailedException) {
resultFuture.completeExceptionally(new WireCommandFailedException(cause, request.getType(), WireCommandFailedException.Reason.ConnectionFailed));
} else {
resultFuture.completeExceptionally(new RuntimeException(cause));
}
return null;
});
resultFuture.whenComplete((result, e) -> {
connectionFuture.thenAccept(ClientConnection::close);
});
}
Aggregations