Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/fluent-bit/flb_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
53 changes: 26 additions & 27 deletions src/flb_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
87 changes: 87 additions & 0 deletions tests/internal/storage_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_storage.h>

#include <chunkio/cio_chunk.h>

#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 }
};
Loading