Search in sources :

Example 26 with ByteBufferInputStream

use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.

the class MessageFormatRecordTest method testBlobRecordV2.

/**
 * Tests Blob Record Version 2
 * Creates test data and creates a blob record version 2 for the specified blob type with the test data
 * Verifies that the stream from blob output is same as the passed in data
 * Corrupts data and verifies that de-serialization fails
 * @param blobSize
 * @param blobType
 * @throws IOException
 * @throws MessageFormatException
 */
private void testBlobRecordV2(int blobSize, BlobType blobType) throws IOException, MessageFormatException {
    ByteBuffer blobContent = ByteBuffer.allocate(blobSize);
    new Random().nextBytes(blobContent.array());
    int size = (int) MessageFormatRecord.Blob_Format_V2.getBlobRecordSize(blobSize);
    ByteBuffer entireBlob = ByteBuffer.allocate(size);
    BlobData blobData = getBlobRecordV2(blobSize, blobType, blobContent, entireBlob);
    Assert.assertEquals("Blob size mismatch", blobSize, blobData.getSize());
    byte[] verify = new byte[blobSize];
    blobData.content().readBytes(verify);
    Assert.assertArrayEquals("BlobContent mismatch", blobContent.array(), verify);
    blobData.release();
    // corrupt blob record V2
    entireBlob.flip();
    byte savedByte = entireBlob.get(blobSize / 2);
    entireBlob.put(blobSize / 2, (byte) (savedByte + 1));
    try {
        MessageFormatRecord.deserializeBlob(new ByteBufferInputStream(entireBlob));
        fail("Failed to detect corruption of blob record");
    } catch (MessageFormatException e) {
        Assert.assertEquals("Error code mismatch", MessageFormatErrorCodes.Data_Corrupt, e.getErrorCode());
    }
}
Also used : Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 27 with ByteBufferInputStream

use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.

the class MessageFormatRecordTest method testUpdateRecordV1.

/**
 * Tests UpdateRecord V1 for serialization and deserialization
 * @throws IOException
 * @throws MessageFormatException
 */
@Test
public void testUpdateRecordV1() throws IOException, MessageFormatException {
    // Test update V1 record
    // irrespective of what values are set for acccountId, containerId and updateTimeMs, legacy values will be returned
    // with Update_Format_V1
    short accountId = Utils.getRandomShort(TestUtils.RANDOM);
    short containerId = Utils.getRandomShort(TestUtils.RANDOM);
    long updateTimeMs = SystemTime.getInstance().milliseconds() + TestUtils.RANDOM.nextInt();
    ByteBuffer updateRecord = ByteBuffer.allocate(Update_Format_V1.getRecordSize());
    Update_Format_V1.serialize(updateRecord, new UpdateRecord(accountId, containerId, updateTimeMs, new DeleteSubRecord()));
    updateRecord.flip();
    UpdateRecord deserializeUpdateRecord = MessageFormatRecord.deserializeUpdateRecord(new ByteBufferInputStream(updateRecord));
    Assert.assertEquals("AccountId mismatch ", UNKNOWN_ACCOUNT_ID, deserializeUpdateRecord.getAccountId());
    Assert.assertEquals("ContainerId mismatch ", UNKNOWN_CONTAINER_ID, deserializeUpdateRecord.getContainerId());
    Assert.assertEquals("DeletionTime mismatch ", Utils.Infinite_Time, deserializeUpdateRecord.getUpdateTimeInMs());
    Assert.assertEquals("Type of update record incorrect", SubRecord.Type.DELETE, deserializeUpdateRecord.getType());
    Assert.assertNotNull("DeleteSubRecord is null", deserializeUpdateRecord.getDeleteSubRecord());
    // corrupt update V1 record
    updateRecord.flip();
    byte toCorrupt = updateRecord.get(10);
    updateRecord.put(10, (byte) (toCorrupt + 1));
    try {
        MessageFormatRecord.deserializeUpdateRecord(new ByteBufferInputStream(updateRecord));
        fail("Deserialization of a corrupt update record V1 should have failed ");
    } catch (MessageFormatException e) {
        Assert.assertEquals(e.getErrorCode(), MessageFormatErrorCodes.Data_Corrupt);
    }
}
Also used : ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 28 with ByteBufferInputStream

use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.

the class MessageFormatRecordTest method deserializeMetadataContentV3.

private CompositeBlobInfo deserializeMetadataContentV3(ByteBuffer metadataContent, StoreKeyFactory storeKeyFactory) throws IOException {
    ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(metadataContent);
    DataInputStream inputStream = new DataInputStream(byteBufferInputStream);
    short metadataContentVersion = inputStream.readShort();
    Assert.assertEquals("Metadata Content Version mismatch ", MessageFormatRecord.Metadata_Content_Version_V3, metadataContentVersion);
    return MessageFormatRecord.Metadata_Content_Format_V3.deserializeMetadataContentRecord(inputStream, storeKeyFactory);
}
Also used : ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) DataInputStream(java.io.DataInputStream)

Example 29 with ByteBufferInputStream

use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.

the class BoundedNettyByteBufReceiveTest method testBoundedByteBufferReceiveOnLargeRequest.

/**
 * Test when the request size is bigger than the maximum size
 * @throws Exception
 */
@Test
public void testBoundedByteBufferReceiveOnLargeRequest() throws Exception {
    int bufferSize = 2000;
    ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
    buffer.putLong(bufferSize);
    byte[] buf = new byte[bufferSize - Long.BYTES];
    new Random().nextBytes(buf);
    buffer.put(buf);
    buffer.flip();
    BoundedNettyByteBufReceive set = new BoundedNettyByteBufReceive(100);
    // The max request size is 100, but the buffer size is 2000, will result in IOException
    try {
        set.readFrom(Channels.newChannel(new ByteBufferInputStream(buffer)));
        Assert.fail("Should fail with IOException");
    } catch (IOException e) {
    }
    buffer.clear();
}
Also used : Random(java.util.Random) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 30 with ByteBufferInputStream

use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.

the class BoundedNettyByteBufReceiveTest method testBoundedByteBufferReceive.

/**
 * Test basic operation of {@link BoundedNettyByteBufReceive}.
 * @throws Exception
 */
@Test
public void testBoundedByteBufferReceive() throws Exception {
    int bufferSize = 2000;
    ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
    buffer.putLong(bufferSize);
    byte[] buf = new byte[bufferSize - Long.BYTES];
    new Random().nextBytes(buf);
    buffer.put(buf);
    buffer.flip();
    BoundedNettyByteBufReceive set = new BoundedNettyByteBufReceive(100000);
    Assert.assertEquals("Wrong number of bytes read", bufferSize, set.readFrom(Channels.newChannel(new ByteBufferInputStream(buffer))));
    buffer.clear();
    ByteBuf payload = set.content();
    for (int i = 8; i < bufferSize; i++) {
        Assert.assertEquals(buffer.array()[i], payload.readByte());
    }
    payload.release();
}
Also used : Random(java.util.Random) ByteBufferInputStream(com.github.ambry.utils.ByteBufferInputStream) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ByteBufferInputStream (com.github.ambry.utils.ByteBufferInputStream)79 ByteBuffer (java.nio.ByteBuffer)48 DataInputStream (java.io.DataInputStream)34 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)19 InputStream (java.io.InputStream)16 MessageInfo (com.github.ambry.store.MessageInfo)15 StoreKey (com.github.ambry.store.StoreKey)14 IOException (java.io.IOException)12 MetricRegistry (com.codahale.metrics.MetricRegistry)10 Random (java.util.Random)10 GetResponse (com.github.ambry.protocol.GetResponse)8 MockId (com.github.ambry.store.MockId)8 ByteBufferOutputStream (com.github.ambry.utils.ByteBufferOutputStream)8 Crc32 (com.github.ambry.utils.Crc32)6 BlobId (com.github.ambry.commons.BlobId)5 BlobProperties (com.github.ambry.messageformat.BlobProperties)5 CrcInputStream (com.github.ambry.utils.CrcInputStream)5 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)4 RequestInfo (com.github.ambry.network.RequestInfo)4