Commit 045e110e615e552b82bff57c5b96f7768e9f1ffa

Authored by 云中非
1 parent 47514ea1

refactor: 脚本引擎模块调整

TCP模块调整到transport-api
用户脚本测试
Showing 16 changed files with 50 additions and 52 deletions
... ... @@ -98,10 +98,6 @@
98 98 <scope>test</scope>
99 99 </dependency>
100 100
101   - <dependency>
102   - <groupId>org.javadelight</groupId>
103   - <artifactId>delight-nashorn-sandbox</artifactId>
104   - </dependency>
105 101 </dependencies>
106 102
107 103 </project>
... ...
... ... @@ -15,8 +15,6 @@
15 15 */
16 16 package org.thingsboard.server.transport.tcp.adaptors;
17 17
18   -import com.fasterxml.jackson.databind.JsonNode;
19   -import com.fasterxml.jackson.databind.ObjectMapper;
20 18 import com.google.common.util.concurrent.Futures;
21 19 import com.google.common.util.concurrent.ListenableFuture;
22 20 import com.google.common.util.concurrent.MoreExecutors;
... ... @@ -38,8 +36,8 @@ import org.thingsboard.server.common.data.ota.OtaPackageType;
38 36 import org.thingsboard.server.common.transport.adaptor.AdaptorException;
39 37 import org.thingsboard.server.common.transport.adaptor.JsonConverter;
40 38 import org.thingsboard.server.gen.transport.TransportProtos;
41   -import org.thingsboard.server.transport.tcp.script.JsInvokeService;
42   -import org.thingsboard.server.transport.tcp.script.JsScriptType;
  39 +import org.thingsboard.server.common.yunteng.script.YtScriptInvokeService;
  40 +import org.thingsboard.server.common.yunteng.script.YtScriptType;
43 41 import org.thingsboard.server.transport.tcp.session.DeviceSessionCtx;
44 42
45 43 import java.nio.charset.Charset;
... ... @@ -59,7 +57,7 @@ public class JsonTcpAdaptor implements TcpTransportAdaptor {
59 57
60 58 protected static final Charset UTF8 = StandardCharsets.UTF_8;
61 59 @Autowired
62   - private JsInvokeService jsEngine;
  60 + private YtScriptInvokeService jsEngine;
63 61 private static final JsonParser parser = new JsonParser();
64 62 @Override
65 63 public TransportProtos.PostTelemetryMsg convertToPostTelemetry(DeviceSessionCtx ctx, String inbound) throws AdaptorException {
... ... @@ -78,7 +76,7 @@ public class JsonTcpAdaptor implements TcpTransportAdaptor {
78 76
79 77 @Override
80 78 public UUID getJsScriptEngineFunctionId(String scriptBody, String... argNames) throws ExecutionException, InterruptedException {
81   - return jsEngine.eval(JsScriptType.TCP_TRANSPORT_SCRIPT, scriptBody, argNames).get();
  79 + return jsEngine.eval(YtScriptType.TCP_TRANSPORT_SCRIPT, scriptBody, argNames).get();
82 80 }
83 81
84 82 @Override
... ...
... ... @@ -137,6 +137,11 @@
137 137 <groupId>org.bouncycastle</groupId>
138 138 <artifactId>bcpkix-jdk15on</artifactId>
139 139 </dependency>
  140 +
  141 + <dependency>
  142 + <groupId>org.javadelight</groupId>
  143 + <artifactId>delight-nashorn-sandbox</artifactId>
  144 + </dependency>
140 145 </dependencies>
141 146
142 147 <build>
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/AbstractNashornYtScriptInvokeService.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/AbstractNashornJsInvokeService.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import com.google.common.util.concurrent.FutureCallback;
19 19 import com.google.common.util.concurrent.Futures;
... ... @@ -26,6 +26,9 @@ import lombok.extern.slf4j.Slf4j;
26 26 import org.springframework.beans.factory.annotation.Value;
27 27 import org.springframework.scheduling.annotation.Scheduled;
28 28 import org.thingsboard.common.util.ThingsBoardExecutors;
  29 +import org.thingsboard.server.common.yunteng.script.AbstractYtScriptInvokeService;
  30 +import org.thingsboard.server.common.yunteng.script.YtScriptExecutorService;
  31 +import org.thingsboard.server.common.yunteng.script.YtScriptStatCallback;
29 32 import org.thingsboard.server.queue.usagestats.TbApiUsageClient;
30 33
31 34 import javax.annotation.PostConstruct;
... ... @@ -42,7 +45,7 @@ import java.util.concurrent.atomic.AtomicInteger;
42 45 import java.util.concurrent.locks.ReentrantLock;
43 46
44 47 @Slf4j
45   -public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeService {
  48 +public abstract class AbstractNashornYtScriptInvokeService extends AbstractYtScriptInvokeService {
46 49
47 50 private NashornSandbox sandbox;
48 51 private ScriptEngine engine;
... ... @@ -53,13 +56,13 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer
53 56 private final AtomicInteger jsEvalMsgs = new AtomicInteger(0);
54 57 private final AtomicInteger jsFailedMsgs = new AtomicInteger(0);
55 58 private final AtomicInteger jsTimeoutMsgs = new AtomicInteger(0);
56   - private final FutureCallback<UUID> evalCallback = new JsStatCallback<>(jsEvalMsgs, jsTimeoutMsgs, jsFailedMsgs);
57   - private final FutureCallback<Object> invokeCallback = new JsStatCallback<>(jsInvokeMsgs, jsTimeoutMsgs, jsFailedMsgs);
  59 + private final FutureCallback<UUID> evalCallback = new YtScriptStatCallback<>(jsEvalMsgs, jsTimeoutMsgs, jsFailedMsgs);
  60 + private final FutureCallback<Object> invokeCallback = new YtScriptStatCallback<>(jsInvokeMsgs, jsTimeoutMsgs, jsFailedMsgs);
58 61
59 62 private final ReentrantLock evalLock = new ReentrantLock();
60 63
61 64 @Getter
62   - private final TcpJsExecutorService jsExecutor;
  65 + private final YtScriptExecutorService jsExecutor;
63 66
64 67 @Value("${js.local.max_requests_timeout:0}")
65 68 private long maxRequestsTimeout;
... ... @@ -67,7 +70,7 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer
67 70 @Value("${js.local.stats.enabled:false}")
68 71 private boolean statsEnabled;
69 72
70   - public AbstractNashornJsInvokeService(TbApiUsageClient apiUsageClient, TcpJsExecutorService jsExecutor) {
  73 + public AbstractNashornYtScriptInvokeService(TbApiUsageClient apiUsageClient, YtScriptExecutorService jsExecutor) {
71 74 super();
72 75 this.jsExecutor = jsExecutor;
73 76 }
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/AbstractYtScriptInvokeService.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/AbstractJsInvokeService.java
... ... @@ -13,15 +13,12 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import com.google.common.util.concurrent.Futures;
19 19 import com.google.common.util.concurrent.ListenableFuture;
20 20 import lombok.extern.slf4j.Slf4j;
21 21 import org.thingsboard.common.util.ThingsBoardThreadFactory;
22   -import org.thingsboard.server.common.data.ApiUsageRecordKey;
23   -import org.thingsboard.server.common.data.id.CustomerId;
24   -import org.thingsboard.server.common.data.id.TenantId;
25 22
26 23 import java.util.Map;
27 24 import java.util.UUID;
... ... @@ -34,7 +31,7 @@ import java.util.concurrent.atomic.AtomicInteger;
34 31 * Created by ashvayka on 26.09.18.
35 32 */
36 33 @Slf4j
37   -public abstract class AbstractJsInvokeService implements JsInvokeService {
  34 +public abstract class AbstractYtScriptInvokeService implements YtScriptInvokeService {
38 35
39 36
40 37
... ... @@ -57,7 +54,7 @@ public abstract class AbstractJsInvokeService implements JsInvokeService {
57 54 }
58 55
59 56 @Override
60   - public ListenableFuture<UUID> eval(JsScriptType scriptType, String scriptBody, String... argNames) {
  57 + public ListenableFuture<UUID> eval(YtScriptType scriptType, String scriptBody, String... argNames) {
61 58 UUID scriptId = UUID.randomUUID();
62 59 String functionName = "invokeInternal_" + scriptId.toString().replace('-', '_');
63 60 String jsScript = generateJsScript(scriptType, functionName, scriptBody, argNames);
... ... @@ -112,9 +109,9 @@ public abstract class AbstractJsInvokeService implements JsInvokeService {
112 109 disableListInfo.incrementAndGet();
113 110 }
114 111
115   - private String generateJsScript(JsScriptType scriptType, String functionName, String scriptBody, String... argNames) {
116   - if (scriptType == JsScriptType.TCP_TRANSPORT_SCRIPT) {
117   - return YtTcpScriptFactory.generateRuleNodeScript(functionName, scriptBody, argNames);
  112 + private String generateJsScript(YtScriptType scriptType, String functionName, String scriptBody, String... argNames) {
  113 + if (scriptType == YtScriptType.TCP_TRANSPORT_SCRIPT) {
  114 + return YtScriptFactory.generateRuleNodeScript(functionName, scriptBody, argNames);
118 115
119 116 }
120 117 throw new RuntimeException("No script factory implemented for scriptType: " + scriptType);
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/NashornYtScriptInvokeService.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/TcpNashornJsInvokeService.java
... ... @@ -13,12 +13,14 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.springframework.beans.factory.annotation.Value;
20 20 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
21 21 import org.springframework.stereotype.Service;
  22 +import org.thingsboard.server.common.yunteng.script.AbstractNashornYtScriptInvokeService;
  23 +import org.thingsboard.server.common.yunteng.script.YtScriptExecutorService;
22 24 import org.thingsboard.server.queue.usagestats.TbApiUsageClient;
23 25
24 26 import java.util.concurrent.TimeUnit;
... ... @@ -27,7 +29,7 @@ import java.util.concurrent.TimeUnit;
27 29 @ConditionalOnProperty(prefix = "js", value = "evaluator", havingValue = "local", matchIfMissing = true)
28 30 @Service
29 31 public class
30   -TcpNashornJsInvokeService extends AbstractNashornJsInvokeService {
  32 +NashornYtScriptInvokeService extends AbstractNashornYtScriptInvokeService {
31 33
32 34 @Value("${js.local.use_js_sandbox}")
33 35 private boolean useJsSandbox;
... ... @@ -44,7 +46,7 @@ TcpNashornJsInvokeService extends AbstractNashornJsInvokeService {
44 46 @Value("${js.local.max_black_list_duration_sec:60}")
45 47 private int maxBlackListDurationSec;
46 48
47   - public TcpNashornJsInvokeService(TbApiUsageClient apiUsageClient, TcpJsExecutorService jsExecutor) {
  49 + public NashornYtScriptInvokeService(TbApiUsageClient apiUsageClient, YtScriptExecutorService jsExecutor) {
48 50 super(apiUsageClient, jsExecutor);
49 51 }
50 52
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/RemoteJsRequestEncoder.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/RemoteJsRequestEncoder.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import com.google.protobuf.InvalidProtocolBufferException;
19 19 import com.google.protobuf.util.JsonFormat;
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/RemoteJsResponseDecoder.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/RemoteJsResponseDecoder.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import com.google.protobuf.util.JsonFormat;
19 19 import org.thingsboard.server.gen.js.JsInvokeProtos;
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/YtRemoteJsInvokeService.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/TcpRemoteJsInvokeService.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import com.google.common.util.concurrent.FutureCallback;
19 19 import com.google.common.util.concurrent.Futures;
... ... @@ -42,9 +42,9 @@ import java.util.concurrent.*;
42 42 import java.util.concurrent.atomic.AtomicInteger;
43 43
44 44 @Slf4j
45   -@ConditionalOnExpression("'${js.evaluator:null}'=='remote' && ('${service.type:null}'=='monolith' || '${service.type:null}'=='tb-core' || '${service.type:null}'=='tb-rule-engine')")
  45 +@ConditionalOnExpression("'${js.evaluator:null}'=='remote' && ('${service.type:null}'=='monolith' || '${service.type:null}'=='tb-core' || '${service.type:null}'=='tb-transport')")
46 46 @Service
47   -public class TcpRemoteJsInvokeService extends AbstractJsInvokeService {
  47 +public class YtRemoteJsInvokeService extends AbstractYtScriptInvokeService {
48 48
49 49 @Value("${queue.js.max_eval_requests_timeout}")
50 50 private long maxEvalRequestsTimeout;
... ... @@ -70,7 +70,7 @@ public class TcpRemoteJsInvokeService extends AbstractJsInvokeService {
70 70 private final ExecutorService callbackExecutor = Executors.newFixedThreadPool(
71 71 Runtime.getRuntime().availableProcessors(), ThingsBoardThreadFactory.forName("js-executor-remote-callback"));
72 72
73   - public TcpRemoteJsInvokeService(TbApiUsageClient apiUsageClient) {
  73 + public YtRemoteJsInvokeService(TbApiUsageClient apiUsageClient) {
74 74 super();
75 75 }
76 76
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/YtScriptExecutorService.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/TcpJsExecutorService.java
... ... @@ -13,14 +13,14 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import org.springframework.beans.factory.annotation.Value;
19 19 import org.springframework.stereotype.Component;
20 20 import org.thingsboard.common.util.AbstractListeningExecutor;
21 21
22 22 @Component
23   -public class TcpJsExecutorService extends AbstractListeningExecutor {
  23 +public class YtScriptExecutorService extends AbstractListeningExecutor {
24 24
25 25 @Value("${actors.rule.js_thread_pool_size}")
26 26 private int jsExecutorThreadPoolSize;
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/YtScriptFactory.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/YtTcpScriptFactory.java
... ... @@ -13,9 +13,9 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18   -public class YtTcpScriptFactory {
  18 +public class YtScriptFactory {
19 19
20 20 public static final String MSG = "msg";
21 21
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/YtScriptInvokeRequest.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/JsInvokeRequest.java
... ... @@ -13,14 +13,14 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import java.util.List;
19 19
20 20 /**
21 21 * Created by ashvayka on 25.09.18.
22 22 */
23   -public class JsInvokeRequest {
  23 +public class YtScriptInvokeRequest {
24 24
25 25 private String scriptId;
26 26 private String scriptBody;
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/YtScriptInvokeResponse.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/JsInvokeResponse.java
... ... @@ -13,14 +13,14 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import java.util.List;
19 19
20 20 /**
21 21 * Created by ashvayka on 25.09.18.
22 22 */
23   -public class JsInvokeResponse {
  23 +public class YtScriptInvokeResponse {
24 24
25 25 private String scriptId;
26 26 private String scriptBody;
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/YtScriptInvokeService.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/JsInvokeService.java
... ... @@ -13,17 +13,15 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import com.google.common.util.concurrent.ListenableFuture;
19   -import org.thingsboard.server.common.data.id.CustomerId;
20   -import org.thingsboard.server.common.data.id.TenantId;
21 19
22 20 import java.util.UUID;
23 21
24   -public interface JsInvokeService {
  22 +public interface YtScriptInvokeService {
25 23
26   - ListenableFuture<UUID> eval(JsScriptType scriptType, String scriptBody, String... argNames);
  24 + ListenableFuture<UUID> eval(YtScriptType scriptType, String scriptBody, String... argNames);
27 25
28 26 ListenableFuture<Object> invokeFunction(UUID scriptId, Object... args);
29 27
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/YtScriptStatCallback.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/JsStatCallback.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18 18 import com.google.common.util.concurrent.FutureCallback;
19 19 import lombok.AllArgsConstructor;
... ... @@ -23,7 +23,7 @@ import java.util.concurrent.TimeoutException;
23 23 import java.util.concurrent.atomic.AtomicInteger;
24 24
25 25 @AllArgsConstructor
26   -public class JsStatCallback<T> implements FutureCallback<T> {
  26 +public class YtScriptStatCallback<T> implements FutureCallback<T> {
27 27
28 28 private final AtomicInteger jsSuccessMsgs;
29 29 private final AtomicInteger jsTimeoutMsgs;
... ...
common/transport/transport-api/src/main/java/org/thingsboard/server/common/yunteng/script/YtScriptType.java renamed from common/transport/tcp/src/main/java/org/thingsboard/server/transport/tcp/script/JsScriptType.java
... ... @@ -13,9 +13,8 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.transport.tcp.script;
  16 +package org.thingsboard.server.common.yunteng.script;
17 17
18   -public enum JsScriptType {
19   - RULE_NODE_SCRIPT,
  18 +public enum YtScriptType {
20 19 TCP_TRANSPORT_SCRIPT
21 20 }
... ...