Commit ee74bbed218e6a106f672c6804b1dc65e4f51ffc

Authored by YevhenBondarenko
Committed by Andrew Shvayka
1 parent bfb055cc

fixed race condition during unreq and updating lwm2m client

... ... @@ -166,7 +166,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
166 166 this.lwM2mClientsByRegistrationId.put(registration.getId(), client);
167 167 client.setState(LwM2MClientState.REGISTERED);
168 168 onUplink(client);
169   - if(!compareAndSetSleepFlag(client, false)){
  169 + if (!compareAndSetSleepFlag(client, false)) {
170 170 clientStore.put(client);
171 171 }
172 172 } finally {
... ... @@ -316,7 +316,11 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
316 316 public void update(LwM2mClient client) {
317 317 client.lock();
318 318 try {
319   - clientStore.put(client);
  319 + if (client.getState().equals(LwM2MClientState.REGISTERED)) {
  320 + clientStore.put(client);
  321 + } else {
  322 + log.error("[{}] Client is in invalid state: {}!", client.getEndpoint(), client.getState());
  323 + }
320 324 } finally {
321 325 client.unlock();
322 326 }
... ...
... ... @@ -106,6 +106,7 @@ public abstract class LwM2MClientOtaInfo<Strategy, State, Result> {
106 106
107 107 public abstract OtaPackageType getType();
108 108
  109 + @JsonIgnore
109 110 public String getTargetPackageId() {
110 111 return getPackageId(targetName, targetVersion);
111 112 }
... ...
... ... @@ -15,11 +15,13 @@
15 15 */
16 16 package org.thingsboard.server.transport.lwm2m.server.store;
17 17
  18 +import lombok.extern.slf4j.Slf4j;
18 19 import org.nustaq.serialization.FSTConfiguration;
19 20 import org.springframework.data.redis.connection.RedisClusterConnection;
20 21 import org.springframework.data.redis.connection.RedisConnectionFactory;
21 22 import org.springframework.data.redis.core.Cursor;
22 23 import org.springframework.data.redis.core.ScanOptions;
  24 +import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientState;
23 25 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient;
24 26
25 27 import java.util.ArrayList;
... ... @@ -27,6 +29,7 @@ import java.util.HashSet;
27 29 import java.util.List;
28 30 import java.util.Set;
29 31
  32 +@Slf4j
30 33 public class TbRedisLwM2MClientStore implements TbLwM2MClientStore {
31 34
32 35 private static final String CLIENT_EP = "CLIENT#EP#";
... ... @@ -76,9 +79,13 @@ public class TbRedisLwM2MClientStore implements TbLwM2MClientStore {
76 79
77 80 @Override
78 81 public void put(LwM2mClient client) {
79   - byte[] clientSerialized = serializer.asByteArray(client);
80   - try (var connection = connectionFactory.getConnection()) {
81   - connection.getSet(getKey(client.getEndpoint()), clientSerialized);
  82 + if (client.getState().equals(LwM2MClientState.UNREGISTERED)) {
  83 + log.error("[{}] Client is in invalid state: {}!", client.getEndpoint(), client.getState(), new Exception());
  84 + } else {
  85 + byte[] clientSerialized = serializer.asByteArray(client);
  86 + try (var connection = connectionFactory.getConnection()) {
  87 + connection.getSet(getKey(client.getEndpoint()), clientSerialized);
  88 + }
82 89 }
83 90 }
84 91
... ...