Search in sources :

Example 21 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class NetworkPublication method resend.

public void resend(final int termId, final int termOffset, final int length) {
    final long senderPosition = this.senderPosition.get();
    final long resendPosition = computePosition(termId, termOffset, positionBitsToShift, initialTermId);
    if (resendPosition < senderPosition && resendPosition >= (senderPosition - rawLog.termLength())) {
        final int activeIndex = indexByPosition(resendPosition, positionBitsToShift);
        final UnsafeBuffer termBuffer = termBuffers[activeIndex];
        final ByteBuffer sendBuffer = sendBuffers[activeIndex];
        int remainingBytes = length;
        int bytesSent = 0;
        int offset = termOffset;
        do {
            offset += bytesSent;
            final long scanOutcome = scanForAvailability(termBuffer, offset, mtuLength);
            final int available = available(scanOutcome);
            if (available <= 0) {
                break;
            }
            sendBuffer.limit(offset + available).position(offset);
            if (available != channelEndpoint.send(sendBuffer)) {
                shortSends.increment();
                break;
            }
            bytesSent = available + padding(scanOutcome);
            remainingBytes -= bytesSent;
        } while (remainingBytes > 0);
        retransmitsSent.orderedIncrement();
    }
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer) SendChannelEndpoint(io.aeron.driver.media.SendChannelEndpoint)

Example 22 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class TermAppenderTest method shouldAppendFrameToEmptyLog.

@Test
public void shouldAppendFrameToEmptyLog() {
    final int headerLength = DEFAULT_HEADER.capacity();
    final UnsafeBuffer buffer = new UnsafeBuffer(new byte[128]);
    final int msgLength = 20;
    final int frameLength = msgLength + headerLength;
    final int alignedFrameLength = align(frameLength, FRAME_ALIGNMENT);
    final int tail = 0;
    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, pack(TERM_ID, tail));
    assertThat(termAppender.appendUnfragmentedMessage(headerWriter, buffer, 0, msgLength, RVS), is((long) alignedFrameLength));
    assertThat(rawTailVolatile(logMetaDataBuffer, PARTITION_INDEX), is(pack(TERM_ID, tail + alignedFrameLength)));
    final InOrder inOrder = inOrder(termBuffer, headerWriter);
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength, buffer, 0, msgLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength);
}
Also used : InOrder(org.mockito.InOrder) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 23 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class TermAppenderTest method shouldPadLogAndTripWhenAppendingWithInsufficientRemainingCapacity.

@Test
public void shouldPadLogAndTripWhenAppendingWithInsufficientRemainingCapacity() {
    final int msgLength = 120;
    final int headerLength = DEFAULT_HEADER.capacity();
    final int requiredFrameSize = align(headerLength + msgLength, FRAME_ALIGNMENT);
    final int tailValue = TERM_BUFFER_LENGTH - align(msgLength, FRAME_ALIGNMENT);
    final UnsafeBuffer buffer = new UnsafeBuffer(new byte[128]);
    final int frameLength = TERM_BUFFER_LENGTH - tailValue;
    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, pack(TERM_ID, tailValue));
    final long expectResult = pack(TERM_ID, TRIPPED);
    assertThat(termAppender.appendUnfragmentedMessage(headerWriter, buffer, 0, msgLength, RVS), is(expectResult));
    assertThat(rawTailVolatile(logMetaDataBuffer, PARTITION_INDEX), is(pack(TERM_ID, tailValue + requiredFrameSize)));
    final InOrder inOrder = inOrder(termBuffer, headerWriter);
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tailValue, frameLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putShort(typeOffset(tailValue), (short) PADDING_FRAME_TYPE, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tailValue, frameLength);
}
Also used : InOrder(org.mockito.InOrder) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 24 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class TermRebuilderTest method shouldFillSingleGap.

@Test
public void shouldFillSingleGap() {
    final int frameLength = 50;
    final int alignedFrameLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final int tail = alignedFrameLength;
    final int termOffset = tail;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(alignedFrameLength));
    TermRebuilder.insert(termBuffer, termOffset, packet, alignedFrameLength);
    verify(termBuffer).putBytes(tail + HEADER_LENGTH, packet, srcOffset + HEADER_LENGTH, alignedFrameLength - HEADER_LENGTH);
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Example 25 with UnsafeBuffer

use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.

the class TermRebuilderTest method shouldFillAfterAGap.

@Test
public void shouldFillAfterAGap() {
    final int frameLength = 50;
    final int alignedFrameLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(alignedFrameLength));
    final int termOffset = alignedFrameLength * 2;
    TermRebuilder.insert(termBuffer, termOffset, packet, alignedFrameLength);
    verify(termBuffer).putBytes((alignedFrameLength * 2) + HEADER_LENGTH, packet, srcOffset + HEADER_LENGTH, alignedFrameLength - HEADER_LENGTH);
}
Also used : UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.Test)

Aggregations

UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)78 Test (org.junit.Test)42 StatusMessageFlyweight (io.aeron.protocol.StatusMessageFlyweight)8 MediaDriver (io.aeron.driver.MediaDriver)7 MutableDirectBuffer (org.agrona.MutableDirectBuffer)6 Header (io.aeron.logbuffer.Header)5 InOrder (org.mockito.InOrder)5 MappedByteBuffer (java.nio.MappedByteBuffer)4 Theory (org.junit.experimental.theories.Theory)4 ReceiveChannelEndpoint (io.aeron.driver.media.ReceiveChannelEndpoint)3 File (java.io.File)3 Date (java.util.Date)3 DirectBuffer (org.agrona.DirectBuffer)3 Before (org.junit.Before)3 DataPoint (org.junit.experimental.theories.DataPoint)3 SendChannelEndpoint (io.aeron.driver.media.SendChannelEndpoint)2 BufferClaim (io.aeron.logbuffer.BufferClaim)2 FragmentHandler (io.aeron.logbuffer.FragmentHandler)2 PrintStream (java.io.PrintStream)2 InetSocketAddress (java.net.InetSocketAddress)2