Search in sources :

Example 1 with HardDeleteInfo

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

the class HardDeleteRecoveryMetadata method getHardDeleteInfo.

/**
 * For the message at readSetIndex, does the following:
 *   1. Reads the whole blob and does a crc check. If the crc check fails, returns null - this means that the record
 *   is not retrievable anyway.
 *   2. Adds to a hard delete replacement write set.
 *   3. Returns the hard delete info.
 */
private HardDeleteInfo getHardDeleteInfo(int readSetIndex) {
    HardDeleteInfo hardDeleteInfo = null;
    try {
        /* Read the version field in the header */
        ByteBuffer headerVersionBuf = ByteBuffer.allocate(Version_Field_Size_In_Bytes);
        readSet.writeTo(readSetIndex, Channels.newChannel(new ByteBufferOutputStream(headerVersionBuf)), 0, Version_Field_Size_In_Bytes);
        headerVersionBuf.flip();
        short headerVersion = headerVersionBuf.getShort();
        if (!isValidHeaderVersion(headerVersion)) {
            throw new MessageFormatException("Unknown header version during hard delete " + headerVersion + " storeKey " + readSet.getKeyAt(readSetIndex), MessageFormatErrorCodes.Unknown_Format_Version);
        }
        ByteBuffer header = ByteBuffer.allocate(getHeaderSizeForVersion(headerVersion));
        /* Read the rest of the header */
        header.putShort(headerVersion);
        readSet.writeTo(readSetIndex, Channels.newChannel(new ByteBufferOutputStream(header)), Version_Field_Size_In_Bytes, header.capacity() - Version_Field_Size_In_Bytes);
        header.flip();
        MessageHeader_Format headerFormat = getMessageHeader(headerVersion, header);
        headerFormat.verifyHeader();
        StoreKey storeKey = storeKeyFactory.getStoreKey(new DataInputStream(new MessageReadSetIndexInputStream(readSet, readSetIndex, header.capacity())));
        if (storeKey.compareTo(readSet.getKeyAt(readSetIndex)) != 0) {
            throw new MessageFormatException("Id mismatch between metadata and store - metadataId " + readSet.getKeyAt(readSetIndex) + " storeId " + storeKey, MessageFormatErrorCodes.Store_Key_Id_MisMatch);
        }
        if (!headerFormat.isPutRecord()) {
            throw new MessageFormatException("Cleanup operation for a non-PUT record is unsupported", MessageFormatErrorCodes.IO_Error);
        } else {
            HardDeleteRecoveryMetadata hardDeleteRecoveryMetadata = recoveryInfoMap.get(storeKey);
            int userMetadataRelativeOffset = headerFormat.getUserMetadataRecordRelativeOffset();
            short userMetadataVersion;
            int userMetadataSize;
            short blobRecordVersion;
            BlobType blobType;
            long blobStreamSize;
            DeserializedUserMetadata userMetadataInfo;
            DeserializedBlob blobRecordInfo;
            if (hardDeleteRecoveryMetadata == null) {
                userMetadataInfo = getUserMetadataInfo(readSet, readSetIndex, headerFormat.getUserMetadataRecordRelativeOffset(), headerFormat.getUserMetadataRecordSize());
                userMetadataSize = userMetadataInfo.getUserMetadata().capacity();
                userMetadataVersion = userMetadataInfo.getVersion();
                blobRecordInfo = getBlobRecordInfo(readSet, readSetIndex, headerFormat.getBlobRecordRelativeOffset(), headerFormat.getBlobRecordSize());
                blobStreamSize = blobRecordInfo.getBlobData().getSize();
                blobRecordVersion = blobRecordInfo.getVersion();
                blobType = blobRecordInfo.getBlobData().getBlobType();
                hardDeleteRecoveryMetadata = new HardDeleteRecoveryMetadata(headerVersion, userMetadataVersion, userMetadataSize, blobRecordVersion, blobType, blobStreamSize, storeKey);
            } else {
                logger.trace("Skipping crc check for user metadata and blob stream fields for key {}", storeKey);
                userMetadataVersion = hardDeleteRecoveryMetadata.getUserMetadataVersion();
                blobRecordVersion = hardDeleteRecoveryMetadata.getBlobRecordVersion();
                blobType = hardDeleteRecoveryMetadata.getBlobType();
                userMetadataSize = hardDeleteRecoveryMetadata.getUserMetadataSize();
                blobStreamSize = hardDeleteRecoveryMetadata.getBlobStreamSize();
            }
            HardDeleteMessageFormatInputStream hardDeleteStream = new HardDeleteMessageFormatInputStream(userMetadataRelativeOffset, userMetadataVersion, userMetadataSize, blobRecordVersion, blobType, blobStreamSize);
            hardDeleteInfo = new HardDeleteInfo(Channels.newChannel(hardDeleteStream), hardDeleteStream.getSize(), hardDeleteStream.getHardDeleteStreamRelativeOffset(), hardDeleteRecoveryMetadata.toBytes());
        }
    } catch (Exception e) {
        logger.error("Exception when reading blob: ", e);
    }
    return hardDeleteInfo;
}
Also used : DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer) StoreKey(com.github.ambry.store.StoreKey) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) ByteBufferOutputStream(com.github.ambry.utils.ByteBufferOutputStream) HardDeleteInfo(com.github.ambry.store.HardDeleteInfo)

Example 2 with HardDeleteInfo

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

the class BlobStoreHardDeleteTest method blobStoreHardDeleteTestUtil.

private void blobStoreHardDeleteTestUtil(short[] blobVersions, BlobType[] blobTypes) throws MessageFormatException, IOException {
    MessageStoreHardDelete hardDelete = new BlobStoreHardDelete();
    StoreKeyFactory keyFactory = new MockIdFactory();
    // create log and write to it
    ReadImp readImp = new ReadImp();
    ArrayList<Long> msgOffsets = readImp.initialize(blobVersions, blobTypes);
    Iterator<HardDeleteInfo> iter = hardDelete.getHardDeleteMessages(readImp.getMessageReadSet(), keyFactory, readImp.getRecoveryInfoList());
    List<HardDeleteInfo> hardDeletedList = new ArrayList<HardDeleteInfo>();
    while (iter.hasNext()) {
        hardDeletedList.add(iter.next());
    }
    // msg1
    HardDeleteInfo hardDeleteInfo = hardDeletedList.get(0);
    Assert.assertNotNull(hardDeleteInfo);
    HardDeleteRecoveryMetadata hardDeleteRecoveryMetadata = new HardDeleteRecoveryMetadata(hardDeleteInfo.getRecoveryInfo(), keyFactory);
    Assert.assertEquals(blobTypes[1], hardDeleteRecoveryMetadata.getBlobType());
    Assert.assertEquals(blobVersions[1], hardDeleteRecoveryMetadata.getBlobRecordVersion());
    // msg2
    hardDeleteInfo = hardDeletedList.get(1);
    Assert.assertNotNull(hardDeleteInfo);
    hardDeleteRecoveryMetadata = new HardDeleteRecoveryMetadata(hardDeleteInfo.getRecoveryInfo(), keyFactory);
    Assert.assertEquals(blobTypes[2], hardDeleteRecoveryMetadata.getBlobType());
    Assert.assertEquals(blobVersions[2], hardDeleteRecoveryMetadata.getBlobRecordVersion());
    // msg5
    hardDeleteInfo = hardDeletedList.get(2);
    Assert.assertNotNull(hardDeleteInfo);
    hardDeleteRecoveryMetadata = new HardDeleteRecoveryMetadata(hardDeleteInfo.getRecoveryInfo(), keyFactory);
    Assert.assertEquals(blobTypes[3], hardDeleteRecoveryMetadata.getBlobType());
    Assert.assertEquals(blobVersions[3], hardDeleteRecoveryMetadata.getBlobRecordVersion());
    // msg6 - NULL.
    Assert.assertNull(hardDeletedList.get(3));
}
Also used : StoreKeyFactory(com.github.ambry.store.StoreKeyFactory) MockIdFactory(com.github.ambry.store.MockIdFactory) HardDeleteInfo(com.github.ambry.store.HardDeleteInfo) ArrayList(java.util.ArrayList) MessageStoreHardDelete(com.github.ambry.store.MessageStoreHardDelete)

Aggregations

HardDeleteInfo (com.github.ambry.store.HardDeleteInfo)2 MessageStoreHardDelete (com.github.ambry.store.MessageStoreHardDelete)1 MockIdFactory (com.github.ambry.store.MockIdFactory)1 StoreKey (com.github.ambry.store.StoreKey)1 StoreKeyFactory (com.github.ambry.store.StoreKeyFactory)1 ByteBufferOutputStream (com.github.ambry.utils.ByteBufferOutputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1