mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(kinesis): reject records over the 1 MiB size limit (#1896)
* fix(kinesis): reject records over the 1 MiB size limit PutRecord and PutRecords accepted records of any size; real Kinesis rejects a record whose data blob plus partition key exceeds the stream's maximum record size (1 MiB by default). Floci doesn't implement UpdateMaxRecordSize, so every stream sits at the default. Validate in putRecordWithShardId, covering both operations. An oversized record in PutRecords fails the whole request up front, before anything is written: the per-record ErrorCode field is reserved for ProvisionedThroughputExceededException and InternalFailure, so a size violation cannot be reported per record. Closes #1755 * refactor(kinesis): decode batch entries once and resolve stream first Review round: cache successful Data decodes for the write loop (malformed entries keep their per-record failure), resolve the stream before the size check so a missing stream reports ResourceNotFoundException, and add a wire-level PutRecords over-limit test. * test(kinesis): cover PutRecords per-record failure and unknown stream Two paths the size-limit change touched had no coverage: no test produced a non-zero FailedRecordCount for Kinesis, and none covered PutRecords against a missing stream. Malformed per-record Data must stay a per-record InternalFailure rather than a whole-batch failure. Collapsing the decode fallback to entry.data() drops FailedRecordCount to 0 and stores a record with a null blob, since validateRecordSize only counts the partition key; the next GetRecords then fails on the encode. Resolving the stream up front turns PutRecords against a missing stream into ResourceNotFoundException instead of a per-record InternalFailure.
This commit is contained in:
1 parent
8ef71d6930
commit
5afb60d0f5
4 files changed
+201
-3
No files matched your search
@@ -426,15 +426,37 @@ public class KinesisJsonHandler {
|
||||
|
||||
private Response handlePutRecords(JsonNode request, String region) {
|
||||
String streamName = resolveStreamName(request);
|
||||
service.describeStream(streamName, region);
|
||||
JsonNode recordsNode = request.path("Records");
|
||||
|
||||
// An oversized record fails the whole request before anything is
|
||||
// written — per-record ErrorCode is reserved for throughput/internal
|
||||
// failures. Successful decodes are kept for the write loop; malformed
|
||||
// Data is left null so it stays a per-record failure there.
|
||||
record Entry(JsonNode node, byte[] data) {}
|
||||
List<Entry> entries = new ArrayList<>();
|
||||
for (JsonNode node : recordsNode) {
|
||||
byte[] data;
|
||||
try {
|
||||
data = Base64.getDecoder().decode(node.path("Data").asText());
|
||||
} catch (IllegalArgumentException e) {
|
||||
data = null;
|
||||
}
|
||||
if (data != null) {
|
||||
service.validateRecordSize(data, node.path("PartitionKey").asText());
|
||||
}
|
||||
entries.add(new Entry(node, data));
|
||||
}
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode results = response.putArray("Records");
|
||||
int failed = 0;
|
||||
|
||||
for (JsonNode node : recordsNode) {
|
||||
for (Entry entry : entries) {
|
||||
try {
|
||||
byte[] data = Base64.getDecoder().decode(node.path("Data").asText());
|
||||
String partitionKey = node.path("PartitionKey").asText();
|
||||
byte[] data = entry.data() != null ? entry.data()
|
||||
: Base64.getDecoder().decode(entry.node().path("Data").asText());
|
||||
String partitionKey = entry.node().path("PartitionKey").asText();
|
||||
KinesisService.PutRecordResult result = service.putRecordWithShardId(streamName, data, partitionKey, region);
|
||||
results.addObject()
|
||||
.put("SequenceNumber", result.sequenceNumber())
|
||||
|
||||
@@ -30,6 +30,10 @@ public class KinesisService {
|
||||
private static final String DEFAULT_STREAM_MODE = "PROVISIONED";
|
||||
private static final int DEFAULT_INSPECTION_RECORD_LIMIT = 100;
|
||||
private static final int MAX_INSPECTION_RECORD_LIMIT = 1000;
|
||||
// AWS default per-stream maximum record size (data blob + partition key).
|
||||
// Raisable per stream via UpdateMaxRecordSize, which floci doesn't
|
||||
// implement, so every stream sits at the default.
|
||||
private static final int MAX_RECORD_SIZE_BYTES = 1_048_576;
|
||||
|
||||
private final StorageBackend<String, KinesisStream> store;
|
||||
private final StorageBackend<String, KinesisConsumer> consumerStore;
|
||||
@@ -368,8 +372,19 @@ public class KinesisService {
|
||||
return putRecordWithShardId(streamName, data, partitionKey, region).sequenceNumber();
|
||||
}
|
||||
|
||||
public void validateRecordSize(byte[] data, String partitionKey) {
|
||||
int size = (data != null ? data.length : 0)
|
||||
+ (partitionKey != null ? partitionKey.getBytes(StandardCharsets.UTF_8).length : 0);
|
||||
if (size > MAX_RECORD_SIZE_BYTES) {
|
||||
throw new AwsException("InvalidArgumentException",
|
||||
"Record size (data + partition key) of " + size + " bytes exceeds the maximum of "
|
||||
+ MAX_RECORD_SIZE_BYTES + " bytes.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public PutRecordResult putRecordWithShardId(String streamName, byte[] data, String partitionKey, String region) {
|
||||
KinesisStream stream = resolveStream(streamName, region);
|
||||
validateRecordSize(data, partitionKey);
|
||||
KinesisShard shard = selectShard(stream, partitionKey);
|
||||
|
||||
String sequenceNumber = String.valueOf(sequenceGenerator.incrementAndGet());
|
||||
|
||||
Reference in new issue
Block a user