Search in sources :

Example 1 with LongValue

use of io.camunda.zeebe.msgpack.value.LongValue in project zeebe by camunda.

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 2 with LongValue

use of io.camunda.zeebe.msgpack.value.LongValue in project zeebe by camunda.

the class ResponseMapper method toActivateJobsResponse.

public static ActivateJobsResponse toActivateJobsResponse(final long key, final JobBatchRecord brokerResponse) {
    final ActivateJobsResponse.Builder responseBuilder = ActivateJobsResponse.newBuilder();
    final Iterator<LongValue> jobKeys = brokerResponse.jobKeys().iterator();
    final Iterator<JobRecord> jobs = brokerResponse.jobs().iterator();
    while (jobKeys.hasNext() && jobs.hasNext()) {
        final LongValue jobKey = jobKeys.next();
        final JobRecord job = jobs.next();
        final ActivatedJob activatedJob = ActivatedJob.newBuilder().setKey(jobKey.getValue()).setType(bufferAsString(job.getTypeBuffer())).setBpmnProcessId(job.getBpmnProcessId()).setElementId(job.getElementId()).setProcessInstanceKey(job.getProcessInstanceKey()).setProcessDefinitionVersion(job.getProcessDefinitionVersion()).setProcessDefinitionKey(job.getProcessDefinitionKey()).setElementInstanceKey(job.getElementInstanceKey()).setCustomHeaders(bufferAsJson(job.getCustomHeadersBuffer())).setWorker(bufferAsString(job.getWorkerBuffer())).setRetries(job.getRetries()).setDeadline(job.getDeadline()).setVariables(bufferAsJson(job.getVariablesBuffer())).build();
        responseBuilder.addJobs(activatedJob);
    }
    return responseBuilder.build();
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) LongValue(io.camunda.zeebe.msgpack.value.LongValue)

Example 3 with LongValue

use of io.camunda.zeebe.msgpack.value.LongValue 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 4 with LongValue

use of io.camunda.zeebe.msgpack.value.LongValue in project zeebe by zeebe-io.

the class JobBatchActivatedApplier method applyState.

@Override
public void applyState(final long key, final JobBatchRecord value) {
    final Iterator<JobRecord> iterator = value.jobs().iterator();
    final Iterator<LongValue> keyIt = value.jobKeys().iterator();
    while (iterator.hasNext() && keyIt.hasNext()) {
        final JobRecord jobRecord = iterator.next();
        final LongValue next1 = keyIt.next();
        final long jobKey = next1.getValue();
        jobState.activate(jobKey, jobRecord);
    }
}
Also used : JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord) LongValue(io.camunda.zeebe.msgpack.value.LongValue)

Example 5 with LongValue

use of io.camunda.zeebe.msgpack.value.LongValue in project zeebe by camunda-cloud.

the class JobBatchActivatedApplier method applyState.

@Override
public void applyState(final long key, final JobBatchRecord value) {
    final Iterator<JobRecord> iterator = value.jobs().iterator();
    final Iterator<LongValue> keyIt = value.jobKeys().iterator();
    while (iterator.hasNext() && keyIt.hasNext()) {
        final JobRecord jobRecord = iterator.next();
        final LongValue next1 = keyIt.next();
        final long jobKey = next1.getValue();
        jobState.activate(jobKey, jobRecord);
    }
}
Also used : JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord) LongValue(io.camunda.zeebe.msgpack.value.LongValue)

Aggregations

LongValue (io.camunda.zeebe.msgpack.value.LongValue)9 JobRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobRecord)9 ActivateJobsResponse (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse)3 ActivatedJob (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob)3 JobBatchRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord)3 DirectBuffer (org.agrona.DirectBuffer)3 ExpandableArrayBuffer (org.agrona.ExpandableArrayBuffer)3 MutableInteger (org.agrona.collections.MutableInteger)3 MutableReference (org.agrona.collections.MutableReference)3