Search in sources :

Example 1 with BrokerActivateJobsRequest

use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest 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 BrokerActivateJobsRequest

use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest 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 3 with BrokerActivateJobsRequest

use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest 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)

Example 4 with BrokerActivateJobsRequest

use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest in project zeebe by zeebe-io.

the class LongPollingActivateJobsTest method shouldCancelTimerOnResourceExhausted.

@Test
public void shouldCancelTimerOnResourceExhausted() {
    // given
    final InflightActivateJobsRequest request = getLongPollingActivateJobsRequest();
    brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<?>>() {

        private int count = 0;

        /*
           * First execution of the request (count < partitionCount) -> don't activate jobs
           * Second execution of the request (count >= partitionCount) -> fail immediately
           */
        @Override
        public BrokerResponse<?> handle(final BrokerActivateJobsRequest request) throws Exception {
            if (count >= partitionsCount) {
                return new BrokerErrorResponse<>(new BrokerError(ErrorCode.RESOURCE_EXHAUSTED, "backpressure"));
            }
            count += 1;
            return activateJobsStub.handle(request);
        }
    });
    // when
    handler.activateJobs(request);
    waitUntil(request::hasScheduledTimer);
    brokerClient.notifyJobsAvailable(TYPE);
    Awaitility.await().until(request::isAborted);
    // then
    final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
    verify(request.getResponseObserver(), times(1)).onError(throwableCaptor.capture());
    assertThat(throwableCaptor.getValue()).isInstanceOf(StatusException.class);
    assertThat(request.hasScheduledTimer()).isFalse();
}
Also used : BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerError(io.camunda.zeebe.gateway.impl.broker.response.BrokerError) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) StatusException(io.grpc.StatusException) Test(org.junit.Test)

Example 5 with BrokerActivateJobsRequest

use of io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest in project zeebe by zeebe-io.

the class LongPollingActivateJobsTest method shouldCancelTimerOnBrokerRejectionException.

@Test
public void shouldCancelTimerOnBrokerRejectionException() {
    // given
    final InflightActivateJobsRequest request = getLongPollingActivateJobsRequest();
    brokerClient.registerHandler(BrokerActivateJobsRequest.class, new RequestHandler<BrokerActivateJobsRequest, BrokerResponse<?>>() {

        private int count = 0;

        /*
           * First execution of the request (count < partitionCount) -> don't activate jobs
           * Second execution of the request (count >= partitionCount) -> fail immediately
           */
        @Override
        public BrokerResponse<?> handle(final BrokerActivateJobsRequest request) throws Exception {
            if (count >= partitionsCount) {
                return new BrokerRejectionResponse<>(new BrokerRejection(Intent.UNKNOWN, 1, RejectionType.INVALID_ARGUMENT, "expected"));
            }
            count += 1;
            return activateJobsStub.handle(request);
        }
    });
    // when
    handler.activateJobs(request);
    waitUntil(request::hasScheduledTimer);
    brokerClient.notifyJobsAvailable(TYPE);
    Awaitility.await().until(request::isAborted);
    // then
    final ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
    verify(request.getResponseObserver(), times(1)).onError(throwableCaptor.capture());
    assertThat(throwableCaptor.getValue()).isInstanceOf(BrokerRejectionException.class);
    assertThat(request.hasScheduledTimer()).isFalse();
}
Also used : BrokerResponse(io.camunda.zeebe.gateway.impl.broker.response.BrokerResponse) InflightActivateJobsRequest(io.camunda.zeebe.gateway.impl.job.InflightActivateJobsRequest) BrokerActivateJobsRequest(io.camunda.zeebe.gateway.impl.broker.request.BrokerActivateJobsRequest) BrokerRejection(io.camunda.zeebe.gateway.impl.broker.response.BrokerRejection) BrokerRejectionException(io.camunda.zeebe.gateway.cmd.BrokerRejectionException) StatusException(io.grpc.StatusException) Test(org.junit.Test)

Aggregations

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