Search in sources :

Example 16 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class TbCoreTransportApiService method init.

@PostConstruct
public void init() {
    this.transportCallbackExecutor = ThingsBoardExecutors.newWorkStealingPool(maxCallbackThreads, getClass());
    TbQueueProducer<TbProtoQueueMsg<TransportApiResponseMsg>> producer = tbCoreQueueFactory.createTransportApiResponseProducer();
    TbQueueConsumer<TbProtoQueueMsg<TransportApiRequestMsg>> consumer = tbCoreQueueFactory.createTransportApiRequestConsumer();
    String key = StatsType.TRANSPORT.getName();
    MessagesStats queueStats = statsFactory.createMessagesStats(key);
    DefaultTbQueueResponseTemplate.DefaultTbQueueResponseTemplateBuilder<TbProtoQueueMsg<TransportApiRequestMsg>, TbProtoQueueMsg<TransportApiResponseMsg>> builder = DefaultTbQueueResponseTemplate.builder();
    builder.requestTemplate(consumer);
    builder.responseTemplate(producer);
    builder.maxPendingRequests(maxPendingRequests);
    builder.requestTimeout(requestTimeout);
    builder.pollInterval(responsePollDuration);
    builder.executor(transportCallbackExecutor);
    builder.handler(transportApiService);
    builder.stats(queueStats);
    transportApiTemplate = builder.build();
}
Also used : TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) DefaultTbQueueResponseTemplate(org.thingsboard.server.queue.common.DefaultTbQueueResponseTemplate) MessagesStats(org.thingsboard.server.common.stats.MessagesStats) PostConstruct(javax.annotation.PostConstruct)

Example 17 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class RemoteJsInvokeService method doEval.

@Override
protected ListenableFuture<UUID> doEval(UUID scriptId, String functionName, String scriptBody) {
    JsInvokeProtos.JsCompileRequest jsRequest = JsInvokeProtos.JsCompileRequest.newBuilder().setScriptIdMSB(scriptId.getMostSignificantBits()).setScriptIdLSB(scriptId.getLeastSignificantBits()).setFunctionName(functionName).setScriptBody(scriptBody).build();
    JsInvokeProtos.RemoteJsRequest jsRequestWrapper = JsInvokeProtos.RemoteJsRequest.newBuilder().setCompileRequest(jsRequest).build();
    log.trace("Post compile request for scriptId [{}]", scriptId);
    ListenableFuture<TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>> future = requestTemplate.send(new TbProtoJsQueueMsg<>(UUID.randomUUID(), jsRequestWrapper));
    if (maxEvalRequestsTimeout > 0) {
        future = Futures.withTimeout(future, maxEvalRequestsTimeout, TimeUnit.MILLISECONDS, timeoutExecutorService);
    }
    queuePushedMsgs.incrementAndGet();
    Futures.addCallback(future, new FutureCallback<TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>>() {

        @Override
        public void onSuccess(@Nullable TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse> result) {
            queueEvalMsgs.incrementAndGet();
        }

        @Override
        public void onFailure(Throwable t) {
            if (t instanceof TimeoutException || (t.getCause() != null && t.getCause() instanceof TimeoutException)) {
                queueTimeoutMsgs.incrementAndGet();
            }
            queueFailedMsgs.incrementAndGet();
        }
    }, callbackExecutor);
    return Futures.transform(future, response -> {
        JsInvokeProtos.JsCompileResponse compilationResult = response.getValue().getCompileResponse();
        UUID compiledScriptId = new UUID(compilationResult.getScriptIdMSB(), compilationResult.getScriptIdLSB());
        if (compilationResult.getSuccess()) {
            scriptIdToNameMap.put(scriptId, functionName);
            scriptIdToBodysMap.put(scriptId, scriptBody);
            return compiledScriptId;
        } else {
            log.debug("[{}] Failed to compile script due to [{}]: {}", compiledScriptId, compilationResult.getErrorCode().name(), compilationResult.getErrorDetails());
            throw new RuntimeException(compilationResult.getErrorDetails());
        }
    }, callbackExecutor);
}
Also used : TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) JsInvokeProtos(org.thingsboard.server.gen.js.JsInvokeProtos) UUID(java.util.UUID) TimeoutException(java.util.concurrent.TimeoutException)

Example 18 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class RemoteJsInvokeService method doInvokeFunction.

@Override
protected ListenableFuture<Object> doInvokeFunction(UUID scriptId, String functionName, Object[] args) {
    log.trace("doInvokeFunction js-request for uuid {} with timeout {}ms", scriptId, maxRequestsTimeout);
    final String scriptBody = scriptIdToBodysMap.get(scriptId);
    if (scriptBody == null) {
        return Futures.immediateFailedFuture(new RuntimeException("No script body found for scriptId: [" + scriptId + "]!"));
    }
    JsInvokeProtos.JsInvokeRequest.Builder jsRequestBuilder = JsInvokeProtos.JsInvokeRequest.newBuilder().setScriptIdMSB(scriptId.getMostSignificantBits()).setScriptIdLSB(scriptId.getLeastSignificantBits()).setFunctionName(functionName).setTimeout((int) maxRequestsTimeout).setScriptBody(scriptBody);
    for (Object arg : args) {
        jsRequestBuilder.addArgs(arg.toString());
    }
    JsInvokeProtos.RemoteJsRequest jsRequestWrapper = JsInvokeProtos.RemoteJsRequest.newBuilder().setInvokeRequest(jsRequestBuilder.build()).build();
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ListenableFuture<TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>> future = requestTemplate.send(new TbProtoJsQueueMsg<>(UUID.randomUUID(), jsRequestWrapper));
    if (maxRequestsTimeout > 0) {
        future = Futures.withTimeout(future, maxRequestsTimeout, TimeUnit.MILLISECONDS, timeoutExecutorService);
    }
    queuePushedMsgs.incrementAndGet();
    Futures.addCallback(future, new FutureCallback<TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>>() {

        @Override
        public void onSuccess(@Nullable TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse> result) {
            queueInvokeMsgs.incrementAndGet();
        }

        @Override
        public void onFailure(Throwable t) {
            onScriptExecutionError(scriptId, t, scriptBody);
            if (t instanceof TimeoutException || (t.getCause() != null && t.getCause() instanceof TimeoutException)) {
                queueTimeoutMsgs.incrementAndGet();
            }
            queueFailedMsgs.incrementAndGet();
        }
    }, callbackExecutor);
    return Futures.transform(future, response -> {
        stopWatch.stop();
        log.trace("doInvokeFunction js-response took {}ms for uuid {}", stopWatch.getTotalTimeMillis(), response.getKey());
        JsInvokeProtos.JsInvokeResponse invokeResult = response.getValue().getInvokeResponse();
        if (invokeResult.getSuccess()) {
            return invokeResult.getResult();
        } else {
            final RuntimeException e = new RuntimeException(invokeResult.getErrorDetails());
            onScriptExecutionError(scriptId, e, scriptBody);
            log.debug("[{}] Failed to compile script due to [{}]: {}", scriptId, invokeResult.getErrorCode().name(), invokeResult.getErrorDetails());
            throw e;
        }
    }, callbackExecutor);
}
Also used : TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) StopWatch(org.springframework.util.StopWatch) JsInvokeProtos(org.thingsboard.server.gen.js.JsInvokeProtos) TimeoutException(java.util.concurrent.TimeoutException)

Example 19 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class RemoteJsInvokeService method doRelease.

@Override
protected void doRelease(UUID scriptId, String functionName) throws Exception {
    JsInvokeProtos.JsReleaseRequest jsRequest = JsInvokeProtos.JsReleaseRequest.newBuilder().setScriptIdMSB(scriptId.getMostSignificantBits()).setScriptIdLSB(scriptId.getLeastSignificantBits()).setFunctionName(functionName).build();
    JsInvokeProtos.RemoteJsRequest jsRequestWrapper = JsInvokeProtos.RemoteJsRequest.newBuilder().setReleaseRequest(jsRequest).build();
    ListenableFuture<TbProtoQueueMsg<JsInvokeProtos.RemoteJsResponse>> future = requestTemplate.send(new TbProtoJsQueueMsg<>(UUID.randomUUID(), jsRequestWrapper));
    if (maxRequestsTimeout > 0) {
        future = Futures.withTimeout(future, maxRequestsTimeout, TimeUnit.MILLISECONDS, timeoutExecutorService);
    }
    JsInvokeProtos.RemoteJsResponse response = future.get().getValue();
    JsInvokeProtos.JsReleaseResponse compilationResult = response.getReleaseResponse();
    UUID compiledScriptId = new UUID(compilationResult.getScriptIdMSB(), compilationResult.getScriptIdLSB());
    if (compilationResult.getSuccess()) {
        scriptIdToBodysMap.remove(scriptId);
    } else {
        log.debug("[{}] Failed to release script due", compiledScriptId);
    }
}
Also used : JsInvokeProtos(org.thingsboard.server.gen.js.JsInvokeProtos) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) UUID(java.util.UUID)

Example 20 with TbProtoQueueMsg

use of org.thingsboard.server.queue.common.TbProtoQueueMsg in project thingsboard by thingsboard.

the class DefaultTbApiUsageClient method reportStats.

private void reportStats() {
    ConcurrentMap<OwnerId, ToUsageStatsServiceMsg.Builder> report = new ConcurrentHashMap<>();
    for (ApiUsageRecordKey key : ApiUsageRecordKey.values()) {
        ConcurrentMap<OwnerId, AtomicLong> statsForKey = stats.get(key);
        statsForKey.forEach((ownerId, statsValue) -> {
            long value = statsValue.get();
            if (value == 0)
                return;
            ToUsageStatsServiceMsg.Builder statsMsgBuilder = report.computeIfAbsent(ownerId, id -> {
                ToUsageStatsServiceMsg.Builder newStatsMsgBuilder = ToUsageStatsServiceMsg.newBuilder();
                TenantId tenantId = ownerId.getTenantId();
                newStatsMsgBuilder.setTenantIdMSB(tenantId.getId().getMostSignificantBits());
                newStatsMsgBuilder.setTenantIdLSB(tenantId.getId().getLeastSignificantBits());
                EntityId entityId = ownerId.getEntityId();
                if (entityId != null && entityId.getEntityType() == EntityType.CUSTOMER) {
                    newStatsMsgBuilder.setCustomerIdMSB(entityId.getId().getMostSignificantBits());
                    newStatsMsgBuilder.setCustomerIdLSB(entityId.getId().getLeastSignificantBits());
                }
                return newStatsMsgBuilder;
            });
            statsMsgBuilder.addValues(UsageStatsKVProto.newBuilder().setKey(key.name()).setValue(value).build());
        });
        statsForKey.clear();
    }
    report.forEach(((ownerId, statsMsg) -> {
        // TODO: figure out how to minimize messages into the queue. Maybe group by 100s of messages?
        TenantId tenantId = ownerId.getTenantId();
        EntityId entityId = Optional.ofNullable(ownerId.getEntityId()).orElse(tenantId);
        TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, entityId).newByTopic(msgProducer.getDefaultTopic());
        msgProducer.send(tpi, new TbProtoQueueMsg<>(UUID.randomUUID(), statsMsg.build()), null);
    }));
    if (!report.isEmpty()) {
        log.debug("Reporting API usage statistics for {} tenants and customers", report.size());
    }
}
Also used : UsageStatsKVProto(org.thingsboard.server.gen.transport.TransportProtos.UsageStatsKVProto) PartitionService(org.thingsboard.server.queue.discovery.PartitionService) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey) Random(java.util.Random) TenantId(org.thingsboard.server.common.data.id.TenantId) ConcurrentMap(java.util.concurrent.ConcurrentMap) Value(org.springframework.beans.factory.annotation.Value) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) TbQueueProducer(org.thingsboard.server.queue.TbQueueProducer) SchedulerComponent(org.thingsboard.server.queue.scheduler.SchedulerComponent) EnumMap(java.util.EnumMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) TbQueueProducerProvider(org.thingsboard.server.queue.provider.TbQueueProducerProvider) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Data(lombok.Data) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) TbProtoQueueMsg(org.thingsboard.server.queue.common.TbProtoQueueMsg) ToUsageStatsServiceMsg(org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceMsg) ApiUsageRecordKey(org.thingsboard.server.common.data.ApiUsageRecordKey) EntityId(org.thingsboard.server.common.data.id.EntityId) AtomicLong(java.util.concurrent.atomic.AtomicLong) TenantId(org.thingsboard.server.common.data.id.TenantId) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

TbProtoQueueMsg (org.thingsboard.server.queue.common.TbProtoQueueMsg)21 UUID (java.util.UUID)9 ByteString (com.google.protobuf.ByteString)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 TopicPartitionInfo (org.thingsboard.server.common.msg.queue.TopicPartitionInfo)6 ToCoreNotificationMsg (org.thingsboard.server.gen.transport.TransportProtos.ToCoreNotificationMsg)6 ConcurrentMap (java.util.concurrent.ConcurrentMap)5 List (java.util.List)4 Optional (java.util.Optional)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 TimeUnit (java.util.concurrent.TimeUnit)4 PostConstruct (javax.annotation.PostConstruct)4 Slf4j (lombok.extern.slf4j.Slf4j)4 TbQueueConsumer (org.thingsboard.server.queue.TbQueueConsumer)4 ExecutorService (java.util.concurrent.ExecutorService)3 Executors (java.util.concurrent.Executors)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 PreDestroy (javax.annotation.PreDestroy)3 Value (org.springframework.beans.factory.annotation.Value)3