Skip to content

Persistence: DynamoDB (atomic) write child records in the base table - #6118

Merged
flyrain merged 4 commits into
apache:mainfrom
ZephyrYWZhou:dynamodb-atomic-child-records
Nov 17, 2026
Merged

flyrain merged 4 commits into
apache:mainfrom
ZephyrYWZhou:dynamodb-atomic-child-records

Conversation

@ZephyrYWZhou

@ZephyrYWZhou ZephyrYWZhou commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Part of #6119

What

Writes a strongly consistent child record in the base table for every entity, keyed by the parent's coordinates (realm, catalog, parentId, typeCode) with the child id in the sort key (CHILD#<padded-id>). The record is written in the same TransactWriteItems as the entity on:

  • create — one more Put (attribute_not_exists) alongside the entity and name-sentinel puts
  • re-parent — delete the child record under the old parent, put one under the new parent (a pure rename keeps the key, since it is keyed by parent + id, not name)
  • delete — delete the child record

gsi1 and listing are left unchanged.

Why

Per the discussion on #6016: gsi1 is eventually consistent, so listing/hasChildren can read a stale index and make a wrong correctness decision (e.g. drop-namespace emptiness, location-overlap), even for sequential requests. A base-table child record can be read with ConsistentRead=true, which removes that lag.

This PR is the write-side groundwork only. A follow-up (reworking #6016) will switch listEntities/hasChildren to a base-table Query with ConsistentRead=true and remove gsi1. The record has to exist before listing can read it, hence the split.

Tests

Added IT cases to DynamoDbBasePersistenceIT: create writes the child record, re-parent moves it, a pure rename keeps it, delete removes it (asserted via a strongly consistent base-table GetItem).

Draft while the design is confirmed on #6016.

Write a strongly consistent child record in the base table on create,
re-parent, and delete, in the same TransactWriteItems as the entity.
This is the write-side groundwork so a later change can serve listing
from a base-table Query with ConsistentRead=true instead of the
eventually-consistent gsi1 index. gsi1 and listing are unchanged here.
@ZephyrYWZhou
ZephyrYWZhou marked this pull request as ready for review November 17, 2026 04:02

@orca-review-bot orca-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, tight groundwork. I traced the create/rename/delete transactions. Child ops sit after the sentinel ops, so newSentinelIndex = 2 still points at the right cancellation reason, and a pure rename leaves the child record alone. A few small things inline.

Not anchored to the diff:

  • DynamoDbLayout class javadoc (lines 25-27) still says every item is partitioned by its own identity. Child records are partitioned by parent on purpose, so a busy namespace, or a realm's principals/catalogs (realm#0#0#type), now shares one base-table partition key and pays base-table WCU on every create/delete. Can we say that in the class doc?
  • Tests: can we cover failure paths for the child record? E.g. assert no child record after the rejected create in duplicateNameIsRejectedWithoutPartialWrites. Add a re-parent onto a taken name and check the record stays under the old parent. Assert it's gone after the retried delete in deleteSucceedsAfterAConcurrentUpdate. Atomicity is what the listing follow-up relies on.
  • No changelog needed, since the module isn't wired yet.

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

* so a listing can read children with {@code ConsistentRead=true}. It shares the children
* coordinates with {@link #childrenGsiPk}, but lives on the base table rather than the index.
*/
public static String childPk(String realmId, long catalogId, long parentId, int typeCode) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is byte-for-byte the same as childrenGsiPk. Fine once gsi1 goes away, but until then the two have to stay in sync by hand. Can one delegate to the other in the meantime?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

* the child's id in the sort key, so a parent's children can be listed by a base-table {@code
* Query} with {@code ConsistentRead=true}.
*
* <p>It is written in the same {@code TransactWriteItems} as the entity on create, rename or

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A pure rename doesn't touch the child record. Only create, re-parent and delete do, as your renameWithoutReParentKeepsTheChildRecord test shows. Can we drop "rename" here?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

}

/** The child entity id this record points at, or 0 if absent. */
public static long entityId(Map<String, AttributeValue> item) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing calls this outside the unit test until the listing follow-up. Can it land with the reader instead?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

public static Map<String, AttributeValue> toItem(
String realmId, long catalogId, long parentId, int typeCode, long entityId) {
Map<String, AttributeValue> item = new HashMap<>();
item.put(PK, stringAttr(childPk(realmId, catalogId, parentId, typeCode)));

@flyrain flyrain Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parentID would be 0 in case of catalog entity, which should work.

When we have the typeCode in the PK, the tradeoff is that listing every child(use case: location overlap checking) under a parent would require a query for each type. It is slightly heavier but acceptable. AFAIK, listing per type is a more common operation, e.g., list all Iceberg tables, list all policies, etc.

By saying that, we may add subType code to the PK. This is esp. useful for table like entity listing. e.g., listing all iceberg tables is a very common operation. For any type without subType, it shouldn't affect their listing perf, since the subType would always be 0 in that case. WDYT?

  ICEBERG_TABLE(2, PolarisEntityType.TABLE_LIKE),
  ICEBERG_VIEW(3, PolarisEntityType.TABLE_LIKE),
  GENERIC_TABLE(4, PolarisEntityType.TABLE_LIKE);

@ZephyrYWZhou ZephyrYWZhou Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmmm interesting idea let me think, let me check how many types we are having there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok checked. Subtypes are a fixed enum, just 0/2/3/4, and only table-likes have more than one. So listing Iceberg tables becomes one exact query, and the "all children" case only fans out to a handful.

In that case yeah I agree that we should be ok with a slightly heavier listing all children in exchange for a a cleaner single-query listing per subtype, especially since the fan-out is bounded 0/2/3/4.

I'll add subTypeCode to the PK here so the key's settled before the listing PR. Thanks for the great suggestion here! @flyrain

@flyrain flyrain left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on it, @ZephyrYWZhou ! LGTM overall! We may double check whether need the subTypeCode in the PK before merging.

…path tests

- ChildItem javadoc: a pure rename leaves the record in place (create/re-parent/delete only).
- DynamoDbLayout class javadoc: child records are partitioned by parent on purpose, noting the shared-partition write cost.
- childPk delegates to childrenGsiPk so the two stay in sync while gsi1 exists.
- IT: assert no child record after a rejected create, after a rejected re-parent onto a taken name (stays under the old parent), and after the retried delete.
Per review: key the child record by realm#catalog#parent#type#subtype so a
single subtype (e.g. all Iceberg tables) is one exact query. Types with no
subtype always use 0, so their listing is unaffected; listing all children of
a parent fans out over the fixed subtype set. childPk no longer delegates to
childrenGsiPk, since the two now differ by the subtype segment.

@flyrain flyrain left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 Thanks @ZephyrYWZhou !

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Nov 17, 2026
@flyrain
flyrain merged commit 06057ca into apache:main Nov 17, 2026
28 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in Basic Kanban Board Nov 17, 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.

3 participants