use of com.github.ambry.network.Send in project ambry by linkedin.
the class CompositeSendTest method testCompositeSendWithByteBuf.
@Test
public void testCompositeSendWithByteBuf() {
byte[] buf1 = new byte[1024];
byte[] buf2 = new byte[2048];
byte[] buf3 = new byte[4096];
new Random().nextBytes(buf1);
new Random().nextBytes(buf2);
new Random().nextBytes(buf3);
ByteArraySend byteArraySend1 = new ByteArraySend(buf1, true);
ByteArraySend byteArraySend2 = new ByteArraySend(buf2, true);
ByteArraySend byteArraySend3 = new ByteArraySend(buf3, true);
List<Send> listToSend = new ArrayList<Send>(3);
listToSend.add(byteArraySend1);
listToSend.add(byteArraySend2);
listToSend.add(byteArraySend3);
CompositeSend compositeSend = new CompositeSend(listToSend);
ByteBuf content = compositeSend.content();
for (int i = 0; i < 1024; i++) {
Assert.assertEquals(buf1[i], content.getByte(i));
}
for (int i = 0; i < 2048; i++) {
Assert.assertEquals(buf2[i], content.getByte(1024 + i));
}
for (int i = 0; i < 4096; i++) {
Assert.assertEquals(buf3[i], content.getByte(1024 + 2048 + i));
}
content.release();
}
use of com.github.ambry.network.Send in project ambry by linkedin.
the class CompositeSendTest method testCompositeSendWithoutByteBuf.
@Test
public void testCompositeSendWithoutByteBuf() throws IOException {
byte[] buf1 = new byte[1024];
byte[] buf2 = new byte[2048];
byte[] buf3 = new byte[4096];
new Random().nextBytes(buf1);
new Random().nextBytes(buf2);
new Random().nextBytes(buf3);
ByteArraySend byteArraySend1 = new ByteArraySend(buf1, false);
ByteArraySend byteArraySend2 = new ByteArraySend(buf2, false);
ByteArraySend byteArraySend3 = new ByteArraySend(buf3, false);
List<Send> listToSend = new ArrayList<Send>(3);
listToSend.add(byteArraySend1);
listToSend.add(byteArraySend2);
listToSend.add(byteArraySend3);
CompositeSend compositeSend = new CompositeSend(listToSend);
ByteBuffer bufferToWrite = ByteBuffer.allocate(1024 + 2048 + 4096);
ByteBufferOutputStream bufferToWriteStream = new ByteBufferOutputStream(bufferToWrite);
WritableByteChannel writableByteChannel = Channels.newChannel(bufferToWriteStream);
while (!compositeSend.isSendComplete()) {
compositeSend.writeTo(writableByteChannel);
}
bufferToWrite.flip();
for (int i = 0; i < 1024; i++) {
Assert.assertEquals(buf1[i], bufferToWrite.get(i));
}
for (int i = 0; i < 2048; i++) {
Assert.assertEquals(buf2[i], bufferToWrite.get(1024 + i));
}
for (int i = 0; i < 4096; i++) {
Assert.assertEquals(buf3[i], bufferToWrite.get(1024 + 2048 + i));
}
}
use of com.github.ambry.network.Send in project ambry by linkedin.
the class CompositeSend method writeTo.
@Override
public void writeTo(AsyncWritableChannel channel, Callback<Long> callback) {
int lastIndex = compositeSendList.size() - 1;
int i = 0;
// not all sends in the batch. This may not currently be a problem but is something to look out for.
for (Send send : compositeSendList) {
if (i == lastIndex) {
// only the last one pass in callback
send.writeTo(channel, callback);
} else {
// TODO: stop writing to the channel whenever there is an exception here and stop the for loop.
send.writeTo(channel, null);
}
i++;
}
}
use of com.github.ambry.network.Send in project ambry by linkedin.
the class RequestResponseTest method testGetRequestResponse.
private void testGetRequestResponse(short getVersionToUse, short messageInfoAutoVersion) throws IOException {
GetResponse.CURRENT_VERSION = getVersionToUse;
MessageInfoAndMetadataListSerde.AUTO_VERSION = messageInfoAutoVersion;
MockClusterMap clusterMap = new MockClusterMap();
short accountId = Utils.getRandomShort(TestUtils.RANDOM);
short containerId = Utils.getRandomShort(TestUtils.RANDOM);
BlobId id1 = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, accountId, containerId, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK);
ArrayList<BlobId> blobIdList = new ArrayList<BlobId>();
blobIdList.add(id1);
PartitionRequestInfo partitionRequestInfo1 = new PartitionRequestInfo(new MockPartitionId(), blobIdList);
ArrayList<PartitionRequestInfo> partitionRequestInfoList = new ArrayList<PartitionRequestInfo>();
partitionRequestInfoList.add(partitionRequestInfo1);
GetRequest getRequest = new GetRequest(1234, "clientId", MessageFormatFlags.Blob, partitionRequestInfoList, GetOption.None);
DataInputStream requestStream = serAndPrepForRead(getRequest, -1, true);
GetRequest deserializedGetRequest = GetRequest.readFrom(requestStream, clusterMap);
Assert.assertEquals(deserializedGetRequest.getClientId(), "clientId");
Assert.assertEquals(deserializedGetRequest.getPartitionInfoList().size(), 1);
Assert.assertEquals(deserializedGetRequest.getPartitionInfoList().get(0).getBlobIds().size(), 1);
Assert.assertEquals(deserializedGetRequest.getPartitionInfoList().get(0).getBlobIds().get(0), id1);
getRequest.release();
// Test GetResponse with InputStream
long operationTimeMs = SystemTime.getInstance().milliseconds() + TestUtils.RANDOM.nextInt();
byte[] encryptionKey = TestUtils.getRandomBytes(256);
MessageInfo messageInfo = new MessageInfo(id1, 1000, false, false, true, 1000, null, accountId, containerId, operationTimeMs, (short) 1);
MessageMetadata messageMetadata = new MessageMetadata(ByteBuffer.wrap(encryptionKey));
ArrayList<MessageInfo> messageInfoList = new ArrayList<>();
ArrayList<MessageMetadata> messageMetadataList = new ArrayList<>();
messageInfoList.add(messageInfo);
messageMetadataList.add(messageMetadata);
PartitionResponseInfo partitionResponseInfo = new PartitionResponseInfo(clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), messageInfoList, messageMetadataList);
List<PartitionResponseInfo> partitionResponseInfoList = new ArrayList<PartitionResponseInfo>();
partitionResponseInfoList.add(partitionResponseInfo);
byte[] buf = TestUtils.getRandomBytes(1000);
ByteArrayInputStream byteStream = new ByteArrayInputStream(buf);
GetResponse response = new GetResponse(1234, "clientId", partitionResponseInfoList, byteStream, ServerErrorCode.No_Error);
requestStream = serAndPrepForRead(response, -1, false);
GetResponse deserializedGetResponse = GetResponse.readFrom(requestStream, clusterMap);
Assert.assertEquals(deserializedGetResponse.getCorrelationId(), 1234);
Assert.assertEquals(deserializedGetResponse.getError(), ServerErrorCode.No_Error);
Assert.assertEquals(deserializedGetResponse.getPartitionResponseInfoList().size(), 1);
Assert.assertEquals(deserializedGetResponse.getPartitionResponseInfoList().get(0).getMessageInfoList().size(), 1);
MessageInfo msgInfo = deserializedGetResponse.getPartitionResponseInfoList().get(0).getMessageInfoList().get(0);
Assert.assertEquals(msgInfo.getSize(), 1000);
Assert.assertEquals(msgInfo.getStoreKey(), id1);
Assert.assertEquals(msgInfo.getExpirationTimeInMs(), 1000);
Assert.assertEquals(deserializedGetResponse.getPartitionResponseInfoList().get(0).getMessageMetadataList().size(), 1);
if (GetResponse.getCurrentVersion() >= GetResponse.GET_RESPONSE_VERSION_V_4) {
MessageMetadata messageMetadataInResponse = deserializedGetResponse.getPartitionResponseInfoList().get(0).getMessageMetadataList().get(0);
Assert.assertEquals(messageMetadata.getEncryptionKey().rewind(), messageMetadataInResponse.getEncryptionKey());
} else {
Assert.assertNull(deserializedGetResponse.getPartitionResponseInfoList().get(0).getMessageMetadataList().get(0));
}
if (GetResponse.getCurrentVersion() >= GetResponse.GET_RESPONSE_VERSION_V_3) {
Assert.assertEquals("AccountId mismatch ", accountId, msgInfo.getAccountId());
Assert.assertEquals("ConatinerId mismatch ", containerId, msgInfo.getContainerId());
Assert.assertEquals("OperationTime mismatch ", operationTimeMs, msgInfo.getOperationTimeMs());
} else {
Assert.assertEquals("AccountId mismatch ", UNKNOWN_ACCOUNT_ID, msgInfo.getAccountId());
Assert.assertEquals("ConatinerId mismatch ", UNKNOWN_CONTAINER_ID, msgInfo.getContainerId());
Assert.assertEquals("OperationTime mismatch ", Utils.Infinite_Time, msgInfo.getOperationTimeMs());
}
if (messageInfoAutoVersion >= MessageInfoAndMetadataListSerde.VERSION_6) {
Assert.assertTrue(msgInfo.isUndeleted());
Assert.assertEquals("LifeVersion mismatch", (short) 1, msgInfo.getLifeVersion());
} else {
Assert.assertFalse(msgInfo.isUndeleted());
Assert.assertEquals("LifeVersion mismatch", (short) 0, msgInfo.getLifeVersion());
}
response.release();
// Test GetResponse with Send
for (boolean useComposite : new boolean[] { false, true }) {
for (boolean withContent : new boolean[] { false, true }) {
operationTimeMs = SystemTime.getInstance().milliseconds() + TestUtils.RANDOM.nextInt();
encryptionKey = TestUtils.getRandomBytes(256);
messageInfo = new MessageInfo(id1, 1000, false, false, true, 1000, null, accountId, containerId, operationTimeMs, (short) 1);
messageMetadata = new MessageMetadata(ByteBuffer.wrap(encryptionKey));
messageInfoList.clear();
messageMetadataList.clear();
messageInfoList.add(messageInfo);
messageMetadataList.add(messageMetadata);
partitionResponseInfo = new PartitionResponseInfo(clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), messageInfoList, messageMetadataList);
partitionResponseInfoList.clear();
partitionResponseInfoList.add(partitionResponseInfo);
Send send;
if (withContent) {
send = new SendWithContent(1000, useComposite);
} else {
send = new SendWithoutContent(1000, useComposite);
}
response = new GetResponse(1234, "clientId", partitionResponseInfoList, send, ServerErrorCode.No_Error);
requestStream = serAndPrepForRead(response, -1, false);
deserializedGetResponse = GetResponse.readFrom(requestStream, clusterMap);
Assert.assertEquals(deserializedGetResponse.getCorrelationId(), 1234);
Assert.assertEquals(deserializedGetResponse.getError(), ServerErrorCode.No_Error);
Assert.assertEquals(deserializedGetResponse.getPartitionResponseInfoList().size(), 1);
Assert.assertEquals(deserializedGetResponse.getPartitionResponseInfoList().get(0).getMessageInfoList().size(), 1);
msgInfo = deserializedGetResponse.getPartitionResponseInfoList().get(0).getMessageInfoList().get(0);
Assert.assertEquals(msgInfo.getSize(), 1000);
Assert.assertEquals(msgInfo.getStoreKey(), id1);
Assert.assertEquals(msgInfo.getExpirationTimeInMs(), 1000);
Assert.assertEquals(deserializedGetResponse.getPartitionResponseInfoList().get(0).getMessageMetadataList().size(), 1);
response.release();
}
}
}
Aggregations