Added ability to process tellNext messages from Queue
This commit is contained in:
parent
c52d0d26d3
commit
ff3fd89ace
@ -40,6 +40,7 @@ import org.thingsboard.server.common.data.SearchTextBasedWithAdditionalInfo;
|
|||||||
import org.thingsboard.server.common.data.alarm.Alarm;
|
import org.thingsboard.server.common.data.alarm.Alarm;
|
||||||
import org.thingsboard.server.common.data.asset.Asset;
|
import org.thingsboard.server.common.data.asset.Asset;
|
||||||
import org.thingsboard.server.common.data.id.EntityId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
|
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.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
import org.thingsboard.server.common.data.rule.RuleNode;
|
import org.thingsboard.server.common.data.rule.RuleNode;
|
||||||
@ -175,6 +176,9 @@ class DefaultTbContext implements TbContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void enqueueForTellNext(TopicPartitionInfo tpi, TbMsg tbMsg, Set<String> relationTypes, Runnable onSuccess, Consumer<Throwable> onFailure) {
|
private void enqueueForTellNext(TopicPartitionInfo tpi, TbMsg tbMsg, Set<String> relationTypes, Runnable onSuccess, Consumer<Throwable> onFailure) {
|
||||||
|
RuleChainId ruleChainId = nodeCtx.getSelf().getRuleChainId();
|
||||||
|
RuleNodeId ruleNodeId = nodeCtx.getSelf().getId();
|
||||||
|
tbMsg = TbMsg.newMsg(tbMsg, ruleChainId, ruleNodeId);
|
||||||
TransportProtos.ToRuleEngineMsg msg = TransportProtos.ToRuleEngineMsg.newBuilder()
|
TransportProtos.ToRuleEngineMsg msg = TransportProtos.ToRuleEngineMsg.newBuilder()
|
||||||
.setTenantIdMSB(getTenantId().getId().getMostSignificantBits())
|
.setTenantIdMSB(getTenantId().getId().getMostSignificantBits())
|
||||||
.setTenantIdLSB(getTenantId().getId().getLeastSignificantBits())
|
.setTenantIdLSB(getTenantId().getId().getLeastSignificantBits())
|
||||||
|
|||||||
@ -34,18 +34,18 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
|
|||||||
import org.thingsboard.server.common.data.rule.RuleChain;
|
import org.thingsboard.server.common.data.rule.RuleChain;
|
||||||
import org.thingsboard.server.common.data.rule.RuleNode;
|
import org.thingsboard.server.common.data.rule.RuleNode;
|
||||||
import org.thingsboard.server.common.msg.TbMsg;
|
import org.thingsboard.server.common.msg.TbMsg;
|
||||||
import org.thingsboard.server.common.msg.queue.PartitionChangeMsg;
|
|
||||||
import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg;
|
import org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg;
|
||||||
|
import org.thingsboard.server.common.msg.queue.PartitionChangeMsg;
|
||||||
import org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg;
|
import org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg;
|
||||||
import org.thingsboard.server.dao.rule.RuleChainService;
|
|
||||||
import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg;
|
|
||||||
import org.thingsboard.server.queue.common.MultipleTbQueueTbMsgCallbackWrapper;
|
|
||||||
import org.thingsboard.server.queue.TbQueueCallback;
|
|
||||||
import org.thingsboard.server.queue.TbQueueProducer;
|
|
||||||
import org.thingsboard.server.queue.common.TbQueueTbMsgCallbackWrapper;
|
|
||||||
import org.thingsboard.server.queue.common.TbProtoQueueMsg;
|
|
||||||
import org.thingsboard.server.common.msg.queue.ServiceType;
|
import org.thingsboard.server.common.msg.queue.ServiceType;
|
||||||
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
|
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
|
||||||
|
import org.thingsboard.server.dao.rule.RuleChainService;
|
||||||
|
import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg;
|
||||||
|
import org.thingsboard.server.queue.TbQueueCallback;
|
||||||
|
import org.thingsboard.server.queue.TbQueueProducer;
|
||||||
|
import org.thingsboard.server.queue.common.MultipleTbQueueTbMsgCallbackWrapper;
|
||||||
|
import org.thingsboard.server.queue.common.TbProtoQueueMsg;
|
||||||
|
import org.thingsboard.server.queue.common.TbQueueTbMsgCallbackWrapper;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -194,6 +194,7 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor<RuleCh
|
|||||||
void onQueueToRuleEngineMsg(QueueToRuleEngineMsg envelope) {
|
void onQueueToRuleEngineMsg(QueueToRuleEngineMsg envelope) {
|
||||||
TbMsg msg = envelope.getTbMsg();
|
TbMsg msg = envelope.getTbMsg();
|
||||||
log.trace("[{}][{}] Processing message [{}]: {}", entityId, firstId, msg.getId(), msg);
|
log.trace("[{}][{}] Processing message [{}]: {}", entityId, firstId, msg.getId(), msg);
|
||||||
|
if (envelope.getRelationTypes() == null) {
|
||||||
try {
|
try {
|
||||||
checkActive();
|
checkActive();
|
||||||
RuleNodeId targetId = msg.getRuleNodeId();
|
RuleNodeId targetId = msg.getRuleNodeId();
|
||||||
@ -206,7 +207,7 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor<RuleCh
|
|||||||
}
|
}
|
||||||
if (targetCtx != null) {
|
if (targetCtx != null) {
|
||||||
log.trace("[{}][{}] Pushing message to target rule node", entityId, targetId);
|
log.trace("[{}][{}] Pushing message to target rule node", entityId, targetId);
|
||||||
pushMsgToNode(firstNode, msg, "");
|
pushMsgToNode(targetCtx, msg, "");
|
||||||
} else {
|
} else {
|
||||||
log.trace("[{}][{}] Rule node does not exist. Probably old message", entityId, targetId);
|
log.trace("[{}][{}] Rule node does not exist. Probably old message", entityId, targetId);
|
||||||
msg.getCallback().onSuccess();
|
msg.getCallback().onSuccess();
|
||||||
@ -214,6 +215,9 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor<RuleCh
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
envelope.getTbMsg().getCallback().onFailure(e);
|
envelope.getTbMsg().getCallback().onFailure(e);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
onTellNext(envelope.getTbMsg(), envelope.getTbMsg().getRuleNodeId(), envelope.getRelationTypes());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void onRuleChainToRuleChainMsg(RuleChainToRuleChainMsg envelope) {
|
void onRuleChainToRuleChainMsg(RuleChainToRuleChainMsg envelope) {
|
||||||
@ -226,22 +230,24 @@ public class RuleChainActorMessageProcessor extends ComponentMsgProcessor<RuleCh
|
|||||||
}
|
}
|
||||||
|
|
||||||
void onTellNext(RuleNodeToRuleChainTellNextMsg envelope) {
|
void onTellNext(RuleNodeToRuleChainTellNextMsg envelope) {
|
||||||
TbMsg msg = envelope.getMsg();
|
onTellNext(envelope.getMsg(), envelope.getOriginator(), envelope.getRelationTypes());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onTellNext(TbMsg msg, RuleNodeId originatorNodeId, Set<String> relationTypes) {
|
||||||
try {
|
try {
|
||||||
checkActive();
|
checkActive();
|
||||||
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, tenantId, entityId);
|
||||||
RuleNodeId originatorNodeId = envelope.getOriginator();
|
|
||||||
List<RuleNodeRelation> relations = nodeRoutes.get(originatorNodeId).stream()
|
List<RuleNodeRelation> relations = nodeRoutes.get(originatorNodeId).stream()
|
||||||
.filter(r -> contains(envelope.getRelationTypes(), r.getType()))
|
.filter(r -> contains(relationTypes, r.getType()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
int relationsCount = relations.size();
|
int relationsCount = relations.size();
|
||||||
if (relationsCount == 0) {
|
if (relationsCount == 0) {
|
||||||
log.trace("[{}][{}][{}] No outbound relations to process", tenantId, entityId, msg.getId());
|
log.trace("[{}][{}][{}] No outbound relations to process", tenantId, entityId, msg.getId());
|
||||||
if (envelope.getRelationTypes().contains(TbRelationTypes.FAILURE)) {
|
if (relationTypes.contains(TbRelationTypes.FAILURE)) {
|
||||||
log.debug("[{}] Failure during message processing by Rule Node [{}]. Enable and see debug events for more info", entityId, envelope.getOriginator().getId());
|
log.debug("[{}] Failure during message processing by Rule Node [{}]. Enable and see debug events for more info", entityId, originatorNodeId.getId());
|
||||||
//TODO 2.5: Introduce our own RuleEngineFailureException to track what is wrong
|
//TODO 2.5: Introduce our own RuleEngineFailureException to track what is wrong
|
||||||
msg.getCallback().onFailure(new RuntimeException("Failure during message processing by Rule Node [" + envelope.getOriginator().getId().toString() + "]"));
|
msg.getCallback().onFailure(new RuntimeException("Failure during message processing by Rule Node [" + originatorNodeId.getId().toString() + "]"));
|
||||||
} else {
|
} else {
|
||||||
msg.getCallback().onSuccess();
|
msg.getCallback().onSuccess();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -73,6 +73,10 @@ public final class TbMsg implements Serializable {
|
|||||||
data, origMsg.getTransactionData(), origMsg.getRuleChainId(), origMsg.getRuleNodeId(), origMsg.getCallback());
|
data, origMsg.getTransactionData(), origMsg.getRuleChainId(), origMsg.getRuleNodeId(), origMsg.getCallback());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static TbMsg newMsg(TbMsg tbMsg, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
|
||||||
|
return new TbMsg(UUID.randomUUID(), tbMsg.getType(), tbMsg.getOriginator(), tbMsg.getMetaData().copy(), tbMsg.getDataType(), tbMsg.getData(), ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
private TbMsg(UUID id, String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data,
|
private TbMsg(UUID id, String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data,
|
||||||
RuleChainId ruleChainId, RuleNodeId ruleNodeId, TbMsgCallback callback) {
|
RuleChainId ruleChainId, RuleNodeId ruleNodeId, TbMsgCallback callback) {
|
||||||
this(id, type, originator, metaData, dataType, data, new TbMsgTransactionData(id, originator), ruleChainId, ruleNodeId, callback);
|
this(id, type, originator, metaData, dataType, data, new TbMsgTransactionData(id, originator), ruleChainId, ruleNodeId, callback);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user