Search in sources :

Example 16 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldRetryWithMoreTasksThanSubscriptionCapacity.

/**
 * This tests a case that should not occur under normal circumstances, but might occur
 * in case of inconsistencies between broker and client state (e.g. due to bugs in either of them)
 */
@Test
public void shouldRetryWithMoreTasksThanSubscriptionCapacity() throws InterruptedException {
    // given
    broker.stubTaskSubscriptionApi(123L);
    broker.onExecuteCommandRequest(EventType.TASK_EVENT, "COMPLETE").respondWith().key((r) -> r.key()).event().allOf((r) -> r.getCommand()).put("state", "COMPLETED").done().register();
    final WaitingTaskHandler handler = new WaitingTaskHandler();
    final Properties clientProperties = ((ZeebeClientImpl) client).getInitializationProperties();
    final int numExecutionThreads = Integer.parseInt(clientProperties.getProperty(ClientProperties.CLIENT_SUBSCRIPTION_EXECUTION_THREADS));
    final int taskCapacity = 4;
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler).lockOwner("owner").lockTime(1000L).taskFetchSize(taskCapacity).taskType("foo").open();
    final RemoteAddress clientAddress = broker.getReceivedControlMessageRequests().get(0).getSource();
    for (int i = 0; i < taskCapacity + numExecutionThreads; i++) {
        broker.pushLockedTask(clientAddress, 123L, i, i, "owner", "foo");
    }
    TestUtil.waitUntil(() -> handler.numWaitingThreads.get() > 0);
    // pushing one more event, exceeding client capacity
    broker.pushLockedTask(clientAddress, 123L, Integer.MAX_VALUE, Integer.MAX_VALUE, "owner", "foo");
    // waiting for the client to receive all pending tasks
    Thread.sleep(500L);
    // when
    handler.shouldWait = false;
    continueTaskHandlingThreads();
    // then the additional event is handled nevertheless (i.e. client applies backpressure)
    TestUtil.waitUntil(() -> handler.numHandledEvents.get() == taskCapacity + numExecutionThreads + 1);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test) TaskSubscription(io.zeebe.client.task.TaskSubscription) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Collectors(java.util.stream.Collectors) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) SubscriptionType(io.zeebe.protocol.clientapi.SubscriptionType) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) ClientProperties(io.zeebe.client.ClientProperties) TaskHandler(io.zeebe.client.task.TaskHandler) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Subscriber(io.zeebe.client.task.impl.subscription.Subscriber) TestUtil(io.zeebe.test.util.TestUtil) RemoteAddress(io.zeebe.transport.RemoteAddress) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) Test(org.junit.Test)

Example 17 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class CreateTopicTest method shouldRejectPartitionCreationAndNotBreak.

@Test
public void shouldRejectPartitionCreationAndNotBreak() {
    // given
    final ClientTransport transport = apiRule.getTransport();
    final RemoteAddress remoteAddress = transport.registerRemoteAndAwaitChannel(BROKER_MGMT_ADDRESS);
    final ClientOutput output = transport.getOutput();
    final CreatePartitionRequest partitionMessage = new CreatePartitionRequest();
    final DirectBuffer topicName = BufferUtil.wrapString("foo");
    final int partition1 = 142;
    final int partition2 = 143;
    partitionMessage.topicName(topicName);
    partitionMessage.partitionId(partition1);
    // => should create partition
    doRepeatedly(() -> output.sendRequest(remoteAddress, partitionMessage)).until(r -> r != null);
    // => should be rejected/ignored
    doRepeatedly(() -> output.sendRequest(remoteAddress, partitionMessage)).until(r -> r != null);
    // when creating another partition
    partitionMessage.partitionId(partition2);
    doRepeatedly(() -> output.sendRequest(remoteAddress, partitionMessage)).until(r -> r != null);
    // then this should be successful (i.e. the rejected request should not have jammed the broker)
    waitUntil(() -> arePublished(partition1, partition2));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) RemoteAddress(io.zeebe.transport.RemoteAddress) CreatePartitionRequest(io.zeebe.broker.clustering.management.message.CreatePartitionRequest) ClientTransport(io.zeebe.transport.ClientTransport) ClientOutput(io.zeebe.transport.ClientOutput) Test(org.junit.Test)

Example 18 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class RemoteWorkflowsManager method sendMessage.

private boolean sendMessage(final BufferWriter message, final SocketAddress addr) {
    final RemoteAddress remoteAddress = managementClient.registerRemoteAddress(addr);
    transportMessage.remoteAddress(remoteAddress).writer(message);
    return output.sendMessage(transportMessage);
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress)

Example 19 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldInvokeTaskHandler.

@Test
public void shouldInvokeTaskHandler() throws JsonParseException, JsonMappingException, IOException {
    // given
    broker.stubTaskSubscriptionApi(123L);
    stubTaskCompleteRequest();
    final RecordingTaskHandler handler = new RecordingTaskHandler();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler).lockOwner("owner").lockTime(10000L).taskType("type").open();
    final RemoteAddress clientAddress = getSubscribeRequests().findFirst().get().getSource();
    final MsgPackHelper msgPackHelper = new MsgPackHelper();
    final Map<String, Object> taskPayload = new HashMap<>();
    taskPayload.put("payloadKey", "payloadValue");
    final Map<String, Object> taskHeaders = new HashMap<>();
    taskPayload.put("headerKey", "headerValue");
    final long lockTime = System.currentTimeMillis();
    // when
    broker.newSubscribedEvent().partitionId(StubBrokerRule.TEST_PARTITION_ID).key(4L).position(5L).eventType(EventType.TASK_EVENT).subscriberKey(123L).subscriptionType(SubscriptionType.TASK_SUBSCRIPTION).event().put("type", "type").put("lockTime", lockTime).put("retries", 3).put("payload", msgPackHelper.encodeAsMsgPack(taskPayload)).put("headers", taskHeaders).done().push(clientAddress);
    // then
    TestUtil.waitUntil(() -> !handler.getHandledTasks().isEmpty());
    assertThat(handler.getHandledTasks()).hasSize(1);
    final TaskEvent task = handler.getHandledTasks().get(0);
    assertThat(task.getMetadata().getKey()).isEqualTo(4L);
    assertThat(task.getType()).isEqualTo("type");
    assertThat(task.getHeaders()).isEqualTo(taskHeaders);
    assertThat(task.getLockExpirationTime()).isEqualTo(Instant.ofEpochMilli(lockTime));
    final ObjectMapper objectMapper = new ObjectMapper();
    @SuppressWarnings("unchecked") final Map<String, Object> receivedPayload = objectMapper.readValue(task.getPayload(), Map.class);
    assertThat(receivedPayload).isEqualTo(taskPayload);
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) HashMap(java.util.HashMap) TaskEvent(io.zeebe.client.event.TaskEvent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 20 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldMarkTaskAsFailedOnExpcetion.

@Test
public void shouldMarkTaskAsFailedOnExpcetion() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    broker.onExecuteCommandRequest(isTaskFailCommand()).respondWith().event().allOf(r -> r.getCommand()).put("state", "FAILED").done().register();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler((c, t) -> {
        throw new RuntimeException("expected failure");
    }).lockOwner("foo").lockTime(10000L).taskType("bar").open();
    final RemoteAddress clientAddress = getSubscribeRequests().findFirst().get().getSource();
    // when
    broker.pushLockedTask(clientAddress, 123L, 4L, 5L, "foo", "bar");
    // then
    final ExecuteCommandRequest taskRequest = TestUtil.doRepeatedly(() -> broker.getReceivedCommandRequests().stream().filter(r -> r.eventType() == EventType.TASK_EVENT).findFirst()).until(r -> r.isPresent()).get();
    assertThat(taskRequest.partitionId()).isEqualTo(clientRule.getDefaultPartitionId());
    assertThat(taskRequest.key()).isEqualTo(4L);
    assertThat(taskRequest.getCommand()).containsEntry("state", "FAIL").containsEntry("type", "bar").containsEntry("lockOwner", "foo");
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test) TaskSubscription(io.zeebe.client.task.TaskSubscription) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Collectors(java.util.stream.Collectors) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) SubscriptionType(io.zeebe.protocol.clientapi.SubscriptionType) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) ClientProperties(io.zeebe.client.ClientProperties) TaskHandler(io.zeebe.client.task.TaskHandler) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Subscriber(io.zeebe.client.task.impl.subscription.Subscriber) TestUtil(io.zeebe.test.util.TestUtil) RemoteAddress(io.zeebe.transport.RemoteAddress) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) Test(org.junit.Test)

Aggregations

RemoteAddress (io.zeebe.transport.RemoteAddress)35 Test (org.junit.Test)34 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 ControlMessageRequest (io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest)12 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)12 ZeebeClient (io.zeebe.client.ZeebeClient)11 ClientRule (io.zeebe.client.util.ClientRule)11 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)11 ErrorCode (io.zeebe.protocol.clientapi.ErrorCode)11 EventType (io.zeebe.protocol.clientapi.EventType)11 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)11 TestUtil (io.zeebe.test.util.TestUtil)11 TestUtil.waitUntil (io.zeebe.test.util.TestUtil.waitUntil)11 Duration (java.time.Duration)11 List (java.util.List)11 TimeUnit (java.util.concurrent.TimeUnit)11 Collectors (java.util.stream.Collectors)11 Stream (java.util.stream.Stream)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11