use of com.github.ambry.utils.NettyByteBufDataInputStream in project ambry by linkedin.
the class ServerAdminTool method getGetResponse.
/**
* Sends a {@link GetRequest} based on the provided parameters and returns the response stream if the request was
* successful. {@code null} otherwise.
* @param dataNodeId the {@link DataNodeId} to contact.
* @param blobId the {@link BlobId} to operate on.
* @param flags the {@link MessageFormatFlags} associated with the {@link GetRequest}.
* @param getOption the {@link GetOption} to send with the {@link GetRequest}.
* @param clusterMap the {@link ClusterMap} to use.
* @return the {@link ServerErrorCode} and response stream if the request was successful. {@code null} for the
* response stream otherwise.
* @throws Exception
*/
private Pair<ServerErrorCode, InputStream> getGetResponse(DataNodeId dataNodeId, BlobId blobId, MessageFormatFlags flags, GetOption getOption, ClusterMap clusterMap) throws Exception {
PartitionId partitionId = blobId.getPartition();
PartitionRequestInfo partitionRequestInfo = new PartitionRequestInfo(partitionId, Collections.singletonList(blobId));
List<PartitionRequestInfo> partitionRequestInfos = new ArrayList<>();
partitionRequestInfos.add(partitionRequestInfo);
GetRequest getRequest = new GetRequest(correlationId.incrementAndGet(), CLIENT_ID, flags, partitionRequestInfos, getOption);
ResponseInfo response = sendRequestGetResponse(dataNodeId, partitionId, getRequest);
InputStream serverResponseStream = new NettyByteBufDataInputStream(response.content());
response.release();
GetResponse getResponse = GetResponse.readFrom(new DataInputStream(serverResponseStream), clusterMap);
ServerErrorCode partitionErrorCode = getResponse.getPartitionResponseInfoList().get(0).getErrorCode();
ServerErrorCode errorCode = partitionErrorCode == ServerErrorCode.No_Error ? getResponse.getError() : partitionErrorCode;
InputStream stream = errorCode == ServerErrorCode.No_Error ? getResponse.getInputStream() : null;
return new Pair<>(errorCode, stream);
}
use of com.github.ambry.utils.NettyByteBufDataInputStream in project ambry by linkedin.
the class ServerAdminTool method triggerCompaction.
/**
* Triggers compaction on {@code dataNodeId} for the partition defined in {@code partitionIdStr}.
* @param dataNodeId the {@link DataNodeId} to contact.
* @param partitionId the {@link PartitionId} to compact.
* @return the {@link ServerErrorCode} that is returned.
* @throws IOException
* @throws TimeoutException
*/
public ServerErrorCode triggerCompaction(DataNodeId dataNodeId, PartitionId partitionId) throws IOException, TimeoutException {
AdminRequest adminRequest = new AdminRequest(AdminRequestOrResponseType.TriggerCompaction, partitionId, correlationId.incrementAndGet(), CLIENT_ID);
ResponseInfo response = sendRequestGetResponse(dataNodeId, partitionId, adminRequest);
AdminResponse adminResponse = AdminResponse.readFrom(new NettyByteBufDataInputStream(response.content()));
response.release();
return adminResponse.getError();
}
use of com.github.ambry.utils.NettyByteBufDataInputStream in project ambry by linkedin.
the class ServerAdminTool method isCaughtUp.
/**
* Sends a {@link CatchupStatusAdminRequest} for {@code partitionIdStr} to {@code dataNodeId}.
* @param dataNodeId the {@link DataNodeId} to contact.
* @param partitionId the {@link PartitionId} to check catchup status for. If {@code null}, status is for all
* partitions on {@code dataNodeId}
* @param acceptableLagInBytes that lag in bytes that is considered OK.
* @param numReplicasCaughtUpPerPartition the number of replicas that have to be within {@code acceptableLagInBytes}
* (per partition). The min of this value or the total count of replicas - 1 is
* considered.
* @return the {@link ServerErrorCode} and the catchup status that is returned if the error code is
* {@link ServerErrorCode#No_Error}, otherwise {@code false}.
* @throws IOException
* @throws TimeoutException
*/
public Pair<ServerErrorCode, Boolean> isCaughtUp(DataNodeId dataNodeId, PartitionId partitionId, long acceptableLagInBytes, short numReplicasCaughtUpPerPartition) throws IOException, TimeoutException {
AdminRequest adminRequest = new AdminRequest(AdminRequestOrResponseType.CatchupStatus, partitionId, correlationId.incrementAndGet(), CLIENT_ID);
CatchupStatusAdminRequest catchupStatusRequest = new CatchupStatusAdminRequest(acceptableLagInBytes, numReplicasCaughtUpPerPartition, adminRequest);
ResponseInfo response = sendRequestGetResponse(dataNodeId, partitionId, catchupStatusRequest);
CatchupStatusAdminResponse adminResponse = CatchupStatusAdminResponse.readFrom(new NettyByteBufDataInputStream(response.content()));
response.release();
return new Pair<>(adminResponse.getError(), adminResponse.getError() == ServerErrorCode.No_Error && adminResponse.isCaughtUp());
}
use of com.github.ambry.utils.NettyByteBufDataInputStream in project ambry by linkedin.
the class Http2BlockingChannel method sendAndReceive.
public ChannelOutput sendAndReceive(Send request) throws IOException {
Channel streamChannel;
try {
streamChannel = channelPool.acquire().get(http2ClientConfig.http2BlockingChannelAcquireTimeoutMs, TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new IOException("Can't acquire stream channel from " + getRemoteHost() + ":" + getRemotePort(), e);
}
CompletableFuture<ByteBuf> responsePromise = new CompletableFuture<ByteBuf>();
streamChannel.attr(RESPONSE_PROMISE).set(responsePromise);
streamChannel.attr(CHANNEL_POOL_ATTRIBUTE_KEY).set(channelPool);
boolean success = streamChannel.writeAndFlush(request).awaitUninterruptibly(http2ClientConfig.http2BlockingChannelSendTimeoutMs, TimeUnit.MILLISECONDS);
if (!success) {
if (streamChannel.attr(RESPONSE_PROMISE).getAndSet(null) != null) {
channelPool.release(streamChannel);
}
throw new IOException("Failed to write and flush request on time, from " + getRemoteHost() + ":" + getRemotePort());
}
ByteBuf responseByteBuf;
try {
responseByteBuf = responsePromise.get(http2ClientConfig.http2BlockingChannelReceiveTimeoutMs, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
if (streamChannel.attr(RESPONSE_PROMISE).getAndSet(null) != null) {
channelPool.release(streamChannel);
}
throw new IOException("Failed to receive response from " + getRemoteHost() + ":" + getRemotePort(), e);
}
NettyByteBufDataInputStream dataInputStream = new NettyByteBufDataInputStream(responseByteBuf);
// the size of remaining data should be dataInputStream.readLong() - 8
return new ChannelOutput(dataInputStream, dataInputStream.readLong() - Long.BYTES);
}
use of com.github.ambry.utils.NettyByteBufDataInputStream in project ambry by linkedin.
the class MessageInfoAndMetadataListSerDeTest method testSerDe.
@Test
public void testSerDe() throws Exception {
MockClusterMap mockMap = new MockClusterMap();
MockPartitionId partitionId = new MockPartitionId();
short[] accountIds = { 100, 101, 102, 103 };
short[] containerIds = { 10, 11, 12, 13 };
boolean[] isDeletedVals = { false, true, false, true };
boolean[] isTtlUpdatedVals = { true, false, false, true };
boolean[] isUndeletedVals = { true, false, false, true };
short[] lifeVersionVals = { 1, 2, 3, 4 };
Long[] crcs = { null, 100L, Long.MIN_VALUE, Long.MAX_VALUE };
StoreKey[] keys = { new BlobId(TestUtils.getRandomElement(BlobId.getAllValidVersions()), BlobId.BlobIdType.NATIVE, (byte) 0, accountIds[0], containerIds[0], partitionId, false, BlobId.BlobDataType.DATACHUNK), new BlobId(TestUtils.getRandomElement(BlobId.getAllValidVersions()), BlobId.BlobIdType.NATIVE, (byte) 0, accountIds[1], containerIds[1], partitionId, false, BlobId.BlobDataType.DATACHUNK), new BlobId(TestUtils.getRandomElement(BlobId.getAllValidVersions()), BlobId.BlobIdType.NATIVE, (byte) 0, accountIds[2], containerIds[2], partitionId, false, BlobId.BlobDataType.DATACHUNK), new BlobId(TestUtils.getRandomElement(BlobId.getAllValidVersions()), BlobId.BlobIdType.NATIVE, (byte) 0, accountIds[3], containerIds[3], partitionId, false, BlobId.BlobDataType.DATACHUNK) };
long[] blobSizes = { 1024, 2048, 4096, 8192 };
long[] times = { SystemTime.getInstance().milliseconds(), SystemTime.getInstance().milliseconds() - 1, SystemTime.getInstance().milliseconds() + TimeUnit.SECONDS.toMillis(5), Utils.Infinite_Time };
MessageMetadata[] messageMetadata = new MessageMetadata[4];
messageMetadata[0] = new MessageMetadata(ByteBuffer.wrap(getRandomBytes(100)));
messageMetadata[1] = new MessageMetadata(null);
messageMetadata[2] = null;
messageMetadata[3] = new MessageMetadata(ByteBuffer.wrap(getRandomBytes(200)));
List<MessageInfo> messageInfoList = new ArrayList<>(4);
List<MessageMetadata> messageMetadataList = new ArrayList<>(4);
for (int i = 0; i < 4; i++) {
messageInfoList.add(new MessageInfo(keys[i], blobSizes[i], isDeletedVals[i], isTtlUpdatedVals[i], isUndeletedVals[i], times[i], crcs[i], accountIds[i], containerIds[i], times[i], lifeVersionVals[i]));
messageMetadataList.add(messageMetadata[i]);
}
// Serialize and then deserialize
MessageInfoAndMetadataListSerde messageInfoAndMetadataListSerde = new MessageInfoAndMetadataListSerde(messageInfoList, messageMetadataList, serDeVersion);
ByteBuf buffer = Unpooled.buffer(messageInfoAndMetadataListSerde.getMessageInfoAndMetadataListSize());
messageInfoAndMetadataListSerde.serializeMessageInfoAndMetadataList(buffer);
if (serDeVersion >= MessageInfoAndMetadataListSerde.VERSION_5) {
// should fail if the wrong version is provided
try {
MessageInfoAndMetadataListSerde.deserializeMessageInfoAndMetadataList(new NettyByteBufDataInputStream(buffer), mockMap, (short) (serDeVersion - 1));
Assert.fail("Should have failed to deserialize");
} catch (IllegalArgumentException e) {
// expected. Nothing to do
}
}
buffer.readerIndex(0);
MessageInfoAndMetadataListSerde messageInfoAndMetadataList = MessageInfoAndMetadataListSerde.deserializeMessageInfoAndMetadataList(new NettyByteBufDataInputStream(buffer), mockMap, serDeVersion);
// Verify
List<MessageInfo> responseMessageInfoList = messageInfoAndMetadataList.getMessageInfoList();
List<MessageMetadata> responseMessageMetadataList = messageInfoAndMetadataList.getMessageMetadataList();
Assert.assertEquals(4, responseMessageInfoList.size());
Assert.assertEquals(4, responseMessageMetadataList.size());
for (int i = 0; i < 4; i++) {
assertMessageInfoEquality(messageInfoList.get(i), responseMessageInfoList.get(i));
assertMessageMetadataEquality(messageMetadataList.get(i), responseMessageMetadataList.get(i));
}
}
Aggregations