Message is pushed to correct queue in case of duplication
This commit is contained in:
		
							parent
							
								
									480d4d6935
								
							
						
					
					
						commit
						65c644b86f
					
				@ -221,8 +221,8 @@ class DefaultTbContext implements TbContext {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data) {
 | 
					    public TbMsg newMsg(String queueName, String type, EntityId originator, TbMsgMetaData metaData, String data) {
 | 
				
			||||||
        return TbMsg.newMsg(type, originator, metaData, data, nodeCtx.getSelf().getRuleChainId(), nodeCtx.getSelf().getId());
 | 
					        return TbMsg.newMsg(queueName, type, originator, metaData, data, nodeCtx.getSelf().getRuleChainId(), nodeCtx.getSelf().getId());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
 | 
				
			|||||||
@ -244,7 +244,7 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor<RuleCh
 | 
				
			|||||||
        try {
 | 
					        try {
 | 
				
			||||||
            checkActive(msg);
 | 
					            checkActive(msg);
 | 
				
			||||||
            EntityId entityId = msg.getOriginator();
 | 
					            EntityId entityId = msg.getOriginator();
 | 
				
			||||||
            TopicPartitionInfo tpi = systemContext.resolve(ServiceType.TB_RULE_ENGINE, tenantId, entityId);
 | 
					            TopicPartitionInfo tpi = systemContext.resolve(ServiceType.TB_RULE_ENGINE, msg.getQueueName(), tenantId, entityId);
 | 
				
			||||||
            List<RuleNodeRelation> relations = nodeRoutes.get(originatorNodeId).stream()
 | 
					            List<RuleNodeRelation> relations = nodeRoutes.get(originatorNodeId).stream()
 | 
				
			||||||
                    .filter(r -> contains(relationTypes, r.getType()))
 | 
					                    .filter(r -> contains(relationTypes, r.getType()))
 | 
				
			||||||
                    .collect(Collectors.toList());
 | 
					                    .collect(Collectors.toList());
 | 
				
			||||||
 | 
				
			|||||||
@ -166,7 +166,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
 | 
				
			|||||||
                            TbMsgCallback callback = new TbMsgPackCallback(id, tenantId, ctx);
 | 
					                            TbMsgCallback callback = new TbMsgPackCallback(id, tenantId, ctx);
 | 
				
			||||||
                            try {
 | 
					                            try {
 | 
				
			||||||
                                if (toRuleEngineMsg.getTbMsg() != null && !toRuleEngineMsg.getTbMsg().isEmpty()) {
 | 
					                                if (toRuleEngineMsg.getTbMsg() != null && !toRuleEngineMsg.getTbMsg().isEmpty()) {
 | 
				
			||||||
                                    forwardToRuleEngineActor(tenantId, toRuleEngineMsg, callback);
 | 
					                                    forwardToRuleEngineActor(configuration.getName(), tenantId, toRuleEngineMsg, callback);
 | 
				
			||||||
                                } else {
 | 
					                                } else {
 | 
				
			||||||
                                    callback.onSuccess();
 | 
					                                    callback.onSuccess();
 | 
				
			||||||
                                }
 | 
					                                }
 | 
				
			||||||
@ -180,7 +180,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
 | 
				
			|||||||
                            timeout = true;
 | 
					                            timeout = true;
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        TbRuleEngineProcessingResult result = new TbRuleEngineProcessingResult(timeout, ctx);
 | 
					                        TbRuleEngineProcessingResult result = new TbRuleEngineProcessingResult(configuration.getName(), timeout, ctx);
 | 
				
			||||||
                        TbRuleEngineProcessingDecision decision = ackStrategy.analyze(result);
 | 
					                        TbRuleEngineProcessingDecision decision = ackStrategy.analyze(result);
 | 
				
			||||||
                        if (statsEnabled) {
 | 
					                        if (statsEnabled) {
 | 
				
			||||||
                            stats.log(result, decision.isCommit());
 | 
					                            stats.log(result, decision.isCommit());
 | 
				
			||||||
@ -246,8 +246,8 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void forwardToRuleEngineActor(TenantId tenantId, ToRuleEngineMsg toRuleEngineMsg, TbMsgCallback callback) {
 | 
					    private void forwardToRuleEngineActor(String queueName, TenantId tenantId, ToRuleEngineMsg toRuleEngineMsg, TbMsgCallback callback) {
 | 
				
			||||||
        TbMsg tbMsg = TbMsg.fromBytes(toRuleEngineMsg.getTbMsg().toByteArray(), callback);
 | 
					        TbMsg tbMsg = TbMsg.fromBytes(queueName, toRuleEngineMsg.getTbMsg().toByteArray(), callback);
 | 
				
			||||||
        QueueToRuleEngineMsg msg;
 | 
					        QueueToRuleEngineMsg msg;
 | 
				
			||||||
        ProtocolStringList relationTypesList = toRuleEngineMsg.getRelationTypesList();
 | 
					        ProtocolStringList relationTypesList = toRuleEngineMsg.getRelationTypesList();
 | 
				
			||||||
        Set<String> relationTypes = null;
 | 
					        Set<String> relationTypes = null;
 | 
				
			||||||
 | 
				
			|||||||
@ -27,6 +27,8 @@ import java.util.concurrent.ConcurrentMap;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
public class TbRuleEngineProcessingResult {
 | 
					public class TbRuleEngineProcessingResult {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Getter
 | 
				
			||||||
 | 
					    private final String queueName;
 | 
				
			||||||
    @Getter
 | 
					    @Getter
 | 
				
			||||||
    private final boolean success;
 | 
					    private final boolean success;
 | 
				
			||||||
    @Getter
 | 
					    @Getter
 | 
				
			||||||
@ -34,7 +36,8 @@ public class TbRuleEngineProcessingResult {
 | 
				
			|||||||
    @Getter
 | 
					    @Getter
 | 
				
			||||||
    private final TbMsgPackProcessingContext ctx;
 | 
					    private final TbMsgPackProcessingContext ctx;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public TbRuleEngineProcessingResult(boolean timeout, TbMsgPackProcessingContext ctx) {
 | 
					    public TbRuleEngineProcessingResult(String queueName, boolean timeout, TbMsgPackProcessingContext ctx) {
 | 
				
			||||||
 | 
					        this.queueName = queueName;
 | 
				
			||||||
        this.timeout = timeout;
 | 
					        this.timeout = timeout;
 | 
				
			||||||
        this.ctx = ctx;
 | 
					        this.ctx = ctx;
 | 
				
			||||||
        this.success = !timeout && ctx.getPendingMap().isEmpty() && ctx.getFailedMap().isEmpty();
 | 
					        this.success = !timeout && ctx.getPendingMap().isEmpty() && ctx.getFailedMap().isEmpty();
 | 
				
			||||||
 | 
				
			|||||||
@ -100,7 +100,7 @@ public class TbRuleEngineProcessingStrategyFactory {
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    log.debug("[{}] Going to reprocess {} messages", queueName, toReprocess.size());
 | 
					                    log.debug("[{}] Going to reprocess {} messages", queueName, toReprocess.size());
 | 
				
			||||||
                    if (log.isTraceEnabled()) {
 | 
					                    if (log.isTraceEnabled()) {
 | 
				
			||||||
                        toReprocess.forEach((id, msg) -> log.trace("Going to reprocess [{}]: {}", id, TbMsg.fromBytes(msg.getValue().getTbMsg().toByteArray(), TbMsgCallback.EMPTY)));
 | 
					                        toReprocess.forEach((id, msg) -> log.trace("Going to reprocess [{}]: {}", id, TbMsg.fromBytes(result.getQueueName(), msg.getValue().getTbMsg().toByteArray(), TbMsgCallback.EMPTY)));
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    if (pauseBetweenRetries > 0) {
 | 
					                    if (pauseBetweenRetries > 0) {
 | 
				
			||||||
                        try {
 | 
					                        try {
 | 
				
			||||||
@ -129,10 +129,10 @@ public class TbRuleEngineProcessingStrategyFactory {
 | 
				
			|||||||
                log.debug("[{}] Reprocessing skipped for {} failed and {} timeout messages", queueName, result.getFailedMap().size(), result.getPendingMap().size());
 | 
					                log.debug("[{}] Reprocessing skipped for {} failed and {} timeout messages", queueName, result.getFailedMap().size(), result.getPendingMap().size());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (log.isTraceEnabled()) {
 | 
					            if (log.isTraceEnabled()) {
 | 
				
			||||||
                result.getFailedMap().forEach((id, msg) -> log.trace("Failed messages [{}]: {}", id, TbMsg.fromBytes(msg.getValue().getTbMsg().toByteArray(), TbMsgCallback.EMPTY)));
 | 
					                result.getFailedMap().forEach((id, msg) -> log.trace("Failed messages [{}]: {}", id, TbMsg.fromBytes(result.getQueueName(), msg.getValue().getTbMsg().toByteArray(), TbMsgCallback.EMPTY)));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (log.isTraceEnabled()) {
 | 
					            if (log.isTraceEnabled()) {
 | 
				
			||||||
                result.getPendingMap().forEach((id, msg) -> log.trace("Timeout messages [{}]: {}", id, TbMsg.fromBytes(msg.getValue().getTbMsg().toByteArray(), TbMsgCallback.EMPTY)));
 | 
					                result.getPendingMap().forEach((id, msg) -> log.trace("Timeout messages [{}]: {}", id, TbMsg.fromBytes(result.getQueueName(), msg.getValue().getTbMsg().toByteArray(), TbMsgCallback.EMPTY)));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return new TbRuleEngineProcessingDecision(true, null);
 | 
					            return new TbRuleEngineProcessingDecision(true, null);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.id.EntityIdFactory;
 | 
				
			|||||||
import org.thingsboard.server.common.data.id.RuleChainId;
 | 
					import org.thingsboard.server.common.data.id.RuleChainId;
 | 
				
			||||||
import org.thingsboard.server.common.data.id.RuleNodeId;
 | 
					import org.thingsboard.server.common.data.id.RuleNodeId;
 | 
				
			||||||
import org.thingsboard.server.common.msg.gen.MsgProtos;
 | 
					import org.thingsboard.server.common.msg.gen.MsgProtos;
 | 
				
			||||||
 | 
					import org.thingsboard.server.common.msg.queue.ServiceQueue;
 | 
				
			||||||
import org.thingsboard.server.common.msg.queue.TbMsgCallback;
 | 
					import org.thingsboard.server.common.msg.queue.TbMsgCallback;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
@ -39,6 +40,7 @@ import java.util.UUID;
 | 
				
			|||||||
@Slf4j
 | 
					@Slf4j
 | 
				
			||||||
public final class TbMsg implements Serializable {
 | 
					public final class TbMsg implements Serializable {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final String queueName;
 | 
				
			||||||
    private final UUID id;
 | 
					    private final UUID id;
 | 
				
			||||||
    private final long ts;
 | 
					    private final long ts;
 | 
				
			||||||
    private final String type;
 | 
					    private final String type;
 | 
				
			||||||
@ -51,39 +53,44 @@ public final class TbMsg implements Serializable {
 | 
				
			|||||||
    //This field is not serialized because we use queues and there is no need to do it
 | 
					    //This field is not serialized because we use queues and there is no need to do it
 | 
				
			||||||
    transient private final TbMsgCallback callback;
 | 
					    transient private final TbMsgCallback callback;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data) {
 | 
					    public static TbMsg newMsg(String queueName, String type, EntityId originator, TbMsgMetaData metaData, String data, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
 | 
				
			||||||
        return new TbMsg(UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, TbMsgCallback.EMPTY);
 | 
					        return new TbMsg(queueName, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
 | 
					    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data) {
 | 
				
			||||||
        return new TbMsg(UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
 | 
					        return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, TbMsgCallback.EMPTY);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static TbMsg newMsg(String queueName, String type, EntityId originator, TbMsgMetaData metaData, String data) {
 | 
				
			||||||
 | 
					        return new TbMsg(queueName, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, TbMsgCallback.EMPTY);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data) {
 | 
					    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data) {
 | 
				
			||||||
        return new TbMsg(UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), dataType, data, null, null, TbMsgCallback.EMPTY);
 | 
					        return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), dataType, data, null, null, TbMsgCallback.EMPTY);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
 | 
					    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
 | 
				
			||||||
        return new TbMsg(UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), dataType, data, ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
 | 
					        return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), dataType, data, ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data, TbMsgCallback callback) {
 | 
					    public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data, TbMsgCallback callback) {
 | 
				
			||||||
        return new TbMsg(UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, callback);
 | 
					        return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, callback);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static TbMsg transformMsg(TbMsg origMsg, String type, EntityId originator, TbMsgMetaData metaData, String data) {
 | 
					    public static TbMsg transformMsg(TbMsg origMsg, String type, EntityId originator, TbMsgMetaData metaData, String data) {
 | 
				
			||||||
        return new TbMsg(origMsg.getId(), origMsg.getTs(), type, originator, metaData.copy(), origMsg.getDataType(),
 | 
					        return new TbMsg(origMsg.getQueueName(), origMsg.getId(), origMsg.getTs(), type, originator, metaData.copy(), origMsg.getDataType(),
 | 
				
			||||||
                data, origMsg.getRuleChainId(), origMsg.getRuleNodeId(), origMsg.getCallback());
 | 
					                data, origMsg.getRuleChainId(), origMsg.getRuleNodeId(), origMsg.getCallback());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static TbMsg newMsg(TbMsg tbMsg, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
 | 
					    public static TbMsg newMsg(TbMsg tbMsg, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
 | 
				
			||||||
        return new TbMsg(UUID.randomUUID(), tbMsg.getTs(), tbMsg.getType(), tbMsg.getOriginator(), tbMsg.getMetaData().copy(),
 | 
					        return new TbMsg(tbMsg.getQueueName(), UUID.randomUUID(), tbMsg.getTs(), tbMsg.getType(), tbMsg.getOriginator(), tbMsg.getMetaData().copy(),
 | 
				
			||||||
                tbMsg.getDataType(), tbMsg.getData(), ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
 | 
					                tbMsg.getDataType(), tbMsg.getData(), ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private TbMsg(UUID id, long ts, String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data,
 | 
					    private TbMsg(String queueName, UUID id, long ts, String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data,
 | 
				
			||||||
                  RuleChainId ruleChainId, RuleNodeId ruleNodeId, TbMsgCallback callback) {
 | 
					                  RuleChainId ruleChainId, RuleNodeId ruleNodeId, TbMsgCallback callback) {
 | 
				
			||||||
        this.id = id;
 | 
					        this.id = id;
 | 
				
			||||||
 | 
					        this.queueName = queueName;
 | 
				
			||||||
        if (ts > 0) {
 | 
					        if (ts > 0) {
 | 
				
			||||||
            this.ts = ts;
 | 
					            this.ts = ts;
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
@ -136,7 +143,7 @@ public final class TbMsg implements Serializable {
 | 
				
			|||||||
        return builder.build().toByteArray();
 | 
					        return builder.build().toByteArray();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static TbMsg fromBytes(byte[] data, TbMsgCallback callback) {
 | 
					    public static TbMsg fromBytes(String queueName, byte[] data, TbMsgCallback callback) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            MsgProtos.TbMsgProto proto = MsgProtos.TbMsgProto.parseFrom(data);
 | 
					            MsgProtos.TbMsgProto proto = MsgProtos.TbMsgProto.parseFrom(data);
 | 
				
			||||||
            TbMsgMetaData metaData = new TbMsgMetaData(proto.getMetaData().getDataMap());
 | 
					            TbMsgMetaData metaData = new TbMsgMetaData(proto.getMetaData().getDataMap());
 | 
				
			||||||
@ -150,18 +157,18 @@ public final class TbMsg implements Serializable {
 | 
				
			|||||||
                ruleNodeId = new RuleNodeId(new UUID(proto.getRuleNodeIdMSB(), proto.getRuleNodeIdLSB()));
 | 
					                ruleNodeId = new RuleNodeId(new UUID(proto.getRuleNodeIdMSB(), proto.getRuleNodeIdLSB()));
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            TbMsgDataType dataType = TbMsgDataType.values()[proto.getDataType()];
 | 
					            TbMsgDataType dataType = TbMsgDataType.values()[proto.getDataType()];
 | 
				
			||||||
            return new TbMsg(UUID.fromString(proto.getId()), proto.getTs(), proto.getType(), entityId, metaData, dataType, proto.getData(), ruleChainId, ruleNodeId, callback);
 | 
					            return new TbMsg(queueName, UUID.fromString(proto.getId()), proto.getTs(), proto.getType(), entityId, metaData, dataType, proto.getData(), ruleChainId, ruleNodeId, callback);
 | 
				
			||||||
        } catch (InvalidProtocolBufferException e) {
 | 
					        } catch (InvalidProtocolBufferException e) {
 | 
				
			||||||
            throw new IllegalStateException("Could not parse protobuf for TbMsg", e);
 | 
					            throw new IllegalStateException("Could not parse protobuf for TbMsg", e);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public TbMsg copyWithRuleChainId(RuleChainId ruleChainId) {
 | 
					    public TbMsg copyWithRuleChainId(RuleChainId ruleChainId) {
 | 
				
			||||||
        return new TbMsg(this.id, this.ts, this.type, this.originator, this.metaData, this.dataType, this.data, ruleChainId, null, callback);
 | 
					        return new TbMsg(this.queueName, this.id, this.ts, this.type, this.originator, this.metaData, this.dataType, this.data, ruleChainId, null, callback);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public TbMsg copyWithRuleNodeId(RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
 | 
					    public TbMsg copyWithRuleNodeId(RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
 | 
				
			||||||
        return new TbMsg(this.id, this.ts, this.type, this.originator, this.metaData, this.dataType, this.data, ruleChainId, ruleNodeId, callback);
 | 
					        return new TbMsg(this.queueName, this.id, this.ts, this.type, this.originator, this.metaData, this.dataType, this.data, ruleChainId, ruleNodeId, callback);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public TbMsgCallback getCallback() {
 | 
					    public TbMsgCallback getCallback() {
 | 
				
			||||||
@ -172,4 +179,8 @@ public final class TbMsg implements Serializable {
 | 
				
			|||||||
            return TbMsgCallback.EMPTY;
 | 
					            return TbMsgCallback.EMPTY;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public String getQueueName() {
 | 
				
			||||||
 | 
					        return queueName != null ? queueName : ServiceQueue.MAIN;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -34,7 +34,7 @@ public class ServiceQueue {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public ServiceQueue(ServiceType type, String queue) {
 | 
					    public ServiceQueue(ServiceType type, String queue) {
 | 
				
			||||||
        this.type = type;
 | 
					        this.type = type;
 | 
				
			||||||
        this.queue = queue;
 | 
					        this.queue = queue != null ? queue : MAIN;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public ServiceType getType() {
 | 
					    public ServiceType getType() {
 | 
				
			||||||
 | 
				
			|||||||
@ -130,7 +130,7 @@ public interface TbContext {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    void ack(TbMsg tbMsg);
 | 
					    void ack(TbMsg tbMsg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data);
 | 
					    TbMsg newMsg(String queueName, String type, EntityId originator, TbMsgMetaData metaData, String data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    TbMsg transformMsg(TbMsg origMsg, String type, EntityId originator, TbMsgMetaData metaData, String data);
 | 
					    TbMsg transformMsg(TbMsg origMsg, String type, EntityId originator, TbMsgMetaData metaData, String data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -136,7 +136,7 @@ public class TbCopyAttributesToEntityViewNode implements TbNode {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void transformAndTellNext(TbContext ctx, TbMsg msg, EntityView entityView) {
 | 
					    private void transformAndTellNext(TbContext ctx, TbMsg msg, EntityView entityView) {
 | 
				
			||||||
        ctx.enqueueForTellNext(ctx.newMsg(msg.getType(), entityView.getId(), msg.getMetaData(), msg.getData()), SUCCESS);
 | 
					        ctx.enqueueForTellNext(ctx.newMsg(msg.getQueueName(), msg.getType(), entityView.getId(), msg.getMetaData(), msg.getData()), SUCCESS);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private boolean attributeContainsInEntityView(String scope, String attrKey, EntityView entityView) {
 | 
					    private boolean attributeContainsInEntityView(String scope, String attrKey, EntityView entityView) {
 | 
				
			||||||
 | 
				
			|||||||
@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.plugin.ComponentType;
 | 
				
			|||||||
import org.thingsboard.server.common.msg.TbMsg;
 | 
					import org.thingsboard.server.common.msg.TbMsg;
 | 
				
			||||||
import org.thingsboard.server.common.msg.TbMsgDataType;
 | 
					import org.thingsboard.server.common.msg.TbMsgDataType;
 | 
				
			||||||
import org.thingsboard.server.common.msg.TbMsgMetaData;
 | 
					import org.thingsboard.server.common.msg.TbMsgMetaData;
 | 
				
			||||||
 | 
					import org.thingsboard.server.common.msg.queue.ServiceQueue;
 | 
				
			||||||
import org.thingsboard.server.common.msg.session.SessionMsgType;
 | 
					import org.thingsboard.server.common.msg.session.SessionMsgType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.UUID;
 | 
					import java.util.UUID;
 | 
				
			||||||
@ -75,7 +76,7 @@ public class TbMsgCountNode implements TbNode {
 | 
				
			|||||||
            TbMsgMetaData metaData = new TbMsgMetaData();
 | 
					            TbMsgMetaData metaData = new TbMsgMetaData();
 | 
				
			||||||
            metaData.putValue("delta", Long.toString(System.currentTimeMillis() - lastScheduledTs + delay));
 | 
					            metaData.putValue("delta", Long.toString(System.currentTimeMillis() - lastScheduledTs + delay));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            TbMsg tbMsg = TbMsg.newMsg(SessionMsgType.POST_TELEMETRY_REQUEST.name(), ctx.getTenantId(), metaData, gson.toJson(telemetryJson));
 | 
					            TbMsg tbMsg = TbMsg.newMsg(msg.getQueueName(), SessionMsgType.POST_TELEMETRY_REQUEST.name(), ctx.getTenantId(), metaData, gson.toJson(telemetryJson));
 | 
				
			||||||
            ctx.enqueueForTellNext(tbMsg, SUCCESS);
 | 
					            ctx.enqueueForTellNext(tbMsg, SUCCESS);
 | 
				
			||||||
            scheduleTickMsg(ctx);
 | 
					            scheduleTickMsg(ctx);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
@ -91,7 +92,7 @@ public class TbMsgCountNode implements TbNode {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        lastScheduledTs = lastScheduledTs + delay;
 | 
					        lastScheduledTs = lastScheduledTs + delay;
 | 
				
			||||||
        long curDelay = Math.max(0L, (lastScheduledTs - curTs));
 | 
					        long curDelay = Math.max(0L, (lastScheduledTs - curTs));
 | 
				
			||||||
        TbMsg tickMsg = ctx.newMsg(TB_MSG_COUNT_NODE_MSG, ctx.getSelfId(), new TbMsgMetaData(), "");
 | 
					        TbMsg tickMsg = ctx.newMsg(ServiceQueue.MAIN, TB_MSG_COUNT_NODE_MSG, ctx.getSelfId(), new TbMsgMetaData(), "");
 | 
				
			||||||
        nextTickId = tickMsg.getId();
 | 
					        nextTickId = tickMsg.getId();
 | 
				
			||||||
        ctx.tellSelf(tickMsg, curDelay);
 | 
					        ctx.tellSelf(tickMsg, curDelay);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -26,6 +26,7 @@ import org.thingsboard.server.common.data.plugin.ComponentType;
 | 
				
			|||||||
import org.thingsboard.server.common.msg.TbMsg;
 | 
					import org.thingsboard.server.common.msg.TbMsg;
 | 
				
			||||||
import org.thingsboard.server.common.msg.TbMsgMetaData;
 | 
					import org.thingsboard.server.common.msg.TbMsgMetaData;
 | 
				
			||||||
import org.thingsboard.server.common.msg.queue.PartitionChangeMsg;
 | 
					import org.thingsboard.server.common.msg.queue.PartitionChangeMsg;
 | 
				
			||||||
 | 
					import org.thingsboard.server.common.msg.queue.ServiceQueue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.UUID;
 | 
					import java.util.UUID;
 | 
				
			||||||
import java.util.concurrent.TimeUnit;
 | 
					import java.util.concurrent.TimeUnit;
 | 
				
			||||||
@ -118,7 +119,7 @@ public class TbMsgGeneratorNode implements TbNode {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        lastScheduledTs = lastScheduledTs + delay;
 | 
					        lastScheduledTs = lastScheduledTs + delay;
 | 
				
			||||||
        long curDelay = Math.max(0L, (lastScheduledTs - curTs));
 | 
					        long curDelay = Math.max(0L, (lastScheduledTs - curTs));
 | 
				
			||||||
        TbMsg tickMsg = ctx.newMsg(TB_MSG_GENERATOR_NODE_MSG, ctx.getSelfId(), new TbMsgMetaData(), "");
 | 
					        TbMsg tickMsg = ctx.newMsg(ServiceQueue.MAIN, TB_MSG_GENERATOR_NODE_MSG, ctx.getSelfId(), new TbMsgMetaData(), "");
 | 
				
			||||||
        nextTickId = tickMsg.getId();
 | 
					        nextTickId = tickMsg.getId();
 | 
				
			||||||
        ctx.tellSelf(tickMsg, curDelay);
 | 
					        ctx.tellSelf(tickMsg, curDelay);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -126,13 +127,13 @@ public class TbMsgGeneratorNode implements TbNode {
 | 
				
			|||||||
    private ListenableFuture<TbMsg> generate(TbContext ctx) {
 | 
					    private ListenableFuture<TbMsg> generate(TbContext ctx) {
 | 
				
			||||||
        return ctx.getJsExecutor().executeAsync(() -> {
 | 
					        return ctx.getJsExecutor().executeAsync(() -> {
 | 
				
			||||||
            if (prevMsg == null) {
 | 
					            if (prevMsg == null) {
 | 
				
			||||||
                prevMsg = ctx.newMsg("", originatorId, new TbMsgMetaData(), "{}");
 | 
					                prevMsg = ctx.newMsg(ServiceQueue.MAIN, "", originatorId, new TbMsgMetaData(), "{}");
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if (initialized) {
 | 
					            if (initialized) {
 | 
				
			||||||
                ctx.logJsEvalRequest();
 | 
					                ctx.logJsEvalRequest();
 | 
				
			||||||
                TbMsg generated = jsEngine.executeGenerate(prevMsg);
 | 
					                TbMsg generated = jsEngine.executeGenerate(prevMsg);
 | 
				
			||||||
                ctx.logJsEvalResponse();
 | 
					                ctx.logJsEvalResponse();
 | 
				
			||||||
                prevMsg = ctx.newMsg(generated.getType(), originatorId, generated.getMetaData(), generated.getData());
 | 
					                prevMsg = ctx.newMsg(ServiceQueue.MAIN, generated.getType(), originatorId, generated.getMetaData(), generated.getData());
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            return prevMsg;
 | 
					            return prevMsg;
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
				
			|||||||
@ -26,6 +26,7 @@ import org.thingsboard.rule.engine.api.util.TbNodeUtils;
 | 
				
			|||||||
import org.thingsboard.server.common.data.plugin.ComponentType;
 | 
					import org.thingsboard.server.common.data.plugin.ComponentType;
 | 
				
			||||||
import org.thingsboard.server.common.msg.TbMsg;
 | 
					import org.thingsboard.server.common.msg.TbMsg;
 | 
				
			||||||
import org.thingsboard.server.common.msg.TbMsgMetaData;
 | 
					import org.thingsboard.server.common.msg.TbMsgMetaData;
 | 
				
			||||||
 | 
					import org.thingsboard.server.common.msg.queue.ServiceQueue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.HashMap;
 | 
					import java.util.HashMap;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
@ -70,7 +71,7 @@ public class TbMsgDelayNode implements TbNode {
 | 
				
			|||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            if (pendingMsgs.size() < config.getMaxPendingMsgs()) {
 | 
					            if (pendingMsgs.size() < config.getMaxPendingMsgs()) {
 | 
				
			||||||
                pendingMsgs.put(msg.getId(), msg);
 | 
					                pendingMsgs.put(msg.getId(), msg);
 | 
				
			||||||
                TbMsg tickMsg = ctx.newMsg(TB_MSG_DELAY_NODE_MSG, ctx.getSelfId(), new TbMsgMetaData(), msg.getId().toString());
 | 
					                TbMsg tickMsg = ctx.newMsg(ServiceQueue.MAIN, TB_MSG_DELAY_NODE_MSG, ctx.getSelfId(), new TbMsgMetaData(), msg.getId().toString());
 | 
				
			||||||
                ctx.tellSelf(tickMsg, getDelay(msg));
 | 
					                ctx.tellSelf(tickMsg, getDelay(msg));
 | 
				
			||||||
                ctx.ack(msg);
 | 
					                ctx.ack(msg);
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
 | 
				
			|||||||
@ -112,10 +112,10 @@ public class TbSendRPCRequestNode implements TbNode {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            ctx.getRpcService().sendRpcRequestToDevice(request, ruleEngineDeviceRpcResponse -> {
 | 
					            ctx.getRpcService().sendRpcRequestToDevice(request, ruleEngineDeviceRpcResponse -> {
 | 
				
			||||||
                if (!ruleEngineDeviceRpcResponse.getError().isPresent()) {
 | 
					                if (!ruleEngineDeviceRpcResponse.getError().isPresent()) {
 | 
				
			||||||
                    TbMsg next = ctx.newMsg(msg.getType(), msg.getOriginator(), msg.getMetaData(), ruleEngineDeviceRpcResponse.getResponse().orElse("{}"));
 | 
					                    TbMsg next = ctx.newMsg(msg.getQueueName(), msg.getType(), msg.getOriginator(), msg.getMetaData(), ruleEngineDeviceRpcResponse.getResponse().orElse("{}"));
 | 
				
			||||||
                    ctx.enqueueForTellNext(next, TbRelationTypes.SUCCESS);
 | 
					                    ctx.enqueueForTellNext(next, TbRelationTypes.SUCCESS);
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    TbMsg next = ctx.newMsg(msg.getType(), msg.getOriginator(), msg.getMetaData(), wrap("error", ruleEngineDeviceRpcResponse.getError().get().name()));
 | 
					                    TbMsg next = ctx.newMsg(msg.getQueueName(), msg.getType(), msg.getOriginator(), msg.getMetaData(), wrap("error", ruleEngineDeviceRpcResponse.getError().get().name()));
 | 
				
			||||||
                    ctx.tellFailure(next, new RuntimeException(ruleEngineDeviceRpcResponse.getError().get().name()));
 | 
					                    ctx.tellFailure(next, new RuntimeException(ruleEngineDeviceRpcResponse.getError().get().name()));
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user