use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class ReplicationTestHelper method createPutMessage.
/**
* Constructs an entire message with header, blob properties, user metadata and blob content.
* @param id id for which the message has to be constructed.
* @param accountId accountId of the blob
* @param containerId containerId of the blob
* @param enableEncryption {@code true} if encryption needs to be enabled. {@code false} otherwise
* @param lifeVersion lifeVersion for this hich the message has to be constructed.
* @return a {@link Pair} of {@link ByteBuffer} and {@link MessageInfo} representing the entire message and the
* associated {@link MessageInfo}
* @throws MessageFormatException
* @throws IOException
*/
public static PutMsgInfoAndBuffer createPutMessage(StoreKey id, short accountId, short containerId, boolean enableEncryption, short lifeVersion) throws MessageFormatException, IOException {
Random blobIdRandom = new Random(id.getID().hashCode());
int blobSize = blobIdRandom.nextInt(500) + 501;
int userMetadataSize = blobIdRandom.nextInt(blobSize / 2);
int encryptionKeySize = blobIdRandom.nextInt(blobSize / 4);
byte[] blob = new byte[blobSize];
byte[] usermetadata = new byte[userMetadataSize];
byte[] encryptionKey = enableEncryption ? new byte[encryptionKeySize] : null;
blobIdRandom.nextBytes(blob);
blobIdRandom.nextBytes(usermetadata);
BlobProperties blobProperties = new BlobProperties(blobSize, "test", null, null, false, EXPIRY_TIME_MS - CONSTANT_TIME_MS, CONSTANT_TIME_MS, accountId, containerId, encryptionKey != null, null, null, null);
MessageFormatInputStream stream = new PutMessageFormatInputStream(id, encryptionKey == null ? null : ByteBuffer.wrap(encryptionKey), blobProperties, ByteBuffer.wrap(usermetadata), new ByteBufferInputStream(ByteBuffer.wrap(blob)), blobSize, BlobType.DataBlob, lifeVersion);
byte[] message = Utils.readBytesFromStream(stream, (int) stream.getSize());
return new PutMsgInfoAndBuffer(ByteBuffer.wrap(message), new MessageInfo(id, message.length, false, false, false, EXPIRY_TIME_MS, null, accountId, containerId, CONSTANT_TIME_MS, lifeVersion));
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class GCMCryptoService method decrypt.
@Override
public ByteBuffer decrypt(ByteBuffer toDecrypt, SecretKeySpec key) throws GeneralSecurityException {
try {
Cipher decrypter = Cipher.getInstance(GCM_CRYPTO_INSTANCE, "BC");
byte[] iv = deserializeIV(new ByteBufferInputStream(toDecrypt));
decrypter.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
ByteBuffer decryptedContent = ByteBuffer.allocate(decrypter.getOutputSize(toDecrypt.remaining()));
decrypter.doFinal(toDecrypt, decryptedContent);
decryptedContent.flip();
return decryptedContent;
} catch (Exception e) {
throw new GeneralSecurityException("Exception thrown while decrypting data", e);
}
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class MockReadableStreamChannel method deserializePutRequest.
/**
* Deserialize a {@link PutRequest} from the given serialized ByteBuffer.
* @param serialized the serialized ByteBuffer.
* @return returns the deserialized output.
*/
private PutRequest deserializePutRequest(ByteBuffer serialized) throws IOException {
serialized.getLong();
serialized.getShort();
return PutRequest.readFrom(new DataInputStream(new ByteBufferInputStream(serialized)), mockClusterMap);
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class StoredBlob method send.
/**
* Take in a request in the form of {@link Send} and return a response in the form of a
* {@link BoundedNettyByteBufReceive}.
* @param send the request.
* @return the response.
* @throws IOException if there was an error in interpreting the request.
*/
public BoundedNettyByteBufReceive send(Send send) throws IOException {
if (!shouldRespond) {
return null;
}
ServerErrorCode serverError = hardError != null ? hardError : serverErrors.size() > 0 ? serverErrors.poll() : ServerErrorCode.No_Error;
RequestOrResponseType type = ((RequestOrResponse) send).getRequestType();
RequestOrResponse response;
requestCounts.computeIfAbsent(type, k -> new LongAdder()).increment();
switch(type) {
case PutRequest:
response = makePutResponse((PutRequest) send, serverError);
break;
case GetRequest:
response = makeGetResponse((GetRequest) send, serverError);
break;
case DeleteRequest:
response = makeDeleteResponse((DeleteRequest) send, serverError);
break;
case TtlUpdateRequest:
response = makeTtlUpdateResponse((TtlUpdateRequest) send, serverError);
break;
case UndeleteRequest:
response = makeUndeleteResponse((UndeleteRequest) send, serverError);
break;
default:
throw new IOException("Unknown request type received");
}
ByteBufferChannel channel = new ByteBufferChannel(ByteBuffer.allocate((int) response.sizeInBytes()));
response.writeTo(channel);
response.release();
ByteBuffer payload = channel.getBuffer();
payload.flip();
BoundedNettyByteBufReceive receive = new BoundedNettyByteBufReceive(100 * 1024 * 1024);
receive.readFrom(Channels.newChannel(new ByteBufferInputStream(payload)));
return receive;
}
use of com.github.ambry.utils.ByteBufferInputStream in project ambry by linkedin.
the class AmbryServerRequestsTest method verifyUndelete.
/**
* Verifies that the UNDELETE request was delivered to the {@link Store} correctly.
* @param blobId the {@link BlobId} that was updated
* @param opTimeMs the op time (in ms)
* @param messageWriteSet the {@link MessageWriteSet} received at the {@link Store}
* @throws IOException
* @throws MessageFormatException
* @throws StoreException
*/
private void verifyUndelete(BlobId blobId, long opTimeMs, MessageWriteSet messageWriteSet) throws Exception {
BlobId key = (BlobId) conversionMap.getOrDefault(blobId, blobId);
assertEquals("There should be one message in the write set", 1, messageWriteSet.getMessageSetInfo().size());
MessageInfo info = messageWriteSet.getMessageSetInfo().get(0);
// verify stream
ByteBuffer record = getDataInWriteSet(messageWriteSet, (int) info.getSize());
int expectedSize = record.remaining();
InputStream stream = new ByteBufferInputStream(record);
// verify stream
MessageFormatInputStreamTest.checkUndeleteMessage(stream, null, key, key.getAccountId(), key.getContainerId(), opTimeMs, storageManager.returnValueOfUndelete);
// verify MessageInfo
// since the record has been verified, the buffer size before verification is a good indicator of size
MessageInfoTest.checkGetters(info, key, expectedSize, false, false, true, Utils.Infinite_Time, null, key.getAccountId(), key.getContainerId(), opTimeMs, storageManager.returnValueOfUndelete);
}
Aggregations