Search in sources :

Example 11 with PutResponse

use of com.github.ambry.protocol.PutResponse in project ambry by linkedin.

the class DirectoryUploader method writeToAmbryReplica.

/**
 * Adding a blob to a particular server.
 * @param stream stream containing the actual blob
 * @param blobId blob id generated by ambry
 * @param replicaId replica to which the blob has to be added
 * @param correlationId coorelation id to uniquely identify the call
 * @return true if write succeeds, else false
 * @throws Exception
 */
private boolean writeToAmbryReplica(BlobProperties blobProperties, ByteBuffer userMetaData, InputStream stream, BlobId blobId, ReplicaId replicaId, AtomicInteger correlationId, boolean enableVerboseLogging) throws Exception {
    ArrayList<BlobId> blobIds = new ArrayList<BlobId>();
    blobIds.add(blobId);
    ConnectedChannel blockingChannel = null;
    try {
        blockingChannel = connectionPool.checkOutConnection(replicaId.getDataNodeId().getHostname(), new Port(replicaId.getDataNodeId().getPort(), PortType.PLAINTEXT), 100000);
        int size = (int) blobProperties.getBlobSize();
        ByteBufferInputStream blobStream = new ByteBufferInputStream(stream, size);
        PutRequest putRequest = new PutRequest(correlationId.incrementAndGet(), "consumerThread", blobId, blobProperties, userMetaData, blobStream.getByteBuffer(), size, BlobType.DataBlob, null);
        if (enableVerboseLogging) {
            System.out.println("Put Request to a replica : " + putRequest + " for blobId " + blobId);
        }
        PutResponse putResponse = getPutResponseFromStream(blockingChannel, putRequest, connectionPool);
        if (putResponse == null) {
            System.out.println("PutResponse to a replica " + replicaId + " failed with null Response for blobId " + blobId);
            blockingChannel = null;
            return false;
        } else {
            if (putResponse.getError() != ServerErrorCode.No_Error && putResponse.getError() != ServerErrorCode.Blob_Already_Exists) {
                System.out.println("PutResponse to a replica " + replicaId + " failed with Error code  " + putResponse.getError() + " for blobId " + blobId);
                return false;
            }
            return true;
        }
    } finally {
        if (stream != null) {
            stream.close();
        }
        if (blockingChannel != null) {
            connectionPool.checkInConnection(blockingChannel);
        }
    }
}
Also used : Port(com.github.ambry.network.Port) ArrayList(java.util.ArrayList) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) PutRequest(com.github.ambry.protocol.PutRequest) ConnectedChannel(com.github.ambry.network.ConnectedChannel) PutResponse(com.github.ambry.protocol.PutResponse) BlobId(com.github.ambry.commons.BlobId)

Aggregations

PutResponse (com.github.ambry.protocol.PutResponse)11 PutRequest (com.github.ambry.protocol.PutRequest)9 DataInputStream (java.io.DataInputStream)9 InputStream (java.io.InputStream)6 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)4 CrcInputStream (com.github.ambry.utils.CrcInputStream)4 IOException (java.io.IOException)4 BlobId (com.github.ambry.commons.BlobId)3 BlobProperties (com.github.ambry.messageformat.BlobProperties)3 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)3 ByteBuffer (java.nio.ByteBuffer)3 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)2 MockPartitionId (com.github.ambry.clustermap.MockPartitionId)2 PartitionId (com.github.ambry.clustermap.PartitionId)2 BlobIdFactory (com.github.ambry.commons.BlobIdFactory)2 VerifiableProperties (com.github.ambry.config.VerifiableProperties)2 BlobAll (com.github.ambry.messageformat.BlobAll)2 BlockingChannel (com.github.ambry.network.BlockingChannel)2