thingsboard-memoizeit

Refactoring

9/24/2018 1:20:55 PM

Details

diff --git a/application/src/main/java/org/thingsboard/server/service/cluster/discovery/ZkDiscoveryService.java b/application/src/main/java/org/thingsboard/server/service/cluster/discovery/ZkDiscoveryService.java
index 19a04d8..14196a6 100644
--- a/application/src/main/java/org/thingsboard/server/service/cluster/discovery/ZkDiscoveryService.java
+++ b/application/src/main/java/org/thingsboard/server/service/cluster/discovery/ZkDiscoveryService.java
@@ -143,28 +143,29 @@ public class ZkDiscoveryService implements DiscoveryService, PathChildrenCacheLi
         }
     }
 
-    private boolean reconnectInProgress = false;
-
-    private synchronized ConnectionStateListener checkReconnect(ServerInstance self) {
+    private ConnectionStateListener checkReconnect(ServerInstance self) {
         return (client, newState) -> {
             log.info("[{}:{}] ZK state changed: {}", self.getHost(), self.getPort(), newState);
             if (newState == ConnectionState.LOST) {
-                if (!reconnectInProgress) {
-                    reconnectInProgress = true;
-                    reconnect();
-                }
+                reconnect();
             }
         };
     }
 
-    private void reconnect() {
-        try {
-            client.blockUntilConnected();
-        } catch (InterruptedException e) {
-            log.error("Failed to reconnect to ZK: {}", e.getMessage(), e);
+    private boolean reconnectInProgress = false;
+
+    private synchronized void reconnect() {
+        if (!reconnectInProgress) {
+            reconnectInProgress = true;
+            try {
+                client.blockUntilConnected();
+                publishCurrentServer();
+            } catch (InterruptedException e) {
+                log.error("Failed to reconnect to ZK: {}", e.getMessage(), e);
+            } finally {
+                reconnectInProgress = false;
+            }
         }
-        publishCurrentServer();
-        reconnectInProgress = false;
     }
 
     @Override
diff --git a/application/src/main/java/org/thingsboard/server/service/script/NashornJsInvokeService.java b/application/src/main/java/org/thingsboard/server/service/script/NashornJsInvokeService.java
index c17430f..eadbf28 100644
--- a/application/src/main/java/org/thingsboard/server/service/script/NashornJsInvokeService.java
+++ b/application/src/main/java/org/thingsboard/server/service/script/NashornJsInvokeService.java
@@ -18,22 +18,24 @@ package org.thingsboard.server.service.script;
 
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Service;
 
 @Slf4j
+@ConditionalOnProperty(prefix = "js", value = "evaluator", havingValue = "local", matchIfMissing = true)
 @Service
 public class NashornJsInvokeService extends AbstractNashornJsInvokeService {
 
-    @Value("${actors.rule.js_sandbox.use_js_sandbox}")
+    @Value("${js.local.use_js_sandbox}")
     private boolean useJsSandbox;
 
-    @Value("${actors.rule.js_sandbox.monitor_thread_pool_size}")
+    @Value("${js.local.js_sandbox.monitor_thread_pool_size}")
     private int monitorThreadPoolSize;
 
-    @Value("${actors.rule.js_sandbox.max_cpu_time}")
+    @Value("${js.local.js_sandbox.max_cpu_time}")
     private long maxCpuTime;
 
-    @Value("${actors.rule.js_sandbox.max_errors}")
+    @Value("${js.local.js_sandbox.max_errors}")
     private int maxErrors;
 
     @Override
diff --git a/application/src/main/java/org/thingsboard/server/service/script/RemoteJsInvokeService.java b/application/src/main/java/org/thingsboard/server/service/script/RemoteJsInvokeService.java
new file mode 100644
index 0000000..3a85cc5
--- /dev/null
+++ b/application/src/main/java/org/thingsboard/server/service/script/RemoteJsInvokeService.java
@@ -0,0 +1,46 @@
+/**
+ * Copyright © 2016-2018 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.service.script;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.stereotype.Service;
+import org.thingsboard.server.common.data.id.EntityId;
+
+import java.util.UUID;
+
+@Slf4j
+@ConditionalOnProperty(prefix = "js", value = "evaluator", havingValue = "local", matchIfMissing = true)
+@Service
+public class RemoteJsInvokeService implements JsInvokeService {
+
+    @Override
+    public ListenableFuture<UUID> eval(JsScriptType scriptType, String scriptBody, String... argNames) {
+        return null;
+    }
+
+    @Override
+    public ListenableFuture<Object> invokeFunction(UUID scriptId, EntityId entityId, Object... args) {
+        return null;
+    }
+
+    @Override
+    public ListenableFuture<Void> release(UUID scriptId, EntityId entityId) {
+        return null;
+    }
+
+}
diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml
index 743a860..6851b58 100644
--- a/application/src/main/resources/thingsboard.yml
+++ b/application/src/main/resources/thingsboard.yml
@@ -246,15 +246,6 @@ actors:
     allow_system_mail_service: "${ACTORS_RULE_ALLOW_SYSTEM_MAIL_SERVICE:true}"
     # Specify thread pool size for external call service
     external_call_thread_pool_size: "${ACTORS_RULE_EXTERNAL_CALL_THREAD_POOL_SIZE:10}"
-    js_sandbox:
-      # Use Sandboxed (secured) JavaScript environment
-      use_js_sandbox: "${ACTORS_RULE_JS_SANDBOX_USE_JS_SANDBOX:true}"
-      # Specify thread pool size for JavaScript sandbox resource monitor
-      monitor_thread_pool_size: "${ACTORS_RULE_JS_SANDBOX_MONITOR_THREAD_POOL_SIZE:4}"
-      # Maximum CPU time in milliseconds allowed for script execution
-      max_cpu_time: "${ACTORS_RULE_JS_SANDBOX_MAX_CPU_TIME:100}"
-      # Maximum allowed JavaScript execution errors before JavaScript will be blacklisted
-      max_errors: "${ACTORS_RULE_JS_SANDBOX_MAX_ERRORS:3}"
     chain:
       # Errors for particular actor are persisted once per specified amount of milliseconds
       error_persist_frequency: "${ACTORS_RULE_CHAIN_ERROR_FREQUENCY:3000}"
@@ -413,6 +404,20 @@ audit_log:
 state:
   defaultInactivityTimeoutInSec: 10
   defaultStateCheckIntervalInSec: 10
-# TODO in v2.1
-#  defaultStatePersistenceIntervalInSec: 60
-#  defaultStatePersistencePack: 100
\ No newline at end of file
+
+js:
+  evaluator: "${JS_EVALUATOR:external}" # local/external
+  # Built-in JVM JavaScript environment properties
+  local:
+    # Use Sandboxed (secured) JVM JavaScript environment
+    use_js_sandbox: "${USE_LOCAL_JS_SANDBOX:true}"
+    # Specify thread pool size for JavaScript sandbox resource monitor
+    monitor_thread_pool_size: "${LOCAL_JS_SANDBOX_MONITOR_THREAD_POOL_SIZE:4}"
+    # Maximum CPU time in milliseconds allowed for script execution
+    max_cpu_time: "${LOCAL_JS_SANDBOX_MAX_CPU_TIME:100}"
+    # Maximum allowed JavaScript execution errors before JavaScript will be blacklisted
+    max_errors: "${LOCAL_JS_SANDBOX_MAX_ERRORS:3}"
+  # Remote JavaScript environment properties
+  remote:
+    # Use Sandboxed (secured) JVM JavaScript environment
+    use_js_sandbox: "${USE_REMOTE_JS_SANDBOX:true}"