Commit e823dfb85d5d13cc47b8fb09ac33a69eff5f2d3b

Authored by Andrii Shvaika
1 parent 28f1993a

Min RPC timeout

@@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
20 import com.google.common.util.concurrent.FutureCallback; 20 import com.google.common.util.concurrent.FutureCallback;
21 import lombok.extern.slf4j.Slf4j; 21 import lombok.extern.slf4j.Slf4j;
22 import org.springframework.beans.factory.annotation.Autowired; 22 import org.springframework.beans.factory.annotation.Autowired;
  23 +import org.springframework.beans.factory.annotation.Value;
23 import org.springframework.http.HttpStatus; 24 import org.springframework.http.HttpStatus;
24 import org.springframework.http.ResponseEntity; 25 import org.springframework.http.ResponseEntity;
25 import org.springframework.security.access.prepost.PreAuthorize; 26 import org.springframework.security.access.prepost.PreAuthorize;
@@ -65,7 +66,6 @@ import java.util.UUID; @@ -65,7 +66,6 @@ import java.util.UUID;
65 @Slf4j 66 @Slf4j
66 public class RpcController extends BaseController { 67 public class RpcController extends BaseController {
67 68
68 - public static final int DEFAULT_TIMEOUT = 10000;  
69 protected final ObjectMapper jsonMapper = new ObjectMapper(); 69 protected final ObjectMapper jsonMapper = new ObjectMapper();
70 70
71 @Autowired 71 @Autowired
@@ -74,6 +74,12 @@ public class RpcController extends BaseController { @@ -74,6 +74,12 @@ public class RpcController extends BaseController {
74 @Autowired 74 @Autowired
75 private AccessValidator accessValidator; 75 private AccessValidator accessValidator;
76 76
  77 + @Value("${server.rest.server_side_rpc.min_timeout:5000}")
  78 + private long minTimeout;
  79 +
  80 + @Value("${server.rest.server_side_rpc.default_timeout:10000}")
  81 + private long defaultTimeout;
  82 +
77 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") 83 @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')")
78 @RequestMapping(value = "/oneway/{deviceId}", method = RequestMethod.POST) 84 @RequestMapping(value = "/oneway/{deviceId}", method = RequestMethod.POST)
79 @ResponseBody 85 @ResponseBody
@@ -100,7 +106,8 @@ public class RpcController extends BaseController { @@ -100,7 +106,8 @@ public class RpcController extends BaseController {
100 SecurityUser currentUser = getCurrentUser(); 106 SecurityUser currentUser = getCurrentUser();
101 TenantId tenantId = currentUser.getTenantId(); 107 TenantId tenantId = currentUser.getTenantId();
102 final DeferredResult<ResponseEntity> response = new DeferredResult<>(); 108 final DeferredResult<ResponseEntity> response = new DeferredResult<>();
103 - long timeout = System.currentTimeMillis() + (cmd.getTimeout() != null ? cmd.getTimeout() : DEFAULT_TIMEOUT); 109 + long timeout = cmd.getTimeout() != null ? cmd.getTimeout() : defaultTimeout;
  110 + long expTime = System.currentTimeMillis() + Math.max(minTimeout, timeout);
104 ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(cmd.getMethodName(), cmd.getRequestData()); 111 ToDeviceRpcRequestBody body = new ToDeviceRpcRequestBody(cmd.getMethodName(), cmd.getRequestData());
105 accessValidator.validate(currentUser, Operation.RPC_CALL, deviceId, new HttpValidationCallback(response, new FutureCallback<DeferredResult<ResponseEntity>>() { 112 accessValidator.validate(currentUser, Operation.RPC_CALL, deviceId, new HttpValidationCallback(response, new FutureCallback<DeferredResult<ResponseEntity>>() {
106 @Override 113 @Override
@@ -109,7 +116,7 @@ public class RpcController extends BaseController { @@ -109,7 +116,7 @@ public class RpcController extends BaseController {
109 tenantId, 116 tenantId,
110 deviceId, 117 deviceId,
111 oneWay, 118 oneWay,
112 - timeout, 119 + expTime,
113 body 120 body
114 ); 121 );
115 deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> reply(new LocalRequestMetaData(rpcRequest, currentUser, result), fromDeviceRpcResponse)); 122 deviceRpcService.processRestApiRpcRequest(rpcRequest, fromDeviceRpcResponse -> reply(new LocalRequestMetaData(rpcRequest, currentUser, result), fromDeviceRpcResponse));
@@ -54,6 +54,13 @@ server: @@ -54,6 +54,13 @@ server:
54 customer: 54 customer:
55 enabled: "${TB_SERVER_REST_LIMITS_CUSTOMER_ENABLED:false}" 55 enabled: "${TB_SERVER_REST_LIMITS_CUSTOMER_ENABLED:false}"
56 configuration: "${TB_SERVER_REST_LIMITS_CUSTOMER_CONFIGURATION:50:1,1000:60}" 56 configuration: "${TB_SERVER_REST_LIMITS_CUSTOMER_CONFIGURATION:50:1,1000:60}"
  57 + server_side_rpc:
  58 + # Minimum value of the server side RPC timeout. May override value provided in the REST API call.
  59 + # Since 2.5 migration to queues, the RPC delay depends on the size of the pending messages in the queue,
  60 + # so default UI parameter of 500ms may not be sufficient for loaded environments.
  61 + min_timeout: "${MIN_SERVER_SIDE_RPC_TIMEOUT:5000}"
  62 + # Default value of the server side RPC timeout.
  63 + default_timeout: "${DEFAULT_SERVER_SIDE_RPC_TIMEOUT:10000}"
57 64
58 # Zookeeper connection parameters. Used for service discovery. 65 # Zookeeper connection parameters. Used for service discovery.
59 zk: 66 zk: