onEdgeDisconnect persisted active=false based only on this node's in-memory
session map. When an edge drops and reconnects to a different TB-Core node, the
new node sets active=true while the old node writes active=false whenever it
finally notices its dead stream. If the stale write lands last, the edge stays
marked inactive while its gRPC session is alive.
That flag is not only a status indicator: saveEdgeEvent() consults it before
queueing any server-to-edge event, and actions that are not on the offline
allowlist - device and entity-group assignments among them - are dropped
outright rather than queued. A stale active=false therefore makes assignments
silently never reach the edge, with a full "Sync Edge" as the only recovery.
- Guard the write with the cluster-wide service-id cache, skipping it only when
another node is the recorded owner and that node is still a live TB_CORE
service. An absent entry means ownership is unknown, not claimed elsewhere, so
that case still persists the inactive state; and a node that died without
evicting its claim cannot leave the edge marked active forever.
- Publish the ownership claim before writing active=true in onEdgeConnect, so a
disconnect callback on the node the edge just left cannot read a stale owner.
- Only evict the service-id cache entry when it still points at this node.
A stale callback would otherwise wipe the live owner's claim and defeat the
guard above for any later disconnect.
- Remove the session atomically in onEdgeDisconnect. get() and remove() were not
atomic, so a session replaced between the two was dropped from the map, torn
down and marked inactive by the stale callback even though it was the live one.
LAST_DISCONNECT_TIME and the rule-engine DISCONNECT_EVENT are intentionally left
unguarded - they are accurate statements about this session, and the timestamps
are the main diagnostic signal for flapping edges.
Backport of thingsboard/thingsboard#16155 to lts-4.2, which has no disconnect
notification debounce and therefore none of the ownership helpers the 4.3 change
builds on; they are introduced here.