Search in sources :

Example 1 with MockIdFactory

use of com.github.ambry.store.MockIdFactory in project ambry by linkedin.

the class MessageFormatInputStreamTest method messageFormatPutNoArgReadTest.

/**
 * Test calling the no-arg read method
 * @throws IOException
 * @throws MessageFormatException
 */
@Test
public void messageFormatPutNoArgReadTest() throws Exception, MessageFormatException {
    StoreKey key = new MockId("id1");
    StoreKeyFactory keyFactory = new MockIdFactory();
    short accountId = Utils.getRandomShort(TestUtils.RANDOM);
    short containerId = Utils.getRandomShort(TestUtils.RANDOM);
    BlobProperties prop = new BlobProperties(10, "servid", accountId, containerId, false);
    byte[] encryptionKey = new byte[100];
    new Random().nextBytes(encryptionKey);
    byte[] usermetadata = new byte[1000];
    new Random().nextBytes(usermetadata);
    int blobContentSize = 2000;
    byte[] data = new byte[blobContentSize];
    new Random().nextBytes(data);
    ByteBufferInputStream stream = new ByteBufferInputStream(ByteBuffer.wrap(data));
    MessageFormatInputStream messageFormatStream = new PutMessageFormatInputStream(key, ByteBuffer.wrap(encryptionKey), prop, ByteBuffer.wrap(usermetadata), stream, blobContentSize, BlobType.DataBlob);
    TestUtils.validateInputStreamContract(messageFormatStream);
    TestUtils.readInputStreamAndValidateSize(messageFormatStream, messageFormatStream.getSize());
}
Also used : StoreKeyFactory(com.github.ambry.store.StoreKeyFactory) Random(java.util.Random) MockIdFactory(com.github.ambry.store.MockIdFactory) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) MockId(com.github.ambry.store.MockId) StoreKey(com.github.ambry.store.StoreKey) Test(org.junit.Test)

Example 2 with MockIdFactory

use of com.github.ambry.store.MockIdFactory in project ambry by linkedin.

the class MessageFormatSendTest method sendWriteTestWithBadId.

@Test
public void sendWriteTestWithBadId() throws IOException, MessageFormatException {
    // add header,system metadata, user metadata and data to the buffers
    ByteBuffer buf1 = ByteBuffer.allocate(1010);
    // fill header
    // version
    buf1.putShort((short) 1);
    // total size
    buf1.putLong(950);
    // put relative offsets
    // blob property relative offset
    buf1.putInt(60);
    // delete relative offset
    buf1.putInt(-1);
    // user metadata relative offset
    buf1.putInt(81);
    // data relative offset
    buf1.putInt(191);
    Crc32 crc = new Crc32();
    crc.update(buf1.array(), 0, buf1.position());
    // crc
    buf1.putLong(crc.getValue());
    // blob id
    String id = new String("012345678910123456789012");
    buf1.putShort((short) id.length());
    buf1.put(id.getBytes());
    // blob property version
    buf1.putShort((short) 1);
    String attribute1 = "ttl";
    String attribute2 = "del";
    // ttl name
    buf1.put(attribute1.getBytes());
    // ttl value
    buf1.putLong(12345);
    // delete name
    buf1.put(attribute2.getBytes());
    byte b = 1;
    // delete flag
    buf1.put(b);
    // crc
    buf1.putInt(456);
    // user metadata version
    buf1.putShort((short) 1);
    buf1.putInt(100);
    byte[] usermetadata = new byte[100];
    new Random().nextBytes(usermetadata);
    buf1.put(usermetadata);
    buf1.putInt(123);
    // blob version
    buf1.putShort((short) 0);
    // blob size
    buf1.putLong(805);
    // blob
    byte[] data = new byte[805];
    new Random().nextBytes(data);
    buf1.put(data);
    // blob crc
    buf1.putInt(123);
    buf1.flip();
    ArrayList<ByteBuffer> listbuf = new ArrayList<ByteBuffer>();
    listbuf.add(buf1);
    ArrayList<StoreKey> storeKeys = new ArrayList<StoreKey>();
    storeKeys.add(new MockId("012345678910123223233456789012"));
    MessageReadSet readSet = new MockMessageReadSet(listbuf, storeKeys);
    MetricRegistry registry = new MetricRegistry();
    MessageFormatMetrics metrics = new MessageFormatMetrics(registry);
    // get all
    MessageFormatSend send = new MessageFormatSend(readSet, MessageFormatFlags.All, metrics, new MockIdFactory());
    Assert.assertEquals(send.sizeInBytes(), 1010);
    ByteBuffer bufresult = ByteBuffer.allocate(1010);
    WritableByteChannel channel1 = Channels.newChannel(new ByteBufferOutputStream(bufresult));
    while (!send.isSendComplete()) {
        send.writeTo(channel1);
    }
    Assert.assertArrayEquals(buf1.array(), bufresult.array());
    try {
        // get blob
        MessageFormatSend send1 = new MessageFormatSend(readSet, MessageFormatFlags.Blob, metrics, new MockIdFactory());
        Assert.fail("Exception is expected");
    } catch (MessageFormatException e) {
        Assert.assertTrue(e.getErrorCode() == MessageFormatErrorCodes.Store_Key_Id_MisMatch);
    }
}
Also used : MessageReadSet(com.github.ambry.store.MessageReadSet) MetricRegistry(com.codahale.metrics.MetricRegistry) ArrayList(java.util.ArrayList) WritableByteChannel(java.nio.channels.WritableByteChannel) MockId(com.github.ambry.store.MockId) ByteBuffer(java.nio.ByteBuffer) StoreKey(com.github.ambry.store.StoreKey) Random(java.util.Random) MockIdFactory(com.github.ambry.store.MockIdFactory) ByteBufferOutputStream(com.github.ambry.utils.ByteBufferOutputStream) Crc32(com.github.ambry.utils.Crc32) Test(org.junit.Test)

Example 3 with MockIdFactory

use of com.github.ambry.store.MockIdFactory in project ambry by linkedin.

the class MessageFormatRecordTest method testMetadataContentRecordV2.

@Test
public void testMetadataContentRecordV2() throws IOException, MessageFormatException {
    // Test Metadata Blob V2
    List<StoreKey> keys = getKeys(60, 5);
    int[] chunkSizes = { ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE), 15 };
    long[] totalSizes = { (long) keys.size() * chunkSizes[0], ((long) keys.size() * chunkSizes[1]) - 11 };
    for (int i = 0; i < chunkSizes.length; i++) {
        ByteBuffer metadataContent = getSerializedMetadataContentV2(chunkSizes[i], totalSizes[i], keys);
        CompositeBlobInfo compositeBlobInfo = deserializeMetadataContentV2(metadataContent, new MockIdFactory());
        Assert.assertEquals("Chunk size doesn't match", chunkSizes[i], compositeBlobInfo.getChunkSize());
        Assert.assertEquals("Total size doesn't match", totalSizes[i], compositeBlobInfo.getTotalSize());
        Assert.assertEquals("List of keys dont match", keys, compositeBlobInfo.getKeys());
    // no testing of corruption as the metadata content record doesn't have crc
    }
}
Also used : MockIdFactory(com.github.ambry.store.MockIdFactory) StoreKey(com.github.ambry.store.StoreKey) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with MockIdFactory

use of com.github.ambry.store.MockIdFactory in project ambry by linkedin.

the class BlobStoreRecoveryTest method recoveryTest.

@Test
public void recoveryTest() throws MessageFormatException, IOException {
    MessageStoreRecovery recovery = new BlobStoreRecovery();
    // create log and write to it
    ReadImp readrecovery = new ReadImp();
    readrecovery.initialize();
    List<MessageInfo> recoveredMessages = recovery.recover(readrecovery, 0, readrecovery.getSize(), new MockIdFactory());
    Assert.assertEquals(recoveredMessages.size(), 6);
    verifyInfo(recoveredMessages.get(0), readrecovery.keys[0], readrecovery.sizes.get(0), readrecovery.expectedExpirationTimeMs, false, false);
    verifyInfo(recoveredMessages.get(1), readrecovery.keys[1], readrecovery.sizes.get(1), Utils.Infinite_Time, false, false);
    verifyInfo(recoveredMessages.get(2), readrecovery.keys[2], readrecovery.sizes.get(2), Utils.Infinite_Time, false, false);
    verifyInfo(recoveredMessages.get(4), readrecovery.keys[1], readrecovery.sizes.get(4), Utils.Infinite_Time, true, false);
    if (MessageFormatRecord.headerVersionToUse >= MessageFormatRecord.Message_Header_Version_V2) {
        verifyInfo(recoveredMessages.get(3), readrecovery.keys[1], readrecovery.sizes.get(3), Utils.Infinite_Time, false, true);
        verifyInfo(recoveredMessages.get(5), readrecovery.keys[0], readrecovery.sizes.get(5), Utils.Infinite_Time, false, true);
    } else {
        verifyInfo(recoveredMessages.get(3), readrecovery.keys[3], readrecovery.sizes.get(3), Utils.Infinite_Time, false, false);
        verifyInfo(recoveredMessages.get(5), readrecovery.keys[4], readrecovery.sizes.get(5), Utils.Infinite_Time, false, false);
    }
}
Also used : MessageStoreRecovery(com.github.ambry.store.MessageStoreRecovery) MockIdFactory(com.github.ambry.store.MockIdFactory) MessageInfo(com.github.ambry.store.MessageInfo) Test(org.junit.Test)

Example 5 with MockIdFactory

use of com.github.ambry.store.MockIdFactory in project ambry by linkedin.

the class MessageFormatRecordTest method testBlobRecordWithMetadataContentV2.

@Test
public void testBlobRecordWithMetadataContentV2() throws IOException, MessageFormatException {
    // Test Blob V2 with actual metadata blob V2
    // construct metadata blob
    List<StoreKey> keys = getKeys(60, 5);
    int[] chunkSizes = { ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE), 15 };
    long[] totalSizes = { (long) keys.size() * chunkSizes[0], ((long) keys.size() * chunkSizes[1]) - 11 };
    for (int i = 0; i < chunkSizes.length; i++) {
        ByteBuffer metadataContent = getSerializedMetadataContentV2(chunkSizes[i], totalSizes[i], keys);
        int metadataContentSize = MessageFormatRecord.Metadata_Content_Format_V2.getMetadataContentSize(keys.get(0).sizeInBytes(), keys.size());
        long blobSize = MessageFormatRecord.Blob_Format_V2.getBlobRecordSize(metadataContentSize);
        ByteBuffer blob = ByteBuffer.allocate((int) blobSize);
        BlobData blobData = getBlobRecordV2(metadataContentSize, BlobType.MetadataBlob, metadataContent, blob);
        Assert.assertEquals(metadataContentSize, blobData.getSize());
        byte[] verify = new byte[metadataContentSize];
        blobData.content().readBytes(verify);
        Assert.assertArrayEquals("Metadata content mismatch", metadataContent.array(), verify);
        blobData.release();
        // deserialize and check for metadata contents
        metadataContent.rewind();
        CompositeBlobInfo compositeBlobInfo = deserializeMetadataContentV2(metadataContent, new MockIdFactory());
        Assert.assertEquals("Chunk size doesn't match", chunkSizes[i], compositeBlobInfo.getChunkSize());
        Assert.assertEquals("Total size doesn't match", totalSizes[i], compositeBlobInfo.getTotalSize());
        Assert.assertEquals("List of keys dont match", keys, compositeBlobInfo.getKeys());
        testBlobCorruption(blob, blobSize, metadataContentSize);
    }
}
Also used : MockIdFactory(com.github.ambry.store.MockIdFactory) StoreKey(com.github.ambry.store.StoreKey) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

MockIdFactory (com.github.ambry.store.MockIdFactory)12 ByteBuffer (java.nio.ByteBuffer)9 StoreKey (com.github.ambry.store.StoreKey)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 MockId (com.github.ambry.store.MockId)4 ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 StoreKeyFactory (com.github.ambry.store.StoreKeyFactory)3 ByteBufferOutputStream (com.github.ambry.utils.ByteBufferOutputStream)3 DataInputStream (java.io.DataInputStream)3 WritableByteChannel (java.nio.channels.WritableByteChannel)3 Random (java.util.Random)3 MessageReadSet (com.github.ambry.store.MessageReadSet)2 Crc32 (com.github.ambry.utils.Crc32)2 Pair (com.github.ambry.utils.Pair)2 ByteBuf (io.netty.buffer.ByteBuf)2 HardDeleteInfo (com.github.ambry.store.HardDeleteInfo)1 MessageInfo (com.github.ambry.store.MessageInfo)1 MessageStoreHardDelete (com.github.ambry.store.MessageStoreHardDelete)1