use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class CommandEncoderTest method testExactBatch.
@Test
public void testExactBatch() throws IOException {
AppendBatchSizeTracker batchSizeTracker = new FixedBatchSizeTracker(100);
DecodingOutputStream output = new DecodingOutputStream();
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
CommandEncoder commandEncoder = new CommandEncoder(x -> batchSizeTracker, null, output, null, endpoint);
UUID writerId = UUID.randomUUID();
WireCommand setupAppend = new WireCommands.SetupAppend(0, writerId, "seg", "");
commandEncoder.write(setupAppend);
assertEquals(output.decoded.remove(0), setupAppend);
ByteBuf data = Unpooled.wrappedBuffer(new byte[100]);
Event event = new WireCommands.Event(data);
Append append = new Append("seg", writerId, 1, event, 0);
commandEncoder.write(append);
AppendBlock block = (AppendBlock) output.decoded.remove(0);
assertEquals(108, block.getData().readableBytes());
AppendBlockEnd blockEnd = (AppendBlockEnd) output.decoded.remove(0);
assertEquals(writerId, blockEnd.getWriterId());
assertEquals(108, blockEnd.getSizeOfWholeEvents());
assertEquals(0, blockEnd.getData().readableBytes());
assertEquals(1, blockEnd.getNumEvents());
}
use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class CommandEncoderTest method testOverBatchSize.
@Test
public void testOverBatchSize() throws IOException {
AppendBatchSizeTracker batchSizeTracker = new FixedBatchSizeTracker(100);
DecodingOutputStream output = new DecodingOutputStream();
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
CommandEncoder commandEncoder = new CommandEncoder(x -> batchSizeTracker, null, output, null, endpoint);
UUID writerId = UUID.randomUUID();
WireCommand setupAppend = new WireCommands.SetupAppend(0, writerId, "seg", "");
commandEncoder.write(setupAppend);
assertEquals(output.decoded.remove(0), setupAppend);
ByteBuf data = Unpooled.wrappedBuffer(new byte[200]);
Event event = new WireCommands.Event(data);
Append append = new Append("seg", writerId, 1, event, 0);
commandEncoder.write(append);
AppendBlock block = (AppendBlock) output.decoded.remove(0);
assertEquals(208, block.getData().readableBytes());
AppendBlockEnd blockEnd = (AppendBlockEnd) output.decoded.remove(0);
assertEquals(writerId, blockEnd.getWriterId());
assertEquals(208, blockEnd.getSizeOfWholeEvents());
assertEquals(0, blockEnd.getData().readableBytes());
assertEquals(1, blockEnd.getNumEvents());
}
use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class TableSegmentImplTest method testGetALotOfKeys.
/**
* Tests the {@link TableSegmentImpl#get} method when the response coming back from the server is truncated.
* Connection reset failures are not tested here; they're checked in {@link #testReconnect()}.
*/
@Test
public void testGetALotOfKeys() throws Exception {
// Manual calculation of TableSegmentImpl.MAX_GET_KEY_BATCH_SIZE.
val expectedBatchKeyLength = 15;
val requestKeys = LongStream.range(0, 61).mapToObj(l -> l * 100).collect(Collectors.toList());
val expectedEntries = requestKeys.stream().map(key -> versionedEntry(key, key.toString(), key)).collect(Collectors.toList());
// This key does not exist.
expectedEntries.set(expectedEntries.size() - 1, null);
val expectedWireKeys = toWireKeys(requestKeys.stream().map(this::buf).map(TableSegmentKey::unversioned).collect(Collectors.toList()));
// Calculate the expected sent/receive ranges.
// Key: Start Index, Value: End Index.
val expectedRanges = new ArrayList<Map.Entry<Integer, Integer>>();
int idx = 0;
while (idx < expectedEntries.size()) {
val expectedLength = Math.min(expectedBatchKeyLength, expectedEntries.size() - idx);
expectedRanges.add(new AbstractMap.SimpleImmutableEntry<>(idx, idx + expectedLength));
idx += expectedLength;
}
@Cleanup val context = new TestContext();
// Initiate a get request.
val getResult = context.segment.get(requestKeys.stream().map(this::buf).iterator());
// Capture all the requests sent over the wire and verify that we've sent as many as we expected to.
val sentWireCommands = context.getConnection().getLastSentWireCommands(expectedRanges.size());
Assert.assertEquals(expectedRanges.size(), sentWireCommands.size());
// Validate the sent keys and send appropriate responses.
for (int i = 0; i < expectedRanges.size(); i++) {
int rangeStart = expectedRanges.get(i).getKey();
int rangeEnd = expectedRanges.get(i).getValue();
val expectedSentKeys = expectedWireKeys.subList(rangeStart, rangeEnd);
val wireCommand = (WireCommands.ReadTable) sentWireCommands.get(i);
checkWireCommand(expectedSentKeys, wireCommand.getKeys());
val returnedKeys = requestKeys.subList(rangeStart, rangeEnd);
val returnedEntries = expectedEntries.subList(rangeStart, rangeEnd);
val replyWireEntries = toWireEntries(returnedEntries, returnedKeys);
context.sendReply(new WireCommands.TableRead(wireCommand.getRequestId(), SEGMENT.getScopedName(), replyWireEntries));
}
// Validate final result.
val actualEntries = getResult.get(SHORT_TIMEOUT, TimeUnit.MILLISECONDS);
AssertExtensions.assertListEquals("Unexpected return value", expectedEntries, actualEntries, this::entryEquals);
}
use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class SegmentHelper method removeTableKeys.
/**
* This method sends a WireCommand to remove table keys.
*
* @param tableName Qualified table name.
* @param keys List of {@link TableSegmentKey}s to be removed. Only if all the elements in the list has version
* as {@link TableSegmentKeyVersion#NO_VERSION} then an unconditional update/removal is performed.
* Else an atomic conditional update (removal) is performed.
* @param delegationToken The token to be presented to the Segment Store.
* @param clientRequestId Request id.
* @return A CompletableFuture that will complete normally when the provided keys are deleted.
* If the operation failed, the future will be failed with the causing exception. If the exception can be
* retried then the future will be failed with {@link WireCommandFailedException}.
*/
public CompletableFuture<Void> removeTableKeys(final String tableName, final List<TableSegmentKey> keys, String delegationToken, final long clientRequestId) {
final Controller.NodeUri uri = getTableUri(tableName);
final WireCommandType type = WireCommandType.REMOVE_TABLE_KEYS;
List<WireCommands.TableKey> keyList = keys.stream().map(x -> {
WireCommands.TableKey key = convertToWireCommand(x);
return key;
}).collect(Collectors.toList());
RawClient connection = new RawClient(ModelHelper.encode(uri), connectionPool);
final long requestId = connection.getFlow().asLong();
WireCommands.RemoveTableKeys request = new WireCommands.RemoveTableKeys(requestId, tableName, delegationToken, keyList, WireCommands.NULL_TABLE_SEGMENT_OFFSET);
return sendRequest(connection, clientRequestId, request).thenAccept(rpl -> handleReply(clientRequestId, rpl, connection, tableName, WireCommands.RemoveTableKeys.class, type));
}
use of io.pravega.shared.protocol.netty.WireCommand in project pravega by pravega.
the class AppendTest method sendRequest.
static Reply sendRequest(EmbeddedChannel channel, Request request) throws Exception {
channel.writeInbound(request);
log.info("Request {} sent to Segment store", request);
Object encodedReply = channel.readOutbound();
for (int i = 0; encodedReply == null && i < 500; i++) {
channel.runPendingTasks();
Thread.sleep(10);
encodedReply = channel.readOutbound();
}
if (encodedReply == null) {
log.error("Error while try waiting for a response from Segment Store");
throw new IllegalStateException("No reply to request: " + request);
}
WireCommand decoded = CommandDecoder.parseCommand((ByteBuf) encodedReply);
((ByteBuf) encodedReply).release();
assertNotNull(decoded);
return (Reply) decoded;
}
Aggregations