fixed race condition during unreq and updating lwm2m client

This commit is contained in:
YevhenBondarenko 2021-08-03 14:18:46 +03:00 committed by Andrew Shvayka
parent bfb055cc6e
commit ee74bbed21
3 changed files with 17 additions and 5 deletions

View File

@ -316,7 +316,11 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
public void update(LwM2mClient client) { public void update(LwM2mClient client) {
client.lock(); client.lock();
try { try {
if (client.getState().equals(LwM2MClientState.REGISTERED)) {
clientStore.put(client); clientStore.put(client);
} else {
log.error("[{}] Client is in invalid state: {}!", client.getEndpoint(), client.getState());
}
} finally { } finally {
client.unlock(); client.unlock();
} }

View File

@ -106,6 +106,7 @@ public abstract class LwM2MClientOtaInfo<Strategy, State, Result> {
public abstract OtaPackageType getType(); public abstract OtaPackageType getType();
@JsonIgnore
public String getTargetPackageId() { public String getTargetPackageId() {
return getPackageId(targetName, targetVersion); return getPackageId(targetName, targetVersion);
} }

View File

@ -15,11 +15,13 @@
*/ */
package org.thingsboard.server.transport.lwm2m.server.store; package org.thingsboard.server.transport.lwm2m.server.store;
import lombok.extern.slf4j.Slf4j;
import org.nustaq.serialization.FSTConfiguration; import org.nustaq.serialization.FSTConfiguration;
import org.springframework.data.redis.connection.RedisClusterConnection; import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions; import org.springframework.data.redis.core.ScanOptions;
import org.thingsboard.server.transport.lwm2m.server.client.LwM2MClientState;
import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,6 +29,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@Slf4j
public class TbRedisLwM2MClientStore implements TbLwM2MClientStore { public class TbRedisLwM2MClientStore implements TbLwM2MClientStore {
private static final String CLIENT_EP = "CLIENT#EP#"; private static final String CLIENT_EP = "CLIENT#EP#";
@ -76,11 +79,15 @@ public class TbRedisLwM2MClientStore implements TbLwM2MClientStore {
@Override @Override
public void put(LwM2mClient client) { public void put(LwM2mClient client) {
if (client.getState().equals(LwM2MClientState.UNREGISTERED)) {
log.error("[{}] Client is in invalid state: {}!", client.getEndpoint(), client.getState(), new Exception());
} else {
byte[] clientSerialized = serializer.asByteArray(client); byte[] clientSerialized = serializer.asByteArray(client);
try (var connection = connectionFactory.getConnection()) { try (var connection = connectionFactory.getConnection()) {
connection.getSet(getKey(client.getEndpoint()), clientSerialized); connection.getSet(getKey(client.getEndpoint()), clientSerialized);
} }
} }
}
@Override @Override
public void remove(String endpoint) { public void remove(String endpoint) {