use of com.github.ambry.utils.ByteBufferChannel in project ambry by linkedin.
the class GetBlobOperationTest method getBlobBufferFromServer.
/**
* @param server the server hosting the blob.
* @return the ByteBuffer for the blob contents on the specified server.
* @throws IOException
*/
private ByteBuffer getBlobBufferFromServer(MockServer server) throws IOException {
PartitionRequestInfo requestInfo = new PartitionRequestInfo(blobId.getPartition(), Collections.singletonList(blobId));
GetRequest getRequest = new GetRequest(1, "assertBlobReadSuccess", MessageFormatFlags.All, Collections.singletonList(requestInfo), GetOption.None);
GetResponse getResponse = server.makeGetResponse(getRequest, ServerErrorCode.No_Error);
getRequest.release();
// simulating server sending response over the wire
ByteBufferChannel channel = new ByteBufferChannel(ByteBuffer.allocate((int) getResponse.sizeInBytes()));
getResponse.writeTo(channel);
getResponse.release();
// simulating the Router receiving the data from the wire
ByteBuffer data = channel.getBuffer();
data.flip();
DataInputStream stream = new DataInputStream(new ByteBufferInputStream(data));
// read off the size because GetResponse.readFrom() expects that this be read off
stream.readLong();
// construct a GetResponse as the Router would have
getResponse = GetResponse.readFrom(stream, mockClusterMap);
byte[] blobData = Utils.readBytesFromStream(getResponse.getInputStream(), (int) getResponse.getPartitionResponseInfoList().get(0).getMessageInfoList().get(0).getSize());
// set the put content buf to the data in the stream
return ByteBuffer.wrap(blobData);
}
use of com.github.ambry.utils.ByteBufferChannel in project ambry by linkedin.
the class StoreCopierTest method copyTest.
/**
* Tests {@link StoreCopier#copy(FindToken)}.
* @throws Exception
*/
@Test
public void copyTest() throws Exception {
storeCopier.copy(new StoreFindTokenFactory(STORE_KEY_FACTORY).getNewFindToken());
storeCopier.close();
// copy the store descriptor file over
StoreMetrics storeMetrics = new StoreMetrics(new MetricRegistry());
Files.copy(new File(srcDir, StoreDescriptor.STORE_DESCRIPTOR_FILENAME).toPath(), new File(tgtDir, StoreDescriptor.STORE_DESCRIPTOR_FILENAME).toPath(), StandardCopyOption.REPLACE_EXISTING);
BlobStore tgt = new BlobStore(STORE_ID, storeConfig, null, null, DISK_IO_SCHEDULER, StoreTestUtils.DEFAULT_DISK_SPACE_ALLOCATOR, storeMetrics, storeMetrics, tgtDir.getAbsolutePath(), STORE_CAPACITY, STORE_KEY_FACTORY, null, null, time);
tgt.start();
try {
// should not be able to get expired or deleted ids
StoreKey[] failKeys = { expiredId, deletedId, putTtlUpdatedAndDeletedId };
for (StoreKey key : failKeys) {
try {
tgt.get(Collections.singletonList(key), EnumSet.allOf(StoreGetOptions.class));
fail("Should have failed to get " + key);
} catch (StoreException e) {
assertEquals("Unexpected StoreErrorCode", StoreErrorCodes.ID_Not_Found, e.getErrorCode());
}
}
// should be able to get the non expired, non deleted entries
Map<StoreKey, Pair<byte[], Long>> successKeys = new HashMap<>();
successKeys.put(permanentPutId, new Pair<>(permanentPutData, Utils.Infinite_Time));
successKeys.put(putAndTtlUpdatedId, new Pair<>(putAndTtlUpdatedData, Utils.Infinite_Time));
successKeys.put(temporaryPutId, new Pair<>(temporaryPutData, temporaryPutExpiryTimeMs));
for (Map.Entry<StoreKey, Pair<byte[], Long>> entry : successKeys.entrySet()) {
StoreInfo storeInfo = tgt.get(Collections.singletonList(entry.getKey()), EnumSet.noneOf(StoreGetOptions.class));
MessageInfo messageInfo = storeInfo.getMessageReadSetInfo().get(0);
byte[] data = entry.getValue().getFirst();
assertEquals("Size does not match", data.length, messageInfo.getSize());
assertEquals("Size does not match", data.length, storeInfo.getMessageReadSet().sizeInBytes(0));
assertFalse("Should not be deleted or expired", messageInfo.isDeleted() || messageInfo.isExpired());
assertEquals("Ttl update flag not as expected", putAndTtlUpdatedId.equals(entry.getKey()), messageInfo.isTtlUpdated());
assertEquals("Expiration time does not match", entry.getValue().getSecond().longValue(), messageInfo.getExpirationTimeInMs());
ByteBufferChannel channel = new ByteBufferChannel(ByteBuffer.allocate(data.length));
storeInfo.getMessageReadSet().writeTo(0, channel, 0, data.length);
assertArrayEquals("Data put does not match data copied", data, channel.getBuffer().array());
}
} finally {
tgt.shutdown();
}
}
Aggregations