Search in sources :

Example 26 with PutRequest

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

the class DirectSender method run.

@Override
public void run() {
    try {
        for (int i = 0; i < blobIds.size(); i++) {
            PutRequest putRequest = new PutRequest(1, "client1", blobIds.get(i), blobProperties, ByteBuffer.wrap(usermetadata), Unpooled.wrappedBuffer(data), blobProperties.getBlobSize(), BlobType.DataBlob, encryptionKey != null ? ByteBuffer.wrap(encryptionKey) : null);
            DataInputStream putResponseStream = channel.sendAndReceive(putRequest).getInputStream();
            PutResponse response = PutResponse.readFrom(putResponseStream);
            if (putResponseStream instanceof NettyByteBufDataInputStream) {
                // Release the underlying netty buf if the stream is NettyByteBuf based.
                // The ByteBuf should also be released if exception happens, but we ignore it since this is test and exception is not expected.
                ((NettyByteBufDataInputStream) putResponseStream).getBuffer().release();
            }
            Assert.assertEquals(response.getError(), ServerErrorCode.No_Error);
        }
    } catch (Exception e) {
        Assert.assertTrue(false);
    } finally {
        endLatch.countDown();
    }
}
Also used : NettyByteBufDataInputStream(com.github.ambry.utils.NettyByteBufDataInputStream) PutRequest(com.github.ambry.protocol.PutRequest) DataInputStream(java.io.DataInputStream) NettyByteBufDataInputStream(com.github.ambry.utils.NettyByteBufDataInputStream) PutResponse(com.github.ambry.protocol.PutResponse)

Example 27 with PutRequest

use of com.github.ambry.protocol.PutRequest 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, Unpooled.wrappedBuffer(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

PutRequest (com.github.ambry.protocol.PutRequest)27 BlobId (com.github.ambry.commons.BlobId)19 BlobProperties (com.github.ambry.messageformat.BlobProperties)19 DataInputStream (java.io.DataInputStream)18 ArrayList (java.util.ArrayList)18 PutResponse (com.github.ambry.protocol.PutResponse)15 ByteBuffer (java.nio.ByteBuffer)14 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)12 VerifiableProperties (com.github.ambry.config.VerifiableProperties)12 GetResponse (com.github.ambry.protocol.GetResponse)12 PartitionRequestInfo (com.github.ambry.protocol.PartitionRequestInfo)12 IOException (java.io.IOException)12 Properties (java.util.Properties)12 BlobIdFactory (com.github.ambry.commons.BlobIdFactory)11 ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)11 NettyByteBufDataInputStream (com.github.ambry.utils.NettyByteBufDataInputStream)11 InMemAccountService (com.github.ambry.account.InMemAccountService)10 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)9 BlobType (com.github.ambry.messageformat.BlobType)9 HashMap (java.util.HashMap)9