device creation lock upgrade

This commit is contained in:
vzikratyi 2020-07-17 13:09:50 +03:00 committed by Andrew Shvayka
parent 30210c7079
commit 61e1ce44aa

View File

@ -71,7 +71,7 @@ public class GatewaySessionHandler {
private final TransportService transportService;
private final DeviceInfoProto gateway;
private final UUID sessionId;
private final Lock deviceCreationLock = new ReentrantLock();
private final ConcurrentMap<String, Lock> deviceCreationLockMap;
private final ConcurrentMap<String, GatewayDeviceSessionCtx> devices;
private final ConcurrentMap<String, SettableFuture<GatewayDeviceSessionCtx>> deviceFutures;
private final ConcurrentMap<MqttTopicMatcher, Integer> mqttQoSMap;
@ -86,6 +86,7 @@ public class GatewaySessionHandler {
this.sessionId = sessionId;
this.devices = new ConcurrentHashMap<>();
this.deviceFutures = new ConcurrentHashMap<>();
this.deviceCreationLockMap = new ConcurrentHashMap<>();
this.mqttQoSMap = deviceSessionCtx.getMqttQoSMap();
this.channel = deviceSessionCtx.getChannel();
}
@ -113,6 +114,7 @@ public class GatewaySessionHandler {
private ListenableFuture<GatewayDeviceSessionCtx> onDeviceConnect(String deviceName, String deviceType) {
GatewayDeviceSessionCtx result = devices.get(deviceName);
if (result == null) {
Lock deviceCreationLock = deviceCreationLockMap.computeIfAbsent(deviceName, s -> new ReentrantLock());
deviceCreationLock.lock();
try {
result = devices.get(deviceName);
@ -145,6 +147,7 @@ public class GatewaySessionHandler {
public void onSuccess(GetOrCreateDeviceFromGatewayResponseMsg msg) {
GatewayDeviceSessionCtx deviceSessionCtx = new GatewayDeviceSessionCtx(GatewaySessionHandler.this, msg.getDeviceInfo(), mqttQoSMap);
if (devices.putIfAbsent(deviceName, deviceSessionCtx) == null) {
log.trace("[{}] First got or created device [{}], type [{}] for the gateway session", sessionId, deviceName, deviceType);
SessionInfoProto deviceSessionInfo = deviceSessionCtx.getSessionInfo();
transportService.registerAsyncSession(deviceSessionInfo, deviceSessionCtx);
transportService.process(deviceSessionInfo, DefaultTransportService.getSessionEventMsg(TransportProtos.SessionEvent.OPEN), null);