mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
Partition parallel scans by hashing the partition key attribute rather than filtering by result list index. This ensures stable, deterministic segment assignment that is unaffected by concurrent mutations or deletions in earlier segments.
This commit is contained in:
1 parent
e3766eac6f
commit
9c9aa670e4
3 files changed
+187
-10
No files matched your search
+1
-10
@@ -1115,18 +1115,9 @@ public class DynamoDbJsonHandler {
|
||||
|
||||
DynamoDbService.ScanResult result = dynamoDbService.scan(
|
||||
tableName, filterExpr, exprAttrNames, exprAttrValues, scanFilter, limit,
|
||||
exclusiveStartKey, indexNameScan, region);
|
||||
exclusiveStartKey, indexNameScan, segment, totalSegments, region);
|
||||
|
||||
List<JsonNode> scanItems = result.items();
|
||||
// Apply parallel scan segment partitioning
|
||||
if (segment != null && totalSegments != null && totalSegments > 1) {
|
||||
final int seg = segment, total = totalSegments;
|
||||
final List<JsonNode> allItems = scanItems;
|
||||
scanItems = new ArrayList<>();
|
||||
for (int si = 0; si < allItems.size(); si++) {
|
||||
if (si % total == seg) scanItems.add(allItems.get(si));
|
||||
}
|
||||
}
|
||||
// Apply index projection (KEYS_ONLY / INCLUDE) when scanning a secondary index
|
||||
if (indexNameScan != null && projectionExpressionScan == null && attributesToGetScan == null) {
|
||||
scanItems = applyIndexProjection(scanItems, scanTable, scanAccessPath, select);
|
||||
|
||||
@@ -1095,6 +1095,14 @@ public class DynamoDbService implements ResourceProvider {
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
JsonNode scanFilter, Integer limit, JsonNode exclusiveStartKey,
|
||||
String indexName, String region) {
|
||||
return scan(tableName, filterExpression, expressionAttrNames, expressionAttrValues,
|
||||
scanFilter, limit, exclusiveStartKey, indexName, null, null, region);
|
||||
}
|
||||
|
||||
public ScanResult scan(String tableName, String filterExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
JsonNode scanFilter, Integer limit, JsonNode exclusiveStartKey,
|
||||
String indexName, Integer segment, Integer totalSegments, String region) {
|
||||
DynamoDbReservedWords.check(filterExpression, "FilterExpression");
|
||||
String canonicalTableName = canonicalTableName(region, tableName);
|
||||
String storageKey = regionKey(region, canonicalTableName);
|
||||
@@ -1140,6 +1148,12 @@ public class DynamoDbService implements ResourceProvider {
|
||||
&& (lekSkName == null || hasNonNullAttribute(item, lekSkName)))) {
|
||||
continue;
|
||||
}
|
||||
if (segment != null && totalSegments != null && totalSegments > 1) {
|
||||
JsonNode pkAttr = item.get(lekPkName);
|
||||
if (computeSegment(pkAttr, totalSegments) != segment) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// Stop at whichever boundary the read reaches first: the 1 MB cap or
|
||||
// Limit. The size check comes first — per the API reference, "if the
|
||||
// processed dataset size exceeds 1 MB before DynamoDB reaches this
|
||||
@@ -3269,6 +3283,27 @@ public class DynamoDbService implements ResourceProvider {
|
||||
return keyNode;
|
||||
}
|
||||
|
||||
int computeSegment(JsonNode pkAttr, int totalSegments) {
|
||||
if (totalSegments <= 1 || pkAttr == null) {
|
||||
return 0;
|
||||
}
|
||||
String scalar = extractScalarValue(pkAttr);
|
||||
if (scalar == null) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] digest = md.digest(scalar.getBytes(StandardCharsets.UTF_8));
|
||||
long val = ((long) (digest[0] & 0xFF) << 24)
|
||||
| ((long) (digest[1] & 0xFF) << 16)
|
||||
| ((long) (digest[2] & 0xFF) << 8)
|
||||
| ((long) (digest[3] & 0xFF));
|
||||
return (int) (val % totalSegments);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
return Math.floorMod(scalar.hashCode(), totalSegments);
|
||||
}
|
||||
}
|
||||
|
||||
private String extractScalarValue(JsonNode attrValue) {
|
||||
if (attrValue == null) return null;
|
||||
if (attrValue.has("S")) return attrValue.get("S").asText();
|
||||
|
||||
Reference in new issue
Block a user