Repository navigation
Persistence: DynamoDB (atomic) single-table data model - #5707
Conversation
0a4b94e to
47e48f7
Compare
dimas-b
left a comment
There was a problem hiding this comment.
No opinion on DynamoDB specifics (lack of knowledge on my part), but the code looks reasonable in general.
47e48f7 to
bad6e35
Compare
dimas-b
left a comment
There was a problem hiding this comment.
LGTM 👍 I'd appreciate more reviews, specifically for DynamoDB parts.
vigneshio
left a comment
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Yes, it is possible that two distinct sets of inputs produce the same key. Good point!
There was a problem hiding this comment.
Forbidding SEP in realmId should clear this problem, I think. Name can remain unrestricted.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ??
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
length prefix looks reasonable here.
bad6e35 to
c54f80a
Compare
|
Tagging @flyrain and @dennishuo for review on this PR as well. |
dimas-b
left a comment
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
nit: technically this is not a constant 🤔 WDYT about renaming the class to DynamoDBUtils?
There was a problem hiding this comment.
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<>(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
c54f80a to
008b775
Compare
|
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 |
|
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. |
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-atomicmodule: 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
realmIdused only as a key prefix for tenant isolation (never a partition key on its own):PK = realmId#id,SK = ENTITY. Carries the full entity state plus the by-name GSI keys.PK = realmId#catalogId#parentId#typeCode#name,SK = NAME, payload = owning entityid. This is the atomic stand-in for a multi-column UNIQUE constraint: a create claims the name viaattribute_not_existsinside the sameTransactWriteItemsas the entity write, so uniqueness holds atomically.gsi1pk = realmId#catalogId#parentId#typeCode,gsi1sk = name— used to list a parent's children.The entity item carries its
gsi1pk/gsi1skso 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 thedynamodbdependency.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
BasePersistencelogic (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
CHANGELOG.mdupdate needed