Search in sources :

Example 1 with TbRuleEngineSubmitStrategy

use of org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy in project thingsboard by thingsboard.

the class TbMsgPackProcessingContextTest method testHighConcurrencyCase.

@Test
public void testHighConcurrencyCase() throws InterruptedException {
    // log.warn("preparing the test...");
    int msgCount = 1000;
    int parallelCount = 5;
    executorService = Executors.newFixedThreadPool(parallelCount, ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope"));
    ConcurrentMap<UUID, TbProtoQueueMsg<TransportProtos.ToRuleEngineMsg>> messages = new ConcurrentHashMap<>(msgCount);
    for (int i = 0; i < msgCount; i++) {
        messages.put(UUID.randomUUID(), new TbProtoQueueMsg<>(UUID.randomUUID(), null));
    }
    TbRuleEngineSubmitStrategy strategyMock = mock(TbRuleEngineSubmitStrategy.class);
    when(strategyMock.getPendingMap()).thenReturn(messages);
    TbMsgPackProcessingContext context = new TbMsgPackProcessingContext("Main", strategyMock, false);
    for (UUID uuid : messages.keySet()) {
        final CountDownLatch readyLatch = new CountDownLatch(parallelCount);
        final CountDownLatch startLatch = new CountDownLatch(1);
        final CountDownLatch finishLatch = new CountDownLatch(parallelCount);
        for (int i = 0; i < parallelCount; i++) {
            // final String taskName = "" + uuid + " " + i;
            executorService.submit(() -> {
                // log.warn("ready {}", taskName);
                readyLatch.countDown();
                try {
                    startLatch.await();
                } catch (InterruptedException e) {
                    Assert.fail("failed to await");
                }
                // log.warn("go    {}", taskName);
                context.onSuccess(uuid);
                finishLatch.countDown();
            });
        }
        assertTrue(readyLatch.await(TIMEOUT, TimeUnit.SECONDS));
        Thread.yield();
        // run all-at-once submitted tasks
        startLatch.countDown();
        assertTrue(finishLatch.await(TIMEOUT, TimeUnit.SECONDS));
    }
    assertTrue(context.await(TIMEOUT, TimeUnit.SECONDS));
    verify(strategyMock, times(msgCount)).onSuccess(any(UUID.class));
}
Also used : TbRuleEngineSubmitStrategy(org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) UUID(java.util.UUID) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with TbRuleEngineSubmitStrategy

use of org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy in project thingsboard by thingsboard.

the class DefaultTbRuleEngineConsumerService method consumerLoop.

void consumerLoop(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, String threadSuffix) {
    updateCurrentThreadName(threadSuffix);
    while (!stopped && !consumer.isStopped()) {
        try {
            List<TbProtoQueueMsg<ToRuleEngineMsg>> msgs = consumer.poll(pollDuration);
            if (msgs.isEmpty()) {
                continue;
            }
            final TbRuleEngineSubmitStrategy submitStrategy = getSubmitStrategy(configuration);
            final TbRuleEngineProcessingStrategy ackStrategy = getAckStrategy(configuration);
            submitStrategy.init(msgs);
            while (!stopped) {
                TbMsgPackProcessingContext ctx = new TbMsgPackProcessingContext(configuration.getName(), submitStrategy, ackStrategy.isSkipTimeoutMsgs());
                submitStrategy.submitAttempt((id, msg) -> submitExecutor.submit(() -> submitMessage(configuration, stats, ctx, id, msg)));
                final boolean timeout = !ctx.await(configuration.getPackProcessingTimeout(), TimeUnit.MILLISECONDS);
                TbRuleEngineProcessingResult result = new TbRuleEngineProcessingResult(configuration.getName(), timeout, ctx);
                if (timeout) {
                    printFirstOrAll(configuration, ctx, ctx.getPendingMap(), "Timeout");
                }
                if (!ctx.getFailedMap().isEmpty()) {
                    printFirstOrAll(configuration, ctx, ctx.getFailedMap(), "Failed");
                }
                ctx.printProfilerStats();
                TbRuleEngineProcessingDecision decision = ackStrategy.analyze(result);
                if (statsEnabled) {
                    stats.log(result, decision.isCommit());
                }
                ctx.cleanup();
                if (decision.isCommit()) {
                    submitStrategy.stop();
                    break;
                } else {
                    submitStrategy.update(decision.getReprocessMap());
                }
            }
            consumer.commit();
        } catch (Exception e) {
            if (!stopped) {
                log.warn("Failed to process messages from queue.", e);
                try {
                    Thread.sleep(pollDuration);
                } catch (InterruptedException e2) {
                    log.trace("Failed to wait until the server has capacity to handle new requests", e2);
                }
            }
        }
    }
    log.info("TB Rule Engine Consumer stopped.");
}
Also used : TbRuleEngineProcessingStrategy(org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStrategy) TbRuleEngineProcessingDecision(org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingDecision) TbRuleEngineSubmitStrategy(org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy) TbRuleEngineProcessingResult(org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingResult) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException)

Aggregations

TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)2 TbRuleEngineSubmitStrategy (org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy)2 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 RuleEngineException (org.thingsboard.server.common.msg.queue.RuleEngineException)1 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)1 TbRuleEngineProcessingDecision (org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingDecision)1 TbRuleEngineProcessingResult (org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingResult)1 TbRuleEngineProcessingStrategy (org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStrategy)1