Skip to content

Add ThingsBoard latest-telemetry and attributes DAOs on IoTDB Table Mode#112

Closed
PDGGK wants to merge 4 commits into
apache:masterfrom
PDGGK:pr2-latest-attributes
Closed

Add ThingsBoard latest-telemetry and attributes DAOs on IoTDB Table Mode#112
PDGGK wants to merge 4 commits into
apache:masterfrom
PDGGK:pr2-latest-attributes

Conversation

@PDGGK

@PDGGK PDGGK commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

This is the second PR of the GSoC series (follows #110, the timeseries TimeseriesDao, now merged). It adds the latest-telemetry and attributes storage paths for running ThingsBoard on Apache IoTDB 2.x Table Mode.

What this adds

1. IoTDBTableLatestDao — the ThingsBoard TimeseriesLatestDao SPI on Table Mode.

  • Reads are derived-latest over the existing telemetry table (ORDER BY time DESC LIMIT 1 for a single key; LAST_BY(col, time) + MAX(time) GROUP BY key for all keys), so the normal full-save path needs no separate latest table.
  • Plus a minimal latest overlay (telemetry_latest) that captures the latest-only write/delete cases the single-table derived read cannot satisfy (e.g. the EntityView LATEST_AND_WS copy path calls saveLatest with no paired save()). findLatest/findAllLatest merge the derived latest with the overlay by max timestamp per key. The overlay table is created only under the latest selector.

2. IoTDBTableAttributesDao — the ThingsBoard AttributesDao SPI on Table Mode, against a dedicated entity_attributes table.

  • This is a stretch / opt-in path: it is inert by default. It activates only when database.attributes.type=iotdb-table is set — a selector that no current ThingsBoard release exposes — so a normal deployment never instantiates it and attributes keep flowing to the host entity DB. No @Repository; an AttributesDaoConflictGuard fails startup if a conflicting host AttributesDao is present; a constructor gate requires an explicit iotdb.attributes.cluster_mode acknowledgement.

Both follow the same activation isolation + compile-only ThingsBoard stub surface (excluded from the built jar) as #110, and reuse the module's single named session pool (deduped via @ConditionalOnMissingBean(name=...)); each DAO can be activated independently.

Testing

Verified with the build cache off: 141 unit tests + 31 Testcontainers integration tests (Latest 11 / Timeseries 9 / Attributes 11) against apache/iotdb:2.0.8-standalone; apache-rat, checkstyle, spotless, dependency-analyze, and enforcer all clean. The compile-only SPI stubs are pinned to the real ThingsBoard v4.3.1.2 signatures by StrategyFContractTest.

Points I'd appreciate review on

  • Latest overlay shape. To close the latest-only data-loss gap, saveLatest writes the overlay on every call: the async batch writer means the DAO cannot tell a latest-only saveLatest from a full-save's paired saveLatest, so telemetry_latest ends up being a per-key latest store (one row per key, delete-then-insert), merged with the derived read. It is therefore effectively a latest table (one extra write per latest update). It closes the gaps; I'd welcome confirmation the shape is acceptable, or a preference for a narrower variant.
  • Documented Phase-1 residuals (in the IoTDBTableLatestDao javadoc): version is always null (IoTDB has no per-series sequence — same as the Cassandra backend; the EDQS latest-index onUpdate is suppressed for latest-only writes); for a purely telemetry-derived (full-save) latest, removeLatest and a concurrent historical delete are separate futures, so a re-read can transiently disagree until the historical delete lands; telemetry_latest is TTL='INF' with no entity-level cleanup; a same-timestamp cross-store type change resolves to the overlay value.
  • AttributesDao.findAllKeysByDeviceProfileId with a non-null DeviceProfileId returns an empty list — matching the official non-relational backend (CassandraBaseTimeseriesLatestDao.findAllKeysByDeviceProfileId also returns Collections.emptyList()); the sole caller is a TENANT_ADMIN config-time UI key-enumeration endpoint (GET /api/deviceProfile/devices/keys/attributes) that tolerates empty. The rationale is documented in the DAO javadoc + README; a real device→profile lookup is a future optional enhancement.

Happy to iterate.

PDGGK added 4 commits June 26, 2026 23:31
… Mode (GSOC-304 Wk 5)

Implements the ThingsBoard AttributesDao SPI (v4.3.1.2, 14 methods) on IoTDB
Table Mode against the entity_attributes table, as a Path-3 stretch artifact
that stays inert by default.

- 13/14 methods implemented; findNextBatch is a relational keyset-pagination
  migration helper with no IoTDB equivalent, so it throws
  UnsupportedOperationException. The three v4.3.1.2 additions
  (findAllKeysByEntityIdsAndScopeAsync, findLatestByEntityIdsAndScope and its
  async form) are implemented.
- save is a tag-only delete-then-insert under an entity read lock plus a
  per-identity lock; find takes the same identity lock so the gap is invisible.
- Inert by default: no @repository on the DAO; an independent
  database.attributes.type selector decoupled from the timeseries selectors; an
  AttributesDaoConflictGuard fail-fast; and a constructor cluster-mode
  acknowledgement gate (iotdb.attributes.cluster_mode).
- Strategy F: eight compile-only ThingsBoard/commons stubs, excluded from the
  built jar.
- Documented Phase-1 limitations: null version (IoTDB has no sequence), empty
  non-null-profile key lookup, best-effort bulk reads.
- 114 unit tests and 11 Testcontainers integration tests against
  apache/iotdb:2.0.8-standalone.

Signed-off-by: Zihan Dai <99155080+PDGGK@users.noreply.github.com>
…04 Wk 4)

Adapts the Wk4 IoTDBTableLatestDao (single-table derived-latest, no shadow
table) onto the merged PR-1 base so it coexists with the timeseries and
attributes DAOs. No latest overlay yet — that is a follow-up.

- LatestDao: findLatest (ORDER BY time DESC LIMIT 1), findAllLatest
  (LAST_BY(col,time) + MAX(time) GROUP BY key); saveLatest no-op and
  removeLatest half-open-window are documented derived-latest gaps.
- Coherent opt-in activation via IoTDBTableLatestEnabledCondition
  (database.ts.type=iotdb-table AND database.ts_latest.type=iotdb-table +
  experimental flag), reusing the shared named session pool; a
  TimeseriesLatestDaoConflictGuard fails startup on a conflicting host DAO.
- v4.3.1.2 TimeseriesLatestDao SPI stub (10 methods incl. findLatestByEntityIds
  /+Async) verified byte-for-byte against the real ThingsBoard source; pinned by
  StrategyFContractTest with a getDeclaredMethods length guard.
- All three DAOs share one session pool via @ConditionalOnMissingBean(name).
- 131 unit tests + 24 Testcontainers ITs (Latest 4 + Timeseries 9 +
  Attributes 11) against apache/iotdb:2.0.8-standalone.

Signed-off-by: Zihan Dai <99155080+PDGGK@users.noreply.github.com>
Closes the saveLatest/removeLatest gaps the single-table derived-latest cannot
satisfy (mentor-confirmed: add a minimal latest overlay rather than document the
latest-only paths as unsupported).

- New telemetry_latest overlay table (telemetry shape, TTL='INF'), created only
  under the latest selector via a second schema-bootstrap bean.
- saveLatest does a tag-only delete-then-insert into the overlay under a
  per-identity lock; the async batch writer prevents distinguishing latest-only
  from full-save, so the overlay is written on every saveLatest (it is therefore
  a per-key latest store, written once per latest update). version stays null.
- findLatest/findAllLatest merge the derived latest (telemetry) with the overlay
  by max timestamp per key (overlay wins ties); the merge is max-by-ts, never
  additive, so latest-only keys surface and stale overlay rows are shadowed.
- removeLatest snapshots the merged latest under the per-identity lock, deletes
  the in-window overlay row, and on rewriteLatestIfDeleted resurrects the
  next-older telemetry value into the overlay.
- Documented Phase-1 residuals (flagged for review): version always null
  (Cassandra parity), a telemetry-derived removeLatest race for full-save values,
  overlay TTL='INF' growth, and the same-ts overlay-wins tie-break.
- 141 unit tests + 31 Testcontainers ITs (Latest 11) against
  apache/iotdb:2.0.8-standalone.

Signed-off-by: Zihan Dai <99155080+PDGGK@users.noreply.github.com>
…304 PR-2)

Make the non-null deviceProfileId degradation rationale precise in the DAO
javadoc + the module README: it mirrors the official non-relational backend
(CassandraBaseTimeseriesLatestDao.findAllKeysByDeviceProfileId also returns
Collections.emptyList()), entity_attributes has no device_profile_id tag, and the
sole upstream caller is DeviceProfileController
GET /api/deviceProfile/devices/keys/attributes (TENANT_ADMIN, a config-time UI key
enumeration) which tolerates an empty result. Failing loud would 500 under IoTDB
while Cassandra returns empty -- an avoidable backend inconsistency. A real
device->profile lookup remains a Phase-2 optional enhancement.

Signed-off-by: Zihan Dai <99155080+PDGGK@users.noreply.github.com>
@PDGGK PDGGK closed this Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant