Repository navigation
Fix gateway asset restoration and migration order - #3657
Conversation
- Run the application version migration after the 1.35.1 migrations - Preserve stored agent links during gateway attribute processing - Restore owned pending assets while enforcing ancestry checks - Log rejected gateway asset IDs and clean up test fixtures
richturner
left a comment
There was a problem hiding this comment.
Thanks for the follow-up. The pending-deletion and migration changes look good. One change requested on the agent link fix.
Load the asset read-only in AssetProcessingService instead of copying the meta in GatewayService
Copying the MetaMap in GatewayService.onAttributeEventIntercepted stops the agent link from being lost. But it leaves the root cause in place for every other handler.
The bug comes from #3547 replacing assetStorageService.find(...) with em.find(Asset.class, id, lockMode) in processAttributeEvent. The old lookup ran a native query with AvailableHints.HINT_READ_ONLY, so Hibernate never checked the entity for changes. em.find returns a managed entity, and new AttributeEvent(asset, attribute, ...) takes attribute.getMeta() by reference. So any AttributeEventInterceptor that changes the event's meta changes the stored asset, and so does any interceptor that changes the asset it gets from em.find in the same transaction. AttributeEventInterceptor is a public extension point, so extensions and custom projects are exposed to this too. The copy also has gaps:
- It is shallow. The
MetaIteminstances are still shared, so a handler that changes an item's value still writes it through. - It only covers this one call site. Other interceptors and other mutable state, such as asset fields, are still exposed.
Loading the asset read-only restores the behavior from before #3547 for all handlers at once. It also removes the dirty check at flush:
// Read-only: interceptors may change the event or the asset in memory, but nothing is flushed.
// The value is persisted separately by the native update in updateAttributeValue.
Asset<?> asset =
em.find(Asset.class, event.getId(), lockMode, Map.of(AvailableHints.HINT_READ_ONLY, true));- Lock: the row lock belongs to the transaction, not the entity, so
PESSIMISTIC_WRITEstill applies. - Coverage: the later
em.findinGatewayServicereturns this same instance from the persistence context, so it is read-only as well. - Fallback: if
findturns out to ignore the hint in our Hibernate version,em.unwrap(Session.class).setReadOnly(asset, true)straight after the load does the same thing.
With that in place, the MetaMap copy in GatewayService can go. The plain event.getMeta().remove(MetaItemType.AGENT_LINK) only affects the event again, as it did on main before #3547, and the merged branch for gateway and internal sources can stay.
Tests:
- The new
GatewayMappingTestcase assertsattribute.hasMeta(AGENT_LINK)on the source attribute. That only holds because of the copy, so it would fail once the copy is removed. Keep the assertions that the event no longer has the link and that ownership is checked, and drop the assertion on the source attribute. - The real protection lives in
AssetProcessingServiceand needs a container to test, so please add an integration test. Either works:- In
GatewayTest: after the initial sync, have the edge client send anAttributeEventfor an agent-linked attribute. Once the value lands, re-read the central asset and assert thattempstill hasAGENT_LINKand thatversionhas not changed. - A general guard: register an interceptor with
assetProcessingService.addEventInterceptor { em, event -> event.meta.remove(AGENT_LINK); false }, asRulesEngineFiringTestdoes. Send an event, then assert the stored meta and version are unchanged. This covers any future handler, not just the gateway one.
- In
Other notes
- Migration rename: this is correct. 1.35.1 shipped
V20261125_1401andV20261130_1200, so an upgrade from 1.35.1 withoutOfOrder=falsewould fail onV20261105_1530. One risk remains: a database that ran amainbuild after #3547 already hasV20261105_1530applied. Flyway validation will report it as applied but not found, and the renamed script will fail onCREATE TABLE application_version. ConsiderCREATE TABLE IF NOT EXISTSin the renamed script, and add a note for anyone runningmainsnapshots about cleaning up the oldflyway_schema_historyrow, e.g. withflyway repair. - Pending deletion:
PendingDeletion.ALLOW_SUBJECTplus movingfailedAssetDeleteIds.removeto after a successful merge looks right. The test covers a pending ancestor, an invalid replacement and a valid restore. - Nit:
saveAssetLocallynow logs and rethrows, andonGatewayEventlogs the same exception again before disconnecting. Consider attaching the IDs to the rethrown exception instead, so each failure is logged once.
- Keep gateway row locks while preventing interceptor mutations from persisting - Persist gateway credential repairs explicitly in the attribute transaction - Include asset IDs in sync errors and cover read-only interception
|
Addressed the review feedback in 2ac3580. Attribute processing now loads assets read-only while keeping the gateway row lock and preserving explicit credential repairs. Sync failures now log the edge and central asset IDs once. Added integration coverage for interceptor changes and verified that an inbound gateway update preserves |
|



Description
Follow up on the post-merge feedback on #3547 and fix the Flyway upgrade from 1.35.1.
setupSpecand clean up its assets.Validation
git diff --checkpassed.GatewayTestused a temporary Keycloak port override for the isolated test stack.Checklist