Skip to content

Persistence: DynamoDB (atomic) single-table data model - #5707

Merged
dimas-b merged 1 commit into
apache:mainfrom
ZephyrYWZhou:dynamodb-atomic-data-model
Oct 19, 2026
Merged

dimas-b merged 1 commit into
apache:mainfrom
ZephyrYWZhou:dynamodb-atomic-data-model

Conversation

@ZephyrYWZhou

@ZephyrYWZhou ZephyrYWZhou commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Next step of the optional DynamoDB persistence backend (#5615), building on the merged skeleton (#5622). This PR adds the single-table data model for the dynamodb-atomic module: the key/attribute encodings and the item mappers that translate Polaris objects to and from DynamoDB items.

This is a pure serialization layer — no DynamoDB client calls, no persistence logic, and no runtime wiring. Nothing selects or runs the backend yet, so there is no behavior change for existing deployments.

Design

Single base table, with realmId used only as a key prefix for tenant isolation (never a partition key on its own):

  • Entity item — PK = realmId#id, SK = ENTITY. Carries the full entity state plus the by-name GSI keys.
  • Name-sentinel item — PK = realmId#catalogId#parentId#typeCode#name, SK = NAME, payload = owning entity id. This is the atomic stand-in for a multi-column UNIQUE constraint: a create claims the name via attribute_not_exists inside the same TransactWriteItems as the entity write, so uniqueness holds atomically.
  • By-name GSI — gsi1pk = realmId#catalogId#parentId#typeCode, gsi1sk = name — used to list a parent's children.

The entity item carries its gsi1pk/gsi1sk so the item shape is final; the GSI itself and the listing query that reads it land in a later PR.

Changes

  • DynamoDbConstants — key encodings and attribute names for the entity and name-sentinel items and the by-name GSI.
  • EntityItem — PolarisBaseEntity ↔ entity item, including property maps and the GSI keys.
  • NameSentinelItem — name-sentinel item (parent coordinates → owning entity id).
  • build.gradle.kts — add the AWS SDK BOM and the dynamodb dependency.

Testing

./gradlew :polaris-persistence-dynamodb-atomic:build — compile, checkstyle, spotless, javadoc, and 9 unit tests all pass. The tests are pure JVM (no DynamoDB required): key-encoding assertions and full round-trips for each item type, covering property maps and the empty-map / absent-attribute edge cases.

Schema/table creation and conformance tests against DynamoDB-Local land with the implementation PRs.

Scope / follow-ups

Landing the entity + name-sentinel layout on its own so it can be reviewed before anything is built on it. Follow-ups: schema/table creation, the BasePersistence logic (writes, CAS, lookups, listing), grant and policy item types alongside their logic, and the factory/CDI/config wiring that makes the backend selectable. End-to-end illustrative draft: #5694.

Checklist

@ZephyrYWZhou
ZephyrYWZhou force-pushed the dynamodb-atomic-data-model branch 2 times, most recently from 0a4b94e to 47e48f7 Compare October 6, 2026 23:43
@ZephyrYWZhou
ZephyrYWZhou marked this pull request as ready for review October 6, 2026 23:58
dimas-b
dimas-b previously approved these changes Oct 7, 2026

@dimas-b dimas-b 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.

No opinion on DynamoDB specifics (lack of knowledge on my part), but the code looks reasonable in general.

dimas-b
dimas-b previously approved these changes Oct 7, 2026

@dimas-b dimas-b 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.

LGTM 👍 I'd appreciate more reviews, specifically for DynamoDB parts.

@dimas-b
dimas-b requested review from flyrain and snazy October 7, 2026 18:47

@vigneshio vigneshio 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.

Makes sense to lock the item layout before the write path. Thanks @ZephyrYWZhou LGTM..

/** Name-sentinel partition key: {@code realmId#catalogId#parentId#typeCode#name}. */
public static String sentinelPk(
String realmId, long catalogId, long parentId, int typeCode, String name) {
return realmId + SEP + catalogId + SEP + parentId + SEP + typeCode + SEP + name;

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.

Does this stay unique if realmId or the name contains #? Iceberg names can't have #, but realm ids and catalog names aren't run through EntityNameValidator. WDYT ? @ZephyrYWZhou

cc: @dimas-b

@dimas-b dimas-b Oct 8, 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.

Yes, it is possible that two distinct sets of inputs produce the same key. Good point!

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.

Forbidding SEP in realmId should clear this problem, I think. Name can remain unrestricted.

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.

Good catch @vigneshio! And thanks to @dimas-b @vigneshio both for digging into this.

Before we settle on forbidding # in realmId, I'd like to put another option on the table that keeps realmId unrestricted and still guarantees uniqueness.

The idea is to length-prefix the realmId in the key, so a realmId of a#b encodes as 3#a#b#.... The reason we have the bug today is that realmId and name are both free-form, so the numeric fields between them can "slide", and two different (realmId, name) combinations can line up on the exact same key when one of them happens to contain a #. Writing realmId's length up front pins down exactly where it ends, so that sliding can't happen and two distinct realmIds can never collide, even if one of them contains the separator.

There's effectively no added complexity or latency here. We never parse these keys back, so it's just measuring a string's length and prepending it, not a real encode/decode scheme.

The only real cost is that the keys get a little harder to read, which I'd personally consider a fair trade against telling users they can't use a particular character in their realm id. What do you both think?

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.

I'd just ban # in the realm id. The name can still have # because it sits at the end of the key.

Putting the length in front of the realm id also works, but then every key that starts with the realm has to do it, and the keys get harder to read. I wouldn't do that unless we really need # in realm ids.

@dimas-b does banning # in the realm id still look right to you? WDYT ??

@ZephyrYWZhou ZephyrYWZhou Oct 8, 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.

Agreed @dimas-b. I'd rather not ban a character from Polaris realm IDs just to accommodate one backend. Changing the encoding not only avoids putting that limitation on users but also unblocks this backend option implementation without requiring a separate dev discussion. (The development process here is incremental so one PR will strictly rely on last one)

I personally prefer to go with length-prefixed encoding since that is the best I can think of for now but If anyone has a cleaner encoding approach, I'm very open to suggestions.

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.

How about JSON object as a string (very short property names)? I do not think lightning fast performance is a concern here. JSON will rename human-readable.

Cf. PageToken.

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.

Using a non-printable char for the separator is also an option. I do not think they will ever be a concern in realm names.

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.

Thanks @dimas-b. Following your point that we should keep the encoding isomorphic rather than restrict realm ids, I went with a length-prefix on the realm id, so realm ids stay unrestricted and the keys are still guaranteed unique.

A couple of reasons I settled on this for now:

  • It keeps the chain moving. The backend is landing as a stack of incremental PRs that each build on the last, so I'd rather unblock it with a small, lightweight fix than hold it up on a longer encoding debate.
  • It's isomorphic with essentially zero overhead, just a length measurement and a prepend, no new dependency, and nothing to parse back.

There are other reasonable options too (the JSON-string idea you raised, etc.), but since none of them differ functionally or on performance for this use case, I didn't want to block progress on the choice.

Happy to revisit the encoding later if we'd prefer something more self-describing, very open to further discussion. Does that sounds good to you? @dimas-b

@dimas-b dimas-b Oct 19, 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.

length prefix looks reasonable here.

@ZephyrYWZhou

ZephyrYWZhou commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Tagging @flyrain and @dennishuo for review on this PR as well.

@ZephyrYWZhou
ZephyrYWZhou requested a review from dimas-b October 15, 2026 20:44

@dimas-b dimas-b 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.

A few more minor comments. In general this PR LGTM 👍

* when {@code realmId} or {@code name} contains the separator; both are free-form, whereas the
* numeric coordinates cannot contain it.
*/
public static String sentinelPk(

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.

nit: technically this is not a constant 🤔 WDYT about renaming the class to DynamoDBUtils?

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.

Yep make sense


/** Serialize an entity to a DynamoDB item map for the given realm. */
public static Map<String, AttributeValue> toItem(String realmId, PolarisBaseEntity e) {
Map<String, AttributeValue> item = new HashMap<>();

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.

Since this appear to be involved in interactions with the database, it would be nice to make the returned value immutable. It greatly simplifies reasoning about behaviour correctness.

@ZephyrYWZhou ZephyrYWZhou Oct 19, 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.

Actually this is a very good callout - appreicate it I will change it.

Adds the DynamoDB item layout for the dynamodb-atomic backend: key encodings and attribute names (DynamoDbConstants) plus the entity, grant, and name-sentinel item mappers, with unit tests. Pure serialization layer; persistence logic and runtime wiring follow in later PRs.
@ZephyrYWZhou
ZephyrYWZhou force-pushed the dynamodb-atomic-data-model branch from c54f80a to 008b775 Compare October 19, 2026 17:56
@ZephyrYWZhou
ZephyrYWZhou requested a review from dimas-b October 19, 2026 18:22
@dimas-b

dimas-b commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

This PR is an intermediate step. The code it adds is not exposed to users yet and it has been in review for quite some time.

I'm merging it in its current state. If other reviews have any follow-up concerns we have time to address them on main before the new backend is released.

@dimas-b
dimas-b merged commit 87adc3c into apache:main Oct 19, 2026
24 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in Basic Kanban Board Oct 19, 2026
@flyrain

flyrain commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

It was merged while I'm reviewing. I got a few comments, given this is one of multiple PRs, we can continue to work on it in followups. Thanks @ZephyrYWZhou for the contribution. Thanks @dimas-b and @vigneshio for the review.

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.

4 participants