From f8f45e2cebd0f79be813d85dfa04e97a5752dff7 Mon Sep 17 00:00:00 2001 From: ShvaykaD Date: Thu, 12 Jan 2023 10:55:59 +0200 Subject: [PATCH] minor improvements & code refactoring --- .../deduplicate/TbMsgDeDuplicateNode.java | 17 +++++++---- .../TbMsgDeDuplicateNodeConfiguration.java | 4 +-- .../action/TbMsgDeDuplicateNodeTest.java | 29 ++++++++++--------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/deduplicate/TbMsgDeDuplicateNode.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/deduplicate/TbMsgDeDuplicateNode.java index 2e0eb3196e..1bb1aa48f3 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/deduplicate/TbMsgDeDuplicateNode.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/deduplicate/TbMsgDeDuplicateNode.java @@ -74,7 +74,7 @@ public class TbMsgDeDuplicateNode implements TbNode { @Override public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException { this.config = TbNodeUtils.convert(configuration, TbMsgDeDuplicateNodeConfiguration.class); - this.deDuplicationInterval = TimeUnit.SECONDS.toMillis(config.getDelay()); + this.deDuplicationInterval = TimeUnit.SECONDS.toMillis(config.getInterval()); this.deDuplicateByOriginator = config.isDeDuplicateByOriginator(); this.deDuplicateStateMap = new HashMap<>(); scheduleTickMsg(ctx); @@ -84,9 +84,9 @@ public class TbMsgDeDuplicateNode implements TbNode { public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException { if (TB_MSG_DEDUPLICATION_TIMEOUT_MSG.equals(msg.getType())) { if (msg.getId().equals(nextTickId)) { - List deDuplicateDataList = getDeDuplicateDataList(); - if (!deDuplicateDataList.isEmpty()) { - if (deDuplicateByOriginator) { + if (deDuplicateByOriginator) { + List deDuplicateDataList = getDeDuplicateDataList(); + if (!deDuplicateDataList.isEmpty()) { deDuplicateDataList.forEach(deDuplicateData -> { EntityId entityId = deDuplicateData.getEntityId(); List deDuplicateStates = deDuplicateData.getDeDuplicateStates(); @@ -117,9 +117,9 @@ public class TbMsgDeDuplicateNode implements TbNode { }); })); }); - } else { - // todo implement } + } else { + // todo implement } scheduleTickMsg(ctx); } else { @@ -138,6 +138,11 @@ public class TbMsgDeDuplicateNode implements TbNode { } } + @Override + public void destroy() { + deDuplicateStateMap.clear(); + } + private List getDeDuplicateDataList() { if (deDuplicateStateMap.isEmpty()) { return Collections.emptyList(); diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/deduplicate/TbMsgDeDuplicateNodeConfiguration.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/deduplicate/TbMsgDeDuplicateNodeConfiguration.java index e93bf25c23..75141176e1 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/deduplicate/TbMsgDeDuplicateNodeConfiguration.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/deduplicate/TbMsgDeDuplicateNodeConfiguration.java @@ -21,7 +21,7 @@ import org.thingsboard.rule.engine.api.NodeConfiguration; @Data public class TbMsgDeDuplicateNodeConfiguration implements NodeConfiguration { - private int delay; + private int interval; private int maxPendingMsgs; private int maxRetries; private boolean deDuplicateByOriginator; @@ -35,7 +35,7 @@ public class TbMsgDeDuplicateNodeConfiguration implements NodeConfiguration) invocationOnMock -> { @@ -126,8 +127,8 @@ public class TbMsgDeDuplicateNodeTest { log.error("Failed to execute tellSelf method call due to: ", e); } }, scheduleTimeout.get(), TimeUnit.SECONDS); - if (incrementScheduleTimeout) { - scheduleTimeout.set(scheduleTimeout.get() * 3); + if (delayScheduleTimeout) { + scheduleTimeout.set(scheduleTimeout.get() * delayMultiplier); } } @@ -146,9 +147,9 @@ public class TbMsgDeDuplicateNodeTest { int wantedNumberOfTellSelfInvocation = 2; int msgCount = 100; awaitTellSelfLatch = new CountDownLatch(wantedNumberOfTellSelfInvocation); - invokeTellSelf(wantedNumberOfTellSelfInvocation, false); + invokeTellSelf(wantedNumberOfTellSelfInvocation); - config.setDelay(deDuplicationInterval); + config.setInterval(deDuplicationInterval); config.setMaxPendingMsgs(msgCount); nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); node.init(ctx, nodeConfiguration); @@ -182,10 +183,10 @@ public class TbMsgDeDuplicateNodeTest { int wantedNumberOfTellSelfInvocation = 2; int msgCount = 100; awaitTellSelfLatch = new CountDownLatch(wantedNumberOfTellSelfInvocation); - invokeTellSelf(wantedNumberOfTellSelfInvocation, false); + invokeTellSelf(wantedNumberOfTellSelfInvocation); config.setStrategy(DeDuplicateStrategy.LAST); - config.setDelay(deDuplicationInterval); + config.setInterval(deDuplicationInterval); config.setMaxPendingMsgs(msgCount); nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); node.init(ctx, nodeConfiguration); @@ -227,9 +228,9 @@ public class TbMsgDeDuplicateNodeTest { int wantedNumberOfTellSelfInvocation = 2; int msgCount = 100; awaitTellSelfLatch = new CountDownLatch(wantedNumberOfTellSelfInvocation); - invokeTellSelf(wantedNumberOfTellSelfInvocation, false); + invokeTellSelf(wantedNumberOfTellSelfInvocation); - config.setDelay(deDuplicationInterval); + config.setInterval(deDuplicationInterval); config.setMaxPendingMsgs(msgCount); config.setStrategy(DeDuplicateStrategy.ALL); nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); @@ -264,9 +265,9 @@ public class TbMsgDeDuplicateNodeTest { int wantedNumberOfTellSelfInvocation = 2; int msgCount = 100; awaitTellSelfLatch = new CountDownLatch(wantedNumberOfTellSelfInvocation); - invokeTellSelf(wantedNumberOfTellSelfInvocation, true); + invokeTellSelf(wantedNumberOfTellSelfInvocation, true, 3); - config.setDelay(deDuplicationInterval); + config.setInterval(deDuplicationInterval); config.setMaxPendingMsgs(msgCount); config.setStrategy(DeDuplicateStrategy.ALL); nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));