@Override public int getMessageLength() { return messageLength(buffer.getInt(lengthOffset(fragmentOffset), Protocol.ENDIANNESS)); }
@Override public int getMessageLength() { return messageLength(buffer.getInt(lengthOffset(fragmentOffset), Protocol.ENDIANNESS)); }
public static int getFragmentLength(final DirectBuffer buffer, final int offset) { return alignedLength(buffer.getInt(lengthOffset(offset), Protocol.ENDIANNESS)); }
/** Commit the fragment so that it can be read by subscriptions. */ public void commit() { // commit the message by writing the positive framed length buffer.putIntOrdered(lengthOffset(0), buffer.capacity()); onCompleteHandler.run(); reset(); }
public static int getFragmentLength(final DirectBuffer buffer, final int offset) { return alignedLength(buffer.getInt(lengthOffset(offset), Protocol.ENDIANNESS)); }
/** Commit the fragment so that it can be read by subscriptions. */ public void commit() { // commit the message by writing the positive framed length buffer.putIntOrdered(lengthOffset(0), buffer.capacity()); onCompleteHandler.run(); reset(); }
/** * Commit all fragments of the batch and mark them as failed. They will be ignored by * subscriptions. */ public void abort() { // discard all fragments by set the type to padding int fragmentOffset = 0; while (fragmentOffset < nextOffset) { final int fragmentLength = -buffer.getInt(lengthOffset(fragmentOffset)); buffer.putInt(typeOffset(fragmentOffset), TYPE_PADDING); buffer.putIntOrdered(lengthOffset(fragmentOffset), fragmentLength); fragmentOffset += DataFrameDescriptor.alignedLength(fragmentLength); } fillRemainingBatchSize(); onCompleteHandler.run(); reset(); }
/** Commit the fragment and mark it as failed. It will be ignored by subscriptions. */ public void abort() { // abort the message by setting type to padding and writing the positive framed length buffer.putInt(typeOffset(0), TYPE_PADDING); buffer.putIntOrdered(lengthOffset(0), buffer.capacity()); onCompleteHandler.run(); reset(); }
/** Commit the fragment and mark it as failed. It will be ignored by subscriptions. */ public void abort() { // abort the message by setting type to padding and writing the positive framed length buffer.putInt(typeOffset(0), TYPE_PADDING); buffer.putIntOrdered(lengthOffset(0), buffer.capacity()); onCompleteHandler.run(); reset(); }
private void fillRemainingBatchSize() { // since the claimed batch size can be longer than the written fragment // size, we need to fill the rest with a padding fragment final int remainingLength = buffer.capacity() - nextOffset; if (remainingLength >= HEADER_LENGTH) { buffer.putInt(lengthOffset(nextOffset), remainingLength); buffer.putShort(typeOffset(nextOffset), TYPE_PADDING); } }
/** * Commit all fragments of the batch and mark them as failed. They will be ignored by * subscriptions. */ public void abort() { // discard all fragments by set the type to padding int fragmentOffset = 0; while (fragmentOffset < nextOffset) { final int fragmentLength = -buffer.getInt(lengthOffset(fragmentOffset)); buffer.putInt(typeOffset(fragmentOffset), TYPE_PADDING); buffer.putIntOrdered(lengthOffset(fragmentOffset), fragmentLength); fragmentOffset += DataFrameDescriptor.alignedLength(fragmentLength); } fillRemainingBatchSize(); onCompleteHandler.run(); reset(); }
@SuppressWarnings("restriction") protected int onEndOfPartition(final LogBufferPartition partition, final int partitionOffset) { int newTail = RESULT_END_OF_PARTITION; final int padLength = partition.getPartitionSize() - partitionOffset; if (padLength >= HEADER_LENGTH) { // this message tripped the end of the partition, fill buffer with padding final UnsafeBuffer buffer = partition.getDataBuffer(); buffer.putIntOrdered(lengthOffset(partitionOffset), -padLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(partitionOffset), TYPE_PADDING); buffer.putIntOrdered(lengthOffset(partitionOffset), padLength); newTail = RESULT_PADDING_AT_END_OF_PARTITION; } return newTail; } }
private void fillRemainingBatchSize() { // since the claimed batch size can be longer than the written fragment // size, we need to fill the rest with a padding fragment final int remainingLength = buffer.capacity() - nextOffset; if (remainingLength >= HEADER_LENGTH) { buffer.putInt(lengthOffset(nextOffset), remainingLength); buffer.putShort(typeOffset(nextOffset), TYPE_PADDING); } }
@SuppressWarnings("restriction") protected int onEndOfPartition(final LogBufferPartition partition, final int partitionOffset) { int newTail = RESULT_END_OF_PARTITION; final int padLength = partition.getPartitionSize() - partitionOffset; if (padLength >= HEADER_LENGTH) { // this message tripped the end of the partition, fill buffer with padding final UnsafeBuffer buffer = partition.getDataBuffer(); buffer.putIntOrdered(lengthOffset(partitionOffset), -padLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(partitionOffset), TYPE_PADDING); buffer.putIntOrdered(lengthOffset(partitionOffset), padLength); newTail = RESULT_PADDING_AT_END_OF_PARTITION; } return newTail; } }
@Override public DirectBuffer next() { final int framedFragmentLength = bufferView.getInt(DataFrameDescriptor.lengthOffset(iterationOffset)); final int fragmentLength = DataFrameDescriptor.messageLength(framedFragmentLength); final int messageOffset = DataFrameDescriptor.messageOffset(iterationOffset); buffer.wrap(bufferView, messageOffset, fragmentLength); iterationOffset += DataFrameDescriptor.alignedLength(framedFragmentLength); return buffer; } }
@Override public DirectBuffer next() { final int framedFragmentLength = bufferView.getInt(DataFrameDescriptor.lengthOffset(iterationOffset)); final int fragmentLength = DataFrameDescriptor.messageLength(framedFragmentLength); final int messageOffset = DataFrameDescriptor.messageOffset(iterationOffset); buffer.wrap(bufferView, messageOffset, fragmentLength); iterationOffset += DataFrameDescriptor.alignedLength(framedFragmentLength); return buffer; } }
/** Commit all fragments of the batch so that it can be read by subscriptions. */ public void commit() { final int firstFragmentFramedLength = -buffer.getInt(lengthOffset(FIRST_FRAGMENT_OFFSET)); // do not set batch flags if only one fragment in the batch if (currentOffset > 0) { // set batch begin flag final byte firstFragmentFlags = buffer.getByte(flagsOffset(FIRST_FRAGMENT_OFFSET)); buffer.putByte(flagsOffset(FIRST_FRAGMENT_OFFSET), enableFlagBatchBegin(firstFragmentFlags)); // set positive length => commit fragment int fragmentOffset = DataFrameDescriptor.alignedLength(firstFragmentFramedLength); while (fragmentOffset < nextOffset) { final int fragmentFramedLength = -buffer.getInt(lengthOffset(fragmentOffset)); buffer.putInt(lengthOffset(fragmentOffset), fragmentFramedLength); fragmentOffset += DataFrameDescriptor.alignedLength(fragmentFramedLength); } // set batch end flag final byte lastFragmentFlags = buffer.getByte(flagsOffset(currentOffset)); buffer.putByte(flagsOffset(currentOffset), enableFlagBatchEnd(lastFragmentFlags)); } fillRemainingBatchSize(); // commit the first fragment at the end so that the batch can be read at // once buffer.putIntOrdered(lengthOffset(FIRST_FRAGMENT_OFFSET), firstFragmentFramedLength); onCompleteHandler.run(); reset(); }
/** * Finish reading and consume the fragments (i.e. update the subscription position). Mark all * fragments as failed. */ public void markFailed() { int fragmentOffset = 0; while (fragmentOffset < blockLength) { int framedFragmentLength = bufferView.getInt(DataFrameDescriptor.lengthOffset(fragmentOffset)); if (framedFragmentLength < 0) { framedFragmentLength = -framedFragmentLength; } final int frameLength = DataFrameDescriptor.alignedLength(framedFragmentLength); final int flagsOffset = DataFrameDescriptor.flagsOffset(fragmentOffset); final byte flags = bufferView.getByte(flagsOffset); bufferView.putByte(flagsOffset, DataFrameDescriptor.enableFlagFailed(flags)); fragmentOffset += frameLength; } updatePosition(); }
/** * Finish reading and consume the fragments (i.e. update the subscription position). Mark all * fragments as failed. */ public void markFailed() { int fragmentOffset = 0; while (fragmentOffset < blockLength) { int framedFragmentLength = bufferView.getInt(DataFrameDescriptor.lengthOffset(fragmentOffset)); if (framedFragmentLength < 0) { framedFragmentLength = -framedFragmentLength; } final int frameLength = DataFrameDescriptor.alignedLength(framedFragmentLength); final int flagsOffset = DataFrameDescriptor.flagsOffset(fragmentOffset); final byte flags = bufferView.getByte(flagsOffset); bufferView.putByte(flagsOffset, DataFrameDescriptor.enableFlagFailed(flags)); fragmentOffset += frameLength; } updatePosition(); }
@SuppressWarnings("restriction") public int claim( final LogBufferPartition partition, final int activePartitionId, final ClaimedFragment claim, final int length, final int streamId, Runnable onComplete) { final int partitionSize = partition.getPartitionSize(); final int framedMessageLength = framedLength(length); final int alignedFrameLength = alignedLength(framedMessageLength); // move the tail of the partition final int frameOffset = partition.getAndAddTail(alignedFrameLength); int newTail = frameOffset + alignedFrameLength; if (newTail <= (partitionSize - HEADER_LENGTH)) { final UnsafeBuffer buffer = partition.getDataBuffer(); // write negative length field buffer.putIntOrdered(lengthOffset(frameOffset), -framedMessageLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(frameOffset), TYPE_MESSAGE); buffer.putInt(streamIdOffset(frameOffset), streamId); claim.wrap(buffer, frameOffset, framedMessageLength, onComplete); // Do not commit the message } else { newTail = onEndOfPartition(partition, frameOffset); } return newTail; }