Search in sources :

Example 6 with ChannelOutput

use of com.github.ambry.network.ChannelOutput in project ambry by linkedin.

the class ReplicaThread method getReplicaMetadataResponse.

/**
 * Gets the replica metadata response for a list of remote replicas on a given remote data node
 * @param replicasToReplicatePerNode The list of remote replicas for a node
 * @param connectedChannel The connection channel to the node
 * @param remoteNode The remote node from which replication needs to happen
 * @return ReplicaMetadataResponse, the response from replica metadata request to remote node
 * @throws ReplicationException
 * @throws IOException
 */
ReplicaMetadataResponse getReplicaMetadataResponse(List<RemoteReplicaInfo> replicasToReplicatePerNode, ConnectedChannel connectedChannel, DataNodeId remoteNode) throws ReplicationException, IOException {
    long replicaMetadataRequestStartTime = time.milliseconds();
    List<ReplicaMetadataRequestInfo> replicaMetadataRequestInfoList = new ArrayList<ReplicaMetadataRequestInfo>();
    for (RemoteReplicaInfo remoteReplicaInfo : replicasToReplicatePerNode) {
        ReplicaMetadataRequestInfo replicaMetadataRequestInfo = new ReplicaMetadataRequestInfo(remoteReplicaInfo.getReplicaId().getPartitionId(), remoteReplicaInfo.getToken(), dataNodeId.getHostname(), remoteReplicaInfo.getLocalReplicaId().getReplicaPath(), remoteReplicaInfo.getReplicaId().getReplicaType(), replicationConfig.replicaMetadataRequestVersion);
        replicaMetadataRequestInfoList.add(replicaMetadataRequestInfo);
        logger.trace("Remote node: {} Thread name: {} Remote replica: {} Token going to be sent to remote: {} ", remoteNode, threadName, remoteReplicaInfo.getReplicaId(), remoteReplicaInfo.getToken());
    }
    ChannelOutput channelOutput = null;
    try {
        ReplicaMetadataRequest request = new ReplicaMetadataRequest(correlationIdGenerator.incrementAndGet(), "replication-metadata-" + dataNodeId.getHostname() + "[" + dataNodeId.getDatacenterName() + "]", replicaMetadataRequestInfoList, replicationConfig.replicationFetchSizeInBytes, replicationConfig.replicaMetadataRequestVersion);
        channelOutput = connectedChannel.sendAndReceive(request);
        logger.trace("Remote node: {} Thread name: {} Remote replicas: {} Stream size after deserialization: {} ", remoteNode, threadName, replicasToReplicatePerNode, channelOutput.getInputStream().available());
        ReplicaMetadataResponse response = ReplicaMetadataResponse.readFrom(channelOutput.getInputStream(), findTokenHelper, clusterMap);
        long metadataRequestTime = time.milliseconds() - replicaMetadataRequestStartTime;
        replicationMetrics.updateMetadataRequestTime(metadataRequestTime, replicatingFromRemoteColo, replicatingOverSsl, datacenterName);
        if (response.getError() != ServerErrorCode.No_Error || response.getReplicaMetadataResponseInfoList().size() != replicasToReplicatePerNode.size()) {
            int replicaMetadataResponseInfoListSize = response.getReplicaMetadataResponseInfoList() == null ? 0 : response.getReplicaMetadataResponseInfoList().size();
            logger.error("Remote node: {} Thread name: {} Remote replicas: {} Replica metadata response error: {} ReplicaMetadataResponseInfoListSize: {} ReplicasToReplicatePerNodeSize: {}", remoteNode, threadName, replicasToReplicatePerNode, response.getError(), replicaMetadataResponseInfoListSize, replicasToReplicatePerNode.size());
            throw new ReplicationException("Replica Metadata Response Error " + response.getError());
        }
        return response;
    } catch (Exception e) {
        responseHandler.onEvent(replicasToReplicatePerNode.get(0).getReplicaId(), e);
        throw e;
    } finally {
        if (channelOutput != null && (channelOutput.getInputStream() instanceof NettyByteBufDataInputStream)) {
            // Release buffer if and only if the inputStream is NettyByteBuf based.
            ((NettyByteBufDataInputStream) channelOutput.getInputStream()).getBuffer().release();
        }
    }
}
Also used : NettyByteBufDataInputStream(com.github.ambry.utils.NettyByteBufDataInputStream) ReplicaMetadataResponse(com.github.ambry.protocol.ReplicaMetadataResponse) ReplicaMetadataRequest(com.github.ambry.protocol.ReplicaMetadataRequest) ReplicaMetadataRequestInfo(com.github.ambry.protocol.ReplicaMetadataRequestInfo) ChannelOutput(com.github.ambry.network.ChannelOutput) ArrayList(java.util.ArrayList) StoreException(com.github.ambry.store.StoreException) IOException(java.io.IOException)

Aggregations

ChannelOutput (com.github.ambry.network.ChannelOutput)6 IOException (java.io.IOException)5 ByteBuf (io.netty.buffer.ByteBuf)3 ArrayList (java.util.ArrayList)3 BlobId (com.github.ambry.commons.BlobId)2 GetRequest (com.github.ambry.protocol.GetRequest)2 GetResponse (com.github.ambry.protocol.GetResponse)2 PartitionRequestInfo (com.github.ambry.protocol.PartitionRequestInfo)2 StoreKey (com.github.ambry.store.StoreKey)2 NettyByteBufDataInputStream (com.github.ambry.utils.NettyByteBufDataInputStream)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 InetSocketAddress (java.net.InetSocketAddress)2 Random (java.util.Random)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.Test)2 CloudDataNode (com.github.ambry.clustermap.CloudDataNode)1 ConnectedChannel (com.github.ambry.network.ConnectedChannel)1 ReplicaMetadataRequest (com.github.ambry.protocol.ReplicaMetadataRequest)1 ReplicaMetadataRequestInfo (com.github.ambry.protocol.ReplicaMetadataRequestInfo)1