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