mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(firehose): keep buffered records when S3 delivery fails (#3471)
* fix(firehose): keep buffered records when S3 delivery fails A failed S3 write dropped the batch it was delivering. Records that came in through PutRecord were lost; records polled from a Kinesis source were only recovered by re-reading from the committed checkpoint. A failed delivery now restores the batch at the front of the buffer, ahead of records that arrived while the write was in flight, keeps the buffer's original start time so the next scheduled tick retries it, and re-pends the source checkpoint so a later successful delivery commits it. Successful delivery, compression and the scheduled flush are unchanged. * fix(firehose): serialize delivery per stream so a later flush cannot commit past an unresolved batch The buffer monitor only made the snapshot atomic. A poll that had read past a first flush's checkpoint could buffer its records after that flush took the pending position, and a second flush would then deliver them and commit a position past the first batch while its S3 write was still in flight. If that write failed, the durable checkpoint already sat past records that were only buffered for retry, and a restart before the retry skipped them. One delivery at a time per stream, from taking the buffer to committing or restoring its checkpoint, closes that window; the poller keeps buffering under the buffer lock alone, so a slow write never stalls a poll. * docs(firehose): a failed S3 write now keeps its batch buffered * docs(firehose): drop the stale no-retry note in the Parquet converter
This commit is contained in:
1 parent
c18c80e557
commit
6507f32aea
4 files changed
+269
-38
No files matched your search
+2
-3
@@ -204,9 +204,8 @@ public class FirehoseParquetConverter {
|
||||
}
|
||||
|
||||
// Still before the Parquet: a failure here delivers nothing at all and the
|
||||
// flush fails as a whole. Nothing is kept for a retry, the staged object goes
|
||||
// on the way out either way, so recovery belongs to the source: a Kinesis
|
||||
// stream re-reads its uncommitted checkpoint, a DirectPut buffer cannot.
|
||||
// flush fails as a whole. The staged object goes on the way out either way;
|
||||
// the batch stays buffered in the service and is retried on the next flush.
|
||||
String errorKey = failures.isEmpty()
|
||||
? null
|
||||
: writeErrorOutput(stream, s3, bucket, failures, deliveryTime, schemaConfig);
|
||||
|
||||
@@ -66,6 +66,13 @@ public class FirehoseService implements ResourceProvider {
|
||||
private final AccountAwareStorageBackend<DeliveryStreamDescription> streamStore;
|
||||
private final Map<String, List<byte[]>> buffers = new ConcurrentHashMap<>();
|
||||
private final Map<String, Instant> bufferSince = new ConcurrentHashMap<>();
|
||||
// One delivery at a time per stream, from taking the buffer to committing or
|
||||
// restoring its checkpoint. The buffer monitor alone only makes the snapshot atomic:
|
||||
// a second flush could still take records polled past a first flush's checkpoint,
|
||||
// deliver them and commit that position while the first write is still in flight.
|
||||
// If the first write then failed, the durable checkpoint would already sit past its
|
||||
// records, and a restart before the retry would skip them for good.
|
||||
private final Map<String, Object> flushLocks = new ConcurrentHashMap<>();
|
||||
private final S3Service s3Service;
|
||||
private final KinesisService kinesisService;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -752,8 +759,8 @@ public class FirehoseService implements ResourceProvider {
|
||||
}
|
||||
|
||||
// Rebased on whatever is current rather than on a map carried across the shard loop,
|
||||
// so that a flush which discarded pending (because its S3 write failed) rolls the
|
||||
// other shards back to committed instead of having this poll re-assert them.
|
||||
// so that a flush which took pending (and, if its S3 write failed, restored it) is
|
||||
// never overwritten by a stale copy this poll carried past that flush.
|
||||
private void advanceSourceIterator(String streamKey, String shardId, String iterator) {
|
||||
Map<String, String> advanced = new LinkedHashMap<>(sourceIteratorsInUse(streamKey));
|
||||
advanced.put(shardId, iterator);
|
||||
@@ -848,35 +855,38 @@ public class FirehoseService implements ResourceProvider {
|
||||
return;
|
||||
}
|
||||
|
||||
List<byte[]> toFlush;
|
||||
// Taken with the records, under the same lock: this is the position the records
|
||||
// about to be written cover, and committing it is this flush's job.
|
||||
Map<String, String> checkpoint;
|
||||
synchronized (buffer) {
|
||||
toFlush = new ArrayList<>(buffer);
|
||||
buffer.clear();
|
||||
bufferSince.remove(streamName);
|
||||
checkpoint = pendingSourceIterators.remove(streamName);
|
||||
}
|
||||
if (toFlush.isEmpty()) {
|
||||
// Lost the race against a concurrent flush; nothing left to deliver. Any
|
||||
// checkpoint taken here covers no undelivered records -- it can only have
|
||||
// come from a poll that read an empty page -- so it commits as it stands.
|
||||
commitSourceIterators(streamName, checkpoint);
|
||||
return;
|
||||
}
|
||||
synchronized (flushLocks.computeIfAbsent(streamName, k -> new Object())) {
|
||||
List<byte[]> toFlush;
|
||||
// Taken with the records, under the same lock: this is the position the records
|
||||
// about to be written cover, and committing it is this flush's job.
|
||||
Map<String, String> checkpoint;
|
||||
Instant since;
|
||||
synchronized (buffer) {
|
||||
toFlush = new ArrayList<>(buffer);
|
||||
buffer.clear();
|
||||
since = bufferSince.remove(streamName);
|
||||
checkpoint = pendingSourceIterators.remove(streamName);
|
||||
}
|
||||
if (toFlush.isEmpty()) {
|
||||
// Lost the race for the stream lock; nothing left to deliver. Any
|
||||
// checkpoint taken here covers no undelivered records -- it can only have
|
||||
// come from a poll that read an empty page -- so it commits as it stands.
|
||||
commitSourceIterators(streamName, checkpoint);
|
||||
return;
|
||||
}
|
||||
|
||||
// Every store this delivery touches, Glue and S3 included, reads the account
|
||||
// from the request context, and a scheduled flush has none. Without this the
|
||||
// work would run as the default account rather than the stream's owner: the
|
||||
// stream's Glue table would be missed and its objects would land in the wrong
|
||||
// partition. The sidecar follows the same context, so both sides stay together.
|
||||
RequestScopes.runAs(stream.getAccountId(),
|
||||
() -> deliverBuffer(streamName, stream, toFlush, checkpoint));
|
||||
// Every store this delivery touches, Glue and S3 included, reads the account
|
||||
// from the request context, and a scheduled flush has none. Without this the
|
||||
// work would run as the default account rather than the stream's owner: the
|
||||
// stream's Glue table would be missed and its objects would land in the wrong
|
||||
// partition. The sidecar follows the same context, so both sides stay together.
|
||||
RequestScopes.runAs(stream.getAccountId(),
|
||||
() -> deliverBuffer(streamName, stream, toFlush, since, checkpoint));
|
||||
}
|
||||
}
|
||||
|
||||
private void deliverBuffer(String streamName, DeliveryStreamDescription stream,
|
||||
List<byte[]> toFlush, Map<String, String> checkpoint) {
|
||||
List<byte[]> toFlush, Instant since, Map<String, String> checkpoint) {
|
||||
try {
|
||||
String bucket = resolveBucket(stream);
|
||||
S3Destination s3 = stream.s3Destination();
|
||||
@@ -935,10 +945,34 @@ public class FirehoseService implements ResourceProvider {
|
||||
// Only now: the records these iterators were read past are durable.
|
||||
commitSourceIterators(streamName, checkpoint);
|
||||
} catch (Exception e) {
|
||||
LOG.errorv("Failed to flush Firehose stream {0}: {1}", streamName, e.getMessage());
|
||||
// checkpoint is deliberately neither committed nor put back. The durable
|
||||
// checkpoint stays where it was, so the next poll reads these records again
|
||||
// and this failed delivery repairs itself.
|
||||
LOG.errorv("Failed to flush Firehose stream {0}; keeping {1} records buffered for retry: {2}",
|
||||
streamName, toFlush.size(), e.getMessage());
|
||||
restoreFailedFlush(streamName, toFlush, since, checkpoint);
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreFailedFlush(String streamName, List<byte[]> toFlush, Instant since,
|
||||
Map<String, String> checkpoint) {
|
||||
List<byte[]> buffer = buffers.get(streamName);
|
||||
if (buffer != null) {
|
||||
synchronized (buffer) {
|
||||
List<byte[]> restored = new ArrayList<>(toFlush);
|
||||
restored.addAll(buffer);
|
||||
buffer.clear();
|
||||
buffer.addAll(restored);
|
||||
// The restored batch is the oldest data in the buffer again, so the buffering
|
||||
// window starts where it did before the failed flush, not at a record that
|
||||
// arrived while the write was in flight.
|
||||
bufferSince.put(streamName, since != null ? since : clock.instant());
|
||||
if (checkpoint != null) {
|
||||
Map<String, String> pending = new LinkedHashMap<>(checkpoint);
|
||||
Map<String, String> concurrent = pendingSourceIterators.get(streamName);
|
||||
if (concurrent != null) {
|
||||
pending.putAll(concurrent);
|
||||
}
|
||||
pendingSourceIterators.put(streamName, Collections.unmodifiableMap(pending));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user