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();
}
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);
}
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;
}
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);
}
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());
}
Aggregations