From be3f16927dd407e6d91a356dc40a321a004c43f9 Mon Sep 17 00:00:00 2001 From: Shrey Nahata Date: Sun, 22 Nov 2026 11:19:27 -0800 Subject: [PATCH 1/3] out_s3: keep $INDEX metadata outside the schema buffer root With parquet.schema_file set, the file store is rooted at /msgpack and the $INDEX sequence stream was created inside it. Its directory holds no chunks, so store shutdown removed it, and it also did not match the location used without a schema. Either way the index restarted at zero, and because $INDEX keys have no random suffix, new uploads could overwrite existing S3 objects. In schema mode, write the index to /sequence/index_metadata, the same path schema-less mode uses, outside the MessagePack store. Skip the shared sequence directory when warning about buffers from the other mode, so it is not reported as pending JSON-lines data. Assisted-by: Kiro CLI 3.5.0 (claude-opus-5.5) Signed-off-by: Shrey Nahata --- plugins/out_s3/s3.c | 55 +++++++++++++++++++++++++++++++++++++++------ plugins/out_s3/s3.h | 1 + 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/plugins/out_s3/s3.c b/plugins/out_s3/s3.c index f39d9162d0c..83de35e9fd3 100644 --- a/plugins/out_s3/s3.c +++ b/plugins/out_s3/s3.c @@ -812,6 +812,10 @@ static void s3_warn_other_buffer_mode(struct flb_s3 *ctx, char *root) if (entry->d_name[0] == '.') { continue; } + /* $INDEX metadata is shared by both modes; it is not buffered data. */ + if (strcmp(entry->d_name, "sequence") == 0) { + continue; + } is_msgpack = strcmp(entry->d_name, "msgpack") == 0; if ((ctx->arrow_schema != NULL) == is_msgpack) { continue; @@ -932,16 +936,44 @@ static int init_seq_index(void *context) { ctx->key_fmt_has_seq_index = FLB_TRUE; - ctx->stream_metadata = flb_fstore_stream_create(ctx->fs, "sequence"); - if (!ctx->stream_metadata) { - flb_plg_error(ctx->ins, "could not initialize metadata stream"); - flb_fstore_destroy(ctx->fs); - ctx->fs = NULL; - return -1; + /* + * Keep the index under the mode-independent buffer root. With a schema the + * file store is rooted at /msgpack; putting the index there would + * restart it at zero whenever parquet.schema_file is toggled, and with + * $INDEX keys (no random suffix) that can overwrite existing S3 objects. + */ + if (ctx->arrow_schema == NULL) { + ctx->stream_metadata = flb_fstore_stream_create(ctx->fs, "sequence"); + if (!ctx->stream_metadata) { + flb_plg_error(ctx->ins, "could not initialize metadata stream"); + flb_fstore_destroy(ctx->fs); + ctx->fs = NULL; + return -1; + } + ctx->metadata_dir = flb_sds_create(ctx->stream_metadata->path); + } + else { + tmp = "/sequence"; + ctx->metadata_dir = flb_sds_create(ctx->buffer_root); + if (ctx->metadata_dir != NULL && + flb_sds_cat_safe(&ctx->metadata_dir, tmp, strlen(tmp)) < 0) { + flb_sds_destroy(ctx->metadata_dir); + ctx->metadata_dir = NULL; + } + if (ctx->metadata_dir != NULL) { +#ifdef FLB_SYSTEM_WINDOWS + ret = mkdir(ctx->metadata_dir); +#else + ret = mkdir(ctx->metadata_dir, 0700); +#endif + if (ret < 0 && errno != EEXIST) { + flb_plg_error(ctx->ins, "Failed to create metadata directory"); + return -1; + } + } } /* Construct directories and file path names */ - ctx->metadata_dir = flb_sds_create(ctx->stream_metadata->path); if (ctx->metadata_dir == NULL) { flb_plg_error(ctx->ins, "Failed to create metadata path"); flb_errno(); @@ -1086,6 +1118,10 @@ static void s3_context_destroy(struct flb_s3 *ctx) flb_sds_destroy(ctx->buffer_dir); } + if (ctx->buffer_root) { + flb_sds_destroy(ctx->buffer_root); + } + if (ctx->metadata_dir) { flb_sds_destroy(ctx->metadata_dir); } @@ -1344,6 +1380,11 @@ static int s3_init(struct flb_output_instance *ins, } s3_warn_other_buffer_mode(ctx, tmp_sds); ctx->buffer_dir = tmp_sds; + ctx->buffer_root = flb_sds_create(tmp_sds); + if (ctx->buffer_root == NULL) { + flb_errno(); + return -1; + } if (ctx->arrow_schema != NULL) { tmp_sds = flb_sds_cat(ctx->buffer_dir, "/msgpack", 8); if (tmp_sds == NULL) { diff --git a/plugins/out_s3/s3.h b/plugins/out_s3/s3.h index e2c35d7f90c..38503a5544b 100644 --- a/plugins/out_s3/s3.h +++ b/plugins/out_s3/s3.h @@ -186,6 +186,7 @@ struct flb_s3 { flb_sds_t date_key; flb_sds_t buffer_dir; + flb_sds_t buffer_root; /* bucket buffer path, same in every buffer mode */ char *store_dir; struct flb_fstore *fs; From 40b2cb7762a492c11246897feb528f0d26fd6e46 Mon Sep 17 00:00:00 2001 From: Shrey Nahata Date: Sun, 22 Nov 2026 11:19:28 -0800 Subject: [PATCH 2/3] aws: rename columnar conversion input to data compress_columnar() now receives either JSON lines or Fluent Bit MessagePack events depending on whether a schema is set, so the parameter name json was misleading. Assisted-by: Kiro CLI 3.5.0 (claude-opus-5.5) Signed-off-by: Shrey Nahata --- src/aws/compression/arrow/compress.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/aws/compression/arrow/compress.c b/src/aws/compression/arrow/compress.c index 3698553597d..e4f07699962 100644 --- a/src/aws/compression/arrow/compress.c +++ b/src/aws/compression/arrow/compress.c @@ -268,11 +268,12 @@ static GArrowResizableBuffer* table_to_parquet_buffer(GArrowTable *table, } #endif +/* data is JSON lines when schema is NULL, otherwise Fluent Bit MessagePack events. */ static int compress_columnar(int columnar_format, - void *json, size_t size, - void **out_buf, size_t *out_size, - int compression_type, - struct flb_arrow_schema *schema) + void *data, size_t size, + void **out_buf, size_t *out_size, + int compression_type, + struct flb_arrow_schema *schema) { GArrowTable *table; GArrowResizableBuffer *buffer; @@ -290,10 +291,10 @@ static int compress_columnar(int columnar_format, } if (schema != NULL) { - table = flb_arrow_schema_table(schema, json, size); + table = flb_arrow_schema_table(schema, data, size); } else { - table = parse_json((uint8_t *) json, size); + table = parse_json((uint8_t *) data, size); } if (table == NULL) { flb_error("[aws][compress] Failed to build Arrow Table"); From 5a99c5643ff7a7e8c3d57991d066ea20a0407412 Mon Sep 17 00:00:00 2001 From: Shrey Nahata Date: Sun, 22 Nov 2026 11:19:28 -0800 Subject: [PATCH 3/3] tests: integration: cover $INDEX across schema restarts Restart Fluent Bit with an $INDEX-only key while keeping or removing parquet.schema_file, and assert that no S3 key is reused. Assisted-by: Kiro CLI 3.5.0 (claude-opus-5.5) Signed-off-by: Shrey Nahata --- .../scenarios/out_s3/tests/test_out_s3_001.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/integration/scenarios/out_s3/tests/test_out_s3_001.py b/tests/integration/scenarios/out_s3/tests/test_out_s3_001.py index 8d9a09cc79f..22cbea1eb6f 100644 --- a/tests/integration/scenarios/out_s3/tests/test_out_s3_001.py +++ b/tests/integration/scenarios/out_s3/tests/test_out_s3_001.py @@ -883,3 +883,41 @@ def test_out_s3_schema_accepts_arrow_metadata(tmp_path, metadata): assert table.to_pylist() == [{"x": True}] assert not table.schema.metadata assert not table.schema.field("x").metadata + + +# Schema-less restarts also reset $INDEX today (empty-stream cleanup at +# shutdown removes the sequence directory); that is tracked separately, so +# every sequence here starts with the schema enabled. +@pytest.mark.parametrize("modes", [(True, True), (True, False)]) +def test_out_s3_schema_restart_preserves_seq_index(tmp_path, modes): + # $INDEX keys have no random suffix, so a sequence index that restarts at + # zero across schema restarts or a schema toggle would overwrite objects. + fields = [_arrow_field("x", "int", False, bitWidth=32, isSigned=True)] + config_file, config = _schema_config(tmp_path, "arrow", fields, {"x": 42}) + output = config["pipeline"]["outputs"][0] + output["s3_key_format"] = "/$TAG/$INDEX" + schema_file = output.pop("parquet.schema_file") + + keys = [] + for use_schema in modes: + if use_schema: + output["parquet.schema_file"] = schema_file + else: + output.pop("parquet.schema_file", None) + config_file.write_text(yaml.safe_dump(config)) + service = Service(str(config_file)) + _start_or_skip_unsupported_columnar_format(service, "requires arrow-glib") + try: + request = service.wait_for_request() + log = Path(service.service.flb.log_file).read_text() + finally: + service.stop() + assert request["method"] == "PUT" + # The shared $INDEX metadata directory must not look like pending + # JSON-lines buffers when the schema is enabled. + if use_schema: + assert "JSON-lines buffer directory exists" not in log + keys.append(request["path"]) + + indexes = [int(key.rsplit("/", 1)[1]) for key in keys] + assert indexes == sorted(set(indexes)), f"S3 keys reused or out of order: {keys}"