feat(iot): search fleet things by connectivity status (#6025)

* feat(iot): search fleet things by connectivity status

SearchIndex refused every connectivity field and its documents carried
no connectivity, so an application that finds its things by connection
state got InvalidQueryException.

With connectivity indexing on, a thing now reports the MQTT broker
session whose client id is its name, and connectivity.connected,
connectivity.clientId and connectivity.disconnectReason can be
searched. The other connectivity fields are still refused with
InvalidQueryException. A thing whose client id never connected reports
connected false and timestamp 0, as on AWS.

The broker now records the account, region, connect time, keep alive
and clean session of each session, and keeps in memory how the last
one of each client id ended in each account and region, until a state
reset. The reason is the one the session's disconnected lifecycle
event carries, recorded by the same single endSession under the
session lock, so the late end of a displaced session cannot overwrite
its successor's. A plaintext session counts in the default account and
region, a TLS session in its certificate's.

Closes #4912

* fix(iot): serialize connectivity reads and resets with session ends

A connectivity lookup read the live session without the session lock,
so a search racing DeleteConnection could see the session connected
after its disconnect was recorded. A state reset cleared the history
without the lock too. Both now run under the session lock; the lookup
tests the session's ended flag instead of the endpoint, as the lock is
never held across endpoint calls.

Tests cover the reset contract, a lookup and a reset racing a held
DeleteConnection, and a TLS session counting in its certificate's
account and region.
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-19 19:45:53 -04:00
1 parent 1eb402d623
commit 34d904fde5
11 files changed
+536 -67

No files matched your search

@@ -425,6 +425,13 @@ class IotTest {
assertThat(plain.attributes()).containsOnly(Map.entry("provider", provider + "1"));
for (ThingDocument thing : all) {
assertThat(thing.thingId()).isNotBlank();
// A thing whose client id never connected, in the shape AWS reports it.
assertThat(thing.connectivity().clientId()).isEqualTo(thing.thingName());
assertThat(thing.connectivity().connected()).isFalse();
assertThat(thing.connectivity().timestamp()).isZero();
assertThat(thing.connectivity().disconnectReason()).isNull();
assertThat(thing.connectivity().keepAliveDuration()).isNull();
assertThat(thing.connectivity().cleanSession()).isNull();
}
awaitSearch("attributes.provider:" + provider + "1", prefix, "a", "b");
@@ -437,6 +444,7 @@ class IotTest {
awaitSearch("thingName:" + prefix + "* AND NOT attributes.provider:" + provider + "1", prefix, "c");
awaitSearch("attributes.provider:" + provider + "?", prefix, "a", "b", "c");
awaitSearch("thingGroupNames:" + group, prefix, "a");
awaitSearch("connectivity.connected:false AND thingName:" + prefix + "*", prefix, "a", "b", "c");
Set<String> paged = new TreeSet<>();
String nextToken = null;