Commit 67014b45a0c30eaa7ad24cb805960f2b44eb923c

Authored by Igor Kulikov
Committed by GitHub
2 parents d5c20851 d6852dcb

Merge pull request #5178 from smatvienko-tb/thread-pool-naming-aug-2021

Thread pool naming (aug 2021)
Showing 31 changed files with 147 additions and 53 deletions
... ... @@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
23 23 import org.springframework.beans.factory.annotation.Value;
24 24 import org.springframework.context.annotation.Lazy;
25 25 import org.springframework.stereotype.Service;
  26 +import org.thingsboard.common.util.ThingsBoardExecutors;
26 27 import org.thingsboard.common.util.ThingsBoardThreadFactory;
27 28 import org.thingsboard.rule.engine.api.MailService;
28 29 import org.thingsboard.server.common.data.ApiFeature;
... ... @@ -486,7 +487,7 @@ public class DefaultTbApiUsageStateService extends TbApplicationEventListener<Pa
486 487 log.info("Initializing tenant states.");
487 488 updateLock.lock();
488 489 try {
489   - ExecutorService tmpInitExecutor = Executors.newWorkStealingPool(20);
  490 + ExecutorService tmpInitExecutor = ThingsBoardExecutors.newWorkStealingPool(20, "init-tenant-states-from-db");
490 491 try {
491 492 PageDataIterable<Tenant> tenantIterator = new PageDataIterable<>(tenantService::findTenants, 1024);
492 493 List<Future<?>> futures = new ArrayList<>();
... ...
... ... @@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode;
19 19 import lombok.extern.slf4j.Slf4j;
20 20 import org.springframework.beans.factory.annotation.Autowired;
21 21 import org.springframework.stereotype.Service;
  22 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
22 23 import org.thingsboard.server.common.data.edge.Edge;
23 24 import org.thingsboard.server.common.data.edge.EdgeEvent;
24 25 import org.thingsboard.server.common.data.edge.EdgeEventActionType;
... ... @@ -79,7 +80,7 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
79 80
80 81 @PostConstruct
81 82 public void initExecutor() {
82   - tsCallBackExecutor = Executors.newSingleThreadExecutor();
  83 + tsCallBackExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("edge-notifications"));
83 84 }
84 85
85 86 @PreDestroy
... ...
... ... @@ -103,8 +103,8 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
103 103 private final ConcurrentMap<String, TbRuleEngineQueueConfiguration> consumerConfigurations = new ConcurrentHashMap<>();
104 104 private final ConcurrentMap<String, TbRuleEngineConsumerStats> consumerStats = new ConcurrentHashMap<>();
105 105 private final ConcurrentMap<String, TbTopicWithConsumerPerPartition> topicsConsumerPerPartition = new ConcurrentHashMap<>();
106   - final ExecutorService submitExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-service-submit-executor"));
107   - final ScheduledExecutorService repartitionExecutor = Executors.newScheduledThreadPool(1, ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-repartition-executor"));
  106 + final ExecutorService submitExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-submit"));
  107 + final ScheduledExecutorService repartitionExecutor = Executors.newScheduledThreadPool(1, ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-repartition"));
108 108
109 109 public DefaultTbRuleEngineConsumerService(TbRuleEngineProcessingStrategyFactory processingStrategyFactory,
110 110 TbRuleEngineSubmitStrategyFactory submitStrategyFactory,
... ... @@ -146,6 +146,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
146 146 public void stop() {
147 147 super.destroy();
148 148 submitExecutor.shutdownNow();
  149 + repartitionExecutor.shutdownNow();
149 150 ruleEngineSettings.getQueues().forEach(config -> consumerConfigurations.put(config.getName(), config));
150 151 }
151 152
... ...
... ... @@ -21,6 +21,7 @@ import org.junit.Assert;
21 21 import org.junit.Test;
22 22 import org.junit.runner.RunWith;
23 23 import org.mockito.junit.MockitoJUnitRunner;
  24 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
24 25 import org.thingsboard.server.gen.transport.TransportProtos;
25 26 import org.thingsboard.server.queue.common.TbProtoQueueMsg;
26 27 import org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy;
... ... @@ -59,7 +60,7 @@ public class TbMsgPackProcessingContextTest {
59 60 //log.warn("preparing the test...");
60 61 int msgCount = 1000;
61 62 int parallelCount = 5;
62   - executorService = Executors.newFixedThreadPool(parallelCount);
  63 + executorService = Executors.newFixedThreadPool(parallelCount, ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope"));
63 64
64 65 ConcurrentMap<UUID, TbProtoQueueMsg<TransportProtos.ToRuleEngineMsg>> messages = new ConcurrentHashMap<>(msgCount);
65 66 for (int i = 0; i < msgCount; i++) {
... ...
... ... @@ -52,6 +52,16 @@ import static org.junit.Assert.assertNotNull;
52 52 @Slf4j
53 53 public abstract class AbstractCoapIntegrationTest extends AbstractTransportIntegrationTest {
54 54
  55 + protected CoapClient client;
  56 +
  57 + @Override
  58 + protected void processAfterTest() throws Exception {
  59 + if (client != null) {
  60 + client.shutdown();
  61 + }
  62 + super.processAfterTest();
  63 + }
  64 +
55 65 protected void processBeforeTest(String deviceName, CoapDeviceType coapDeviceType, TransportPayloadType payloadType) throws Exception {
56 66 this.processBeforeTest(deviceName, coapDeviceType, payloadType, null, null, null, null, null, null, DeviceProfileProvisionType.DISABLED);
57 67 }
... ...
... ... @@ -75,7 +75,7 @@ public abstract class AbstractCoapAttributesRequestIntegrationTest extends Abstr
75 75
76 76 String keys = "attribute1,attribute2,attribute3,attribute4,attribute5";
77 77 String featureTokenUrl = getFeatureTokenUrl(accessToken, FeatureType.ATTRIBUTES) + "?clientKeys=" + keys + "&sharedKeys=" + keys;
78   - CoapClient client = getCoapClient(featureTokenUrl);
  78 + client = getCoapClient(featureTokenUrl);
79 79
80 80 CoapResponse getAttributesResponse = client.setTimeout(CLIENT_REQUEST_TIMEOUT).get();
81 81 validateResponse(getAttributesResponse);
... ... @@ -83,7 +83,7 @@ public abstract class AbstractCoapAttributesRequestIntegrationTest extends Abstr
83 83
84 84 protected void postAttributes() throws Exception {
85 85 doPostAsync("/api/plugins/telemetry/DEVICE/" + savedDevice.getId().getId() + "/attributes/SHARED_SCOPE", POST_ATTRIBUTES_PAYLOAD, String.class, status().isOk());
86   - CoapClient client = getCoapClient(FeatureType.ATTRIBUTES);
  86 + client = getCoapClient(FeatureType.ATTRIBUTES);
87 87 CoapResponse coapResponse = client.setTimeout(CLIENT_REQUEST_TIMEOUT).post(POST_ATTRIBUTES_PAYLOAD.getBytes(), MediaTypeRegistry.APPLICATION_JSON);
88 88 assertEquals(CoAP.ResponseCode.CREATED, coapResponse.getCode());
89 89 }
... ...
... ... @@ -21,7 +21,6 @@ import com.google.protobuf.DynamicMessage;
21 21 import com.google.protobuf.InvalidProtocolBufferException;
22 22 import com.squareup.wire.schema.internal.parser.ProtoFileElement;
23 23 import lombok.extern.slf4j.Slf4j;
24   -import org.eclipse.californium.core.CoapClient;
25 24 import org.eclipse.californium.core.CoapResponse;
26 25 import org.eclipse.californium.core.coap.CoAP;
27 26 import org.eclipse.californium.core.coap.MediaTypeRegistry;
... ... @@ -124,7 +123,7 @@ public abstract class AbstractCoapAttributesRequestProtoIntegrationTest extends
124 123 .setField(postAttributesMsgDescriptor.findFieldByName("attribute5"), jsonObject)
125 124 .build();
126 125 byte[] payload = postAttributesMsg.toByteArray();
127   - CoapClient client = getCoapClient(FeatureType.ATTRIBUTES);
  126 + client = getCoapClient(FeatureType.ATTRIBUTES);
128 127 CoapResponse coapResponse = client.setTimeout(CLIENT_REQUEST_TIMEOUT).post(payload, MediaTypeRegistry.APPLICATION_JSON);
129 128 assertEquals(CoAP.ResponseCode.CREATED, coapResponse.getCode());
130 129 }
... ...
... ... @@ -71,7 +71,7 @@ public abstract class AbstractCoapAttributesUpdatesIntegrationTest extends Abstr
71 71 if (!emptyCurrentStateNotification) {
72 72 doPostAsync("/api/plugins/telemetry/DEVICE/" + savedDevice.getId().getId() + "/attributes/SHARED_SCOPE", POST_ATTRIBUTES_PAYLOAD_ON_CURRENT_STATE_NOTIFICATION, String.class, status().isOk());
73 73 }
74   - CoapClient client = getCoapClient(FeatureType.ATTRIBUTES);
  74 + client = getCoapClient(FeatureType.ATTRIBUTES);
75 75
76 76 CountDownLatch latch = new CountDownLatch(1);
77 77 TestCoapCallback callback = new TestCoapCallback(latch);
... ...
... ... @@ -90,7 +90,7 @@ public abstract class AbstractCoapClaimDeviceTest extends AbstractCoapIntegratio
90 90
91 91 protected void processTestClaimingDevice(boolean emptyPayload) throws Exception {
92 92 log.warn("[testClaimingDevice] Device: {}, Transport type: {}", savedDevice.getName(), savedDevice.getType());
93   - CoapClient client = getCoapClient(FeatureType.CLAIM);
  93 + client = getCoapClient(FeatureType.CLAIM);
94 94 byte[] payloadBytes;
95 95 byte[] failurePayloadBytes;
96 96 if (emptyPayload) {
... ...
... ... @@ -49,7 +49,7 @@ public abstract class AbstractCoapClaimProtoDeviceTest extends AbstractCoapClaim
49 49
50 50 @Override
51 51 protected void processTestClaimingDevice(boolean emptyPayload) throws Exception {
52   - CoapClient client = getCoapClient(FeatureType.CLAIM);
  52 + client = getCoapClient(FeatureType.CLAIM);
53 53 byte[] payloadBytes;
54 54 if (emptyPayload) {
55 55 TransportApiProtos.ClaimDevice claimDevice = getClaimDevice(0, emptyPayload);
... ...
... ... @@ -180,7 +180,7 @@ public abstract class AbstractCoapProvisionJsonDeviceTest extends AbstractCoapIn
180 180
181 181 private CoapResponse createCoapClientAndPublish(String deviceCredentials) throws Exception {
182 182 String provisionRequestMsg = createTestProvisionMessage(deviceCredentials);
183   - CoapClient client = getCoapClient(FeatureType.PROVISION);
  183 + client = getCoapClient(FeatureType.PROVISION);
184 184 return postProvision(client, provisionRequestMsg.getBytes());
185 185 }
186 186
... ...
... ... @@ -172,11 +172,13 @@ public abstract class AbstractCoapProvisionProtoDeviceTest extends AbstractCoapI
172 172
173 173 private CoapResponse createCoapClientAndPublish() throws Exception {
174 174 byte[] provisionRequestMsg = createTestProvisionMessage();
175   - return createCoapClientAndPublish(provisionRequestMsg);
  175 + CoapResponse coapResponse = createCoapClientAndPublish(provisionRequestMsg);
  176 + Assert.assertNotNull("COAP response", coapResponse);
  177 + return coapResponse;
176 178 }
177 179
178 180 private CoapResponse createCoapClientAndPublish(byte[] provisionRequestMsg) throws Exception {
179   - CoapClient client = getCoapClient(FeatureType.PROVISION);
  181 + client = getCoapClient(FeatureType.PROVISION);
180 182 return postProvision(client, provisionRequestMsg);
181 183 }
182 184
... ...
... ... @@ -54,7 +54,7 @@ public abstract class AbstractCoapServerSideRpcIntegrationTest extends AbstractC
54 54 }
55 55
56 56 protected void processOneWayRpcTest() throws Exception {
57   - CoapClient client = getCoapClient(FeatureType.RPC);
  57 + client = getCoapClient(FeatureType.RPC);
58 58 client.useCONs();
59 59
60 60 CountDownLatch latch = new CountDownLatch(1);
... ... @@ -82,7 +82,7 @@ public abstract class AbstractCoapServerSideRpcIntegrationTest extends AbstractC
82 82 }
83 83
84 84 protected void processTwoWayRpcTest(String expectedResponseResult) throws Exception {
85   - CoapClient client = getCoapClient(FeatureType.RPC);
  85 + client = getCoapClient(FeatureType.RPC);
86 86 client.useCONs();
87 87
88 88 CountDownLatch latch = new CountDownLatch(1);
... ...
... ... @@ -69,7 +69,7 @@ public abstract class AbstractCoapAttributesIntegrationTest extends AbstractCoap
69 69 }
70 70
71 71 protected void processAttributesTest(List<String> expectedKeys, byte[] payload, boolean presenceFieldsTest) throws Exception {
72   - CoapClient client = getCoapClient(FeatureType.ATTRIBUTES);
  72 + client = getCoapClient(FeatureType.ATTRIBUTES);
73 73
74 74 postAttributes(client, payload);
75 75
... ...
... ... @@ -71,8 +71,8 @@ public abstract class AbstractCoapTimeseriesIntegrationTest extends AbstractCoap
71 71 }
72 72
73 73 protected void processTestPostTelemetry(byte[] payloadBytes, List<String> expectedKeys, boolean withTs, boolean presenceFieldsTest) throws Exception {
74   - CoapClient coapClient = getCoapClient(FeatureType.TELEMETRY);
75   - postTelemetry(coapClient, payloadBytes);
  74 + client = getCoapClient(FeatureType.TELEMETRY);
  75 + postTelemetry(client, payloadBytes);
76 76
77 77 String deviceId = savedDevice.getId().getId().toString();
78 78
... ...
... ... @@ -23,6 +23,7 @@ import org.eclipse.leshan.core.node.LwM2mResource;
23 23 import org.eclipse.leshan.core.response.ExecuteResponse;
24 24 import org.eclipse.leshan.core.response.ReadResponse;
25 25 import org.eclipse.leshan.core.response.WriteResponse;
  26 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
26 27
27 28 import javax.security.auth.Destroyable;
28 29 import java.util.Arrays;
... ... @@ -37,7 +38,7 @@ public class FwLwM2MDevice extends BaseInstanceEnabler implements Destroyable {
37 38
38 39 private static final List<Integer> supportedResources = Arrays.asList(0, 1, 2, 3, 5, 6, 7, 9);
39 40
40   - private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  41 + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope"));
41 42
42 43 private final AtomicInteger state = new AtomicInteger(0);
43 44
... ...
... ... @@ -45,6 +45,7 @@ import org.eclipse.leshan.core.request.BootstrapRequest;
45 45 import org.eclipse.leshan.core.request.DeregisterRequest;
46 46 import org.eclipse.leshan.core.request.RegisterRequest;
47 47 import org.eclipse.leshan.core.request.UpdateRequest;
  48 +import org.junit.Assert;
48 49
49 50 import java.io.IOException;
50 51 import java.net.InetSocketAddress;
... ... @@ -65,8 +66,11 @@ public class LwM2MTestClient {
65 66 private final ScheduledExecutorService executor;
66 67 private final String endpoint;
67 68 private LeshanClient client;
  69 + private FwLwM2MDevice fwLwM2MDevice;
  70 + private SwLwM2MDevice swLwM2MDevice;
68 71
69 72 public void init(Security security, NetworkConfig coapConfig) throws InvalidDDFFileException, IOException {
  73 + Assert.assertNull("client already initialized", client);
70 74 String[] resources = new String[]{"0.xml", "1.xml", "2.xml", "3.xml", "5.xml", "9.xml"};
71 75 List<ObjectModel> models = new ArrayList<>();
72 76 for (String resourceName : resources) {
... ... @@ -77,8 +81,8 @@ public class LwM2MTestClient {
77 81 initializer.setInstancesForObject(SECURITY, security);
78 82 initializer.setInstancesForObject(SERVER, new Server(123, 300));
79 83 initializer.setInstancesForObject(DEVICE, new SimpleLwM2MDevice());
80   - initializer.setInstancesForObject(FIRMWARE, new FwLwM2MDevice());
81   - initializer.setInstancesForObject(SOFTWARE_MANAGEMENT, new SwLwM2MDevice());
  84 + initializer.setInstancesForObject(FIRMWARE, fwLwM2MDevice = new FwLwM2MDevice());
  85 + initializer.setInstancesForObject(SOFTWARE_MANAGEMENT, swLwM2MDevice = new SwLwM2MDevice());
82 86 initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, DummyInstanceEnabler.class);
83 87
84 88 DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
... ... @@ -229,6 +233,12 @@ public class LwM2MTestClient {
229 233
230 234 public void destroy() {
231 235 client.destroy(true);
  236 + if (fwLwM2MDevice != null) {
  237 + fwLwM2MDevice.destroy();
  238 + }
  239 + if (swLwM2MDevice != null) {
  240 + swLwM2MDevice.destroy();
  241 + }
232 242 }
233 243
234 244 }
... ...
... ... @@ -24,6 +24,7 @@ import org.eclipse.leshan.core.node.LwM2mResource;
24 24 import org.eclipse.leshan.core.response.ExecuteResponse;
25 25 import org.eclipse.leshan.core.response.ReadResponse;
26 26 import org.eclipse.leshan.core.response.WriteResponse;
  27 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
27 28
28 29 import javax.security.auth.Destroyable;
29 30 import java.util.Arrays;
... ... @@ -38,7 +39,7 @@ public class SwLwM2MDevice extends BaseInstanceEnabler implements Destroyable {
38 39
39 40 private static final List<Integer> supportedResources = Arrays.asList(0, 1, 2, 3, 4, 6, 7, 9);
40 41
41   - private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
  42 + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope"));
42 43
43 44 private final AtomicInteger state = new AtomicInteger(0);
44 45
... ...
... ... @@ -17,10 +17,12 @@ package org.thingsboard.server.util;
17 17
18 18 import com.google.common.util.concurrent.MoreExecutors;
19 19 import lombok.extern.slf4j.Slf4j;
  20 +import org.junit.After;
20 21 import org.junit.Test;
21 22 import org.junit.runner.RunWith;
22 23 import org.mockito.Mockito;
23 24 import org.mockito.junit.MockitoJUnitRunner;
  25 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
24 26 import org.thingsboard.server.utils.EventDeduplicationExecutor;
25 27
26 28 import java.util.concurrent.ExecutorService;
... ... @@ -31,6 +33,16 @@ import java.util.function.Consumer;
31 33 @RunWith(MockitoJUnitRunner.class)
32 34 public class EventDeduplicationExecutorTest {
33 35
  36 + ThingsBoardThreadFactory threadFactory = ThingsBoardThreadFactory.forName(getClass().getSimpleName());
  37 + ExecutorService executor;
  38 +
  39 + @After
  40 + public void tearDown() throws Exception {
  41 + if (executor != null) {
  42 + executor.shutdownNow();
  43 + }
  44 + }
  45 +
34 46 @Test
35 47 public void testSimpleFlowSameThread() throws InterruptedException {
36 48 simpleFlow(MoreExecutors.newDirectExecutorService());
... ... @@ -48,32 +60,38 @@ public class EventDeduplicationExecutorTest {
48 60
49 61 @Test
50 62 public void testSimpleFlowSingleThread() throws InterruptedException {
51   - simpleFlow(Executors.newSingleThreadExecutor());
  63 + executor = Executors.newSingleThreadExecutor(threadFactory);
  64 + simpleFlow(executor);
52 65 }
53 66
54 67 @Test
55 68 public void testPeriodicFlowSingleThread() throws InterruptedException {
56   - periodicFlow(Executors.newSingleThreadExecutor());
  69 + executor = Executors.newSingleThreadExecutor(threadFactory);
  70 + periodicFlow(executor);
57 71 }
58 72
59 73 @Test
60 74 public void testExceptionFlowSingleThread() throws InterruptedException {
61   - exceptionFlow(Executors.newSingleThreadExecutor());
  75 + executor = Executors.newSingleThreadExecutor(threadFactory);
  76 + exceptionFlow(executor);
62 77 }
63 78
64 79 @Test
65 80 public void testSimpleFlowMultiThread() throws InterruptedException {
66   - simpleFlow(Executors.newFixedThreadPool(3));
  81 + executor = Executors.newFixedThreadPool(3, threadFactory);
  82 + simpleFlow(executor);
67 83 }
68 84
69 85 @Test
70 86 public void testPeriodicFlowMultiThread() throws InterruptedException {
71   - periodicFlow(Executors.newFixedThreadPool(3));
  87 + executor = Executors.newFixedThreadPool(3, threadFactory);
  88 + periodicFlow(executor);
72 89 }
73 90
74 91 @Test
75 92 public void testExceptionFlowMultiThread() throws InterruptedException {
76   - exceptionFlow(Executors.newFixedThreadPool(3));
  93 + executor = Executors.newFixedThreadPool(3, threadFactory);
  94 + exceptionFlow(executor);
77 95 }
78 96
79 97 private void simpleFlow(ExecutorService executorService) throws InterruptedException {
... ...
... ... @@ -22,6 +22,8 @@ import org.junit.Before;
22 22 import org.junit.Test;
23 23 import org.junit.runner.RunWith;
24 24 import org.mockito.junit.MockitoJUnitRunner;
  25 +import org.thingsboard.common.util.ThingsBoardExecutors;
  26 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
25 27 import org.thingsboard.server.common.data.id.DeviceId;
26 28
27 29 import java.util.ArrayList;
... ... @@ -45,6 +47,7 @@ public class ActorSystemTest {
45 47
46 48 private volatile TbActorSystem actorSystem;
47 49 private volatile ExecutorService submitPool;
  50 + private ExecutorService executor;
48 51 private int parallelism;
49 52
50 53 @Before
... ... @@ -53,54 +56,64 @@ public class ActorSystemTest {
53 56 parallelism = Math.max(2, cores / 2);
54 57 TbActorSystemSettings settings = new TbActorSystemSettings(5, parallelism, 42);
55 58 actorSystem = new DefaultTbActorSystem(settings);
56   - submitPool = Executors.newFixedThreadPool(parallelism); //order guaranteed
  59 + submitPool = Executors.newFixedThreadPool(parallelism, ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-submit-test-scope")); //order guaranteed
57 60 }
58 61
59 62 @After
60 63 public void shutdownActorSystem() {
61 64 actorSystem.stop();
62 65 submitPool.shutdownNow();
  66 + if (executor != null) {
  67 + executor.shutdownNow();
  68 + }
63 69 }
64 70
65 71 @Test
66 72 public void test1actorsAnd100KMessages() throws InterruptedException {
67   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  73 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  74 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
68 75 testActorsAndMessages(1, _100K, 1);
69 76 }
70 77
71 78 @Test
72 79 public void test10actorsAnd100KMessages() throws InterruptedException {
73   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  80 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  81 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
74 82 testActorsAndMessages(10, _100K, 1);
75 83 }
76 84
77 85 @Test
78 86 public void test100KActorsAnd1Messages5timesSingleThread() throws InterruptedException {
79   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newSingleThreadExecutor());
  87 + executor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName(getClass().getSimpleName()));
  88 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
80 89 testActorsAndMessages(_100K, 1, 5);
81 90 }
82 91
83 92 @Test
84 93 public void test100KActorsAnd1Messages5times() throws InterruptedException {
85   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  94 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  95 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
86 96 testActorsAndMessages(_100K, 1, 5);
87 97 }
88 98
89 99 @Test
90 100 public void test100KActorsAnd10Messages() throws InterruptedException {
91   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  101 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  102 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
92 103 testActorsAndMessages(_100K, 10, 1);
93 104 }
94 105
95 106 @Test
96 107 public void test1KActorsAnd1KMessages() throws InterruptedException {
97   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  108 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  109 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
98 110 testActorsAndMessages(1000, 1000, 10);
99 111 }
100 112
101 113 @Test
102 114 public void testNoMessagesAfterDestroy() throws InterruptedException {
103   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  115 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  116 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
104 117 ActorTestCtx testCtx1 = getActorTestCtx(1);
105 118 ActorTestCtx testCtx2 = getActorTestCtx(1);
106 119
... ... @@ -119,7 +132,8 @@ public class ActorSystemTest {
119 132
120 133 @Test
121 134 public void testOneActorCreated() throws InterruptedException {
122   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  135 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  136 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
123 137 ActorTestCtx testCtx1 = getActorTestCtx(1);
124 138 ActorTestCtx testCtx2 = getActorTestCtx(1);
125 139 TbActorId actorId = new TbEntityActorId(new DeviceId(UUID.randomUUID()));
... ... @@ -145,7 +159,8 @@ public class ActorSystemTest {
145 159
146 160 @Test
147 161 public void testActorCreatorCalledOnce() throws InterruptedException {
148   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  162 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  163 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
149 164 ActorTestCtx testCtx = getActorTestCtx(1);
150 165 TbActorId actorId = new TbEntityActorId(new DeviceId(UUID.randomUUID()));
151 166 final int actorsCount = 1000;
... ... @@ -169,7 +184,8 @@ public class ActorSystemTest {
169 184
170 185 @Test
171 186 public void testFailedInit() throws InterruptedException {
172   - actorSystem.createDispatcher(ROOT_DISPATCHER, Executors.newWorkStealingPool(parallelism));
  187 + executor = ThingsBoardExecutors.newWorkStealingPool(parallelism, getClass());
  188 + actorSystem.createDispatcher(ROOT_DISPATCHER, executor);
173 189 ActorTestCtx testCtx1 = getActorTestCtx(1);
174 190 ActorTestCtx testCtx2 = getActorTestCtx(1);
175 191
... ...
... ... @@ -24,6 +24,7 @@ import org.eclipse.californium.scandium.DTLSConnector;
24 24 import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
25 25 import org.springframework.beans.factory.annotation.Autowired;
26 26 import org.springframework.stereotype.Component;
  27 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
27 28
28 29 import javax.annotation.PostConstruct;
29 30 import javax.annotation.PreDestroy;
... ... @@ -118,7 +119,7 @@ public class DefaultCoapServerService implements CoapServerService {
118 119 CoapEndpoint dtlsCoapEndpoint = dtlsCoapEndpointBuilder.build();
119 120 server.addEndpoint(dtlsCoapEndpoint);
120 121 tbDtlsCertificateVerifier = (TbCoapDtlsCertificateVerifier) dtlsConnectorConfig.getAdvancedCertificateVerifier();
121   - dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor();
  122 + dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName(getClass().getSimpleName()));
122 123 dtlsSessionsExecutor.scheduleAtFixedRate(this::evictTimeoutSessions, new Random().nextInt((int) getDtlsSessionReportTimeout()), getDtlsSessionReportTimeout(), TimeUnit.MILLISECONDS);
123 124 }
124 125 Resource root = server.getRoot();
... ...
... ... @@ -18,6 +18,7 @@ package org.thingsboard.server.queue.common;
18 18 import com.google.common.util.concurrent.ListeningExecutorService;
19 19 import com.google.common.util.concurrent.MoreExecutors;
20 20 import lombok.extern.slf4j.Slf4j;
  21 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
21 22 import org.thingsboard.server.queue.TbQueueMsg;
22 23
23 24 import java.util.concurrent.Executors;
... ... @@ -41,7 +42,7 @@ public abstract class AbstractParallelTbQueueConsumerTemplate<R, T extends TbQue
41 42 log.trace("Interrupted while waiting for consumer executor to stop");
42 43 }
43 44 }
44   - consumerExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadPoolSize));
  45 + consumerExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadPoolSize, ThingsBoardThreadFactory.forName(getClass().getSimpleName())));
45 46 }
46 47
47 48 protected void shutdownExecutor() {
... ...
... ... @@ -21,6 +21,7 @@ import org.eclipse.californium.core.Utils;
21 21 import org.eclipse.californium.elements.DtlsEndpointContext;
22 22 import org.eclipse.californium.elements.EndpointContext;
23 23 import org.eclipse.californium.elements.exception.ConnectorException;
  24 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
24 25
25 26 import java.io.IOException;
26 27 import java.net.URI;
... ... @@ -31,7 +32,7 @@ import java.util.concurrent.Executors;
31 32
32 33 public class NoSecClient {
33 34
34   - private ExecutorService executor = Executors.newFixedThreadPool(1);
  35 + private ExecutorService executor = Executors.newFixedThreadPool(1, ThingsBoardThreadFactory.forName(getClass().getSimpleName()));
35 36 private CoapClient coapClient;
36 37
37 38 public NoSecClient(String host, int port, String accessToken, String clientKeys, String sharedKeys) throws URISyntaxException {
... ...
... ... @@ -22,6 +22,7 @@ import org.eclipse.californium.core.CoapObserveRelation;
22 22 import org.eclipse.californium.core.CoapResponse;
23 23 import org.eclipse.californium.core.coap.CoAP;
24 24 import org.eclipse.californium.core.coap.Request;
  25 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
25 26
26 27 import java.net.URI;
27 28 import java.net.URISyntaxException;
... ... @@ -36,7 +37,7 @@ public class NoSecObserveClient {
36 37
37 38 private CoapClient coapClient;
38 39 private CoapObserveRelation observeRelation;
39   - private ExecutorService executor = Executors.newFixedThreadPool(1);
  40 + private ExecutorService executor = Executors.newFixedThreadPool(1, ThingsBoardThreadFactory.forName(getClass().getSimpleName()));
40 41 private CountDownLatch latch;
41 42
42 43 public NoSecObserveClient(String host, int port, String accessToken) throws URISyntaxException {
... ...
... ... @@ -27,6 +27,7 @@ import org.eclipse.californium.scandium.DTLSConnector;
27 27 import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
28 28 import org.eclipse.californium.scandium.dtls.CertificateType;
29 29 import org.eclipse.californium.scandium.dtls.x509.StaticNewAdvancedCertificateVerifier;
  30 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
30 31
31 32 import java.io.IOException;
32 33 import java.net.URI;
... ... @@ -41,7 +42,7 @@ import java.util.concurrent.Executors;
41 42 public class SecureClientNoAuth {
42 43
43 44 private final DTLSConnector dtlsConnector;
44   - private ExecutorService executor = Executors.newFixedThreadPool(1);
  45 + private ExecutorService executor = Executors.newFixedThreadPool(1, ThingsBoardThreadFactory.forName(getClass().getSimpleName()));
45 46 private CoapClient coapClient;
46 47
47 48 public SecureClientNoAuth(DTLSConnector dtlsConnector, String host, int port, String accessToken, String clientKeys, String sharedKeys) throws URISyntaxException {
... ...
... ... @@ -27,6 +27,7 @@ import org.eclipse.californium.scandium.DTLSConnector;
27 27 import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
28 28 import org.eclipse.californium.scandium.dtls.CertificateType;
29 29 import org.eclipse.californium.scandium.dtls.x509.StaticNewAdvancedCertificateVerifier;
  30 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
30 31
31 32 import java.io.IOException;
32 33 import java.net.URI;
... ... @@ -41,7 +42,7 @@ import java.util.concurrent.Executors;
41 42 public class SecureClientX509 {
42 43
43 44 private final DTLSConnector dtlsConnector;
44   - private ExecutorService executor = Executors.newFixedThreadPool(1);
  45 + private ExecutorService executor = Executors.newFixedThreadPool(1, ThingsBoardThreadFactory.forName(getClass().getSimpleName()));
45 46 private CoapClient coapClient;
46 47
47 48 public SecureClientX509(DTLSConnector dtlsConnector, String host, int port, String clientKeys, String sharedKeys) throws URISyntaxException {
... ...
... ... @@ -34,6 +34,7 @@ import org.snmp4j.transport.DefaultTcpTransportMapping;
34 34 import org.snmp4j.transport.DefaultUdpTransportMapping;
35 35 import org.springframework.beans.factory.annotation.Value;
36 36 import org.springframework.stereotype.Service;
  37 +import org.thingsboard.common.util.ThingsBoardExecutors;
37 38 import org.thingsboard.common.util.ThingsBoardThreadFactory;
38 39 import org.thingsboard.server.common.data.DataConstants;
39 40 import org.thingsboard.server.common.data.TbTransportService;
... ... @@ -90,7 +91,7 @@ public class SnmpTransportService implements TbTransportService {
90 91 @PostConstruct
91 92 private void init() throws IOException {
92 93 queryingExecutor = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), ThingsBoardThreadFactory.forName("snmp-querying"));
93   - responseProcessingExecutor = Executors.newWorkStealingPool(responseProcessingParallelismLevel);
  94 + responseProcessingExecutor = ThingsBoardExecutors.newWorkStealingPool(responseProcessingParallelismLevel, "snmp-response-processing");
94 95
95 96 initializeSnmp();
96 97 configureResponseDataMappers();
... ... @@ -99,6 +100,16 @@ public class SnmpTransportService implements TbTransportService {
99 100 log.info("SNMP transport service initialized");
100 101 }
101 102
  103 + @PreDestroy
  104 + public void stop() {
  105 + if (queryingExecutor != null) {
  106 + queryingExecutor.shutdownNow();
  107 + }
  108 + if (responseProcessingExecutor != null) {
  109 + responseProcessingExecutor.shutdownNow();
  110 + }
  111 + }
  112 +
102 113 private void initializeSnmp() throws IOException {
103 114 TransportMapping<?> transportMapping;
104 115 switch (snmpUnderlyingProtocol) {
... ...
... ... @@ -24,6 +24,7 @@ import org.junit.Assert;
24 24 import org.junit.Before;
25 25 import org.junit.Ignore;
26 26 import org.junit.Test;
  27 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
27 28 import org.thingsboard.server.common.data.Device;
28 29 import org.thingsboard.server.common.data.DeviceProfile;
29 30 import org.thingsboard.server.common.data.DeviceProfileInfo;
... ... @@ -158,7 +159,7 @@ public class BaseDeviceProfileServiceTest extends AbstractServiceTest {
158 159
159 160 @Test
160 161 public void testFindOrCreateDeviceProfile() throws ExecutionException, InterruptedException {
161   - ListeningExecutorService testExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(100));
  162 + ListeningExecutorService testExecutor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(100, ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope")));
162 163 try {
163 164 List<ListenableFuture<DeviceProfile>> futures = new ArrayList<>();
164 165 for (int i = 0; i < 50; i++) {
... ...
... ... @@ -19,8 +19,10 @@ import com.datastax.oss.driver.api.core.uuid.Uuids;
19 19 import com.google.common.util.concurrent.ListenableFuture;
20 20 import com.google.common.util.concurrent.ListeningExecutorService;
21 21 import com.google.common.util.concurrent.MoreExecutors;
  22 +import org.junit.After;
22 23 import org.junit.Test;
23 24 import org.springframework.beans.factory.annotation.Autowired;
  25 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
24 26 import org.thingsboard.server.common.data.Device;
25 27 import org.thingsboard.server.common.data.id.CustomerId;
26 28 import org.thingsboard.server.common.data.id.DeviceId;
... ... @@ -47,6 +49,15 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest {
47 49 @Autowired
48 50 private DeviceDao deviceDao;
49 51
  52 + ListeningExecutorService executor;
  53 +
  54 + @After
  55 + public void tearDown() throws Exception {
  56 + if (executor != null) {
  57 + executor.shutdownNow();
  58 + }
  59 + }
  60 +
50 61 @Test
51 62 public void testFindDevicesByTenantId() {
52 63 UUID tenantId1 = Uuids.timeBased();
... ... @@ -77,8 +88,8 @@ public class JpaDeviceDaoTest extends AbstractJpaDaoTest {
77 88 assertNotNull(entity);
78 89 assertEquals(uuid, entity.getId().getId());
79 90
80   - ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
81   - ListenableFuture<Device> future = service.submit(() -> deviceDao.findById(new TenantId(tenantId), uuid));
  91 + executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10, ThingsBoardThreadFactory.forName(getClass().getSimpleName() + "-test-scope")));
  92 + ListenableFuture<Device> future = executor.submit(() -> deviceDao.findById(new TenantId(tenantId), uuid));
82 93 Device asyncDevice = future.get();
83 94 assertNotNull("Async device expected to be not null", asyncDevice);
84 95 }
... ...
... ... @@ -34,6 +34,7 @@ import org.junit.runner.Description;
34 34 import org.springframework.core.ParameterizedTypeReference;
35 35 import org.springframework.http.HttpMethod;
36 36 import org.springframework.http.ResponseEntity;
  37 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
37 38 import org.thingsboard.mqtt.MqttClient;
38 39 import org.thingsboard.mqtt.MqttClientConfig;
39 40 import org.thingsboard.mqtt.MqttHandler;
... ... @@ -263,7 +264,7 @@ public class MqttClientTest extends AbstractContainerTest {
263 264 JsonObject serverRpcPayload = new JsonObject();
264 265 serverRpcPayload.addProperty("method", "getValue");
265 266 serverRpcPayload.addProperty("params", true);
266   - ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
  267 + ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName(getClass().getSimpleName())));
267 268 ListenableFuture<ResponseEntity> future = service.submit(() -> {
268 269 try {
269 270 return restClient.getRestTemplate()
... ... @@ -287,6 +288,7 @@ public class MqttClientTest extends AbstractContainerTest {
287 288 mqttClient.publish("v1/devices/me/rpc/response/" + requestId, Unpooled.wrappedBuffer(clientResponse.toString().getBytes())).get();
288 289
289 290 ResponseEntity serverResponse = future.get(5, TimeUnit.SECONDS);
  291 + service.shutdownNow();
290 292 Assert.assertTrue(serverResponse.getStatusCode().is2xxSuccessful());
291 293 Assert.assertEquals(clientResponse.toString(), serverResponse.getBody());
292 294
... ...
... ... @@ -33,6 +33,7 @@ import org.junit.Assert;
33 33 import org.junit.Before;
34 34 import org.junit.Test;
35 35 import org.springframework.http.ResponseEntity;
  36 +import org.thingsboard.common.util.ThingsBoardThreadFactory;
36 37 import org.thingsboard.mqtt.MqttClient;
37 38 import org.thingsboard.mqtt.MqttClientConfig;
38 39 import org.thingsboard.mqtt.MqttHandler;
... ... @@ -259,7 +260,7 @@ public class MqttGatewayClientTest extends AbstractContainerTest {
259 260 JsonObject serverRpcPayload = new JsonObject();
260 261 serverRpcPayload.addProperty("method", "getValue");
261 262 serverRpcPayload.addProperty("params", true);
262   - ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
  263 + ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName(getClass().getSimpleName())));
263 264 ListenableFuture<ResponseEntity> future = service.submit(() -> {
264 265 try {
265 266 return restClient.getRestTemplate()
... ... @@ -273,6 +274,7 @@ public class MqttGatewayClientTest extends AbstractContainerTest {
273 274
274 275 // Wait for RPC call from the server and send the response
275 276 MqttEvent requestFromServer = listener.getEvents().poll(10, TimeUnit.SECONDS);
  277 + service.shutdownNow();
276 278
277 279 Assert.assertNotNull(requestFromServer);
278 280 Assert.assertNotNull(requestFromServer.getMessage());
... ...