...
|
...
|
@@ -18,27 +18,27 @@ package org.thingsboard.server.common.msg; |
18
|
18
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
19
|
19
|
import com.google.protobuf.ByteString;
|
20
|
20
|
import com.google.protobuf.InvalidProtocolBufferException;
|
|
21
|
+import lombok.AccessLevel;
|
21
|
22
|
import lombok.Builder;
|
22
|
23
|
import lombok.Data;
|
|
24
|
+import lombok.Getter;
|
23
|
25
|
import lombok.extern.slf4j.Slf4j;
|
24
|
26
|
import org.thingsboard.server.common.data.id.EntityId;
|
25
|
27
|
import org.thingsboard.server.common.data.id.EntityIdFactory;
|
26
|
28
|
import org.thingsboard.server.common.data.id.RuleChainId;
|
27
|
29
|
import org.thingsboard.server.common.data.id.RuleNodeId;
|
28
|
30
|
import org.thingsboard.server.common.msg.gen.MsgProtos;
|
29
|
|
-import org.thingsboard.server.common.msg.queue.RuleNodeInfo;
|
30
|
31
|
import org.thingsboard.server.common.msg.queue.ServiceQueue;
|
31
|
32
|
import org.thingsboard.server.common.msg.queue.TbMsgCallback;
|
32
|
33
|
|
33
|
|
-import java.io.IOException;
|
34
|
34
|
import java.io.Serializable;
|
35
|
35
|
import java.util.UUID;
|
|
36
|
+import java.util.concurrent.atomic.AtomicInteger;
|
36
|
37
|
|
37
|
38
|
/**
|
38
|
39
|
* Created by ashvayka on 13.01.18.
|
39
|
40
|
*/
|
40
|
41
|
@Data
|
41
|
|
-@Builder
|
42
|
42
|
@Slf4j
|
43
|
43
|
public final class TbMsg implements Serializable {
|
44
|
44
|
|
...
|
...
|
@@ -52,51 +52,63 @@ public final class TbMsg implements Serializable { |
52
|
52
|
private final String data;
|
53
|
53
|
private final RuleChainId ruleChainId;
|
54
|
54
|
private final RuleNodeId ruleNodeId;
|
|
55
|
+ @Getter(value = AccessLevel.NONE)
|
|
56
|
+ private final AtomicInteger ruleNodeExecCounter;
|
|
57
|
+
|
|
58
|
+ public int getAndIncrementRuleNodeCounter() {
|
|
59
|
+ return ruleNodeExecCounter.getAndIncrement();
|
|
60
|
+ }
|
|
61
|
+
|
55
|
62
|
//This field is not serialized because we use queues and there is no need to do it
|
56
|
63
|
@JsonIgnore
|
57
|
64
|
transient private final TbMsgCallback callback;
|
58
|
65
|
|
59
|
66
|
public static TbMsg newMsg(String queueName, String type, EntityId originator, TbMsgMetaData metaData, String data, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
|
60
|
|
- return new TbMsg(queueName, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
|
|
67
|
+ return new TbMsg(queueName, UUID.randomUUID(), System.currentTimeMillis(), type, originator,
|
|
68
|
+ metaData.copy(), TbMsgDataType.JSON, data, ruleChainId, ruleNodeId, 0, TbMsgCallback.EMPTY);
|
61
|
69
|
}
|
62
|
70
|
|
63
|
71
|
public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data) {
|
64
|
|
- return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, TbMsgCallback.EMPTY);
|
|
72
|
+ return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, 0, TbMsgCallback.EMPTY);
|
65
|
73
|
}
|
66
|
74
|
|
|
75
|
+ // REALLY NEW MSG
|
|
76
|
+
|
67
|
77
|
public static TbMsg newMsg(String queueName, String type, EntityId originator, TbMsgMetaData metaData, String data) {
|
68
|
|
- return new TbMsg(queueName, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, TbMsgCallback.EMPTY);
|
|
78
|
+ return new TbMsg(queueName, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, 0, TbMsgCallback.EMPTY);
|
69
|
79
|
}
|
70
|
80
|
|
71
|
81
|
public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data) {
|
72
|
|
- return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), dataType, data, null, null, TbMsgCallback.EMPTY);
|
|
82
|
+ return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), dataType, data, null, null, 0, TbMsgCallback.EMPTY);
|
73
|
83
|
}
|
74
|
84
|
|
|
85
|
+ // For Tests only
|
|
86
|
+
|
75
|
87
|
public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
|
76
|
|
- return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), dataType, data, ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
|
|
88
|
+ return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), dataType, data, ruleChainId, ruleNodeId, 0, TbMsgCallback.EMPTY);
|
77
|
89
|
}
|
78
|
90
|
|
79
|
91
|
public static TbMsg newMsg(String type, EntityId originator, TbMsgMetaData metaData, String data, TbMsgCallback callback) {
|
80
|
|
- return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, callback);
|
|
92
|
+ return new TbMsg(ServiceQueue.MAIN, UUID.randomUUID(), System.currentTimeMillis(), type, originator, metaData.copy(), TbMsgDataType.JSON, data, null, null, 0, callback);
|
81
|
93
|
}
|
82
|
94
|
|
83
|
|
- public static TbMsg transformMsg(TbMsg origMsg, String type, EntityId originator, TbMsgMetaData metaData, String data) {
|
84
|
|
- return new TbMsg(origMsg.getQueueName(), origMsg.getId(), origMsg.getTs(), type, originator, metaData.copy(), origMsg.getDataType(),
|
85
|
|
- data, origMsg.getRuleChainId(), origMsg.getRuleNodeId(), origMsg.getCallback());
|
|
95
|
+ public static TbMsg transformMsg(TbMsg tbMsg, String type, EntityId originator, TbMsgMetaData metaData, String data) {
|
|
96
|
+ return new TbMsg(tbMsg.getQueueName(), tbMsg.getId(), tbMsg.getTs(), type, originator, metaData.copy(), tbMsg.getDataType(),
|
|
97
|
+ data, tbMsg.getRuleChainId(), tbMsg.getRuleNodeId(), tbMsg.ruleNodeExecCounter.get(), tbMsg.getCallback());
|
86
|
98
|
}
|
87
|
99
|
|
88
|
|
- public static TbMsg transformMsg(TbMsg origMsg, RuleChainId ruleChainId) {
|
89
|
|
- return new TbMsg(origMsg.queueName, origMsg.id, origMsg.ts, origMsg.type, origMsg.originator, origMsg.metaData, origMsg.dataType,
|
90
|
|
- origMsg.data, ruleChainId, null, origMsg.getCallback());
|
|
100
|
+ public static TbMsg transformMsg(TbMsg tbMsg, RuleChainId ruleChainId) {
|
|
101
|
+ return new TbMsg(tbMsg.queueName, tbMsg.id, tbMsg.ts, tbMsg.type, tbMsg.originator, tbMsg.metaData, tbMsg.dataType,
|
|
102
|
+ tbMsg.data, ruleChainId, null, tbMsg.ruleNodeExecCounter.get(), tbMsg.getCallback());
|
91
|
103
|
}
|
92
|
104
|
|
93
|
105
|
public static TbMsg newMsg(TbMsg tbMsg, RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
|
94
|
106
|
return new TbMsg(tbMsg.getQueueName(), UUID.randomUUID(), tbMsg.getTs(), tbMsg.getType(), tbMsg.getOriginator(), tbMsg.getMetaData().copy(),
|
95
|
|
- tbMsg.getDataType(), tbMsg.getData(), ruleChainId, ruleNodeId, TbMsgCallback.EMPTY);
|
|
107
|
+ tbMsg.getDataType(), tbMsg.getData(), ruleChainId, ruleNodeId, tbMsg.ruleNodeExecCounter.get(), TbMsgCallback.EMPTY);
|
96
|
108
|
}
|
97
|
109
|
|
98
|
110
|
private TbMsg(String queueName, UUID id, long ts, String type, EntityId originator, TbMsgMetaData metaData, TbMsgDataType dataType, String data,
|
99
|
|
- RuleChainId ruleChainId, RuleNodeId ruleNodeId, TbMsgCallback callback) {
|
|
111
|
+ RuleChainId ruleChainId, RuleNodeId ruleNodeId, int ruleNodeExecCounter, TbMsgCallback callback) {
|
100
|
112
|
this.id = id;
|
101
|
113
|
this.queueName = queueName;
|
102
|
114
|
if (ts > 0) {
|
...
|
...
|
@@ -111,6 +123,7 @@ public final class TbMsg implements Serializable { |
111
|
123
|
this.data = data;
|
112
|
124
|
this.ruleChainId = ruleChainId;
|
113
|
125
|
this.ruleNodeId = ruleNodeId;
|
|
126
|
+ this.ruleNodeExecCounter = new AtomicInteger(ruleNodeExecCounter);
|
114
|
127
|
if (callback != null) {
|
115
|
128
|
this.callback = callback;
|
116
|
129
|
} else {
|
...
|
...
|
@@ -147,6 +160,7 @@ public final class TbMsg implements Serializable { |
147
|
160
|
|
148
|
161
|
builder.setDataType(msg.getDataType().ordinal());
|
149
|
162
|
builder.setData(msg.getData());
|
|
163
|
+ builder.setRuleNodeExecCounter(msg.ruleNodeExecCounter.get());
|
150
|
164
|
return builder.build().toByteArray();
|
151
|
165
|
}
|
152
|
166
|
|
...
|
...
|
@@ -164,18 +178,18 @@ public final class TbMsg implements Serializable { |
164
|
178
|
ruleNodeId = new RuleNodeId(new UUID(proto.getRuleNodeIdMSB(), proto.getRuleNodeIdLSB()));
|
165
|
179
|
}
|
166
|
180
|
TbMsgDataType dataType = TbMsgDataType.values()[proto.getDataType()];
|
167
|
|
- return new TbMsg(queueName, UUID.fromString(proto.getId()), proto.getTs(), proto.getType(), entityId, metaData, dataType, proto.getData(), ruleChainId, ruleNodeId, callback);
|
|
181
|
+ return new TbMsg(queueName, UUID.fromString(proto.getId()), proto.getTs(), proto.getType(), entityId, metaData, dataType, proto.getData(), ruleChainId, ruleNodeId, proto.getRuleNodeExecCounter(), callback);
|
168
|
182
|
} catch (InvalidProtocolBufferException e) {
|
169
|
183
|
throw new IllegalStateException("Could not parse protobuf for TbMsg", e);
|
170
|
184
|
}
|
171
|
185
|
}
|
172
|
186
|
|
173
|
187
|
public TbMsg copyWithRuleChainId(RuleChainId ruleChainId) {
|
174
|
|
- return new TbMsg(this.queueName, this.id, this.ts, this.type, this.originator, this.metaData, this.dataType, this.data, ruleChainId, null, callback);
|
|
188
|
+ return new TbMsg(this.queueName, this.id, this.ts, this.type, this.originator, this.metaData, this.dataType, this.data, ruleChainId, null, this.ruleNodeExecCounter.get(), callback);
|
175
|
189
|
}
|
176
|
190
|
|
177
|
191
|
public TbMsg copyWithRuleNodeId(RuleChainId ruleChainId, RuleNodeId ruleNodeId) {
|
178
|
|
- return new TbMsg(this.queueName, this.id, this.ts, this.type, this.originator, this.metaData, this.dataType, this.data, ruleChainId, ruleNodeId, callback);
|
|
192
|
+ return new TbMsg(this.queueName, this.id, this.ts, this.type, this.originator, this.metaData, this.dataType, this.data, ruleChainId, ruleNodeId, this.ruleNodeExecCounter.get(), callback);
|
179
|
193
|
}
|
180
|
194
|
|
181
|
195
|
public TbMsgCallback getCallback() {
|
...
|
...
|
|