mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
* feat(firehose): deliver records from a Kinesis stream source A delivery stream created with DeliveryStreamType=KinesisStreamAsSource recorded its KinesisStreamSourceConfiguration and then ignored it. Nothing read the source stream, so the only way records reached S3 was PutRecord on the delivery stream itself -- which real AWS rejects for a sourced stream. CreateDeliveryStream succeeded, DescribeDeliveryStream reported ACTIVE, and the stream silently delivered nothing. Found by terraform-aws-messaging's own TestKinesisFirehose, which puts one record into the source stream and polls the destination bucket: every API call succeeded and the test failed with "no objects found in the bucket" after six 60s retries. The flusher tick now polls each sourced delivery stream through the ordinary shard-iterator API, so what it ingests is exactly what a GetRecords consumer would see, and the iterator doubles as the checkpoint. The first iterator starts at DeliveryStartTimestamp rather than TRIM_HORIZON: attaching Firehose to a stream that already holds records does not backfill them on AWS. Records are handed to putRecordBatch, so they buffer and trigger flushes on the same terms as records delivered by PutRecord. A source stream that does not exist is logged at debug and skipped, so one broken delivery stream cannot stall polling for the others. * fix(firehose): persist Kinesis source shard checkpoints across a restart The shard iterator each source shard had been consumed to lived only in an in-memory map, while the delivery stream itself and the source stream's records both sit in persisted storage. On restart the checkpoint was the one piece that did not come back, so polling rebuilt every iterator from DeliveryStartTimestamp and delivered the source stream's entire retained history to S3 a second time -- silently, and again on every subsequent restart. The checkpoint moves into a firehose/source-iterators.json store alongside streams.json. A Kinesis shard iterator is safe to persist because it is a self-describing token (streamName|shardId|type|sequenceNumber|index|timestamp, base64) with no JVM-lifetime component, and KinesisService never trims a shard's record list, so an index recorded before a restart still resolves to the same position after one. The checkpoint is written after the polled records are buffered, so a crash between the two re-reads them rather than dropping them, and a graceful shutdown loses nothing at all: onPreShutdown already drains the buffers to S3 before storage is flushed to disk. buffers and bufferSince stay in memory -- losing those loses only what was in flight, which is bounded, where losing the checkpoint replays an unbounded history. * fix(firehose): commit source checkpoints on delivery, not on buffering Persisting the advanced shard iterator inside pollKinesisSource fixed an unbounded replay on restart but introduced a worse failure in its place. Polled records do not go to S3 from the poller; they go through putRecordBatch into the in-memory buffer and reach S3 only when a flush trips. Writing the checkpoint immediately after buffering let the durable checkpoint outrun durable delivery: terminate in that window and the buffer is gone while the persisted checkpoint already says those records were consumed, so the restored poller resumes past them and they are never delivered. Real Firehose is at-least-once, so a duplicate is correct-if-untidy; a silent permanent skip is not. The checkpoint now has two halves. pendingSourceIterators holds the advanced position in memory, and only a flush whose S3 write actually succeeded promotes it into the persisted source-iterators store. The poller reads from pending-or-committed, so a second poll still does not re-read what the first buffered, and the restart case the previous commit fixed still holds: polling resumes from the last committed position, never from DeliveryStartTimestamp. Ordering and failure detail: - The advance runs inside putRecordBatch under the buffer lock, alongside the records it covers, so no flush can observe one without the other, and a throw before buffering leaves the checkpoint alone. - flush takes the pending checkpoint out under that same lock, and flush drains the whole buffer, so a successful write makes every record the checkpoint covers durable. - A failed write neither commits nor restores the checkpoint: the durable position stays put and the next poll reads those records again, which repairs the failed delivery instead of silently dropping it. - Each advance rebases on the current pending-or-committed map instead of one carried across the shard loop, so a discarded pending rolls the other shards back with it. Commits merge per shard, so two flushes completing out of order cost a duplicate rather than dropping a shard's progress. - onPreShutdown is unchanged and now correct by construction: its drain commits each checkpoint it makes durable, before storage is written out. The comment claiming a crash between the two "re-reads them rather than dropping them" was simply wrong -- buffering is not durability -- and is replaced. Records arriving through PutRecord share the buffer and are unaffected: they carry no checkpoint advance, and a flush that mixes them with sourced records still delivers both before anything is committed. * fix(firehose): stamp DeliveryStartTimestamp at creation for a Kinesis source CreateDeliveryStream's KinesisStreamSourceConfiguration carries only the ARN and role -- DeliveryStartTimestamp exists only on the Description shape, which AWS fills in at creation. FirehoseJsonHandler mapped the source straight off the request, so through the API the field was always null and the poller fell back to TRIM_HORIZON, backfilling a pre-populated source stream's whole history into S3. createDeliveryStream now stamps the field itself when a KinesisStreamAsSource request omits it, matching how AWS behaves: attaching Firehose to a stream that already holds records does not replay them. Adds a wire-level regression test that pre-loads a Kinesis stream, creates the delivery stream through the real CreateDeliveryStream API call, and asserts the pre-existing record is never delivered while one added afterward is -- closing the gap where the existing unit test covered this by calling FirehoseService.createDeliveryStream directly and hand-setting the timestamp, stepping around the API path entirely.