From f731464c06883c428fe54e4a9f6f1866f4796a3e Mon Sep 17 00:00:00 2001 From: Dmitri Pavlov Date: Tue, 1 Dec 2026 16:05:45 +0200 Subject: [PATCH 1/2] storage: compare parsed timestamps for recovered chunk ordering Signed-off-by: Dmitri Pavlov --- include/fluent-bit/flb_storage.h | 2 ++ src/flb_storage.c | 53 ++++++++++++++++---------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/include/fluent-bit/flb_storage.h b/include/fluent-bit/flb_storage.h index 25cdc943745..5d717976532 100644 --- a/include/fluent-bit/flb_storage.h +++ b/include/fluent-bit/flb_storage.h @@ -76,6 +76,8 @@ static inline char *flb_storage_get_type(int type) struct flb_input_instance; +int flb_storage_chunk_cmp(const void *a, const void *b); + int flb_storage_create(struct flb_config *ctx); int flb_storage_input_create(struct cio_ctx *cio, struct flb_input_instance *in); diff --git a/src/flb_storage.c b/src/flb_storage.c index 3cab3fe0f9b..218aa90b704 100644 --- a/src/flb_storage.c +++ b/src/flb_storage.c @@ -394,12 +394,14 @@ struct flb_storage_metrics *flb_storage_metrics_create(struct flb_config *ctx) static int parse_stamp(const char *p, time_t *sec, long *nsec) { uint64_t s = 0, ns = 0; + int off = 0; if (!p || !sec || !nsec) { return -1; } - /* expected: "1234567890.123456789.flb" format */ - if (sscanf(p, "%" SCNu64 ".%" SCNu64 ".flb", &s, &ns) != 2) { + /* expected: exactly "1234567890.123456789.flb" */ + if (sscanf(p, "%" SCNu64 ".%" SCNu64 ".flb%n", &s, &ns, &off) != 2 || + p[off] != '\0') { return -1; } if (ns >= 1000000000ULL) { @@ -411,60 +413,57 @@ static int parse_stamp(const char *p, time_t *sec, long *nsec) return 0; } -static int sort_chunk_cmp(const void *a_arg, const void *b_arg) +int flb_storage_chunk_cmp(const void *a_arg, const void *b_arg) { struct cio_chunk *chunk_a = *(struct cio_chunk **) a_arg; struct cio_chunk *chunk_b = *(struct cio_chunk **) b_arg; const char *pa, *pb; - int ra = 0; - int rb = 0; + int valid_a = 0; + int valid_b = 0; struct timespec tm_a = {0}, tm_b = {0}; /* Scan Chunk A */ pa = strrchr(chunk_a->name, '-'); if (pa && parse_stamp(pa + 1, &tm_a.tv_sec, &tm_a.tv_nsec) == 0) { - ra = -1; + valid_a = 1; } /* Scan Chunk B */ pb = strrchr(chunk_b->name, '-'); if (pb && parse_stamp(pb + 1, &tm_b.tv_sec, &tm_b.tv_nsec) == 0) { - rb = -1; + valid_b = 1; } /* Ensure strict ordering in presence of invalid names */ - if (ra != 0 || rb != 0) { + if (!valid_a || !valid_b) { /* valid < invalid */ - if (ra == 0 && rb != 0) { + if (valid_a && !valid_b) { return -1; } /* invalid > valid */ - if (ra != 0 && rb == 0) { + if (!valid_a && valid_b) { return 1; } /* both invalid: deterministic fallback */ return strcmp(chunk_a->name, chunk_b->name); } - /* Compare */ - if (tm_a.tv_sec != tm_b.tv_sec) { - if (tm_a.tv_sec > tm_b.tv_sec) { - return 1; - } - else { - return -1; - } + if (tm_a.tv_sec < tm_b.tv_sec) { + return -1; } - else { - if (tm_a.tv_nsec > tm_b.tv_nsec) { - return 1; - } - else if (tm_a.tv_nsec < tm_b.tv_nsec) { - return -1; - } + else if (tm_a.tv_sec > tm_b.tv_sec) { + return 1; } - return 0; + if (tm_a.tv_nsec < tm_b.tv_nsec) { + return -1; + } + else if (tm_a.tv_nsec > tm_b.tv_nsec) { + return 1; + } + + /* equal timestamps: deterministic lexical fallback */ + return strcmp(chunk_a->name, chunk_b->name); } static void print_storage_info(struct flb_config *ctx, struct cio_ctx *cio) @@ -733,7 +732,7 @@ int flb_storage_create(struct flb_config *ctx) } /* Sort chunks */ - cio_qsort(ctx->cio, sort_chunk_cmp); + cio_qsort(ctx->cio, flb_storage_chunk_cmp); /* * If we have a filesystem storage path, create an instance of the From 0451c38870e1a98b95154aa17f6e62ed3b636511 Mon Sep 17 00:00:00 2001 From: Dmitri Pavlov Date: Tue, 1 Dec 2026 16:05:45 +0200 Subject: [PATCH 2/2] tests: add recovered chunk ordering comparator test Signed-off-by: Dmitri Pavlov --- tests/internal/CMakeLists.txt | 1 + tests/internal/storage_sort.c | 87 +++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 tests/internal/storage_sort.c diff --git a/tests/internal/CMakeLists.txt b/tests/internal/CMakeLists.txt index 41d88f61da8..d184f1d6e15 100644 --- a/tests/internal/CMakeLists.txt +++ b/tests/internal/CMakeLists.txt @@ -70,6 +70,7 @@ set(UNIT_TESTS_FILES unicode.c opentelemetry.c storage_dlq.c + storage_sort.c engine_adaptive_flush.c engine_dispatch.c output_thread.c diff --git a/tests/internal/storage_sort.c b/tests/internal/storage_sort.c new file mode 100644 index 00000000000..bfcf2974a1f --- /dev/null +++ b/tests/internal/storage_sort.c @@ -0,0 +1,87 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +#include +#include +#include + +#include +#include + +#include + +#include "flb_tests_internal.h" + +static void check_first(const char *expected, const char *other) +{ + struct cio_chunk a; + struct cio_chunk b; + struct cio_chunk *arr[2]; + + memset(&a, 0, sizeof(a)); + memset(&b, 0, sizeof(b)); + + a.name = (char *) expected; + b.name = (char *) other; + arr[0] = &a; + arr[1] = &b; + + qsort(arr, 2, sizeof(struct cio_chunk *), flb_storage_chunk_cmp); + TEST_CHECK(strcmp(arr[0]->name, expected) == 0); +} + +/* nsec comparison must be numeric, not lexicographic */ +static void test_nsec_numeric() +{ + check_first("100-1700000100.9.flb", "100-1700000100.10.flb"); +} + +/* pooled names must sort by the timestamp suffix, not by slot number */ +static void test_slot_names() +{ + check_first("slot-000001-1700000100.0.flb", + "slot-000000-1700000200.0.flb"); + check_first("slot-000005-1600000000.0.flb", + "slot-000002-1700000000.0.flb"); + + /* pooled slot vs valid timestamp-bearing fallback name, both orders */ + check_first("12345-1700000100.0.flb", + "slot-000000-1700000200.0.flb"); + check_first("slot-000000-1700000100.0.flb", + "12345-1700000200.0.flb"); +} + +static void test_valid_before_invalid() +{ + check_first("100-1700000100.0.flb", "legacy.flb"); + /* trailing junk must not parse as a valid timestamp */ + check_first("100-1700000100.0.flb", "legacy-1.0junk.flb"); +} + +static void test_equal_timestamp() +{ + struct cio_chunk a; + struct cio_chunk b; + struct cio_chunk *arr[2]; + + memset(&a, 0, sizeof(a)); + memset(&b, 0, sizeof(b)); + + a.name = "slot-000000-1700000000.123.flb"; + b.name = "slot-000001-1700000000.123.flb"; + + /* equal timestamps fall back to a deterministic lexical order */ + arr[0] = &a; + arr[1] = &b; + TEST_CHECK(flb_storage_chunk_cmp(&arr[0], &arr[1]) < 0); + + arr[0] = &b; + arr[1] = &a; + TEST_CHECK(flb_storage_chunk_cmp(&arr[0], &arr[1]) > 0); +} + +TEST_LIST = { + { "storage_sort_nsec_numeric", test_nsec_numeric }, + { "storage_sort_slot_names", test_slot_names }, + { "storage_sort_valid_invalid", test_valid_before_invalid }, + { "storage_sort_equal_timestamp", test_equal_timestamp }, + { NULL, NULL } +};