feat(firehose): honor BufferingHints with time and size based flush (#2314)

* feat(firehose): honor BufferingHints with time and size based flush

Buffers now flush when the stream's BufferingHints.SizeInMBs is reached
or when IntervalInSeconds has elapsed since the first buffered record,
replacing the hardcoded 5-record count trigger that matched neither AWS
nor LocalStack. A single-thread daemon flusher ticks every
floci.services.firehose.tick-interval-seconds (default 10).

Buffers are drained on ShutdownDelayInitiatedEvent so pending records
reach S3 before EmulatorLifecycle persists storage to disk.
DeleteDeliveryStream keeps discarding pending records, matching real
AWS (verified empirically: 3 records buffered under a 300s/5MB hint
never reached the bucket after deletion).

Fixes #1886

* feat(firehose): add opt-in flush-record-count trigger

Emulator-only volume trigger complementing BufferingHints: setting
floci.services.firehose.flush-record-count to N flushes a stream's
buffer as soon as it holds N records. Disabled by default (0) so
out-of-the-box delivery matches real AWS; 1 gives LocalStack-style
record-at-a-time delivery for interactive local dev, where stream
definitions mirror production intervals and waiting for
IntervalInSeconds is impractical. Covers the use case of #1852.

Co-Authored-By: yoshinori-aono-om <yoshinori.aono@omiai.inc>

* test(firehose): cover size and interval flush triggers at the wire level

The size trigger delivers inline on the writing request, and the
interval trigger runs through the real background flusher started on
StartupEvent — previously only verified manually. The test-scope Clock
is the frozen MutableClock alternative, so the interval test advances
it explicitly instead of sleeping through real time.

* fix(firehose): clamp the flush tick interval and mirror config keys in application.yml

A zero or negative floci.services.firehose.tick-interval-seconds reached
scheduleAtFixedRate unchanged, which throws IllegalArgumentException from
the StartupEvent observer and aborts startup. Clamp it to a 1s minimum,
the same guard RdsDataService applies to its cleanup interval.

Also list tick-interval-seconds and flush-record-count under the firehose
service in the main and test application.yml, matching how other tunable
service settings are surfaced there.

* test(firehose): wait for buffered delivery in DataLake and SES compatibility tests

DataLakeTest relied on the removed 5-record flush: with buffered
delivery its Athena query ran before anything reached S3, failing with
NoSuchBucket on floci-firehose-results (caught by the compatibility CI
job). Both tests now set explicit BufferingHints and poll for the
delivered object first. A 5s interval keeps the wait short — floci does
not enforce AWS's 60s server-side minimum and the SDK does no
client-side range validation.

---------

Co-authored-by: Mihai Blidaru <mihai.blidaru@lynxfctech.com>
This commit is contained in:
Mihai BlidaruandMihai Blidaru authored and GitHub committed 2026-08-16 23:28:09 -05:00
1 parent c745f821af
commit bfda938f72
13 files changed
+650 -50

No files matched your search

@@ -9,6 +9,9 @@ import software.amazon.awssdk.services.firehose.model.PutRecordRequest;
import software.amazon.awssdk.services.firehose.model.Record;
import software.amazon.awssdk.services.glue.GlueClient;
import software.amazon.awssdk.services.glue.model.*;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
@@ -26,6 +29,7 @@ class DataLakeTest {
private static AthenaClient athena;
private static GlueClient glue;
private static FirehoseClient firehose;
private static S3Client s3;
private static final String DB_NAME = TestFixtures.uniqueName("test_db");
private static final String TABLE_NAME = "orders";
@@ -36,6 +40,7 @@ class DataLakeTest {
athena = TestFixtures.athenaClient();
glue = TestFixtures.glueClient();
firehose = TestFixtures.firehoseClient();
s3 = TestFixtures.s3Client();
}
@Test
@@ -76,6 +81,12 @@ class DataLakeTest {
.bucketARN("arn:aws:s3:::floci-firehose-results")
.roleARN("arn:aws:iam::000000000000:role/datalake-firehose-role")
.prefix(STREAM_NAME + "/")
// Floci does not enforce AWS's 60s minimum interval, which keeps
// the delivery wait short (would need 60+ against real AWS).
.bufferingHints(software.amazon.awssdk.services.firehose.model.BufferingHints.builder()
.sizeInMBs(1)
.intervalInSeconds(5)
.build())
.build())
.build());
}
@@ -92,6 +103,28 @@ class DataLakeTest {
.build());
}
// Small records stay buffered until the stream's IntervalInSeconds
// elapses; wait for Firehose to deliver before querying, the same way a
// real AWS client would.
long deadline = System.currentTimeMillis() + 30_000;
boolean delivered = false;
while (!delivered && System.currentTimeMillis() < deadline) {
try {
delivered = !s3.listObjectsV2(ListObjectsV2Request.builder()
.bucket("floci-firehose-results")
.prefix(STREAM_NAME + "/")
.build()).contents().isEmpty();
} catch (NoSuchBucketException e) {
// The bucket itself is only created on the first delivery.
}
if (!delivered) {
Thread.sleep(2_000);
}
}
assertThat(delivered)
.as("Firehose should deliver the buffered records within IntervalInSeconds")
.isTrue();
// Athena Query
StartQueryExecutionResponse startResp = athena.startQueryExecution(StartQueryExecutionRequest.builder()
.queryString("SELECT sum(amount) as total FROM " + TABLE_NAME)
@@ -56,6 +56,7 @@ import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;
import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse;
import software.amazon.awssdk.services.firehose.FirehoseClient;
import software.amazon.awssdk.services.firehose.model.BufferingHints;
import software.amazon.awssdk.services.firehose.model.CreateDeliveryStreamRequest;
import software.amazon.awssdk.services.firehose.model.DeleteDeliveryStreamRequest;
import software.amazon.awssdk.services.firehose.model.S3DestinationConfiguration;
@@ -199,6 +200,12 @@ class SesEventPublishingTest {
.bucketARN("arn:aws:s3:::" + firehoseBucket)
.roleARN("arn:aws:iam::000000000000:role/sdk-evt-fh-role")
.prefix(firehoseStreamName + "/")
// Floci does not enforce AWS's 60s minimum interval, which keeps
// the delivery wait short (would need 60+ against real AWS).
.bufferingHints(BufferingHints.builder()
.sizeInMBs(1)
.intervalInSeconds(5)
.build())
.build())
.build())
.deliveryStreamARN();
@@ -478,9 +485,9 @@ class SesEventPublishingTest {
@Test
@Order(5)
@DisplayName("Firehose destination: 5 sends trigger auto-flush to S3 as NDJSON")
void firehoseDestination_fiveSends_triggerAutoFlushToS3() throws Exception {
for (int i = 0; i < 5; i++) {
@DisplayName("Firehose destination: send events are delivered to S3 as NDJSON within the buffering interval")
void firehoseDestination_sendEvents_deliveredWithinBufferingInterval() throws Exception {
for (int i = 0; i < 2; i++) {
ses.sendEmail(SendEmailRequest.builder()
.fromEmailAddress(identity)
.destination(Destination.builder()
@@ -498,12 +505,22 @@ class SesEventPublishingTest {
.build());
}
ListObjectsV2Response listed = s3.listObjectsV2(ListObjectsV2Request.builder()
.bucket(firehoseBucket)
.prefix(firehoseStreamName + "/")
.build());
// Two small events stay well below SizeInMBs, so delivery happens on the
// stream's IntervalInSeconds plus the emulator's flush tick.
ListObjectsV2Response listed = null;
long deadline = System.currentTimeMillis() + 30_000;
while (System.currentTimeMillis() < deadline) {
listed = s3.listObjectsV2(ListObjectsV2Request.builder()
.bucket(firehoseBucket)
.prefix(firehoseStreamName + "/")
.build());
if (!listed.contents().isEmpty()) {
break;
}
Thread.sleep(2_000);
}
assertThat(listed.contents())
.as("Firehose should have flushed exactly one S3 object after 5 records")
.as("Firehose should deliver the buffered events within IntervalInSeconds")
.hasSize(1);
S3Object obj = listed.contents().get(0);
@@ -511,7 +528,7 @@ class SesEventPublishingTest {
.bucket(firehoseBucket).key(obj.key()).build())
.readAllBytes(), StandardCharsets.UTF_8);
String[] lines = body.split("\\R");
assertThat(lines).hasSize(5);
assertThat(lines).hasSize(2);
for (String line : lines) {
JsonNode event = MAPPER.readTree(line);
assertThat(event.path("eventType").asText()).isEqualTo("Send");