Commit d44adcfe7585b800a8585abae2281c861b668b0a
Committed by
Andrew Shvayka
1 parent
9093206b
added removing rpc request from pending map after rpc deleting
Showing
13 changed files
with
103 additions
and
4 deletions
... | ... | @@ -95,6 +95,7 @@ public class AppActor extends ContextAwareActor { |
95 | 95 | case DEVICE_RPC_REQUEST_TO_DEVICE_ACTOR_MSG: |
96 | 96 | case DEVICE_RPC_RESPONSE_TO_DEVICE_ACTOR_MSG: |
97 | 97 | case SERVER_RPC_RESPONSE_TO_DEVICE_ACTOR_MSG: |
98 | + case REMOVE_RPC_TO_DEVICE_ACTOR_MSG: | |
98 | 99 | onToDeviceActorMsg((TenantAwareMsg) msg, true); |
99 | 100 | break; |
100 | 101 | case EDGE_EVENT_UPDATE_TO_EDGE_SESSION_MSG: | ... | ... |
... | ... | @@ -28,6 +28,7 @@ import org.thingsboard.server.common.data.id.TenantId; |
28 | 28 | import org.thingsboard.server.common.msg.TbActorMsg; |
29 | 29 | import org.thingsboard.server.common.msg.timeout.DeviceActorServerSideRpcTimeoutMsg; |
30 | 30 | import org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg; |
31 | +import org.thingsboard.server.service.rpc.RemoveRpcActorMsg; | |
31 | 32 | import org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg; |
32 | 33 | import org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper; |
33 | 34 | |
... | ... | @@ -84,6 +85,9 @@ public class DeviceActor extends ContextAwareActor { |
84 | 85 | case DEVICE_EDGE_UPDATE_TO_DEVICE_ACTOR_MSG: |
85 | 86 | processor.processEdgeUpdate((DeviceEdgeUpdateMsg) msg); |
86 | 87 | break; |
88 | + case REMOVE_RPC_TO_DEVICE_ACTOR_MSG: | |
89 | + processor.processRemoveRpc(ctx, (RemoveRpcActorMsg) msg); | |
90 | + break; | |
87 | 91 | default: |
88 | 92 | return false; |
89 | 93 | } | ... | ... |
... | ... | @@ -88,6 +88,7 @@ import org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceAct |
88 | 88 | import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto; |
89 | 89 | import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; |
90 | 90 | import org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg; |
91 | +import org.thingsboard.server.service.rpc.RemoveRpcActorMsg; | |
91 | 92 | import org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg; |
92 | 93 | import org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper; |
93 | 94 | |
... | ... | @@ -263,6 +264,21 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
263 | 264 | } |
264 | 265 | } |
265 | 266 | |
267 | + void processRemoveRpc(TbActorCtx context, RemoveRpcActorMsg msg) { | |
268 | + log.debug("[{}] Processing remove rpc command", msg.getRequestId()); | |
269 | + Integer requestId = null; | |
270 | + for (Map.Entry<Integer, ToDeviceRpcRequestMetadata> entry : toDeviceRpcPendingMap.entrySet()) { | |
271 | + if (entry.getValue().getMsg().getMsg().getId().equals(msg.getRequestId())) { | |
272 | + requestId = entry.getKey(); | |
273 | + break; | |
274 | + } | |
275 | + } | |
276 | + | |
277 | + if (requestId != null) { | |
278 | + toDeviceRpcPendingMap.remove(requestId); | |
279 | + } | |
280 | + } | |
281 | + | |
266 | 282 | private void registerPendingRpcRequest(TbActorCtx context, ToDeviceRpcRequestActorMsg msg, boolean sent, ToDeviceRpcRequestMsg rpcRequest, long timeout) { |
267 | 283 | toDeviceRpcPendingMap.put(rpcRequest.getRequestId(), new ToDeviceRpcRequestMetadata(msg, sent)); |
268 | 284 | DeviceActorServerSideRpcTimeoutMsg timeoutMsg = new DeviceActorServerSideRpcTimeoutMsg(rpcRequest.getRequestId(), timeout); | ... | ... |
... | ... | @@ -165,6 +165,7 @@ public class TenantActor extends RuleChainManagerActor { |
165 | 165 | case DEVICE_RPC_REQUEST_TO_DEVICE_ACTOR_MSG: |
166 | 166 | case DEVICE_RPC_RESPONSE_TO_DEVICE_ACTOR_MSG: |
167 | 167 | case SERVER_RPC_RESPONSE_TO_DEVICE_ACTOR_MSG: |
168 | + case REMOVE_RPC_TO_DEVICE_ACTOR_MSG: | |
168 | 169 | onToDeviceActorMsg((DeviceAwareMsg) msg, true); |
169 | 170 | break; |
170 | 171 | case RULE_CHAIN_TO_RULE_CHAIN_MSG: | ... | ... |
... | ... | @@ -57,7 +57,7 @@ import java.util.UUID; |
57 | 57 | public abstract class AbstractRpcController extends BaseController { |
58 | 58 | |
59 | 59 | @Autowired |
60 | - private TbCoreDeviceRpcService deviceRpcService; | |
60 | + protected TbCoreDeviceRpcService deviceRpcService; | |
61 | 61 | |
62 | 62 | @Autowired |
63 | 63 | private AccessValidator accessValidator; | ... | ... |
... | ... | @@ -27,6 +27,7 @@ import org.springframework.web.bind.annotation.RequestParam; |
27 | 27 | import org.springframework.web.bind.annotation.ResponseBody; |
28 | 28 | import org.springframework.web.bind.annotation.RestController; |
29 | 29 | import org.springframework.web.context.request.async.DeferredResult; |
30 | +import org.thingsboard.common.util.JacksonUtil; | |
30 | 31 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
31 | 32 | import org.thingsboard.server.common.data.id.DeviceId; |
32 | 33 | import org.thingsboard.server.common.data.id.RpcId; |
... | ... | @@ -35,11 +36,16 @@ import org.thingsboard.server.common.data.page.PageData; |
35 | 36 | import org.thingsboard.server.common.data.page.PageLink; |
36 | 37 | import org.thingsboard.server.common.data.rpc.Rpc; |
37 | 38 | import org.thingsboard.server.common.data.rpc.RpcStatus; |
39 | +import org.thingsboard.server.common.msg.TbMsg; | |
40 | +import org.thingsboard.server.common.msg.TbMsgMetaData; | |
38 | 41 | import org.thingsboard.server.queue.util.TbCoreComponent; |
42 | +import org.thingsboard.server.service.rpc.RemoveRpcActorMsg; | |
39 | 43 | import org.thingsboard.server.service.security.permission.Operation; |
40 | 44 | |
41 | 45 | import java.util.UUID; |
42 | 46 | |
47 | +import static org.thingsboard.server.common.data.DataConstants.RPC_DELETED; | |
48 | + | |
43 | 49 | @RestController |
44 | 50 | @TbCoreComponent |
45 | 51 | @RequestMapping(TbUrlConstants.RPC_V2_URL_PREFIX) |
... | ... | @@ -100,7 +106,21 @@ public class RpcV2Controller extends AbstractRpcController { |
100 | 106 | public void deleteResource(@PathVariable("rpcId") String strRpc) throws ThingsboardException { |
101 | 107 | checkParameter("RpcId", strRpc); |
102 | 108 | try { |
103 | - rpcService.deleteRpc(getTenantId(), new RpcId(UUID.fromString(strRpc))); | |
109 | + RpcId rpcId = new RpcId(UUID.fromString(strRpc)); | |
110 | + Rpc rpc = checkRpcId(rpcId, Operation.DELETE); | |
111 | + | |
112 | + if (rpc != null) { | |
113 | + if (rpc.getStatus().equals(RpcStatus.QUEUED)) { | |
114 | + RemoveRpcActorMsg removeMsg = new RemoveRpcActorMsg(getTenantId(), rpc.getDeviceId(), rpc.getUuidId()); | |
115 | + log.trace("[{}] Forwarding msg {} to queue actor!", rpc.getDeviceId(), rpc); | |
116 | + tbClusterService.pushMsgToCore(removeMsg, null); | |
117 | + } | |
118 | + | |
119 | + rpcService.deleteRpc(getTenantId(), rpcId); | |
120 | + | |
121 | + TbMsg msg = TbMsg.newMsg(RPC_DELETED, rpc.getDeviceId(), TbMsgMetaData.EMPTY, JacksonUtil.toString(rpc)); | |
122 | + tbClusterService.pushMsgToRuleEngine(getTenantId(), rpc.getDeviceId(), msg, null); | |
123 | + } | |
104 | 124 | } catch (Exception e) { |
105 | 125 | throw handleException(e); |
106 | 126 | } | ... | ... |
... | ... | @@ -139,6 +139,12 @@ public class DefaultTbCoreDeviceRpcService implements TbCoreDeviceRpcService { |
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
142 | + @Override | |
143 | + public void processRemoveRpc(RemoveRpcActorMsg removeRpcMsg) { | |
144 | + log.trace("[{}][{}] Processing remove RPC [{}]", removeRpcMsg.getTenantId(), removeRpcMsg.getRequestId(), removeRpcMsg.getDeviceId()); | |
145 | + actorContext.tellWithHighPriority(removeRpcMsg); | |
146 | + } | |
147 | + | |
142 | 148 | private void sendRpcResponseToTbRuleEngine(String originServiceId, FromDeviceRpcResponse response) { |
143 | 149 | if (serviceId.equals(originServiceId)) { |
144 | 150 | if (tbRuleEngineRpcService.isPresent()) { | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.service.rpc; | |
17 | + | |
18 | +import lombok.Getter; | |
19 | +import lombok.RequiredArgsConstructor; | |
20 | +import lombok.ToString; | |
21 | +import org.thingsboard.rule.engine.api.msg.ToDeviceActorNotificationMsg; | |
22 | +import org.thingsboard.server.common.data.id.DeviceId; | |
23 | +import org.thingsboard.server.common.data.id.TenantId; | |
24 | +import org.thingsboard.server.common.msg.MsgType; | |
25 | + | |
26 | +import java.util.UUID; | |
27 | + | |
28 | +@ToString | |
29 | +@RequiredArgsConstructor | |
30 | +public class RemoveRpcActorMsg implements ToDeviceActorNotificationMsg { | |
31 | + | |
32 | + @Getter | |
33 | + private final TenantId tenantId; | |
34 | + @Getter | |
35 | + private final DeviceId deviceId; | |
36 | + | |
37 | + @Getter | |
38 | + private final UUID requestId; | |
39 | + | |
40 | + @Override | |
41 | + public MsgType getMsgType() { | |
42 | + return MsgType.REMOVE_RPC_TO_DEVICE_ACTOR_MSG; | |
43 | + } | |
44 | +} | ... | ... |
... | ... | @@ -89,6 +89,7 @@ public class DataConstants { |
89 | 89 | public static final String RPC_SUCCESSFUL = "RPC_SUCCESSFUL"; |
90 | 90 | public static final String RPC_TIMEOUT = "RPC_TIMEOUT"; |
91 | 91 | public static final String RPC_FAILED = "RPC_FAILED"; |
92 | + public static final String RPC_DELETED = "RPC_DELETED"; | |
92 | 93 | |
93 | 94 | public static final String DEFAULT_SECRET_KEY = ""; |
94 | 95 | public static final String SECRET_KEY_FIELD_NAME = "secretKey"; | ... | ... |
... | ... | @@ -33,7 +33,7 @@ import org.thingsboard.server.common.msg.session.SessionMsgType; |
33 | 33 | type = ComponentType.FILTER, |
34 | 34 | name = "message type switch", |
35 | 35 | configClazz = EmptyNodeConfiguration.class, |
36 | - relationTypes = {"Post attributes", "Post telemetry", "RPC Request from Device", "RPC Request to Device", "RPC Queued", "RPC Delivered", "RPC Successful", "RPC Timeout", "RPC Failed", | |
36 | + relationTypes = {"Post attributes", "Post telemetry", "RPC Request from Device", "RPC Request to Device", "RPC Queued", "RPC Delivered", "RPC Successful", "RPC Timeout", "RPC Failed", "RPC Deleted", | |
37 | 37 | "Activity Event", "Inactivity Event", "Connect Event", "Disconnect Event", "Entity Created", "Entity Updated", "Entity Deleted", "Entity Assigned", |
38 | 38 | "Entity Unassigned", "Attributes Updated", "Attributes Deleted", "Alarm Acknowledged", "Alarm Cleared", "Other", "Entity Assigned From Tenant", "Entity Assigned To Tenant", |
39 | 39 | "Timeseries Updated", "Timeseries Deleted"}, |
... | ... | @@ -105,6 +105,8 @@ public class TbMsgTypeSwitchNode implements TbNode { |
105 | 105 | relationType = "RPC Timeout"; |
106 | 106 | } else if (msg.getType().equals(DataConstants.RPC_FAILED)) { |
107 | 107 | relationType = "RPC Failed"; |
108 | + } else if (msg.getType().equals(DataConstants.RPC_DELETED)) { | |
109 | + relationType = "RPC Deleted"; | |
108 | 110 | } else { |
109 | 111 | relationType = "Other"; |
110 | 112 | } | ... | ... |
... | ... | @@ -118,7 +118,7 @@ public class TbSendRPCRequestNode implements TbNode { |
118 | 118 | .build(); |
119 | 119 | |
120 | 120 | ctx.getRpcService().sendRpcRequestToDevice(request, ruleEngineDeviceRpcResponse -> { |
121 | - if (!ruleEngineDeviceRpcResponse.getError().isPresent()) { | |
121 | + if (ruleEngineDeviceRpcResponse.getError().isEmpty()) { | |
122 | 122 | TbMsg next = ctx.newMsg(msg.getQueueName(), msg.getType(), msg.getOriginator(), msg.getCustomerId(), msg.getMetaData(), ruleEngineDeviceRpcResponse.getResponse().orElse("{}")); |
123 | 123 | ctx.enqueueForTellNext(next, TbRelationTypes.SUCCESS); |
124 | 124 | } else { | ... | ... |