Search in sources :

Example 6 with ByteBufferChannel

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);
}
Also used : ByteBufferChannel(com.github.ambry.utils.ByteBufferChannel) GetRequest(com.github.ambry.protocol.GetRequest) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) PartitionRequestInfo(com.github.ambry.protocol.PartitionRequestInfo) DataInputStream(java.io.DataInputStream) NettyByteBufDataInputStream(com.github.ambry.utils.NettyByteBufDataInputStream) GetResponse(com.github.ambry.protocol.GetResponse) ByteBuffer(java.nio.ByteBuffer)

Example 7 with ByteBufferChannel

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();
    }
}
Also used : HashMap(java.util.HashMap) MetricRegistry(com.codahale.metrics.MetricRegistry) ByteBufferChannel(com.github.ambry.utils.ByteBufferChannel) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ClusterMap(com.github.ambry.clustermap.ClusterMap) MockClusterMap(com.github.ambry.clustermap.MockClusterMap) Pair(com.github.ambry.utils.Pair) Test(org.junit.Test)

Aggregations

ByteBufferChannel (com.github.ambry.utils.ByteBufferChannel)7 ByteBuffer (java.nio.ByteBuffer)4 ArrayList (java.util.ArrayList)3 ClusterMap (com.github.ambry.clustermap.ClusterMap)2 BlobProperties (com.github.ambry.messageformat.BlobProperties)2 GetRequest (com.github.ambry.protocol.GetRequest)2 GetResponse (com.github.ambry.protocol.GetResponse)2 PartitionRequestInfo (com.github.ambry.protocol.PartitionRequestInfo)2 PutRequest (com.github.ambry.protocol.PutRequest)2 PutResponse (com.github.ambry.protocol.PutResponse)2 ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)2 NettyByteBufDataInputStream (com.github.ambry.utils.NettyByteBufDataInputStream)2 DataInputStream (java.io.DataInputStream)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Account (com.github.ambry.account.Account)1 Container (com.github.ambry.account.Container)1 InMemAccountService (com.github.ambry.account.InMemAccountService)1 MockClusterMap (com.github.ambry.clustermap.MockClusterMap)1 ByteBufferReadableStreamChannel (com.github.ambry.commons.ByteBufferReadableStreamChannel)1 LoggingNotificationSystem (com.github.ambry.commons.LoggingNotificationSystem)1