Search in sources :

Example 36 with JobRecord

use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe-process-test by camunda-cloud.

the class GrpcResponseWriter method createJobBatchResponse.

private GeneratedMessageV3 createJobBatchResponse() {
    final JobBatchRecord jobBatch = new JobBatchRecord();
    jobBatch.wrap(valueBufferView);
    final Map<Long, JobRecord> jobsWithKeys = new HashMap<>();
    for (int index = 0; index < jobBatch.getJobKeys().size(); index++) {
        final Long key = jobBatch.getJobKeys().get(index);
        final JobRecord value = (JobRecord) jobBatch.getJobs().get(index);
        jobsWithKeys.put(key, value);
    }
    return ActivateJobsResponse.newBuilder().addAllJobs(jobsWithKeys.entrySet().stream().map((entry) -> {
        final JobRecord job = entry.getValue();
        return ActivatedJob.newBuilder().setKey(entry.getKey()).setType(job.getType()).setRetries(job.getRetries()).setWorker(job.getWorker()).setDeadline(job.getDeadline()).setProcessDefinitionKey(job.getProcessDefinitionKey()).setBpmnProcessId(job.getBpmnProcessId()).setProcessDefinitionVersion(job.getProcessDefinitionVersion()).setProcessInstanceKey(job.getProcessInstanceKey()).setElementId(job.getElementId()).setElementInstanceKey(job.getElementInstanceKey()).setCustomHeaders(MsgPackConverter.convertToJson(job.getCustomHeadersBuffer())).setVariables(MsgPackConverter.convertToJson(job.getVariablesBuffer())).build();
    }).collect(Collectors.toList())).build();
}
Also used : RecordType(io.camunda.zeebe.protocol.record.RecordType) CompleteJobResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.CompleteJobResponse) VariableDocumentRecord(io.camunda.zeebe.protocol.impl.record.value.variable.VariableDocumentRecord) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) CreateProcessInstanceResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.CreateProcessInstanceResponse) FailJobResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.FailJobResponse) ValueType(io.camunda.zeebe.protocol.record.ValueType) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CommandResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.CommandResponseWriter) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord) ProcessMetadata(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ProcessMetadata) DeployProcessResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.DeployProcessResponse) CreateProcessInstanceWithResultResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.CreateProcessInstanceWithResultResponse) Map(java.util.Map) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Code(com.google.rpc.Code) ResolveIncidentResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ResolveIncidentResponse) Intent(io.camunda.zeebe.protocol.record.intent.Intent) MsgPackConverter(io.camunda.zeebe.protocol.impl.encoding.MsgPackConverter) SetVariablesResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.SetVariablesResponse) ProcessInstanceCreationRecord(io.camunda.zeebe.protocol.impl.record.value.processinstance.ProcessInstanceCreationRecord) ThrowErrorResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ThrowErrorResponse) UpdateJobRetriesResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.UpdateJobRetriesResponse) Status(com.google.rpc.Status) BufferWriter(io.camunda.zeebe.util.buffer.BufferWriter) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) CancelProcessInstanceResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.CancelProcessInstanceResponse) Collectors(java.util.stream.Collectors) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) DeploymentRecord(io.camunda.zeebe.protocol.impl.record.value.deployment.DeploymentRecord) PublishMessageResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.PublishMessageResponse) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) ProcessInstanceResultRecord(io.camunda.zeebe.protocol.impl.record.value.processinstance.ProcessInstanceResultRecord) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) MutableDirectBuffer(org.agrona.MutableDirectBuffer) IncidentRecord(io.camunda.zeebe.protocol.impl.record.value.incident.IncidentRecord) BufferUtil(io.camunda.zeebe.util.buffer.BufferUtil) JobIntent(io.camunda.zeebe.protocol.record.intent.JobIntent) DirectBuffer(org.agrona.DirectBuffer) HashMap(java.util.HashMap) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord)

Example 37 with JobRecord

use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe-process-test by camunda-cloud.

the class GrpcToLogStreamGateway method failJob.

@Override
public void failJob(final FailJobRequest request, final StreamObserver<FailJobResponse> responseObserver) {
    final Long requestId = gatewayRequestStore.registerNewRequest(request.getClass(), responseObserver);
    final RecordMetadata recordMetadata = prepareRecordMetadata().requestId(requestId).valueType(ValueType.JOB).intent(JobIntent.FAIL);
    final JobRecord jobRecord = new JobRecord();
    jobRecord.setRetries(request.getRetries());
    jobRecord.setErrorMessage(request.getErrorMessage());
    writer.writeCommandWithKey(request.getJobKey(), jobRecord, recordMetadata);
}
Also used : RecordMetadata(io.camunda.zeebe.protocol.impl.record.RecordMetadata) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord)

Example 38 with JobRecord

use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe-process-test by camunda-cloud.

the class GrpcToLogStreamGateway method updateJobRetries.

@Override
public void updateJobRetries(final UpdateJobRetriesRequest request, final StreamObserver<UpdateJobRetriesResponse> responseObserver) {
    final Long requestId = gatewayRequestStore.registerNewRequest(request.getClass(), responseObserver);
    final RecordMetadata recordMetadata = prepareRecordMetadata().requestId(requestId).valueType(ValueType.JOB).intent(JobIntent.UPDATE_RETRIES);
    final JobRecord jobRecord = new JobRecord();
    jobRecord.setRetries(request.getRetries());
    writer.writeCommandWithKey(request.getJobKey(), jobRecord, recordMetadata);
}
Also used : RecordMetadata(io.camunda.zeebe.protocol.impl.record.RecordMetadata) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord)

Example 39 with JobRecord

use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord in project zeebe-process-test by camunda-cloud.

the class GrpcToLogStreamGateway method throwError.

@Override
public void throwError(final ThrowErrorRequest request, final StreamObserver<ThrowErrorResponse> responseObserver) {
    final Long requestId = gatewayRequestStore.registerNewRequest(request.getClass(), responseObserver);
    final RecordMetadata recordMetadata = prepareRecordMetadata().requestId(requestId).valueType(ValueType.JOB).intent(JobIntent.THROW_ERROR);
    final JobRecord jobRecord = new JobRecord();
    jobRecord.setErrorCode(BufferUtil.wrapString(request.getErrorCode()));
    jobRecord.setErrorMessage(request.getErrorMessage());
    writer.writeCommandWithKey(request.getJobKey(), jobRecord, recordMetadata);
}
Also used : RecordMetadata(io.camunda.zeebe.protocol.impl.record.RecordMetadata) JobRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobRecord)

Example 40 with JobRecord

use of io.camunda.zeebe.protocol.impl.record.value.job.JobRecord 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)

Aggregations

JobRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobRecord)183 Test (org.junit.Test)96 GatewayTest (io.camunda.zeebe.gateway.api.util.GatewayTest)15 BrokerResponse (io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse)9 LongValue (io.camunda.zeebe.msgpack.value.LongValue)9 CompleteJobResponse (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.CompleteJobResponse)8 IncidentRecord (io.camunda.zeebe.protocol.impl.record.value.incident.IncidentRecord)8 DirectBuffer (org.agrona.DirectBuffer)8 RecordMetadata (io.camunda.zeebe.protocol.impl.record.RecordMetadata)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 BrokerCompleteJobRequest (io.camunda.zeebe.gateway.impl.broker.request.BrokerCompleteJobRequest)6 CompleteJobRequest (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.CompleteJobRequest)6 JobState (io.camunda.zeebe.engine.state.immutable.JobState)5 State (io.camunda.zeebe.engine.state.immutable.JobState.State)5 ActivateJobsResponse (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse)5 ActivatedJob (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob)5 FailJobResponse (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.FailJobResponse)5 UpdateJobRetriesResponse (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.UpdateJobRetriesResponse)5 DeploymentRecord (io.camunda.zeebe.protocol.impl.record.value.deployment.DeploymentRecord)5 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)5