Commit dc151ace7717ceff319b65d5cb429d70e425efee

Authored by Igor Kulikov
Committed by GitHub
2 parents 4bc1f2bd 2071890a

Merge pull request #3087 from volodymyr-babak/master

Added device creation lock by device name
... ... @@ -58,6 +58,9 @@ import org.thingsboard.server.service.queue.TbClusterService;
58 58 import org.thingsboard.server.service.state.DeviceStateService;
59 59
60 60 import java.util.UUID;
  61 +import java.util.concurrent.ConcurrentHashMap;
  62 +import java.util.concurrent.ConcurrentMap;
  63 +import java.util.concurrent.locks.Lock;
61 64 import java.util.concurrent.locks.ReentrantLock;
62 65
63 66 /**
... ... @@ -92,7 +95,7 @@ public class DefaultTransportApiService implements TransportApiService {
92 95 @Autowired
93 96 protected TbClusterService tbClusterService;
94 97
95   - private ReentrantLock deviceCreationLock = new ReentrantLock();
  98 + private final ConcurrentMap<String, ReentrantLock> deviceCreationLocks = new ConcurrentHashMap<>();
96 99
97 100 @Override
98 101 public ListenableFuture<TbProtoQueueMsg<TransportApiResponseMsg>> handle(TbProtoQueueMsg<TransportApiRequestMsg> tbProtoQueueMsg) {
... ... @@ -125,6 +128,7 @@ public class DefaultTransportApiService implements TransportApiService {
125 128 DeviceId gatewayId = new DeviceId(new UUID(requestMsg.getGatewayIdMSB(), requestMsg.getGatewayIdLSB()));
126 129 ListenableFuture<Device> gatewayFuture = deviceService.findDeviceByIdAsync(TenantId.SYS_TENANT_ID, gatewayId);
127 130 return Futures.transform(gatewayFuture, gateway -> {
  131 + Lock deviceCreationLock = deviceCreationLocks.computeIfAbsent(requestMsg.getDeviceName(), id -> new ReentrantLock());
128 132 deviceCreationLock.lock();
129 133 try {
130 134 Device device = deviceService.findDeviceByTenantIdAndName(gateway.getTenantId(), requestMsg.getDeviceName());
... ...