-
Notifications
You must be signed in to change notification settings - Fork 2
feat(sqs): partition resolver for HT-FIFO routing (Phase 3.D PR 4-B-2) #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f48eeb
feat(sqs): partition resolver for HT-FIFO routing (Phase 3.D PR 4-B-2)
bootjp 8bbfcb9
fix(sqs): resolver runs on raw key + coordinator helpers consult reso…
bootjp 7f3a643
fix(sqs): buildSQSPartitionResolver returns interface to avoid typed-nil
bootjp f4a12e4
test(sqs): coordinator-level regression for resolver dispatch
bootjp 9cfc779
fix(sqs): fail-closed for recognised-but-unresolved partition keys
bootjp b4bf81c
test(sqs): tighten typed-nil regression + add genuine groupMutations …
bootjp 87561fd
fix(sqs): RecognisesPartitionedKey checks prefix only, not full parse
bootjp eedc17c
fix(sqs): fail-closed for unresolved txn read keys
bootjp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package adapter | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/binary" | ||
| ) | ||
|
|
||
| // SQSPartitionResolver maps a partitioned-SQS key to the operator- | ||
| // chosen Raft group for the (queue, partition) tuple. Implements | ||
| // kv.PartitionResolver via duck typing — see the integration in | ||
| // main.go where the resolver is installed on ShardedCoordinator. | ||
| // | ||
| // The byte-range engine cannot route partitioned queues because | ||
| // adding per-partition routes would break its non-overlapping-cover | ||
| // invariant (a partition route for partition K of one queue would | ||
| // leave a gap for legacy keys that fall lexicographically between | ||
| // partitions K and K+1). The resolver-first dispatch path avoids | ||
| // this — it answers only for keys that match a partitioned family | ||
| // prefix and otherwise lets the engine handle dispatch. | ||
| type SQSPartitionResolver struct { | ||
| routes map[string][]uint64 | ||
| } | ||
|
|
||
| // NewSQSPartitionResolver builds a resolver from the operator- | ||
| // supplied partition map. routes[queue][k] is the Raft group ID | ||
| // that owns partition k of queue, with len(routes[queue]) equal to | ||
| // the queue's PartitionCount. | ||
| // | ||
| // Returns nil when routes is empty so callers can keep the resolver | ||
| // out of the request path entirely on a non-partitioned cluster | ||
| // (kv.ShardRouter.WithPartitionResolver(nil) is a documented no-op). | ||
| // | ||
| // The constructor takes a defensive copy so a later caller mutation | ||
| // to the input map does not leak into the resolver's view at | ||
| // runtime. | ||
| func NewSQSPartitionResolver(routes map[string][]uint64) *SQSPartitionResolver { | ||
| if len(routes) == 0 { | ||
| return nil | ||
| } | ||
| cp := make(map[string][]uint64, len(routes)) | ||
| for queue, groups := range routes { | ||
| ids := make([]uint64, len(groups)) | ||
| copy(ids, groups) | ||
| cp[queue] = ids | ||
| } | ||
| return &SQSPartitionResolver{routes: cp} | ||
| } | ||
|
|
||
| // sqsResolverFamilyPrefixes is the set of partitioned-SQS family | ||
| // prefixes ResolveGroup recognises. Pre-converted to []byte so the | ||
| // hot-path bytes.HasPrefix call avoids an allocation per check | ||
| // (gemini medium on PR #715). Kept package-internal so any future | ||
| // renamed prefix touches both this list and the constant | ||
| // declaration in sqs_keys.go — TestSQSPartitionResolver_PrefixesAlign | ||
| // pins the alignment. | ||
| var sqsResolverFamilyPrefixes = [][]byte{ | ||
| []byte(SqsPartitionedMsgDataPrefix), | ||
| []byte(SqsPartitionedMsgVisPrefix), | ||
| []byte(SqsPartitionedMsgDedupPrefix), | ||
| []byte(SqsPartitionedMsgGroupPrefix), | ||
| []byte(SqsPartitionedMsgByAgePrefix), | ||
| } | ||
|
|
||
| // ResolveGroup decodes the (queue, partition) embedded in a | ||
| // partitioned-SQS key and returns the operator-chosen Raft group. | ||
| // | ||
| // Returns (0, false) for any key that does not match a partitioned | ||
| // family prefix (legacy SQS, KV, S3, DynamoDB, queue-meta records, | ||
| // …) so kv.ShardRouter falls through to its byte-range engine for | ||
| // default routing. | ||
| // | ||
| // Returns (0, false) for a partitioned-shaped key whose queue is | ||
| // not in the routes map or whose partition index is beyond | ||
| // len(routes[queue]). The router pairs this with | ||
| // RecognisesPartitionedKey to fail closed instead of falling | ||
| // through — silently routing through the engine's | ||
| // !sqs|route|global default would mis-route HT-FIFO traffic during | ||
| // partition-map drift (codex P1 round 2 on PR #715). | ||
| func (r *SQSPartitionResolver) ResolveGroup(key []byte) (uint64, bool) { | ||
| if r == nil || len(key) == 0 { | ||
| return 0, false | ||
| } | ||
| queue, partition, ok := parsePartitionedSQSKey(key) | ||
| if !ok { | ||
| return 0, false | ||
| } | ||
| groups, found := r.routes[queue] | ||
| if !found { | ||
| return 0, false | ||
| } | ||
| // Defensive: a partition value outside the slice is a config / | ||
| // upstream-bug signal, not a routable key. Returning false | ||
| // surfaces it as "no route" at the router boundary, which is | ||
| // the correct fail-closed behaviour. | ||
| if uint64(partition) >= uint64(len(groups)) { | ||
| return 0, false | ||
| } | ||
| return groups[partition], true | ||
| } | ||
|
|
||
| // RecognisesPartitionedKey reports whether key has the structural | ||
| // shape of a partitioned-SQS key — i.e. starts with one of the | ||
| // partitioned family prefixes. The check is PREFIX-ONLY, not a | ||
| // full parse: a key with a partitioned prefix followed by a | ||
| // malformed queue / partition segment still answers true, so the | ||
| // router fails closed via kv.PartitionResolver semantics instead | ||
| // of falling through to the engine and silently routing to the | ||
| // SQS catalog default group via routeKey's !sqs|route|global | ||
| // collapse (round 5 review nit on PR #715). | ||
| // | ||
| // A nil receiver returns false so kv.ShardRouter's typed-nil case | ||
| // (ResolveGroup(nil) == (0, false)) pairs with an honest "I don't | ||
| // recognise anything" answer instead of falsely claiming a shape. | ||
| func (r *SQSPartitionResolver) RecognisesPartitionedKey(key []byte) bool { | ||
| if r == nil || len(key) == 0 { | ||
| return false | ||
| } | ||
| _, ok := stripPartitionedFamilyPrefix(key) | ||
| return ok | ||
| } | ||
|
|
||
| // parsePartitionedSQSKey extracts the (queue, partition) pair from | ||
| // a partitioned-SQS key. Returns ok=false for any key that does not | ||
| // match a partitioned family prefix or that has a malformed queue / | ||
| // partition segment. Exposed at package-internal scope so the | ||
| // adapter's reaper / fanout reader can share the same parser | ||
| // (Phase 3.D PR 5). | ||
| func parsePartitionedSQSKey(key []byte) (string, uint32, bool) { | ||
| rest, matched := stripPartitionedFamilyPrefix(key) | ||
| if !matched { | ||
| return "", 0, false | ||
| } | ||
| // After the family prefix, the variable-length encoded queue | ||
| // segment is terminated by '|' (sqsPartitionedQueueTerminator). | ||
| // base64.RawURLEncoding never emits '|', so the first '|' in | ||
| // rest is unambiguously the queue terminator. | ||
| pipeIdx := bytes.IndexByte(rest, sqsPartitionedQueueTerminator) | ||
| if pipeIdx <= 0 { | ||
| return "", 0, false | ||
| } | ||
| encQueue := rest[:pipeIdx] | ||
| rest = rest[pipeIdx+1:] | ||
| const partitionLen = 4 | ||
| if len(rest) < partitionLen { | ||
| return "", 0, false | ||
| } | ||
| partition := binary.BigEndian.Uint32(rest[:partitionLen]) | ||
| queue, err := decodeSQSSegment(string(encQueue)) | ||
| if err != nil { | ||
| return "", 0, false | ||
| } | ||
| return queue, partition, true | ||
| } | ||
|
|
||
| // stripPartitionedFamilyPrefix returns the bytes after the matched | ||
| // family prefix. matched=false if key has none of the known | ||
| // partitioned family prefixes. | ||
| func stripPartitionedFamilyPrefix(key []byte) ([]byte, bool) { | ||
| for _, prefix := range sqsResolverFamilyPrefixes { | ||
| if bytes.HasPrefix(key, prefix) { | ||
| return key[len(prefix):], true | ||
| } | ||
| } | ||
| return nil, false | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
sqsResolverFamilyPrefixesupdated to[][]byte, the loop here can avoid the[]byte(prefix)conversion.