|
|
|
@ -171,7 +171,7 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM |
|
|
|
if (state.isSizeOk()) { |
|
|
|
processStateIfReady(state, Collections.emptyMap(), ctx, Collections.singletonList(ctx.getCfId()), null, msg.getEventType().name(), msg.getCallback()); |
|
|
|
} else { |
|
|
|
throw new RuntimeException(ctx.getSizeExceedsLimitMessage()); |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).errorMessage(ctx.getSizeExceedsLimitMessage()).build(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
msg.getCallback().onSuccess(); |
|
|
|
@ -179,7 +179,8 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM |
|
|
|
} catch (Exception e) { |
|
|
|
log.debug("[{}][{}] Failed to initialize CF state", entityId, ctx.getCfId(), e); |
|
|
|
if (e instanceof CalculatedFieldException cfe) { |
|
|
|
throw cfe; |
|
|
|
persistDebugErrorIfEnabled(cfe, msg.getCallback()); |
|
|
|
return; |
|
|
|
} |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).cause(e).build(); |
|
|
|
} |
|
|
|
@ -261,9 +262,10 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).errorMessage(ctx.getSizeExceedsLimitMessage()).build(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.debug("[{}][{}] Failed to initialize CF state", entityId, ctx.getCfId(), e); |
|
|
|
log.debug("[{}][{}] Failed to handle relation update", entityId, ctx.getCfId(), e); |
|
|
|
if (e instanceof CalculatedFieldException cfe) { |
|
|
|
throw cfe; |
|
|
|
persistDebugErrorIfEnabled(cfe, msg.getCallback()); |
|
|
|
return; |
|
|
|
} |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).cause(e).build(); |
|
|
|
} |
|
|
|
@ -273,31 +275,40 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM |
|
|
|
CalculatedFieldCtx ctx = msg.getCalculatedField(); |
|
|
|
CalculatedFieldId cfId = ctx.getCfId(); |
|
|
|
CalculatedFieldState state = states.get(cfId); |
|
|
|
if (state == null) { |
|
|
|
msg.getCallback().onSuccess(); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (state instanceof RelatedEntitiesAggregationCalculatedFieldState aggState) { |
|
|
|
aggState.cleanupEntityData(msg.getRelatedEntityId()); |
|
|
|
try { |
|
|
|
if (state == null) { |
|
|
|
msg.getCallback().onSuccess(); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (state instanceof RelatedEntitiesAggregationCalculatedFieldState aggState) { |
|
|
|
aggState.cleanupEntityData(msg.getRelatedEntityId()); |
|
|
|
|
|
|
|
state.checkStateSize(new CalculatedFieldEntityCtxId(tenantId, ctx.getCfId(), entityId), ctx.getMaxStateSize()); |
|
|
|
state.checkStateSize(new CalculatedFieldEntityCtxId(tenantId, ctx.getCfId(), entityId), ctx.getMaxStateSize()); |
|
|
|
|
|
|
|
if (state.isSizeOk()) { |
|
|
|
processStateIfReady(state, Collections.emptyMap(), ctx, Collections.singletonList(ctx.getCfId()), null, TbMsgType.RELATION_DELETED.name(), msg.getCallback()); |
|
|
|
} else { |
|
|
|
throw new RuntimeException(ctx.getSizeExceedsLimitMessage()); |
|
|
|
if (state.isSizeOk()) { |
|
|
|
processStateIfReady(state, Collections.emptyMap(), ctx, Collections.singletonList(ctx.getCfId()), null, TbMsgType.RELATION_DELETED.name(), msg.getCallback()); |
|
|
|
} else { |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).errorMessage(ctx.getSizeExceedsLimitMessage()).build(); |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
if (state instanceof PropagationCalculatedFieldState propagationState) { |
|
|
|
PropagationArgumentEntry entry = new PropagationArgumentEntry(); |
|
|
|
entry.setRemoved(msg.getRelatedEntityId()); |
|
|
|
propagationState.update(Map.of(PROPAGATION_CONFIG_ARGUMENT, entry), ctx); |
|
|
|
if (DebugModeUtil.isDebugAllAvailable(ctx.getCalculatedField())) { |
|
|
|
systemContext.persistCalculatedFieldDebugEvent(tenantId, ctx.getCfId(), entityId, state.getArgumentsJson(), null, TbMsgType.RELATION_DELETED.name(), null, null); |
|
|
|
if (state instanceof PropagationCalculatedFieldState propagationState) { |
|
|
|
PropagationArgumentEntry entry = new PropagationArgumentEntry(); |
|
|
|
entry.setRemoved(msg.getRelatedEntityId()); |
|
|
|
propagationState.update(Map.of(PROPAGATION_CONFIG_ARGUMENT, entry), ctx); |
|
|
|
if (DebugModeUtil.isDebugAllAvailable(ctx.getCalculatedField())) { |
|
|
|
systemContext.persistCalculatedFieldDebugEvent(tenantId, ctx.getCfId(), entityId, state.getArgumentsJson(), null, TbMsgType.RELATION_DELETED.name(), null, null); |
|
|
|
} |
|
|
|
} |
|
|
|
msg.getCallback().onSuccess(); |
|
|
|
} catch (Exception e) { |
|
|
|
log.debug("[{}][{}] Failed to handle relation delete", entityId, ctx.getCfId(), e); |
|
|
|
if (e instanceof CalculatedFieldException cfe) { |
|
|
|
persistDebugErrorIfEnabled(cfe, msg.getCallback()); |
|
|
|
return; |
|
|
|
} |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).cause(e).build(); |
|
|
|
} |
|
|
|
msg.getCallback().onSuccess(); |
|
|
|
} |
|
|
|
|
|
|
|
public void process(EntityCalculatedFieldTelemetryMsg msg) throws CalculatedFieldException { |
|
|
|
@ -340,7 +351,8 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM |
|
|
|
} catch (Exception e) { |
|
|
|
log.debug("[{}][{}] Failed to process linked CF telemetry msg: {}", entityId, ctx.getCfId(), msg, e); |
|
|
|
if (e instanceof CalculatedFieldException cfe) { |
|
|
|
throw cfe; |
|
|
|
persistDebugErrorIfEnabled(cfe, callback); |
|
|
|
return; |
|
|
|
} |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).cause(e).build(); |
|
|
|
} |
|
|
|
@ -366,7 +378,8 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM |
|
|
|
} catch (Exception e) { |
|
|
|
log.debug("[{}][{}] Failed to process CF telemetry msg: {}", entityId, ctx.getCfId(), proto, e); |
|
|
|
if (e instanceof CalculatedFieldException cfe) { |
|
|
|
throw cfe; |
|
|
|
persistDebugErrorIfEnabled(cfe, callback); |
|
|
|
return; |
|
|
|
} |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).cause(e).build(); |
|
|
|
} |
|
|
|
@ -384,7 +397,7 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM |
|
|
|
log.debug("[{}][{}] Reevaluating CF state", entityId, cfId); |
|
|
|
processStateIfReady(state, null, ctx, Collections.singletonList(cfId), null, CalculatedFieldEventType.REEVALUATION_MSG.name(), msg.getCallback()); |
|
|
|
} else { |
|
|
|
throw new RuntimeException(ctx.getSizeExceedsLimitMessage()); |
|
|
|
throw CalculatedFieldException.builder().ctx(ctx).eventEntity(entityId).errorMessage(ctx.getSizeExceedsLimitMessage()).build(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -786,4 +799,19 @@ public class CalculatedFieldEntityMessageProcessor extends AbstractContextAwareM |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
private void persistDebugErrorIfEnabled(CalculatedFieldException cfe, TbCallback callback) { |
|
|
|
if (DebugModeUtil.isDebugFailuresAvailable(cfe.getCtx().getCalculatedField())) { |
|
|
|
String message; |
|
|
|
if (cfe.getErrorMessage() != null) { |
|
|
|
message = cfe.getErrorMessage(); |
|
|
|
} else if (cfe.getCause() != null) { |
|
|
|
message = cfe.getCause().getMessage(); |
|
|
|
} else { |
|
|
|
message = "N/A"; |
|
|
|
} |
|
|
|
systemContext.persistCalculatedFieldDebugEvent(tenantId, cfe.getCtx().getCfId(), cfe.getEventEntity(), cfe.getArguments(), cfe.getMsgId(), cfe.getMsgType(), null, message); |
|
|
|
} |
|
|
|
callback.onSuccess(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|