Search in sources :

Example 61 with MutableInteger

use of org.agrona.collections.MutableInteger in project aeron by real-logic.

the class SelectorAndTransportTest method shouldHandleSmFrameFromReceiverToSender.

@Test
@InterruptAfter(10)
public void shouldHandleSmFrameFromReceiverToSender() {
    final MutableInteger controlMessagesReceived = new MutableInteger(0);
    doAnswer((invocation) -> {
        controlMessagesReceived.value++;
        return null;
    }).when(mockPublication).onStatusMessage(any(), any());
    receiveChannelEndpoint = new ReceiveChannelEndpoint(RCV_DST, mockDispatcher, mockReceiveStatusIndicator, context);
    sendChannelEndpoint = new SendChannelEndpoint(SRC_DST, mockSendStatusIndicator, context);
    sendChannelEndpoint.registerForSend(mockPublication);
    receiveChannelEndpoint.openDatagramChannel(mockReceiveStatusIndicator);
    receiveChannelEndpoint.registerForRead(dataTransportPoller);
    sendChannelEndpoint.openDatagramChannel(mockSendStatusIndicator);
    sendChannelEndpoint.registerForRead(controlTransportPoller);
    statusMessage.wrap(buffer);
    statusMessage.streamId(STREAM_ID).sessionId(SESSION_ID).consumptionTermId(TERM_ID).receiverWindowLength(1000).consumptionTermOffset(0).version(HeaderFlyweight.CURRENT_VERSION).flags((short) 0).headerType(HeaderFlyweight.HDR_TYPE_SM).frameLength(StatusMessageFlyweight.HEADER_LENGTH);
    byteBuffer.position(0).limit(statusMessage.frameLength());
    processLoop(dataTransportPoller, 5);
    receiveChannelEndpoint.sendTo(byteBuffer, rcvRemoteAddress);
    while (controlMessagesReceived.get() < 1) {
        processLoop(controlTransportPoller, 1);
    }
    verify(mockStatusMessagesReceivedCounter, times(1)).incrementOrdered();
}
Also used : MutableInteger(org.agrona.collections.MutableInteger) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 62 with MutableInteger

use of org.agrona.collections.MutableInteger in project zeebe by zeebe-io.

the class JobBatchCollector method collectJobs.

/**
 * Collects jobs to be added to the given {@code record}. The jobs and their keys are added
 * directly to the given record.
 *
 * <p>This method will fail only if it could not activate anything because the batch would be too
 * large, but there was at least one job to activate. On failure, it will return that job and its
 * key. On success, it will return the amount of jobs activated.
 *
 * @param record the batch activate command; jobs and their keys will be added directly into it
 * @return the amount of activated jobs on success, or a job which was too large to activate
 */
Either<TooLargeJob, Integer> collectJobs(final TypedRecord<JobBatchRecord> record) {
    final JobBatchRecord value = record.getValue();
    final ValueArray<JobRecord> jobIterator = value.jobs();
    final ValueArray<LongValue> jobKeyIterator = value.jobKeys();
    final Collection<DirectBuffer> requestedVariables = collectVariableNames(value);
    final var maxActivatedCount = value.getMaxJobsToActivate();
    final var activatedCount = new MutableInteger(0);
    final var jobCopyBuffer = new ExpandableArrayBuffer();
    final var unwritableJob = new MutableReference<TooLargeJob>();
    jobState.forEachActivatableJobs(value.getTypeBuffer(), (key, jobRecord) -> {
        // fill in the job record properties first in order to accurately estimate its size before
        // adding it to the batch
        final var deadline = record.getTimestamp() + value.getTimeout();
        jobRecord.setDeadline(deadline).setWorker(value.getWorkerBuffer());
        setJobVariables(requestedVariables, jobRecord, jobRecord.getElementInstanceKey());
        // the expected length is based on the current record's length plus the length of the job
        // record we would add to the batch, the number of bytes taken by the additional job key,
        // as well as one byte required per job key for its type header. if we ever add more, this
        // should be updated accordingly.
        final var jobRecordLength = jobRecord.getLength();
        final var expectedEventLength = record.getLength() + jobRecordLength + Long.BYTES + 1;
        if (activatedCount.value <= maxActivatedCount && canWriteEventOfLength.test(expectedEventLength)) {
            appendJobToBatch(jobIterator, jobKeyIterator, jobCopyBuffer, key, jobRecord);
            activatedCount.increment();
        } else {
            // activate it
            if (activatedCount.value == 0) {
                unwritableJob.set(new TooLargeJob(key, jobRecord));
            }
            value.setTruncated(true);
            return false;
        }
        return activatedCount.value < maxActivatedCount;
    });
    if (unwritableJob.ref != null) {
        return Either.left(unwritableJob.ref);
    }
    return Either.right(activatedCount.value);
}
Also used : DirectBuffer(org.agrona.DirectBuffer) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord) MutableInteger(org.agrona.collections.MutableInteger) LongValue(io.camunda.zeebe.msgpack.value.LongValue) MutableReference(org.agrona.collections.MutableReference) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord)

Example 63 with MutableInteger

use of org.agrona.collections.MutableInteger in project zeebe by zeebe-io.

the class DbVariableState method getVariablesLocalAsDocument.

@Override
public DirectBuffer getVariablesLocalAsDocument(final long scopeKey) {
    writer.wrap(documentResultBuffer, 0);
    writer.reserveMapHeader();
    final MutableInteger variableCount = new MutableInteger();
    visitVariablesLocal(scopeKey, name -> true, (name, value) -> {
        writer.writeString(name.getBuffer());
        writer.writeRaw(value.getValue());
        variableCount.addAndGet(1);
    }, () -> false);
    writer.writeReservedMapHeader(0, variableCount.get());
    resultView.wrap(documentResultBuffer, 0, writer.getOffset());
    return resultView;
}
Also used : MutableInteger(org.agrona.collections.MutableInteger)

Example 64 with MutableInteger

use of org.agrona.collections.MutableInteger in project zeebe by camunda-cloud.

the class JobBatchCollector method collectJobs.

/**
 * Collects jobs to be added to the given {@code record}. The jobs and their keys are added
 * directly to the given record.
 *
 * <p>This method will fail only if it could not activate anything because the batch would be too
 * large, but there was at least one job to activate. On failure, it will return that job and its
 * key. On success, it will return the amount of jobs activated.
 *
 * @param record the batch activate command; jobs and their keys will be added directly into it
 * @return the amount of activated jobs on success, or a job which was too large to activate
 */
Either<TooLargeJob, Integer> collectJobs(final TypedRecord<JobBatchRecord> record) {
    final JobBatchRecord value = record.getValue();
    final ValueArray<JobRecord> jobIterator = value.jobs();
    final ValueArray<LongValue> jobKeyIterator = value.jobKeys();
    final Collection<DirectBuffer> requestedVariables = collectVariableNames(value);
    final var maxActivatedCount = value.getMaxJobsToActivate();
    final var activatedCount = new MutableInteger(0);
    final var jobCopyBuffer = new ExpandableArrayBuffer();
    final var unwritableJob = new MutableReference<TooLargeJob>();
    jobState.forEachActivatableJobs(value.getTypeBuffer(), (key, jobRecord) -> {
        // fill in the job record properties first in order to accurately estimate its size before
        // adding it to the batch
        final var deadline = record.getTimestamp() + value.getTimeout();
        jobRecord.setDeadline(deadline).setWorker(value.getWorkerBuffer());
        setJobVariables(requestedVariables, jobRecord, jobRecord.getElementInstanceKey());
        // the expected length is based on the current record's length plus the length of the job
        // record we would add to the batch, the number of bytes taken by the additional job key,
        // as well as one byte required per job key for its type header. if we ever add more, this
        // should be updated accordingly.
        final var jobRecordLength = jobRecord.getLength();
        final var expectedEventLength = record.getLength() + jobRecordLength + Long.BYTES + 1;
        if (activatedCount.value <= maxActivatedCount && canWriteEventOfLength.test(expectedEventLength)) {
            appendJobToBatch(jobIterator, jobKeyIterator, jobCopyBuffer, key, jobRecord);
            activatedCount.increment();
        } else {
            // activate it
            if (activatedCount.value == 0) {
                unwritableJob.set(new TooLargeJob(key, jobRecord));
            }
            value.setTruncated(true);
            return false;
        }
        return activatedCount.value < maxActivatedCount;
    });
    if (unwritableJob.ref != null) {
        return Either.left(unwritableJob.ref);
    }
    return Either.right(activatedCount.value);
}
Also used : DirectBuffer(org.agrona.DirectBuffer) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord) MutableInteger(org.agrona.collections.MutableInteger) LongValue(io.camunda.zeebe.msgpack.value.LongValue) MutableReference(org.agrona.collections.MutableReference) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord)

Example 65 with MutableInteger

use of org.agrona.collections.MutableInteger in project agrona by real-logic.

the class ManyToOneRingBufferTest method shouldCommitOnEachMessage.

@Test
public void shouldCommitOnEachMessage() {
    final String msg = "Hello World";
    final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
    final int srcLength = srcBuffer.putStringAscii(0, msg);
    final RingBuffer ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
    final MutableInteger counter = new MutableInteger();
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
    final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
        if (0 == counter.getAndIncrement()) {
            assertEquals(0L, ringBuffer.consumerPosition());
        } else {
            assertEquals(ringBuffer.producerPosition() / 2, ringBuffer.consumerPosition());
        }
        return ControlledMessageHandler.Action.COMMIT;
    };
    final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler);
    assertEquals(2, counter.get());
    assertEquals(2, messagesRead);
    assertEquals(ringBuffer.producerPosition(), ringBuffer.consumerPosition());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RingBufferDescriptor(org.agrona.concurrent.ringbuffer.RingBufferDescriptor) IntConsumer(java.util.function.IntConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) RecordDescriptor(org.agrona.concurrent.ringbuffer.RecordDescriptor) MessageHandler(org.agrona.concurrent.MessageHandler) MIN_CAPACITY(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer.MIN_CAPACITY) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) MutableInteger(org.agrona.collections.MutableInteger) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) InOrder(org.mockito.InOrder) PADDING_MSG_TYPE_ID(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer.PADDING_MSG_TYPE_ID) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Arguments(org.junit.jupiter.params.provider.Arguments) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) INSUFFICIENT_CAPACITY(org.agrona.concurrent.ringbuffer.RingBuffer.INSUFFICIENT_CAPACITY) Assertions(org.junit.jupiter.api.Assertions) Matchers.is(org.hamcrest.Matchers.is) TRUE(java.lang.Boolean.TRUE) BitUtil.align(org.agrona.BitUtil.align) ControlledMessageHandler(org.agrona.concurrent.ControlledMessageHandler) MutableInteger(org.agrona.collections.MutableInteger) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

MutableInteger (org.agrona.collections.MutableInteger)151 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)76 DirectBuffer (org.agrona.DirectBuffer)64 Test (org.junit.jupiter.api.Test)61 InterruptAfter (io.aeron.test.InterruptAfter)52 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)42 MethodSource (org.junit.jupiter.params.provider.MethodSource)38 MediaDriver (io.aeron.driver.MediaDriver)34 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)34 Test (org.junit.Test)33 ByteBuffer (java.nio.ByteBuffer)32 CloseHelper (org.agrona.CloseHelper)32 Array32FW (io.aklivity.zilla.specs.binding.kafka.internal.types.Array32FW)28 HEADER (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaConditionType.HEADER)28 HEADERS (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaConditionType.HEADERS)28 KEY (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaConditionType.KEY)28 NOT (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaConditionType.NOT)28 KafkaDeltaType (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaDeltaType)28 KafkaOffsetFW (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaOffsetFW)28 KafkaSkip (io.aklivity.zilla.specs.binding.kafka.internal.types.KafkaSkip)28