Browse Source

relation changed processing

pull/14141/head
IrynaMatveieva 11 months ago
parent
commit
cd15206061
  1. 57
      application/src/main/java/org/thingsboard/server/actors/calculatedField/CalculatedFieldManagerMessageProcessor.java
  2. 2
      application/src/main/java/org/thingsboard/server/service/queue/DefaultTbClusterService.java
  3. 6
      common/message/src/main/java/org/thingsboard/server/common/msg/plugin/ComponentLifecycleMsg.java
  4. 2
      common/proto/src/main/java/org/thingsboard/server/common/util/ProtoUtils.java
  5. 1
      common/proto/src/main/proto/queue.proto

57
application/src/main/java/org/thingsboard/server/actors/calculatedField/CalculatedFieldManagerMessageProcessor.java

@ -40,6 +40,7 @@ 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.page.PageDataIterable; import org.thingsboard.server.common.data.page.PageDataIterable;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent;
import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.EntityRelationPathQuery; import org.thingsboard.server.common.data.relation.EntityRelationPathQuery;
import org.thingsboard.server.common.data.relation.EntitySearchDirection; import org.thingsboard.server.common.data.relation.EntitySearchDirection;
@ -77,6 +78,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.Function;
import static org.thingsboard.server.utils.CalculatedFieldUtils.fromProto; import static org.thingsboard.server.utils.CalculatedFieldUtils.fromProto;
@ -188,16 +190,12 @@ public class CalculatedFieldManagerMessageProcessor extends AbstractContextAware
public void onEntityLifecycleMsg(CalculatedFieldEntityLifecycleMsg msg) throws CalculatedFieldException { public void onEntityLifecycleMsg(CalculatedFieldEntityLifecycleMsg msg) throws CalculatedFieldException {
var event = msg.getData().getEvent(); var event = msg.getData().getEvent();
if (msg.getData().isRelationChanged()) { if (ComponentLifecycleEvent.RELATION_UPDATED.equals(event) || ComponentLifecycleEvent.RELATION_DELETED.equals(event)) {
log.debug("Processing relation [{}] event: ", msg.getData().getEvent()); log.debug("Processing relation [{}] event from entity: [{}]", event, msg.getData().getEntityId());
switch (event) { onRelationChangedEvent(msg.getData(), msg.getCallback());
case RELATION_UPDATED -> onRelationUpdated(msg.getData(), msg.getCallback());
case RELATION_DELETED -> onRelationDeleted(msg.getData(), msg.getCallback());
default -> msg.getCallback().onSuccess();
}
return; return;
} }
log.debug("Processing entity lifecycle event: [{}] for entity: [{}]", msg.getData().getEvent(), msg.getData().getEntityId()); log.debug("Processing entity lifecycle event: [{}] for entity: [{}]", event, msg.getData().getEntityId());
var entityType = msg.getData().getEntityId().getEntityType(); var entityType = msg.getData().getEntityId().getEntityType();
switch (entityType) { switch (entityType) {
case CALCULATED_FIELD -> { case CALCULATED_FIELD -> {
@ -306,36 +304,29 @@ public class CalculatedFieldManagerMessageProcessor extends AbstractContextAware
} }
} }
private void onRelationUpdated(ComponentLifecycleMsg msg, TbCallback callback) { private void onRelationChangedEvent(ComponentLifecycleMsg msg, TbCallback callback) {
try { Function<EntityId, TriConsumer<EntityId, CalculatedFieldCtx, TbCallback>> relationAction = switch (msg.getEvent()) {
EntityRelation entityRelation = JacksonUtil.treeToValue(msg.getInfo(), EntityRelation.class); case RELATION_UPDATED -> relatedId -> (entityId, ctx, cb) -> initRelatedEntity(entityId, relatedId, ctx, cb);
EntityId toId = entityRelation.getTo(); case RELATION_DELETED -> relatedId -> (entityId, ctx, cb) -> deleteRelatedEntity(entityId, relatedId, ctx, cb);
EntityId fromId = entityRelation.getFrom(); default -> null;
String relationType = entityRelation.getType(); };
MultipleTbCallback callbackForToAndFrom = new MultipleTbCallback(2, callback);
processRelationByDirection(EntitySearchDirection.TO, relationType, toId, callbackForToAndFrom, (entityId, ctx, cb) -> initRelatedEntity(entityId, fromId, ctx, cb));
processRelationByDirection(EntitySearchDirection.FROM, relationType, fromId, callbackForToAndFrom, (entityId, ctx, cb) -> initRelatedEntity(entityId, toId, ctx, cb));
} catch (Exception e) {
callback.onSuccess();
}
}
private void onRelationDeleted(ComponentLifecycleMsg msg, TbCallback callback) { if (relationAction == null) {
try {
EntityRelation entityRelation = JacksonUtil.treeToValue(msg.getInfo(), EntityRelation.class);
EntityId toId = entityRelation.getTo();
EntityId fromId = entityRelation.getFrom();
String relationType = entityRelation.getType();
MultipleTbCallback callbackForToAndFrom = new MultipleTbCallback(2, callback);
processRelationByDirection(EntitySearchDirection.TO, relationType, toId, callbackForToAndFrom, (entityId, ctx, cb) -> deleteRelatedEntity(entityId, fromId, ctx, cb));
processRelationByDirection(EntitySearchDirection.FROM, relationType, fromId, callbackForToAndFrom, (entityId, ctx, cb) -> deleteRelatedEntity(entityId, toId, ctx, cb));
} catch (Exception e) {
callback.onSuccess(); callback.onSuccess();
return;
} }
EntityRelation entityRelation = JacksonUtil.treeToValue(msg.getInfo(), EntityRelation.class);
EntityId toId = entityRelation.getTo();
EntityId fromId = entityRelation.getFrom();
String relationType = entityRelation.getType();
MultipleTbCallback callbackForToAndFrom = new MultipleTbCallback(2, callback);
processRelationByDirection(EntitySearchDirection.TO, relationType, toId, callbackForToAndFrom, relationAction.apply(fromId));
processRelationByDirection(EntitySearchDirection.FROM, relationType, fromId, callbackForToAndFrom, relationAction.apply(toId));
} }
private void processRelationByDirection(EntitySearchDirection direction, private void processRelationByDirection(EntitySearchDirection direction,
String relationType, String relationType,
EntityId mainId, EntityId mainId,

2
application/src/main/java/org/thingsboard/server/service/queue/DefaultTbClusterService.java

@ -740,7 +740,6 @@ public class DefaultTbClusterService implements TbClusterService {
ComponentLifecycleMsg msg = ComponentLifecycleMsg.builder() ComponentLifecycleMsg msg = ComponentLifecycleMsg.builder()
.tenantId(tenantId) .tenantId(tenantId)
.entityId(entityRelation.getFrom()) .entityId(entityRelation.getFrom())
.relationChanged(true)
.event(ComponentLifecycleEvent.RELATION_UPDATED) .event(ComponentLifecycleEvent.RELATION_UPDATED)
.info(JacksonUtil.valueToTree(entityRelation)) .info(JacksonUtil.valueToTree(entityRelation))
.build(); .build();
@ -752,7 +751,6 @@ public class DefaultTbClusterService implements TbClusterService {
ComponentLifecycleMsg msg = ComponentLifecycleMsg.builder() ComponentLifecycleMsg msg = ComponentLifecycleMsg.builder()
.tenantId(tenantId) .tenantId(tenantId)
.entityId(entityRelation.getFrom()) .entityId(entityRelation.getFrom())
.relationChanged(true)
.event(ComponentLifecycleEvent.RELATION_DELETED) .event(ComponentLifecycleEvent.RELATION_DELETED)
.info(JacksonUtil.valueToTree(entityRelation)) .info(JacksonUtil.valueToTree(entityRelation))
.build(); .build();

6
common/message/src/main/java/org/thingsboard/server/common/msg/plugin/ComponentLifecycleMsg.java

@ -47,15 +47,14 @@ public class ComponentLifecycleMsg implements TenantAwareMsg, ToAllNodesMsg {
private final EntityId oldProfileId; private final EntityId oldProfileId;
private final EntityId profileId; private final EntityId profileId;
private final boolean ownerChanged; private final boolean ownerChanged;
private final boolean relationChanged;
private final JsonNode info; private final JsonNode info;
public ComponentLifecycleMsg(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent event) { public ComponentLifecycleMsg(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent event) {
this(tenantId, entityId, event, null, null, null, null, false, false, null); this(tenantId, entityId, event, null, null, null, null, false, null);
} }
@Builder @Builder
private ComponentLifecycleMsg(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent event, String oldName, String name, EntityId oldProfileId, EntityId profileId, boolean ownerChanged, boolean relationChanged, JsonNode info) { private ComponentLifecycleMsg(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent event, String oldName, String name, EntityId oldProfileId, EntityId profileId, boolean ownerChanged, JsonNode info) {
this.tenantId = tenantId; this.tenantId = tenantId;
this.entityId = entityId; this.entityId = entityId;
this.event = event; this.event = event;
@ -64,7 +63,6 @@ public class ComponentLifecycleMsg implements TenantAwareMsg, ToAllNodesMsg {
this.oldProfileId = oldProfileId; this.oldProfileId = oldProfileId;
this.profileId = profileId; this.profileId = profileId;
this.ownerChanged = ownerChanged; this.ownerChanged = ownerChanged;
this.relationChanged = relationChanged;
this.info = info; this.info = info;
} }

2
common/proto/src/main/java/org/thingsboard/server/common/util/ProtoUtils.java

@ -130,7 +130,6 @@ public class ProtoUtils {
builder.setOldProfileIdLSB(msg.getOldProfileId().getId().getLeastSignificantBits()); builder.setOldProfileIdLSB(msg.getOldProfileId().getId().getLeastSignificantBits());
} }
builder.setOwnerChanged(msg.isOwnerChanged()); builder.setOwnerChanged(msg.isOwnerChanged());
builder.setRelationChanged(msg.isRelationChanged());
if (msg.getName() != null) { if (msg.getName() != null) {
builder.setName(msg.getName()); builder.setName(msg.getName());
} }
@ -168,7 +167,6 @@ public class ProtoUtils {
builder.oldProfileId(EntityIdFactory.getByTypeAndUuid(profileType, new UUID(proto.getOldProfileIdMSB(), proto.getOldProfileIdLSB()))); builder.oldProfileId(EntityIdFactory.getByTypeAndUuid(profileType, new UUID(proto.getOldProfileIdMSB(), proto.getOldProfileIdLSB())));
} }
builder.ownerChanged(proto.getOwnerChanged()); builder.ownerChanged(proto.getOwnerChanged());
builder.relationChanged(proto.getRelationChanged());
if (proto.hasInfo()) { if (proto.hasInfo()) {
builder.info(JacksonUtil.toJsonNode(proto.getInfo())); builder.info(JacksonUtil.toJsonNode(proto.getInfo()));
} }

1
common/proto/src/main/proto/queue.proto

@ -1296,7 +1296,6 @@ message ComponentLifecycleMsgProto {
int64 profileIdLSB = 12; int64 profileIdLSB = 12;
optional string info = 13; optional string info = 13;
bool ownerChanged = 100; bool ownerChanged = 100;
bool relationChanged = 14;
} }
message EdgeEventMsgProto { message EdgeEventMsgProto {

Loading…
Cancel
Save