Repository navigation
Persistence: DynamoDB (atomic) write child records in the base table - #6118
Conversation
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.
orca-review-bot
left a comment
There was a problem hiding this comment.
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:
DynamoDbLayoutclass 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 indeleteSucceedsAfterAConcurrentUpdate. 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) { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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))); |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
Hmmmmm interesting idea let me think, let me check how many types we are having there.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
+1 Thanks @ZephyrYWZhou !
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 sameTransactWriteItemsas the entity on:Put(attribute_not_exists) alongside the entity and name-sentinel putsgsi1and listing are left unchanged.Why
Per the discussion on #6016:
gsi1is eventually consistent, so listing/hasChildrencan 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 withConsistentRead=true, which removes that lag.This PR is the write-side groundwork only. A follow-up (reworking #6016) will switch
listEntities/hasChildrento a base-tableQuerywithConsistentRead=trueand removegsi1. 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-tableGetItem).Draft while the design is confirmed on #6016.