Search in sources :

Example 1 with ActivateJobsResponse

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse in project zeebe by camunda.

the class ActivateJobsTest method shouldMapRequestAndResponse.

@Test
public void shouldMapRequestAndResponse() {
    // given
    final ActivateJobsStub stub = new ActivateJobsStub();
    stub.registerWith(brokerClient);
    final String jobType = "testJob";
    final String worker = "testWorker";
    final int maxJobsToActivate = 13;
    final Duration timeout = Duration.ofMinutes(12);
    final List<String> fetchVariables = Arrays.asList("foo", "bar", "baz");
    final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(jobType).setWorker(worker).setMaxJobsToActivate(maxJobsToActivate).setTimeout(timeout.toMillis()).addAllFetchVariable(fetchVariables).build();
    stub.addAvailableJobs(jobType, maxJobsToActivate);
    // when
    final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
    // then
    assertThat(responses.hasNext()).isTrue();
    final ActivateJobsResponse response = responses.next();
    assertThat(response.getJobsCount()).isEqualTo(maxJobsToActivate);
    for (int i = 0; i < maxJobsToActivate; i++) {
        final ActivatedJob job = response.getJobs(i);
        assertThat(job.getKey()).isEqualTo(Protocol.encodePartitionId(Protocol.START_PARTITION_ID, i));
        assertThat(job.getType()).isEqualTo(jobType);
        assertThat(job.getWorker()).isEqualTo(worker);
        assertThat(job.getRetries()).isEqualTo(stub.getRetries());
        assertThat(job.getDeadline()).isEqualTo(stub.getDeadline());
        assertThat(job.getProcessInstanceKey()).isEqualTo(stub.getProcessInstanceKey());
        assertThat(job.getBpmnProcessId()).isEqualTo(stub.getBpmnProcessId());
        assertThat(job.getProcessDefinitionVersion()).isEqualTo(stub.getProcessDefinitionVersion());
        assertThat(job.getProcessDefinitionKey()).isEqualTo(stub.getProcessDefinitionKey());
        assertThat(job.getElementId()).isEqualTo(stub.getElementId());
        assertThat(job.getElementInstanceKey()).isEqualTo(stub.getElementInstanceKey());
        JsonUtil.assertEquality(job.getCustomHeaders(), stub.getCustomHeaders());
        JsonUtil.assertEquality(job.getVariables(), stub.getVariables());
    }
    final BrokerActivateJobsRequest brokerRequest = brokerClient.getSingleBrokerRequest();
    final JobBatchRecord brokerRequestValue = brokerRequest.getRequestWriter();
    assertThat(brokerRequestValue.getMaxJobsToActivate()).isEqualTo(maxJobsToActivate);
    assertThat(brokerRequestValue.getTypeBuffer()).isEqualTo(wrapString(jobType));
    assertThat(brokerRequestValue.getTimeout()).isEqualTo(timeout.toMillis());
    assertThat(brokerRequestValue.getWorkerBuffer()).isEqualTo(wrapString(worker));
    assertThat(brokerRequestValue.variables()).extracting(v -> BufferUtil.bufferAsString(v.getValue())).containsExactlyInAnyOrderElementsOf(fetchVariables);
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) Protocol(io.camunda.zeebe.protocol.Protocol) Arrays(java.util.Arrays) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Status(io.grpc.Status) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Parameterized(org.junit.runners.Parameterized) Intent(io.camunda.zeebe.protocol.record.intent.Intent) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Iterator(java.util.Iterator) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) JsonUtil(io.camunda.zeebe.test.util.JsonUtil) Test(org.junit.Test) StatusRuntimeException(io.grpc.StatusRuntimeException) List(java.util.List) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) GatewayCfg(io.camunda.zeebe.gateway.impl.configuration.GatewayCfg) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) BrokerRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest) BufferUtil(io.camunda.zeebe.util.buffer.BufferUtil) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) Duration(java.time.Duration) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) Test(org.junit.Test)

Example 2 with ActivateJobsResponse

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse in project zeebe by camunda.

the class ActivateJobsTest method shouldActivateJobsRoundRobin.

@Test
public void shouldActivateJobsRoundRobin() {
    // given
    final ActivateJobsStub stub = new ActivateJobsStub();
    stub.registerWith(brokerClient);
    final String type = "test";
    final int maxJobsToActivate = 2;
    final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(type).setMaxJobsToActivate(maxJobsToActivate).build();
    for (int partitionOffset = 0; partitionOffset < 3; partitionOffset++) {
        stub.addAvailableJobs(type, maxJobsToActivate);
        // when
        final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
        // then
        assertThat(responses.hasNext()).isTrue();
        final ActivateJobsResponse response = responses.next();
        for (final ActivatedJob activatedJob : response.getJobsList()) {
            assertThat(Protocol.decodePartitionId(activatedJob.getKey())).isEqualTo(Protocol.START_PARTITION_ID + partitionOffset);
        }
    }
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) Test(org.junit.Test)

Example 3 with ActivateJobsResponse

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse in project zeebe by camunda.

the class LongPollingActivateJobsTest method shouldReturnJobsIfSomeBrokersHaveJobsWhileOthersReturnResourceExhaustionResponse.

@Test
public void shouldReturnJobsIfSomeBrokersHaveJobsWhileOthersReturnResourceExhaustionResponse() {
    // given
    final InflightActivateJobsRequest request = new InflightActivateJobsRequest(getNextRequestId(), ActivateJobsRequest.newBuilder().setType(TYPE).setMaxJobsToActivate(15).setRequestTimeout(500).build(), spy(ServerStreamObserver.class));
    brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<?>>() {

        private final ActivateJobsStub jobsAvailableStub = new ActivateJobsStub();

        {
            jobsAvailableStub.addAvailableJobs(TYPE, 10);
        }

        @Override
        public BrokerResponse<?> handle(final BrokerActivateJobsRequest request) throws Exception {
            final int partitionId = request.getPartitionId();
            if (partitionId == 4) {
                return new BrokerErrorResponse<>(new BrokerError(ErrorCode.RESOURCE_EXHAUSTED, "backpressure"));
            } else {
                return jobsAvailableStub.handle(request);
            }
        }
    });
    // when
    handler.activateJobs(request);
    waitUntil(request::isCompleted);
    // then
    assertThat(request.isTimedOut()).isFalse();
    final ArgumentCaptor<ActivateJobsResponse> responseArgumentCaptor = ArgumentCaptor.forClass(ActivateJobsResponse.class);
    verify(request.getResponseObserver()).onNext(responseArgumentCaptor.capture());
    final ActivateJobsResponse response = responseArgumentCaptor.getValue();
    assertThat(response.getJobsList()).hasSize(10);
}
Also used : InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) StatusException(io.grpc.StatusException) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) ServerStreamObserver(io.camunda.zeebe.gateway.grpc.ServerStreamObserver) BrokerError(io.camunda.zeebe.gateway.impl.broker.response.BrokerError) Test(org.junit.Test)

Example 4 with ActivateJobsResponse

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse 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 5 with ActivateJobsResponse

use of io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse in project zeebe by zeebe-io.

the class ActivateJobsTest method shouldMapRequestAndResponse.

@Test
public void shouldMapRequestAndResponse() {
    // given
    final ActivateJobsStub stub = new ActivateJobsStub();
    stub.registerWith(brokerClient);
    final String jobType = "testJob";
    final String worker = "testWorker";
    final int maxJobsToActivate = 13;
    final Duration timeout = Duration.ofMinutes(12);
    final List<String> fetchVariables = Arrays.asList("foo", "bar", "baz");
    final ActivateJobsRequest request = ActivateJobsRequest.newBuilder().setType(jobType).setWorker(worker).setMaxJobsToActivate(maxJobsToActivate).setTimeout(timeout.toMillis()).addAllFetchVariable(fetchVariables).build();
    stub.addAvailableJobs(jobType, maxJobsToActivate);
    // when
    final Iterator<ActivateJobsResponse> responses = client.activateJobs(request);
    // then
    assertThat(responses.hasNext()).isTrue();
    final ActivateJobsResponse response = responses.next();
    assertThat(response.getJobsCount()).isEqualTo(maxJobsToActivate);
    for (int i = 0; i < maxJobsToActivate; i++) {
        final ActivatedJob job = response.getJobs(i);
        assertThat(job.getKey()).isEqualTo(Protocol.encodePartitionId(Protocol.START_PARTITION_ID, i));
        assertThat(job.getType()).isEqualTo(jobType);
        assertThat(job.getWorker()).isEqualTo(worker);
        assertThat(job.getRetries()).isEqualTo(stub.getRetries());
        assertThat(job.getDeadline()).isEqualTo(stub.getDeadline());
        assertThat(job.getProcessInstanceKey()).isEqualTo(stub.getProcessInstanceKey());
        assertThat(job.getBpmnProcessId()).isEqualTo(stub.getBpmnProcessId());
        assertThat(job.getProcessDefinitionVersion()).isEqualTo(stub.getProcessDefinitionVersion());
        assertThat(job.getProcessDefinitionKey()).isEqualTo(stub.getProcessDefinitionKey());
        assertThat(job.getElementId()).isEqualTo(stub.getElementId());
        assertThat(job.getElementInstanceKey()).isEqualTo(stub.getElementInstanceKey());
        JsonUtil.assertEquality(job.getCustomHeaders(), stub.getCustomHeaders());
        JsonUtil.assertEquality(job.getVariables(), stub.getVariables());
    }
    final BrokerActivateJobsRequest brokerRequest = brokerClient.getSingleBrokerRequest();
    final JobBatchRecord brokerRequestValue = brokerRequest.getRequestWriter();
    assertThat(brokerRequestValue.getMaxJobsToActivate()).isEqualTo(maxJobsToActivate);
    assertThat(brokerRequestValue.getTypeBuffer()).isEqualTo(wrapString(jobType));
    assertThat(brokerRequestValue.getTimeout()).isEqualTo(timeout.toMillis());
    assertThat(brokerRequestValue.getWorkerBuffer()).isEqualTo(wrapString(worker));
    assertThat(brokerRequestValue.variables()).extracting(v -> BufferUtil.bufferAsString(v.getValue())).containsExactlyInAnyOrderElementsOf(fetchVariables);
}
Also used : ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) Protocol(io.camunda.zeebe.protocol.Protocol) Arrays(java.util.Arrays) BrokerRejectionResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ActivatedJob(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob) Status(io.grpc.Status) BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) Parameterized(org.junit.runners.Parameterized) Intent(io.camunda.zeebe.protocol.record.intent.Intent) RequestHandler(io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler) Iterator(java.util.Iterator) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) JsonUtil(io.camunda.zeebe.test.util.JsonUtil) Test(org.junit.Test) StatusRuntimeException(io.grpc.StatusRuntimeException) List(java.util.List) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) RejectionType(io.camunda.zeebe.protocol.record.RejectionType) GatewayCfg(io.camunda.zeebe.gateway.impl.configuration.GatewayCfg) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) BrokerRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerRequest) BufferUtil(io.camunda.zeebe.util.buffer.BufferUtil) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) ActivateJobsRequest(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest) ActivateJobsResponse(io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse) Duration(java.time.Duration) BufferUtil.wrapString(io.camunda.zeebe.util.buffer.BufferUtil.wrapString) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) JobBatchRecord(io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord) GatewayTest(io.camunda.zeebe.gateway.api.util.GatewayTest) Test(org.junit.Test)

Aggregations

ActivateJobsResponse (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsResponse)32 Test (org.junit.Test)24 BrokerActivateJobsRequest (io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest)18 ActivateJobsRequest (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivateJobsRequest)18 ActivatedJob (io.camunda.zeebe.gateway.protocol.GatewayOuterClass.ActivatedJob)15 GatewayTest (io.camunda.zeebe.gateway.api.util.GatewayTest)12 BrokerResponse (io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse)12 InflightActivateJobsRequest (io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest)12 Duration (java.time.Duration)12 RequestHandler (io.camunda.zeebe.gateway.api.util.StubbedBrokerClient.RequestHandler)9 BrokerRejection (io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection)9 BrokerRejectionResponse (io.camunda.zeebe.gateway.impl.broker.response.BrokerRejectionResponse)9 JobBatchRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobBatchRecord)9 RejectionType (io.camunda.zeebe.protocol.record.RejectionType)9 Intent (io.camunda.zeebe.protocol.record.intent.Intent)9 BufferUtil.wrapString (io.camunda.zeebe.util.buffer.BufferUtil.wrapString)9 List (java.util.List)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 BrokerRejectionException (io.camunda.zeebe.gateway.cmd.BrokerRejectionException)6 ServerStreamObserver (io.camunda.zeebe.gateway.grpc.ServerStreamObserver)6