mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(kinesis): stop routing records to closed shards (#6414)
* fix(kinesis): stop routing records to closed shards Repeated resharding can leave a selected shard and both of its direct children closed. `selectShard` currently falls back to that closed parent, so a successful write can append after the shard's ending sequence number. - Follow both parent links through closed intermediate shards to an open descendant, with deterministic breadth-first order and visited shard IDs. - Reject an unreachable open destination before appending. Keep the producer lock, ordinary-key hash selection, ExplicitHashKey routing, and actual returned ShardId. - Cover repeated splits, split/merge lineage, malformed graphs, handler success/failure results, and Java SDK writes after resharding. * test(kinesis): protect breadth-first descendant preference Add a mixed-depth split topology so routing to the still-open direct child is distinguished from depth-first traversal through its closed sibling. * test(kinesis): cover both mixed-depth shard layouts Parameterize the split child so the direct-child preference regression rejects depth-first traversal under either child ordering. * fix(kinesis): preserve the open-shard routing fast path Delay the lineage snapshot until the selected shard is closed, avoiding a shard-list copy on ordinary open-shard writes. Name expected descendant destinations in routing fixtures and cover an unrelated open shard preceding the reachable child.
This commit is contained in:
5 files changed
+374
-16
No files matched your search
@@ -23,8 +23,16 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
@@ -1208,21 +1216,42 @@ public class KinesisService implements ResourceProvider {
|
||||
return selectShardByExplicitHashKey(stream, explicitHashKey);
|
||||
}
|
||||
|
||||
// Simple hash-based shard selection among ALL shards, then resolve to open one
|
||||
int index = (int) (Math.abs((long) partitionKey.hashCode()) % stream.getShards().size());
|
||||
KinesisShard shard = stream.getShards().get(index);
|
||||
|
||||
// If closed, find the first open child (simplified)
|
||||
while (shard.isClosed()) {
|
||||
KinesisShard finalShard = shard;
|
||||
shard = stream.getShards().stream()
|
||||
.filter(s -> finalShard.getShardId().equals(s.getParentShardId()) || finalShard.getShardId().equals(s.getAdjacentParentShardId()))
|
||||
.filter(s -> !s.isClosed())
|
||||
.findFirst()
|
||||
.orElse(shard); // Fallback to itself if no open child found
|
||||
if (shard == finalShard) break; // prevent infinite loop
|
||||
// Preserve ordinary-key placement, then resolve historical parents to an open descendant.
|
||||
// The producer holds the stream lock, shared with splitShard and mergeShards.
|
||||
List<KinesisShard> shards = stream.getShards();
|
||||
int index = (int) (Math.abs((long) partitionKey.hashCode()) % shards.size());
|
||||
KinesisShard selected = shards.get(index);
|
||||
if (!selected.isClosed()) {
|
||||
return selected;
|
||||
}
|
||||
return shard;
|
||||
|
||||
List<KinesisShard> snapshot = List.copyOf(shards);
|
||||
Map<String, List<KinesisShard>> children = new HashMap<>();
|
||||
for (KinesisShard shard : snapshot) {
|
||||
if (shard.getParentShardId() != null) {
|
||||
children.computeIfAbsent(shard.getParentShardId(), ignored -> new ArrayList<>()).add(shard);
|
||||
}
|
||||
if (shard.getAdjacentParentShardId() != null) {
|
||||
children.computeIfAbsent(shard.getAdjacentParentShardId(), ignored -> new ArrayList<>()).add(shard);
|
||||
}
|
||||
}
|
||||
Deque<KinesisShard> pending = new ArrayDeque<>();
|
||||
Set<String> visited = new HashSet<>();
|
||||
pending.add(selected);
|
||||
visited.add(selected.getShardId());
|
||||
while (!pending.isEmpty()) {
|
||||
KinesisShard shard = pending.removeFirst();
|
||||
if (!shard.isClosed()) {
|
||||
return shard;
|
||||
}
|
||||
for (KinesisShard child : children.getOrDefault(shard.getShardId(), List.of())) {
|
||||
if (visited.add(child.getShardId())) {
|
||||
pending.addLast(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new AwsException("InvalidArgumentException",
|
||||
"Selected shard " + selected.getShardId() + " has no open descendant.", 400);
|
||||
}
|
||||
|
||||
private KinesisShard selectShardByExplicitHashKey(KinesisStream stream, String explicitHashKey) {
|
||||
|
||||
Reference in new issue
Block a user