Merge pull request #13465 from thingsboard/fix/telemetry-bug
Fix WS updates when updating older timeseries
This commit is contained in:
commit
ec9c3d5614
@ -348,7 +348,7 @@ public class DefaultTbLocalSubscriptionService implements TbLocalSubscriptionSer
|
|||||||
if (sub.isLatestValues()) {
|
if (sub.isLatestValues()) {
|
||||||
for (TsKvEntry kv : data) {
|
for (TsKvEntry kv : data) {
|
||||||
Long stateTs = keyStates.get(kv.getKey());
|
Long stateTs = keyStates.get(kv.getKey());
|
||||||
if (stateTs == null || kv.getTs() > stateTs) {
|
if (stateTs == null || kv.getTs() >= stateTs) {
|
||||||
if (updateData == null) {
|
if (updateData == null) {
|
||||||
updateData = new ArrayList<>();
|
updateData = new ArrayList<>();
|
||||||
}
|
}
|
||||||
@ -362,7 +362,7 @@ public class DefaultTbLocalSubscriptionService implements TbLocalSubscriptionSer
|
|||||||
for (TsKvEntry kv : data) {
|
for (TsKvEntry kv : data) {
|
||||||
Long stateTs = keyStates.get(kv.getKey());
|
Long stateTs = keyStates.get(kv.getKey());
|
||||||
if (stateTs != null) {
|
if (stateTs != null) {
|
||||||
if (!sub.isLatestValues() || kv.getTs() > stateTs) {
|
if (!sub.isLatestValues() || kv.getTs() >= stateTs) {
|
||||||
if (updateData == null) {
|
if (updateData == null) {
|
||||||
updateData = new ArrayList<>();
|
updateData = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,6 +44,7 @@ import org.thingsboard.server.service.ws.WebSocketSessionRef;
|
|||||||
import org.thingsboard.server.service.ws.telemetry.sub.TelemetrySubscriptionUpdate;
|
import org.thingsboard.server.service.ws.telemetry.sub.TelemetrySubscriptionUpdate;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -154,9 +155,8 @@ public abstract class TbAbstractEntityQuerySubCtx<T extends EntityCountQuery> ex
|
|||||||
private void dynamicValueSubUpdate(String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate,
|
private void dynamicValueSubUpdate(String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate,
|
||||||
Map<String, DynamicValueKeySub> dynamicValueKeySubMap) {
|
Map<String, DynamicValueKeySub> dynamicValueKeySubMap) {
|
||||||
Map<String, TsValue> latestUpdate = new HashMap<>();
|
Map<String, TsValue> latestUpdate = new HashMap<>();
|
||||||
subscriptionUpdate.getData().forEach((k, v) -> {
|
subscriptionUpdate.getValues().forEach((key, values) -> {
|
||||||
Object[] data = (Object[]) v.get(0);
|
latestUpdate.put(key, getLatest(values));
|
||||||
latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
boolean invalidateFilter = false;
|
boolean invalidateFilter = false;
|
||||||
@ -283,6 +283,12 @@ public abstract class TbAbstractEntityQuerySubCtx<T extends EntityCountQuery> ex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected TsValue getLatest(List<TsValue> values) {
|
||||||
|
return values.stream()
|
||||||
|
.max(Comparator.comparing(TsValue::getTs))
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class DynamicValueKey {
|
public static class DynamicValueKey {
|
||||||
@Getter
|
@Getter
|
||||||
|
|||||||
@ -38,7 +38,6 @@ import org.thingsboard.server.common.data.query.TsValue;
|
|||||||
import org.thingsboard.server.dao.alarm.AlarmService;
|
import org.thingsboard.server.dao.alarm.AlarmService;
|
||||||
import org.thingsboard.server.dao.attributes.AttributesService;
|
import org.thingsboard.server.dao.attributes.AttributesService;
|
||||||
import org.thingsboard.server.dao.entity.EntityService;
|
import org.thingsboard.server.dao.entity.EntityService;
|
||||||
import org.thingsboard.server.dao.model.ModelConstants;
|
|
||||||
import org.thingsboard.server.dao.sql.query.EntityKeyMapping;
|
import org.thingsboard.server.dao.sql.query.EntityKeyMapping;
|
||||||
import org.thingsboard.server.service.ws.WebSocketService;
|
import org.thingsboard.server.service.ws.WebSocketService;
|
||||||
import org.thingsboard.server.service.ws.WebSocketSessionRef;
|
import org.thingsboard.server.service.ws.WebSocketSessionRef;
|
||||||
@ -191,9 +190,8 @@ public class TbAlarmDataSubCtx extends TbAbstractDataSubCtx<AlarmDataQuery> {
|
|||||||
EntityId entityId = subToEntityIdMap.get(subscriptionUpdate.getSubscriptionId());
|
EntityId entityId = subToEntityIdMap.get(subscriptionUpdate.getSubscriptionId());
|
||||||
if (entityId != null) {
|
if (entityId != null) {
|
||||||
Map<String, TsValue> latestUpdate = new HashMap<>();
|
Map<String, TsValue> latestUpdate = new HashMap<>();
|
||||||
subscriptionUpdate.getData().forEach((k, v) -> {
|
subscriptionUpdate.getValues().forEach((key, values) -> {
|
||||||
Object[] data = (Object[]) v.get(0);
|
latestUpdate.put(key, getLatest(values));
|
||||||
latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
|
|
||||||
});
|
});
|
||||||
EntityData entityData = entitiesMap.get(entityId);
|
EntityData entityData = entitiesMap.get(entityId);
|
||||||
entityData.getLatest().computeIfAbsent(keyType, tmp -> new HashMap<>()).putAll(latestUpdate);
|
entityData.getLatest().computeIfAbsent(keyType, tmp -> new HashMap<>()).putAll(latestUpdate);
|
||||||
|
|||||||
@ -43,7 +43,6 @@ import java.util.HashMap;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -94,15 +93,13 @@ public class TbEntityDataSubCtx extends TbAbstractDataSubCtx<EntityDataQuery> {
|
|||||||
|
|
||||||
private void sendLatestWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) {
|
private void sendLatestWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) {
|
||||||
Map<String, TsValue> latestUpdate = new HashMap<>();
|
Map<String, TsValue> latestUpdate = new HashMap<>();
|
||||||
subscriptionUpdate.getData().forEach((k, v) -> {
|
subscriptionUpdate.getValues().forEach((key, values) -> {
|
||||||
Object[] data = (Object[]) v.get(0);
|
latestUpdate.put(key, getLatest(values));
|
||||||
latestUpdate.put(k, new TsValue((Long) data[0], (String) data[1]));
|
|
||||||
});
|
});
|
||||||
EntityData entityData = getDataForEntity(entityId);
|
EntityData entityData = getDataForEntity(entityId);
|
||||||
if (entityData != null && entityData.getLatest() != null) {
|
if (entityData != null && entityData.getLatest() != null) {
|
||||||
Map<String, TsValue> latestCtxValues = entityData.getLatest().get(keyType);
|
Map<String, TsValue> latestCtxValues = entityData.getLatest().computeIfAbsent(keyType, __ -> new HashMap<>());
|
||||||
log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues);
|
log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues);
|
||||||
if (latestCtxValues != null) {
|
|
||||||
latestCtxValues.forEach((k, v) -> {
|
latestCtxValues.forEach((k, v) -> {
|
||||||
TsValue update = latestUpdate.get(k);
|
TsValue update = latestUpdate.get(k);
|
||||||
if (update != null) {
|
if (update != null) {
|
||||||
@ -121,8 +118,7 @@ public class TbEntityDataSubCtx extends TbAbstractDataSubCtx<EntityDataQuery> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
//Setting new values
|
//Setting new values
|
||||||
latestUpdate.forEach(latestCtxValues::put);
|
latestCtxValues.putAll(latestUpdate);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!latestUpdate.isEmpty()) {
|
if (!latestUpdate.isEmpty()) {
|
||||||
Map<EntityKeyType, Map<String, TsValue>> latestMap = Collections.singletonMap(keyType, latestUpdate);
|
Map<EntityKeyType, Map<String, TsValue>> latestMap = Collections.singletonMap(keyType, latestUpdate);
|
||||||
@ -133,40 +129,39 @@ public class TbEntityDataSubCtx extends TbAbstractDataSubCtx<EntityDataQuery> {
|
|||||||
|
|
||||||
private void sendTsWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) {
|
private void sendTsWsMsg(EntityId entityId, String sessionId, TelemetrySubscriptionUpdate subscriptionUpdate, EntityKeyType keyType) {
|
||||||
Map<String, List<TsValue>> tsUpdate = new HashMap<>();
|
Map<String, List<TsValue>> tsUpdate = new HashMap<>();
|
||||||
subscriptionUpdate.getData().forEach((k, v) -> {
|
subscriptionUpdate.getValues().forEach((key, values) -> {
|
||||||
Object[] data = (Object[]) v.get(0);
|
tsUpdate.put(key, new ArrayList<>(values));
|
||||||
tsUpdate.computeIfAbsent(k, key -> new ArrayList<>()).add(new TsValue((Long) data[0], (String) data[1]));
|
|
||||||
});
|
});
|
||||||
Map<String, TsValue> latestCtxValues = getLatestTsValuesForEntity(entityId);
|
Map<String, TsValue> latestCtxValues = getLatestTsValuesForEntity(entityId);
|
||||||
log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues);
|
log.trace("[{}][{}][{}] Going to compare update with {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), latestCtxValues);
|
||||||
if (latestCtxValues != null) {
|
if (latestCtxValues != null) {
|
||||||
latestCtxValues.forEach((k, v) -> {
|
latestCtxValues.forEach((key, latest) -> {
|
||||||
List<TsValue> updateList = tsUpdate.get(k);
|
List<TsValue> updateList = tsUpdate.get(key);
|
||||||
if (updateList != null) {
|
if (updateList != null) {
|
||||||
for (TsValue update : new ArrayList<>(updateList)) {
|
for (TsValue update : new ArrayList<>(updateList)) {
|
||||||
if (update.getTs() < v.getTs()) {
|
if (update.getTs() < latest.getTs()) {
|
||||||
log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
|
log.trace("[{}][{}][{}] Removed stale update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), key, update.getTs());
|
||||||
// Looks like this is redundant feature and our UI is ready to merge the updates.
|
// Looks like this is redundant feature and our UI is ready to merge the updates.
|
||||||
//updateList.remove(update);
|
//updateList.remove(update);
|
||||||
} else if ((update.getTs() == v.getTs() && update.getValue().equals(v.getValue()))) {
|
} else if ((update.getTs() == latest.getTs() && update.getValue().equals(latest.getValue()))) {
|
||||||
log.trace("[{}][{}][{}] Removed duplicate update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), k, update.getTs());
|
log.trace("[{}][{}][{}] Removed duplicate update for key: {} and ts: {}", sessionId, cmdId, subscriptionUpdate.getSubscriptionId(), key, update.getTs());
|
||||||
updateList.remove(update);
|
updateList.remove(update);
|
||||||
}
|
}
|
||||||
if (updateList.isEmpty()) {
|
if (updateList.isEmpty()) {
|
||||||
tsUpdate.remove(k);
|
tsUpdate.remove(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
//Setting new values
|
//Setting new values
|
||||||
tsUpdate.forEach((k, v) -> {
|
tsUpdate.forEach((key, values) -> {
|
||||||
Optional<TsValue> maxValue = v.stream().max(Comparator.comparingLong(TsValue::getTs));
|
values.stream().max(Comparator.comparingLong(TsValue::getTs))
|
||||||
maxValue.ifPresent(max -> latestCtxValues.put(k, max));
|
.ifPresent(latest -> latestCtxValues.put(key, latest));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!tsUpdate.isEmpty()) {
|
if (!tsUpdate.isEmpty()) {
|
||||||
Map<String, TsValue[]> tsMap = new HashMap<>();
|
Map<String, TsValue[]> tsMap = new HashMap<>();
|
||||||
tsUpdate.forEach((key, tsValue) -> tsMap.put(key, tsValue.toArray(new TsValue[tsValue.size()])));
|
tsUpdate.forEach((key, values) -> tsMap.put(key, values.toArray(new TsValue[0])));
|
||||||
EntityData entityData = new EntityData(entityId, null, tsMap);
|
EntityData entityData = new EntityData(entityId, null, tsMap);
|
||||||
sendWsMsg(new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData), maxEntitiesPerDataSubscription));
|
sendWsMsg(new EntityDataUpdate(cmdId, null, Collections.singletonList(entityData), maxEntitiesPerDataSubscription));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -274,7 +274,7 @@ public class DefaultWebSocketService implements WebSocketService {
|
|||||||
@Override
|
@Override
|
||||||
public void sendUpdate(String sessionId, int cmdId, TelemetrySubscriptionUpdate update) {
|
public void sendUpdate(String sessionId, int cmdId, TelemetrySubscriptionUpdate update) {
|
||||||
// We substitute the subscriptionId with cmdId for old-style subscriptions.
|
// We substitute the subscriptionId with cmdId for old-style subscriptions.
|
||||||
doSendUpdate(sessionId, cmdId, update.copyWithNewSubscriptionId(cmdId));
|
doSendUpdate(sessionId, cmdId, update.withSubscriptionId(cmdId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -16,19 +16,26 @@
|
|||||||
package org.thingsboard.server.service.ws.telemetry.sub;
|
package org.thingsboard.server.service.ws.telemetry.sub;
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.With;
|
||||||
import org.thingsboard.server.common.data.kv.TsKvEntry;
|
import org.thingsboard.server.common.data.kv.TsKvEntry;
|
||||||
|
import org.thingsboard.server.common.data.query.TsValue;
|
||||||
import org.thingsboard.server.service.subscription.SubscriptionErrorCode;
|
import org.thingsboard.server.service.subscription.SubscriptionErrorCode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Getter
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class TelemetrySubscriptionUpdate {
|
public class TelemetrySubscriptionUpdate {
|
||||||
|
|
||||||
|
@With
|
||||||
private final int subscriptionId;
|
private final int subscriptionId;
|
||||||
private int errorCode;
|
private int errorCode;
|
||||||
private String errorMsg;
|
private String errorMsg;
|
||||||
@ -66,11 +73,27 @@ public class TelemetrySubscriptionUpdate {
|
|||||||
this.errorMsg = errorMsg != null ? errorMsg : errorCode.getDefaultMsg();
|
this.errorMsg = errorMsg != null ? errorMsg : errorCode.getDefaultMsg();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSubscriptionId() {
|
public Map<String, List<TsValue>> getValues() {
|
||||||
return subscriptionId;
|
if (data == null || data.isEmpty()) {
|
||||||
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, List<Object>> getData() {
|
Map<String, List<TsValue>> data = new HashMap<>();
|
||||||
|
this.data.forEach((key, entries) -> {
|
||||||
|
if (entries.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<TsValue> values = new ArrayList<>(entries.size());
|
||||||
|
entries.forEach(object -> {
|
||||||
|
if (!(object instanceof Object[] entry) || entry.length < 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
TsValue tsValue = new TsValue((Long) entry[0], (String) entry[1]);
|
||||||
|
values.add(tsValue);
|
||||||
|
});
|
||||||
|
data.put(key, values);
|
||||||
|
});
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,28 +109,17 @@ public class TelemetrySubscriptionUpdate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getErrorCode() {
|
|
||||||
return errorCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getErrorMsg() {
|
|
||||||
return errorMsg;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TelemetrySubscriptionUpdate copyWithNewSubscriptionId(int subscriptionId){
|
|
||||||
return new TelemetrySubscriptionUpdate(subscriptionId, errorCode, errorMsg, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder result = new StringBuilder("TelemetrySubscriptionUpdate [subscriptionId=" + subscriptionId + ", errorCode=" + errorCode + ", errorMsg=" + errorMsg + ", data=");
|
StringBuilder result = new StringBuilder("TelemetrySubscriptionUpdate [subscriptionId=" + subscriptionId + ", errorCode=" + errorCode + ", errorMsg=" + errorMsg + ", data=");
|
||||||
data.forEach((k, v) -> {
|
data.forEach((k, v) -> {
|
||||||
result.append(k).append("=[");
|
result.append(k).append("=[");
|
||||||
for(Object a : v){
|
for (Object a : v) {
|
||||||
result.append(Arrays.toString((Object[])a)).append("|");
|
result.append(Arrays.toString((Object[]) a)).append("|");
|
||||||
}
|
}
|
||||||
result.append("]");
|
result.append("]");
|
||||||
});
|
});
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,11 +32,16 @@ import org.thingsboard.server.cache.limits.RateLimitService;
|
|||||||
import org.thingsboard.server.common.data.id.DeviceId;
|
import org.thingsboard.server.common.data.id.DeviceId;
|
||||||
import org.thingsboard.server.common.data.id.EntityId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
|
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
|
||||||
|
import org.thingsboard.server.common.data.kv.StringDataEntry;
|
||||||
|
import org.thingsboard.server.common.data.kv.TsKvEntry;
|
||||||
import org.thingsboard.server.common.data.limit.LimitedApi;
|
import org.thingsboard.server.common.data.limit.LimitedApi;
|
||||||
|
import org.thingsboard.server.common.msg.queue.TbCallback;
|
||||||
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
|
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
|
||||||
import org.thingsboard.server.gen.transport.TransportProtos;
|
import org.thingsboard.server.gen.transport.TransportProtos;
|
||||||
import org.thingsboard.server.queue.discovery.PartitionService;
|
import org.thingsboard.server.queue.discovery.PartitionService;
|
||||||
import org.thingsboard.server.service.ws.WebSocketSessionRef;
|
import org.thingsboard.server.service.ws.WebSocketSessionRef;
|
||||||
|
import org.thingsboard.server.service.ws.telemetry.sub.TelemetrySubscriptionUpdate;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -44,8 +49,13 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
import static org.awaitility.Awaitility.await;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.ArgumentMatchers.nullable;
|
import static org.mockito.ArgumentMatchers.nullable;
|
||||||
@ -54,11 +64,17 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
public class DefaultTbLocalSubscriptionServiceTest {
|
public class DefaultTbLocalSubscriptionServiceTest {
|
||||||
|
|
||||||
ListAppender<ILoggingEvent> testLogAppender;
|
private final TenantId tenantId = TenantId.fromUUID(UUID.randomUUID());
|
||||||
TbLocalSubscriptionService subscriptionService;
|
private final DeviceId deviceId = new DeviceId(UUID.randomUUID());
|
||||||
|
|
||||||
|
private ListAppender<ILoggingEvent> testLogAppender;
|
||||||
|
private TbLocalSubscriptionService subscriptionService;
|
||||||
|
private ListeningExecutorService executorService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
|
this.executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
|
||||||
|
|
||||||
Logger logger = (Logger) LoggerFactory.getLogger(DefaultTbLocalSubscriptionService.class);
|
Logger logger = (Logger) LoggerFactory.getLogger(DefaultTbLocalSubscriptionService.class);
|
||||||
testLogAppender = new ListAppender<>();
|
testLogAppender = new ListAppender<>();
|
||||||
testLogAppender.start();
|
testLogAppender.start();
|
||||||
@ -69,7 +85,9 @@ public class DefaultTbLocalSubscriptionServiceTest {
|
|||||||
PartitionService partitionService = mock();
|
PartitionService partitionService = mock();
|
||||||
when(partitionService.resolve(any(), any(), any())).thenReturn(TopicPartitionInfo.builder().build());
|
when(partitionService.resolve(any(), any(), any())).thenReturn(TopicPartitionInfo.builder().build());
|
||||||
subscriptionService = new DefaultTbLocalSubscriptionService(mock(), mock(), mock(), partitionService, mock(), mock(), mock(), rateLimitService);
|
subscriptionService = new DefaultTbLocalSubscriptionService(mock(), mock(), mock(), partitionService, mock(), mock(), mock(), rateLimitService);
|
||||||
|
|
||||||
ReflectionTestUtils.setField(subscriptionService, "serviceId", "serviceId");
|
ReflectionTestUtils.setField(subscriptionService, "serviceId", "serviceId");
|
||||||
|
ReflectionTestUtils.setField(subscriptionService, "subscriptionUpdateExecutor", executorService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
@ -79,27 +97,22 @@ public class DefaultTbLocalSubscriptionServiceTest {
|
|||||||
Logger logger = (Logger) LoggerFactory.getLogger(DefaultTbLocalSubscriptionService.class);
|
Logger logger = (Logger) LoggerFactory.getLogger(DefaultTbLocalSubscriptionService.class);
|
||||||
logger.detachAppender(testLogAppender);
|
logger.detachAppender(testLogAppender);
|
||||||
}
|
}
|
||||||
|
if (executorService != null) {
|
||||||
|
executorService.shutdownNow();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void addSubscriptionConcurrentModificationTest() throws Exception {
|
public void addSubscriptionConcurrentModificationTest() throws Exception {
|
||||||
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
|
|
||||||
TenantId tenantId = new TenantId(UUID.randomUUID());
|
|
||||||
DeviceId deviceId = new DeviceId(UUID.randomUUID());
|
|
||||||
WebSocketSessionRef sessionRef = mock();
|
WebSocketSessionRef sessionRef = mock();
|
||||||
ReflectionTestUtils.setField(subscriptionService, "subscriptionUpdateExecutor", executorService);
|
|
||||||
|
|
||||||
List<ListenableFuture<?>> futures = new ArrayList<>();
|
List<ListenableFuture<?>> futures = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
|
||||||
subscriptionService.onCoreStartupMsg(TransportProtos.CoreStartupMsg.newBuilder().addAllPartitions(List.of(0)).getDefaultInstanceForType());
|
subscriptionService.onCoreStartupMsg(TransportProtos.CoreStartupMsg.newBuilder().addAllPartitions(List.of(0)).getDefaultInstanceForType());
|
||||||
for (int i = 0; i < 50; i++) {
|
for (int i = 0; i < 50; i++) {
|
||||||
futures.add(executorService.submit(() -> subscriptionService.addSubscription(createSubscription(tenantId, deviceId), sessionRef)));
|
futures.add(executorService.submit(() -> subscriptionService.addSubscription(createSubscription(tenantId, deviceId), sessionRef)));
|
||||||
}
|
}
|
||||||
Futures.allAsList(futures).get();
|
Futures.allAsList(futures).get();
|
||||||
} finally {
|
|
||||||
executorService.shutdownNow();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<ILoggingEvent> logs = testLogAppender.list;
|
List<ILoggingEvent> logs = testLogAppender.list;
|
||||||
boolean exceptionLogged = logs.stream()
|
boolean exceptionLogged = logs.stream()
|
||||||
@ -123,4 +136,42 @@ public class DefaultTbLocalSubscriptionServiceTest {
|
|||||||
.keyStates(keys)
|
.keyStates(keys)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateOldTelemetryTest() {
|
||||||
|
WebSocketSessionRef sessionRef = mock();
|
||||||
|
|
||||||
|
String key = "temperature";
|
||||||
|
long ts = 1237465456L;
|
||||||
|
|
||||||
|
Map<String, Long> keyStates = new HashMap<>();
|
||||||
|
keyStates.put(key, ts);
|
||||||
|
|
||||||
|
AtomicReference<TelemetrySubscriptionUpdate> capturedUpdate = new AtomicReference<>();
|
||||||
|
TbTimeSeriesSubscription tsSubscription = TbTimeSeriesSubscription.builder()
|
||||||
|
.tenantId(tenantId)
|
||||||
|
.entityId(deviceId)
|
||||||
|
.subscriptionId(2)
|
||||||
|
.sessionId(RandomStringUtils.randomAlphanumeric(5))
|
||||||
|
.keyStates(keyStates)
|
||||||
|
.allKeys(true)
|
||||||
|
.latestValues(true)
|
||||||
|
.updateProcessor((subscription, update) -> capturedUpdate.set(update))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
subscriptionService.addSubscription(tsSubscription, sessionRef);
|
||||||
|
|
||||||
|
// Send telemetry with ts == stateTs
|
||||||
|
TsKvEntry kv = new BasicTsKvEntry(ts, new StringDataEntry(key, "42"));
|
||||||
|
subscriptionService.onTimeSeriesUpdate(deviceId, List.of(kv), mock(TbCallback.class));
|
||||||
|
|
||||||
|
await().atMost(5, TimeUnit.SECONDS)
|
||||||
|
.pollInterval(5, TimeUnit.MILLISECONDS)
|
||||||
|
.untilAsserted(() -> {
|
||||||
|
TelemetrySubscriptionUpdate update = capturedUpdate.get();
|
||||||
|
assertNotNull(update);
|
||||||
|
assertTrue(update.getLatestValues().containsKey(key));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,130 @@
|
|||||||
|
/**
|
||||||
|
* Copyright © 2016-2025 The Thingsboard Authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.thingsboard.server.service.subscription;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
import org.testcontainers.shaded.org.apache.commons.lang3.RandomUtils;
|
||||||
|
import org.thingsboard.server.common.data.id.DeviceId;
|
||||||
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
|
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
|
||||||
|
import org.thingsboard.server.common.data.kv.LongDataEntry;
|
||||||
|
import org.thingsboard.server.common.data.kv.TsKvEntry;
|
||||||
|
import org.thingsboard.server.common.data.page.PageData;
|
||||||
|
import org.thingsboard.server.common.data.query.EntityData;
|
||||||
|
import org.thingsboard.server.common.data.query.EntityKeyType;
|
||||||
|
import org.thingsboard.server.common.data.query.TsValue;
|
||||||
|
import org.thingsboard.server.service.ws.WebSocketService;
|
||||||
|
import org.thingsboard.server.service.ws.WebSocketSessionRef;
|
||||||
|
import org.thingsboard.server.service.ws.telemetry.cmd.v2.CmdUpdate;
|
||||||
|
import org.thingsboard.server.service.ws.telemetry.cmd.v2.EntityDataUpdate;
|
||||||
|
import org.thingsboard.server.service.ws.telemetry.sub.TelemetrySubscriptionUpdate;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.BDDMockito.then;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
public class TbEntityDataSubCtxTest {
|
||||||
|
|
||||||
|
private final DeviceId deviceId = new DeviceId(UUID.randomUUID());
|
||||||
|
|
||||||
|
private final Integer cmdId = RandomUtils.nextInt();
|
||||||
|
private final Integer subscriptionId = RandomUtils.nextInt();
|
||||||
|
private final String serviceId = RandomStringUtils.randomAlphanumeric(10);
|
||||||
|
private final String sessionId = RandomStringUtils.randomAlphanumeric(10);
|
||||||
|
|
||||||
|
private final int maxEntitiesPerDataSubscription = 100;
|
||||||
|
|
||||||
|
private TbEntityDataSubCtx subCtx;
|
||||||
|
@Mock
|
||||||
|
private WebSocketService webSocketService;
|
||||||
|
@Mock
|
||||||
|
private WebSocketSessionRef webSocketSessionRef;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
when(webSocketSessionRef.getSessionId()).thenReturn(sessionId);
|
||||||
|
subCtx = new TbEntityDataSubCtx(serviceId, webSocketService, mock(), mock(), mock(), mock(), webSocketSessionRef, cmdId, maxEntitiesPerDataSubscription);
|
||||||
|
|
||||||
|
Map<Integer, EntityId> subToEntityIdMap = new HashMap<>();
|
||||||
|
subToEntityIdMap.put(subscriptionId, deviceId);
|
||||||
|
ReflectionTestUtils.setField(subCtx, "subToEntityIdMap", subToEntityIdMap);
|
||||||
|
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
long oldTs = now - 1_000_000;
|
||||||
|
|
||||||
|
Map<String, TsValue> latestCtxValues = new HashMap<>();
|
||||||
|
latestCtxValues.put("key", new TsValue(oldTs, "15"));
|
||||||
|
Map<EntityKeyType, Map<String, TsValue>> latest = new HashMap<>();
|
||||||
|
latest.put(EntityKeyType.TIME_SERIES, latestCtxValues);
|
||||||
|
|
||||||
|
EntityData entityData = new EntityData();
|
||||||
|
entityData.setEntityId(deviceId);
|
||||||
|
entityData.setLatest(latest);
|
||||||
|
|
||||||
|
PageData<EntityData> data = new PageData<>(List.of(entityData), 1, 1, true);
|
||||||
|
ReflectionTestUtils.setField(subCtx, "data", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendLatestWsMsg() {
|
||||||
|
long ts = System.currentTimeMillis();
|
||||||
|
List<TsKvEntry> telemetry = List.of(
|
||||||
|
new BasicTsKvEntry(ts - 50000, new LongDataEntry("key", 42L), 34L),
|
||||||
|
new BasicTsKvEntry(ts - 20000, new LongDataEntry("key", 17L), 78L)
|
||||||
|
);
|
||||||
|
|
||||||
|
TelemetrySubscriptionUpdate subUpdate = new TelemetrySubscriptionUpdate(subscriptionId, telemetry);
|
||||||
|
|
||||||
|
subCtx.sendWsMsg(sessionId, subUpdate, EntityKeyType.TIME_SERIES, true);
|
||||||
|
|
||||||
|
Map<EntityKeyType, Map<String, TsValue>> expectedLatest = new HashMap<>();
|
||||||
|
Map<String, TsValue> expectedLatestCtxValues = new HashMap<>();
|
||||||
|
expectedLatestCtxValues.put("key", new TsValue(ts - 20000, "17")); // use latest telemetry
|
||||||
|
expectedLatest.put(EntityKeyType.TIME_SERIES, expectedLatestCtxValues);
|
||||||
|
|
||||||
|
EntityData expectedEntityData = new EntityData();
|
||||||
|
expectedEntityData.setEntityId(deviceId);
|
||||||
|
expectedEntityData.setLatest(expectedLatest);
|
||||||
|
|
||||||
|
List<EntityData> expected = List.of(expectedEntityData);
|
||||||
|
|
||||||
|
ArgumentCaptor<CmdUpdate> cmdUpdateCaptor = ArgumentCaptor.forClass(CmdUpdate.class);
|
||||||
|
then(webSocketService).should().sendUpdate(eq(sessionId), cmdUpdateCaptor.capture());
|
||||||
|
CmdUpdate cmdUpdate = cmdUpdateCaptor.getValue();
|
||||||
|
assertThat(cmdUpdate).isInstanceOf(EntityDataUpdate.class);
|
||||||
|
EntityDataUpdate entityDataUpdate = (EntityDataUpdate) cmdUpdate;
|
||||||
|
assertThat(entityDataUpdate.getCmdId()).isEqualTo(cmdId);
|
||||||
|
assertThat(entityDataUpdate.getData()).isNull();
|
||||||
|
assertThat(entityDataUpdate.getUpdate()).isEqualTo(expected);
|
||||||
|
assertThat(entityDataUpdate.getAllowedEntities()).isEqualTo(maxEntitiesPerDataSubscription);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user