moved kafka from service.js to own module

This commit is contained in:
YevhenBondarenko 2020-04-21 13:30:11 +03:00
parent 12973cecf1
commit 4fb309c37e
7 changed files with 151 additions and 101 deletions

View File

@ -46,17 +46,17 @@ import java.util.concurrent.atomic.AtomicInteger;
@Service @Service
public class RemoteJsInvokeService extends AbstractJsInvokeService { public class RemoteJsInvokeService extends AbstractJsInvokeService {
@Value("${js.remote.max_requests_timeout}") @Value("${queue.js.max_requests_timeout}")
private long maxRequestsTimeout; private long maxRequestsTimeout;
@Getter @Getter
@Value("${js.remote.max_errors}") // @Value("${queue.js.max_errors}")
private int maxErrors; private int maxErrors;
@Value("${js.remote.max_black_list_duration_sec:60}") @Value("${queue.js.max_black_list_duration_sec:60}")
private int maxBlackListDurationSec; private int maxBlackListDurationSec;
@Value("${js.remote.stats.enabled:false}") @Value("${queue.js.stats.enabled:false}")
private boolean statsEnabled; private boolean statsEnabled;
private final AtomicInteger kafkaPushedMsgs = new AtomicInteger(0); private final AtomicInteger kafkaPushedMsgs = new AtomicInteger(0);
@ -65,7 +65,7 @@ public class RemoteJsInvokeService extends AbstractJsInvokeService {
private final AtomicInteger kafkaFailedMsgs = new AtomicInteger(0); private final AtomicInteger kafkaFailedMsgs = new AtomicInteger(0);
private final AtomicInteger kafkaTimeoutMsgs = new AtomicInteger(0); private final AtomicInteger kafkaTimeoutMsgs = new AtomicInteger(0);
@Scheduled(fixedDelayString = "${js.remote.stats.print_interval_ms}") // @Scheduled(fixedDelayString = "${queue.js.stats.print_interval_ms}")
public void printStats() { public void printStats() {
if (statsEnabled) { if (statsEnabled) {
int pushedMsgs = kafkaPushedMsgs.getAndSet(0); int pushedMsgs = kafkaPushedMsgs.getAndSet(0);

View File

@ -415,7 +415,7 @@ state:
persistToTelemetry: "${PERSIST_STATE_TO_TELEMETRY:false}" persistToTelemetry: "${PERSIST_STATE_TO_TELEMETRY:false}"
js: js:
evaluator: "${JS_EVALUATOR:local}" # local/remote evaluator: "${JS_EVALUATOR:remote}" # local/remote
# Built-in JVM JavaScript environment properties # Built-in JVM JavaScript environment properties
local: local:
# Use Sandboxed (secured) JVM JavaScript environment # Use Sandboxed (secured) JVM JavaScript environment
@ -582,9 +582,9 @@ queue:
print-interval-ms: "${TB_QUEUE_CORE_STATS_PRINT_INTERVAL_MS:10000}" print-interval-ms: "${TB_QUEUE_CORE_STATS_PRINT_INTERVAL_MS:10000}"
js: js:
# JS Eval request topic # JS Eval request topic
request_topic: "${REMOTE_JS_EVAL_REQUEST_TOPIC:js_eval.requests}" request_topic: "${REMOTE_JS_EVAL_REQUEST_TOPIC:js.eval.requests}"
# JS Eval responses topic prefix that is combined with node id # JS Eval responses topic prefix that is combined with node id
response_topic_prefix: "${REMOTE_JS_EVAL_RESPONSE_TOPIC:js_eval.responses}" response_topic_prefix: "${REMOTE_JS_EVAL_RESPONSE_TOPIC:js.eval.responses}"
# JS Eval max pending requests # JS Eval max pending requests
max_pending_requests: "${REMOTE_JS_MAX_PENDING_REQUESTS:10000}" max_pending_requests: "${REMOTE_JS_MAX_PENDING_REQUESTS:10000}"
# JS Eval max request timeout # JS Eval max request timeout

View File

@ -31,6 +31,7 @@ const useSandbox = config.get('script.use_sandbox') === 'true';
const maxActiveScripts = Number(config.get('script.max_active_scripts')); const maxActiveScripts = Number(config.get('script.max_active_scripts'));
function JsInvokeMessageProcessor(producer) { function JsInvokeMessageProcessor(producer) {
console.log("Kafka Producer:", producer);
this.producer = producer; this.producer = producer;
this.executor = new JsExecutor(useSandbox); this.executor = new JsExecutor(useSandbox);
this.scriptMap = {}; this.scriptMap = {};
@ -144,17 +145,17 @@ JsInvokeMessageProcessor.prototype.processReleaseRequest = function(requestId, r
JsInvokeMessageProcessor.prototype.sendResponse = function (requestId, responseTopic, scriptId, compileResponse, invokeResponse, releaseResponse) { JsInvokeMessageProcessor.prototype.sendResponse = function (requestId, responseTopic, scriptId, compileResponse, invokeResponse, releaseResponse) {
var remoteResponse = createRemoteResponse(requestId, compileResponse, invokeResponse, releaseResponse); var remoteResponse = createRemoteResponse(requestId, compileResponse, invokeResponse, releaseResponse);
var rawResponse = Buffer.from(JSON.stringify(remoteResponse), 'utf8'); var rawResponse = Buffer.from(JSON.stringify(remoteResponse), 'utf8');
this.producer.send( this.producer.send(responseTopic, scriptId, rawResponse, headers
{ // {
topic: responseTopic, // topic: responseTopic,
messages: [ // messages: [
{ // {
key: scriptId, // key: scriptId,
value: rawResponse, // value: rawResponse,
headers: headers // headers: headers
} // }
] // ]
} // }
).then( ).then(
() => {}, () => {},
(err) => { (err) => {

View File

@ -14,6 +14,8 @@
# limitations under the License. # limitations under the License.
# #
service-type: "TB_SERVICE_TYPE"
kafka: kafka:
request_topic: "REMOTE_JS_EVAL_REQUEST_TOPIC" request_topic: "REMOTE_JS_EVAL_REQUEST_TOPIC"
bootstrap: bootstrap:

View File

@ -14,6 +14,8 @@
# limitations under the License. # limitations under the License.
# #
service-type: "kafka"
kafka: kafka:
request_topic: "js.eval.requests" request_topic: "js.eval.requests"
bootstrap: bootstrap:

View File

@ -0,0 +1,117 @@
/*
* Copyright © 2016-2020 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.
*/
const { logLevel, Kafka } = require('kafkajs');
const config = require('config'),
JsInvokeMessageProcessor = require('../../api/jsInvokeMessageProcessor'),
logger = require('../../config/logger')._logger('main'),
KafkaJsWinstonLogCreator = require('../../config/logger').KafkaJsWinstonLogCreator;
var kafkaClient;
var consumer;
var producer;
function KafkaProducer() {
this.send = async (responseTopic, scriptId, rawResponse, headers) => {
return producer.send(
{
topic: responseTopic,
messages: [
{
key: scriptId,
value: rawResponse,
headers: headers
}
]
});
}
}
(async() => {
try {
logger.info('Starting ThingsBoard JavaScript Executor Microservice...');
const kafkaBootstrapServers = config.get('kafka.bootstrap.servers');
const kafkaRequestTopic = config.get('kafka.request_topic');
logger.info('Kafka Bootstrap Servers: %s', kafkaBootstrapServers);
logger.info('Kafka Requests Topic: %s', kafkaRequestTopic);
kafkaClient = new Kafka({
brokers: kafkaBootstrapServers.split(','),
logLevel: logLevel.INFO,
logCreator: KafkaJsWinstonLogCreator
});
consumer = kafkaClient.consumer({ groupId: 'js-executor-group' });
producer = kafkaClient.producer();
const messageProcessor = new JsInvokeMessageProcessor(new KafkaProducer());
await consumer.connect();
await producer.connect();
await consumer.subscribe({ topic: kafkaRequestTopic});
logger.info('Started ThingsBoard JavaScript Executor Microservice.');
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
messageProcessor.onJsInvokeMessage(message);
},
});
} catch (e) {
logger.error('Failed to start ThingsBoard JavaScript Executor Microservice: %s', e.message);
logger.error(e.stack);
exit(-1);
}
})();
process.on('exit', () => {
exit(0);
});
async function exit(status) {
logger.info('Exiting with status: %d ...', status);
if (consumer) {
logger.info('Stopping Kafka Consumer...');
var _consumer = consumer;
consumer = null;
try {
await _consumer.disconnect();
logger.info('Kafka Consumer stopped.');
await disconnectProducer();
process.exit(status);
} catch (e) {
logger.info('Kafka Consumer stop error.');
await disconnectProducer();
process.exit(status);
}
} else {
process.exit(status);
}
}
async function disconnectProducer() {
if (producer) {
logger.info('Stopping Kafka Producer...');
var _producer = producer;
producer = null;
try {
await _producer.disconnect();
logger.info('Kafka Producer stopped.');
} catch (e) {
logger.info('Kafka Producer stop error.');
}
}
}

View File

@ -13,89 +13,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
const { logLevel, Kafka } = require('kafkajs');
const config = require('config'), const config = require('config');
JsInvokeMessageProcessor = require('./api/jsInvokeMessageProcessor'),
logger = require('./config/logger')._logger('main'),
KafkaJsWinstonLogCreator = require('./config/logger').KafkaJsWinstonLogCreator;
var kafkaClient; const serviceType = config.get('service-type');
var consumer; switch (serviceType) {
var producer; case 'kafka':
require('./queue/kafka/kafkaTemplate');
(async() => { console.log('Used kafka template.');
try { break;
logger.info('Starting ThingsBoard JavaScript Executor Microservice...'); default:
console.error('Unknown service type: ', serviceType);
const kafkaBootstrapServers = config.get('kafka.bootstrap.servers'); process.exit(-1);
const kafkaRequestTopic = config.get('kafka.request_topic');
logger.info('Kafka Bootstrap Servers: %s', kafkaBootstrapServers);
logger.info('Kafka Requests Topic: %s', kafkaRequestTopic);
kafkaClient = new Kafka({
brokers: kafkaBootstrapServers.split(','),
logLevel: logLevel.INFO,
logCreator: KafkaJsWinstonLogCreator
});
consumer = kafkaClient.consumer({ groupId: 'js-executor-group' });
producer = kafkaClient.producer();
const messageProcessor = new JsInvokeMessageProcessor(producer);
await consumer.connect();
await producer.connect();
await consumer.subscribe({ topic: kafkaRequestTopic});
logger.info('Started ThingsBoard JavaScript Executor Microservice.');
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
messageProcessor.onJsInvokeMessage(message);
},
});
} catch (e) {
logger.error('Failed to start ThingsBoard JavaScript Executor Microservice: %s', e.message);
logger.error(e.stack);
exit(-1);
}
})();
process.on('exit', () => {
exit(0);
});
async function exit(status) {
logger.info('Exiting with status: %d ...', status);
if (consumer) {
logger.info('Stopping Kafka Consumer...');
var _consumer = consumer;
consumer = null;
try {
await _consumer.disconnect();
logger.info('Kafka Consumer stopped.');
await disconnectProducer();
process.exit(status);
} catch (e) {
logger.info('Kafka Consumer stop error.');
await disconnectProducer();
process.exit(status);
}
} else {
process.exit(status);
}
} }
async function disconnectProducer() {
if (producer) {
logger.info('Stopping Kafka Producer...');
var _producer = producer;
producer = null;
try {
await _producer.disconnect();
logger.info('Kafka Producer stopped.');
} catch (e) {
logger.info('Kafka Producer stop error.');
}
}
}