Browse Source

js invoke service: logs added when script going to disable due to exception (special log for TimeoutException)

pull/4753/head
Sergey Matvienko 5 years ago
parent
commit
16a55b16e4
  1. 15
      application/src/main/java/org/thingsboard/server/service/script/AbstractJsInvokeService.java
  2. 2
      application/src/main/java/org/thingsboard/server/service/script/AbstractNashornJsInvokeService.java
  3. 7
      application/src/main/java/org/thingsboard/server/service/script/RemoteJsInvokeService.java
  4. 2
      common/queue/src/main/java/org/thingsboard/server/queue/common/DefaultTbQueueRequestTemplate.java

15
application/src/main/java/org/thingsboard/server/service/script/AbstractJsInvokeService.java

@ -30,6 +30,7 @@ import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
@ -84,8 +85,10 @@ public abstract class AbstractJsInvokeService implements JsInvokeService {
apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.JS_EXEC_COUNT, 1); apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.JS_EXEC_COUNT, 1);
return doInvokeFunction(scriptId, functionName, args); return doInvokeFunction(scriptId, functionName, args);
} else { } else {
return Futures.immediateFailedFuture( String message = "Script invocation is blocked due to maximum error count "
new RuntimeException("Script invocation is blocked due to maximum error count " + getMaxErrors() + "!")); + getMaxErrors() + ", scriptId " + scriptId + "!";
log.warn(message);
return Futures.immediateFailedFuture(new RuntimeException(message));
} }
} else { } else {
return Futures.immediateFailedFuture(new RuntimeException("JS Execution is disabled due to API limits!")); return Futures.immediateFailedFuture(new RuntimeException("JS Execution is disabled due to API limits!"));
@ -117,7 +120,13 @@ public abstract class AbstractJsInvokeService implements JsInvokeService {
protected abstract long getMaxBlacklistDuration(); protected abstract long getMaxBlacklistDuration();
protected void onScriptExecutionError(UUID scriptId) { protected void onScriptExecutionError(UUID scriptId, Throwable t) {
if (t instanceof TimeoutException || (t.getCause() != null && t.getCause() instanceof TimeoutException)) {
log.warn("Script has TimeoutException and will increment counter {} on disabledFunctions for id {}", //TODO remove after test
disabledFunctions.computeIfAbsent(scriptId, key -> new DisableListInfo()).get(),
scriptId);
//return; //timeout is not a good reason to disable function
}
disabledFunctions.computeIfAbsent(scriptId, key -> new DisableListInfo()).incrementAndGet(); disabledFunctions.computeIfAbsent(scriptId, key -> new DisableListInfo()).incrementAndGet();
} }

2
application/src/main/java/org/thingsboard/server/service/script/AbstractNashornJsInvokeService.java

@ -160,7 +160,7 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer
return ((Invocable) engine).invokeFunction(functionName, args); return ((Invocable) engine).invokeFunction(functionName, args);
} }
} catch (Exception e) { } catch (Exception e) {
onScriptExecutionError(scriptId); onScriptExecutionError(scriptId, e);
throw new ExecutionException(e); throw new ExecutionException(e);
} }
}); });

7
application/src/main/java/org/thingsboard/server/service/script/RemoteJsInvokeService.java

@ -193,7 +193,7 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
@Override @Override
public void onFailure(Throwable t) { public void onFailure(Throwable t) {
onScriptExecutionError(scriptId); onScriptExecutionError(scriptId, t);
if (t instanceof TimeoutException || (t.getCause() != null && t.getCause() instanceof TimeoutException)) { if (t instanceof TimeoutException || (t.getCause() != null && t.getCause() instanceof TimeoutException)) {
queueTimeoutMsgs.incrementAndGet(); queueTimeoutMsgs.incrementAndGet();
} }
@ -205,9 +205,10 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
if (invokeResult.getSuccess()) { if (invokeResult.getSuccess()) {
return invokeResult.getResult(); return invokeResult.getResult();
} else { } else {
onScriptExecutionError(scriptId); final RuntimeException e = new RuntimeException(invokeResult.getErrorDetails());
onScriptExecutionError(scriptId, e);
log.debug("[{}] Failed to compile script due to [{}]: {}", scriptId, invokeResult.getErrorCode().name(), invokeResult.getErrorDetails()); log.debug("[{}] Failed to compile script due to [{}]: {}", scriptId, invokeResult.getErrorCode().name(), invokeResult.getErrorDetails());
throw new RuntimeException(invokeResult.getErrorDetails()); throw e;
} }
}, callbackExecutor); }, callbackExecutor);
} }

2
common/queue/src/main/java/org/thingsboard/server/queue/common/DefaultTbQueueRequestTemplate.java

@ -109,7 +109,7 @@ public class DefaultTbQueueRequestTemplate<Request extends TbQueueMsg, Response
log.info("Starting template pool topic {}, for pendingRequests {}", responseTemplate.getTopic(), pendingRequestsCount); log.info("Starting template pool topic {}, for pendingRequests {}", responseTemplate.getTopic(), pendingRequestsCount);
List<Response> responses = doPoll(); //poll js responses List<Response> responses = doPoll(); //poll js responses
//if (responses.size() > 0) { //if (responses.size() > 0) {
log.trace("Completed template poll topic {}, for pendingRequests [{}], received [{}]", responseTemplate.getTopic(), pendingRequestsCount, responses.size()); log.info("Completed template poll topic {}, for pendingRequests [{}], received [{}]", responseTemplate.getTopic(), pendingRequestsCount, responses.size()); //TODO reduce verbose after test
//} //}
responses.forEach(this::processResponse); //this can take a long time responses.forEach(this::processResponse); //this can take a long time
responseTemplate.commit(); responseTemplate.commit();

Loading…
Cancel
Save