Search in sources :

Example 46 with StoreException

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

the class CloudMessageReadSet method doPrefetch.

@Override
public void doPrefetch(int index, long relativeOffset, long size) throws IOException {
    BlobReadInfo blobReadInfo = blobReadInfoList.get(index);
    try {
        if (!blobReadInfo.isBlobDownloaded()) {
            blobReadInfo.downloadBlob(blobStore);
        }
        ByteBuf byteBuf = blobReadInfoList.get(index).getBlobContent();
        byteBuf.setIndex((int) (relativeOffset), (int) (relativeOffset + size));
    } catch (StoreException ex) {
        throw new IOException("Prefetch of cloud blob " + blobReadInfo.getBlobId().getID() + " failed", ex);
    }
}
Also used : IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) StoreException(com.github.ambry.store.StoreException)

Example 47 with StoreException

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

the class CloudMessageReadSet method writeTo.

@Override
public long writeTo(int index, WritableByteChannel channel, long relativeOffset, long maxSize) throws IOException {
    validateIndex(index);
    long written = 0;
    BlobReadInfo blobReadInfo = blobReadInfoList.get(index);
    String blobIdStr = blobReadInfo.getBlobId().getID();
    try {
        // or at least to prefetch only the header (requires CloudDestination enhancement)
        if (!blobReadInfo.isBlobDownloaded()) {
            blobReadInfo.downloadBlob(blobStore);
        }
        ByteBuf outputBuf = blobReadInfo.getBlobContent().duplicate();
        long sizeToRead = Math.min(maxSize, blobReadInfo.getBlobSize() - relativeOffset);
        outputBuf.setIndex((int) (relativeOffset), (int) (relativeOffset + sizeToRead));
        written = channel.write(outputBuf.nioBuffer());
    } catch (StoreException ex) {
        throw new IOException("Write of cloud blob " + blobIdStr + " failed", ex);
    }
    logger.trace("Downloaded {} bytes to the write channel from the cloud blob : {}", written, blobIdStr);
    return written;
}
Also used : IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) StoreException(com.github.ambry.store.StoreException)

Example 48 with StoreException

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

the class CloudBlobStoreTest method testStoreUndeletes.

/**
 * Test the CloudBlobStore undelete method.
 */
@Test
public void testStoreUndeletes() throws Exception {
    setupCloudStore(false, true, defaultCacheLimit, true);
    long now = System.currentTimeMillis();
    MessageInfo messageInfo = new MessageInfo(getUniqueId(refAccountId, refContainerId, true, partitionId), SMALL_BLOB_SIZE, refAccountId, refContainerId, now, (short) 1);
    when(dest.undeleteBlob(any(BlobId.class), anyShort(), any(CloudUpdateValidator.class))).thenReturn((short) 1);
    store.undelete(messageInfo);
    verify(dest, times(1)).undeleteBlob(any(BlobId.class), anyShort(), any(CloudUpdateValidator.class));
    verifyCacheHits(1, 0);
    // Call second time with same life version. If isVcr, should hit cache this time.
    try {
        store.undelete(messageInfo);
    } catch (StoreException ex) {
        assertEquals(ex.getErrorCode(), StoreErrorCodes.ID_Undeleted);
    }
    int expectedCount = isVcr ? 1 : 2;
    verify(dest, times(expectedCount)).undeleteBlob(any(BlobId.class), eq((short) 1), any(CloudUpdateValidator.class));
    verifyCacheHits(2, 1);
    // Call again with a smaller life version.
    when(dest.undeleteBlob(any(BlobId.class), anyShort(), any(CloudUpdateValidator.class))).thenReturn((short) 0);
    messageInfo = new MessageInfo(messageInfo.getStoreKey(), SMALL_BLOB_SIZE, refAccountId, refContainerId, now, (short) 0);
    try {
        store.undelete(messageInfo);
    } catch (StoreException ex) {
        assertEquals(StoreErrorCodes.ID_Undeleted, ex.getErrorCode());
    }
    expectedCount = isVcr ? 1 : 3;
    verify(dest, times(expectedCount)).undeleteBlob(any(BlobId.class), anyShort(), any(CloudUpdateValidator.class));
    verifyCacheHits(3, 2);
    // Call again with a higher life version. Should not hit cache this time.
    when(dest.undeleteBlob(any(BlobId.class), anyShort(), any(CloudUpdateValidator.class))).thenReturn((short) 2);
    messageInfo = new MessageInfo(messageInfo.getStoreKey(), SMALL_BLOB_SIZE, refAccountId, refContainerId, now, (short) 2);
    store.undelete(messageInfo);
    expectedCount = isVcr ? 2 : 4;
    verify(dest, times(expectedCount)).undeleteBlob(any(BlobId.class), anyShort(), any(CloudUpdateValidator.class));
    verifyCacheHits(4, 2);
    // undelete for a non existent blob.
    setupCloudStore(true, true, defaultCacheLimit, true);
    try {
        store.undelete(messageInfo);
        fail("Undelete for a non existent blob should throw exception");
    } catch (StoreException ex) {
        assertSame(ex.getErrorCode(), StoreErrorCodes.ID_Not_Found);
    }
    // add blob and then undelete should pass
    MockMessageWriteSet messageWriteSet = new MockMessageWriteSet();
    ByteBuffer buffer = ByteBuffer.wrap(TestUtils.getRandomBytes(SMALL_BLOB_SIZE));
    // currently messageInfo.lifeVersion = 2
    messageWriteSet.add(messageInfo, buffer);
    store.put(messageWriteSet);
    assertEquals(store.undelete(messageInfo), 2);
}
Also used : MockMessageWriteSet(com.github.ambry.store.MockMessageWriteSet) BlobId(com.github.ambry.commons.BlobId) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.github.ambry.store.MessageInfo) StoreException(com.github.ambry.store.StoreException) ReplicationTest(com.github.ambry.replication.ReplicationTest) Test(org.junit.Test)

Example 49 with StoreException

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

the class CloudBlobStoreTest method testStoreNotStarted.

/**
 * Test verifying behavior when store not started.
 */
@Test
public void testStoreNotStarted() throws Exception {
    // Create store and don't start it.
    setupCloudStore(false, true, defaultCacheLimit, false);
    List<StoreKey> keys = Collections.singletonList(getUniqueId(refAccountId, refContainerId, false, partitionId));
    MockMessageWriteSet messageWriteSet = new MockMessageWriteSet();
    CloudTestUtil.addBlobToMessageSet(messageWriteSet, 10, Utils.Infinite_Time, refAccountId, refContainerId, true, false, partitionId, operationTime, isVcr);
    try {
        store.put(messageWriteSet);
        fail("Store put should have failed.");
    } catch (StoreException e) {
        assertEquals(StoreErrorCodes.Store_Not_Started, e.getErrorCode());
    }
    try {
        store.delete(messageWriteSet.getMessageSetInfo());
        fail("Store delete should have failed.");
    } catch (StoreException e) {
        assertEquals(StoreErrorCodes.Store_Not_Started, e.getErrorCode());
    }
    try {
        store.findMissingKeys(keys);
        fail("Store findMissingKeys should have failed.");
    } catch (StoreException e) {
        assertEquals(StoreErrorCodes.Store_Not_Started, e.getErrorCode());
    }
}
Also used : MockMessageWriteSet(com.github.ambry.store.MockMessageWriteSet) StoreKey(com.github.ambry.store.StoreKey) StoreException(com.github.ambry.store.StoreException) ReplicationTest(com.github.ambry.replication.ReplicationTest) Test(org.junit.Test)

Example 50 with StoreException

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

the class LatchBasedInMemoryCloudDestination method downloadBlob.

@Override
public void downloadBlob(BlobId blobId, OutputStream outputStream) throws CloudStorageException {
    StoreErrorCodes serverError = hardError != null ? hardError : serverErrors.size() > 0 ? serverErrors.poll() : null;
    if (serverError != null) {
        throw new CloudStorageException("downloadBlob simulated error for blobid :" + blobId, new StoreException("downloadBlob simulated error for blobid :" + blobId, serverError));
    }
    try {
        if (!map.containsKey(blobId)) {
            throw new CloudStorageException("Blob with blobId " + blobId.getID() + " does not exist.");
        }
        byte[] blobData = map.get(blobId).getSecond();
        outputStream.write(blobData);
    } catch (IOException ex) {
        throw new CloudStorageException("Could not download blob for blobid " + blobId.getID() + " due to " + ex.toString());
    }
    downloadLatch.countDown();
}
Also used : IOException(java.io.IOException) StoreErrorCodes(com.github.ambry.store.StoreErrorCodes) StoreException(com.github.ambry.store.StoreException)

Aggregations

StoreException (com.github.ambry.store.StoreException)59 MessageInfo (com.github.ambry.store.MessageInfo)31 IOException (java.io.IOException)22 ArrayList (java.util.ArrayList)17 BlobId (com.github.ambry.commons.BlobId)16 Store (com.github.ambry.store.Store)16 StoreErrorCodes (com.github.ambry.store.StoreErrorCodes)12 StoreKey (com.github.ambry.store.StoreKey)11 MessageFormatException (com.github.ambry.messageformat.MessageFormatException)10 MockMessageWriteSet (com.github.ambry.store.MockMessageWriteSet)10 DataInputStream (java.io.DataInputStream)10 MessageFormatWriteSet (com.github.ambry.messageformat.MessageFormatWriteSet)9 IdUndeletedStoreException (com.github.ambry.store.IdUndeletedStoreException)8 StoreGetOptions (com.github.ambry.store.StoreGetOptions)8 Test (org.junit.Test)8 PartitionId (com.github.ambry.clustermap.PartitionId)7 MessageFormatInputStream (com.github.ambry.messageformat.MessageFormatInputStream)7 StoreInfo (com.github.ambry.store.StoreInfo)7 DeleteMessageFormatInputStream (com.github.ambry.messageformat.DeleteMessageFormatInputStream)6 ServerNetworkResponseMetrics (com.github.ambry.network.ServerNetworkResponseMetrics)6