From 3d778148abd507642ee3ab2175de55eac9cc4600 Mon Sep 17 00:00:00 2001 From: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> Date: Tue, 17 Oct 2023 21:10:05 +0000 Subject: [PATCH 1/5] in_dummy: Add time_interval setting. Signed-off-by: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> --- plugins/in_dummy/in_dummy.c | 15 +++++++++++---- plugins/in_dummy/in_dummy.h | 1 + 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/plugins/in_dummy/in_dummy.c b/plugins/in_dummy/in_dummy.c index 75d1b73333f..333fb3326ef 100644 --- a/plugins/in_dummy/in_dummy.c +++ b/plugins/in_dummy/in_dummy.c @@ -201,6 +201,7 @@ static int configure(struct flb_dummy *ctx, struct timespec *tm) { const char *msg; + double tmfrac; int root_type; int ret = -1; @@ -214,12 +215,13 @@ static int configure(struct flb_dummy *ctx, } /* interval settings */ - tm->tv_sec = 1; + tm->tv_sec = ctx->time_interval; tm->tv_nsec = 0; if (ctx->rate > 1) { - tm->tv_sec = 0; - tm->tv_nsec = 1000000000 / ctx->rate; + tmfrac = (double) ctx->time_interval / ctx->rate; + tm->tv_sec = tmfrac; + tm->tv_nsec = (tmfrac - (int) tmfrac) * 1000000000; } /* dummy timestamp */ @@ -398,7 +400,12 @@ static struct flb_config_map config_map[] = { { FLB_CONFIG_MAP_INT, "rate", "1", 0, FLB_TRUE, offsetof(struct flb_dummy, rate), - "set a number of events per second." + "set a number of events per time interval." + }, + { + FLB_CONFIG_MAP_INT, "time_interval", "1", + 0, FLB_TRUE, offsetof(struct flb_dummy, time_interval), + "set time interval to generate logs at the set rate." }, { FLB_CONFIG_MAP_INT, "copies", "1", diff --git a/plugins/in_dummy/in_dummy.h b/plugins/in_dummy/in_dummy.h index d351420cb27..fdf3b2de871 100644 --- a/plugins/in_dummy/in_dummy.h +++ b/plugins/in_dummy/in_dummy.h @@ -34,6 +34,7 @@ struct flb_dummy { int copies; int samples; int samples_count; + int time_interval; int dummy_timestamp_set; struct flb_time base_timestamp; From 108a459bb84f4dc03649c5d3733374d35d45c5fe Mon Sep 17 00:00:00 2001 From: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> Date: Wed, 18 Oct 2023 20:31:08 +0000 Subject: [PATCH 2/5] in_dummy: rename time interval variables. Signed-off-by: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> --- plugins/in_dummy/in_dummy.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/plugins/in_dummy/in_dummy.c b/plugins/in_dummy/in_dummy.c index 333fb3326ef..193cec4165f 100644 --- a/plugins/in_dummy/in_dummy.c +++ b/plugins/in_dummy/in_dummy.c @@ -201,7 +201,8 @@ static int configure(struct flb_dummy *ctx, struct timespec *tm) { const char *msg; - double tmfrac; + double tm_interval_fractional; + int tm_interval_seconds; int root_type; int ret = -1; @@ -219,9 +220,10 @@ static int configure(struct flb_dummy *ctx, tm->tv_nsec = 0; if (ctx->rate > 1) { - tmfrac = (double) ctx->time_interval / ctx->rate; - tm->tv_sec = tmfrac; - tm->tv_nsec = (tmfrac - (int) tmfrac) * 1000000000; + tm_interval_fractional = (double) ctx->time_interval / ctx->rate; + tm_interval_seconds = (int) tm_interval_fractional; + tm->tv_sec = tm_interval_seconds; + tm->tv_nsec = (tm_interval_fractional - tm_interval_seconds) * 1000000000; } /* dummy timestamp */ From e68e37ed2f759712df26247343d75e8f4fead584 Mon Sep 17 00:00:00 2001 From: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> Date: Thu, 19 Oct 2023 15:42:42 +0000 Subject: [PATCH 3/5] in_dummy: add unit tests for rate and time_interval. Signed-off-by: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> --- tests/runtime/in_simple_systems.c | 101 ++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 6 deletions(-) diff --git a/tests/runtime/in_simple_systems.c b/tests/runtime/in_simple_systems.c index 7020c4f483e..882ce30b313 100644 --- a/tests/runtime/in_simple_systems.c +++ b/tests/runtime/in_simple_systems.c @@ -294,6 +294,68 @@ void do_test_records_single(char *system, void (*records_cb)(struct callback_rec flb_destroy(ctx); } +void do_test_records_wait_time(char *system, int wait_time, void (*records_cb)(struct callback_records *), ...) +{ + flb_ctx_t *ctx = NULL; + int in_ffd; + int out_ffd; + va_list va; + char *key; + char *value; + int i; + struct flb_lib_out_cb cb; + struct callback_records *records; + + records = flb_calloc(1, sizeof(struct callback_records)); + records->num_records = 0; + records->records = NULL; + cb.cb = callback_add_record; + cb.data = (void *)records; + + /* initialize */ + set_result(0); + + ctx = flb_create(); + + in_ffd = flb_input(ctx, (char *) system, NULL); + TEST_CHECK(in_ffd >= 0); + TEST_CHECK(flb_input_set(ctx, in_ffd, "tag", "test", NULL) == 0); + + va_start(va, records_cb); + while ((key = va_arg(va, char *))) { + value = va_arg(va, char *); + TEST_CHECK(value != NULL); + TEST_CHECK(flb_input_set(ctx, in_ffd, key, value, NULL) == 0); + } + va_end(va); + + out_ffd = flb_output(ctx, (char *) "lib", &cb); + TEST_CHECK(out_ffd >= 0); + TEST_CHECK(flb_output_set(ctx, out_ffd, "match", "test", NULL) == 0); + + TEST_CHECK(flb_service_set(ctx, "Flush", "1", + "Grace", "1", + NULL) == 0); + + /* Start test */ + TEST_CHECK(flb_start(ctx) == 0); + + /* Set wait_time plus 4 sec passed. It must have flushed */ + sleep(wait_time + 4); + + records_cb(records); + + flb_stop(ctx); + + for (i = 0; i < records->num_records; i++) { + flb_lib_free(records->records[i].data); + } + flb_free(records->records); + flb_free(records); + + flb_destroy(ctx); +} + void flb_test_in_disk_flush() { do_test("disk", @@ -471,6 +533,21 @@ void flb_test_dummy_records_message_copies_100(struct callback_records *records) TEST_CHECK(records->num_records >= 100); } +void flb_test_dummy_records_message_time_interval_3_rate_1(struct callback_records *records) +{ + TEST_CHECK(records->num_records >= 1); +} + +void flb_test_dummy_records_message_time_interval_3_rate_10(struct callback_records *records) +{ + TEST_CHECK(records->num_records >= 10); +} + +void flb_test_dummy_records_message_time_interval_6_rate_100(struct callback_records *records) +{ + TEST_CHECK(records->num_records >= 100); +} + void flb_test_in_dummy_flush() { do_test("dummy", NULL); @@ -493,14 +570,26 @@ void flb_test_in_dummy_flush() "fixed_timestamp", "on", NULL); do_test_records_single("dummy", flb_test_dummy_records_message_copies_1, - "copies", "1", - NULL); + "copies", "1", + NULL); do_test_records_single("dummy", flb_test_dummy_records_message_copies_5, - "copies", "5", - NULL); + "copies", "5", + NULL); do_test_records_single("dummy", flb_test_dummy_records_message_copies_100, - "copies", "100", - NULL); + "copies", "100", + NULL); + do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_1, + "time_interval", "3", + "rate", "1", + NULL); + do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_10, + "time_interval", "3", + "rate", "10", + NULL); + do_test_records_wait_time("dummy", 6, flb_test_dummy_records_message_time_interval_6_rate_100, + "time_interval", "6", + "rate", "100", + NULL); } void flb_test_in_dummy_thread_flush() From c3d99a8411fc542dfbb9ac6e02aafabf8c682c8c Mon Sep 17 00:00:00 2001 From: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:35:32 +0000 Subject: [PATCH 4/5] in_dummy: reducing tests maximum duration. Signed-off-by: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> --- tests/runtime/in_simple_systems.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/tests/runtime/in_simple_systems.c b/tests/runtime/in_simple_systems.c index 882ce30b313..32099a6e15a 100644 --- a/tests/runtime/in_simple_systems.c +++ b/tests/runtime/in_simple_systems.c @@ -340,8 +340,8 @@ void do_test_records_wait_time(char *system, int wait_time, void (*records_cb)(s /* Start test */ TEST_CHECK(flb_start(ctx) == 0); - /* Set wait_time plus 4 sec passed. It must have flushed */ - sleep(wait_time + 4); + /* Set wait_time plus 2 sec passed. It must have flushed */ + sleep(wait_time + 2); records_cb(records); @@ -533,17 +533,12 @@ void flb_test_dummy_records_message_copies_100(struct callback_records *records) TEST_CHECK(records->num_records >= 100); } -void flb_test_dummy_records_message_time_interval_3_rate_1(struct callback_records *records) +void flb_test_dummy_records_message_time_interval_2_rate_5(struct callback_records *records) { - TEST_CHECK(records->num_records >= 1); -} - -void flb_test_dummy_records_message_time_interval_3_rate_10(struct callback_records *records) -{ - TEST_CHECK(records->num_records >= 10); + TEST_CHECK(records->num_records >= 5); } -void flb_test_dummy_records_message_time_interval_6_rate_100(struct callback_records *records) +void flb_test_dummy_records_message_time_interval_3_rate_100(struct callback_records *records) { TEST_CHECK(records->num_records >= 100); } @@ -578,16 +573,12 @@ void flb_test_in_dummy_flush() do_test_records_single("dummy", flb_test_dummy_records_message_copies_100, "copies", "100", NULL); - do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_1, - "time_interval", "3", - "rate", "1", + do_test_records_wait_time("dummy", 2, flb_test_dummy_records_message_time_interval_2_rate_5, + "time_interval", "2", + "rate", "5", NULL); - do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_10, + do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_100, "time_interval", "3", - "rate", "10", - NULL); - do_test_records_wait_time("dummy", 6, flb_test_dummy_records_message_time_interval_6_rate_100, - "time_interval", "6", "rate", "100", NULL); } From 7a89022766bba0bfaa5c739e6345c4e620b89109 Mon Sep 17 00:00:00 2001 From: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:54:20 +0000 Subject: [PATCH 5/5] in_dummy: Use interval_sec/nsec instead of time_interval. Signed-off-by: Francisco Valente <1435136+franciscovalentecastro@users.noreply.github.com> --- plugins/in_dummy/in_dummy.c | 41 +++++++++++++++++++++---------- plugins/in_dummy/in_dummy.h | 6 ++++- tests/runtime/in_simple_systems.c | 28 +++++++++++++-------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/plugins/in_dummy/in_dummy.c b/plugins/in_dummy/in_dummy.c index 193cec4165f..3894a410773 100644 --- a/plugins/in_dummy/in_dummy.c +++ b/plugins/in_dummy/in_dummy.c @@ -201,8 +201,6 @@ static int configure(struct flb_dummy *ctx, struct timespec *tm) { const char *msg; - double tm_interval_fractional; - int tm_interval_seconds; int root_type; int ret = -1; @@ -216,14 +214,26 @@ static int configure(struct flb_dummy *ctx, } /* interval settings */ - tm->tv_sec = ctx->time_interval; + if (ctx->interval_sec < 0 || ctx->interval_nsec < 0) { + /* Illegal settings. Override them. */ + ctx->interval_sec = atoi(DEFAULT_INTERVAL_SEC); + ctx->interval_nsec = atoi(DEFAULT_INTERVAL_NSEC); + } + + /* default settings */ + tm->tv_sec = 1; tm->tv_nsec = 0; - if (ctx->rate > 1) { - tm_interval_fractional = (double) ctx->time_interval / ctx->rate; - tm_interval_seconds = (int) tm_interval_fractional; - tm->tv_sec = tm_interval_seconds; - tm->tv_nsec = (tm_interval_fractional - tm_interval_seconds) * 1000000000; + if (ctx->interval_sec > 0 || ctx->interval_nsec > 0) { + /* Set using interval settings. */ + tm->tv_sec = ctx->interval_sec; + tm->tv_nsec = ctx->interval_nsec; + } else { + if (ctx->rate > 1) { + /* Set using rate settings. */ + tm->tv_sec = 0; + tm->tv_nsec = 1000000000 / ctx->rate; + } } /* dummy timestamp */ @@ -400,14 +410,19 @@ static struct flb_config_map config_map[] = { "set the sample metadata to be generated. It should be a JSON object." }, { - FLB_CONFIG_MAP_INT, "rate", "1", + FLB_CONFIG_MAP_INT, "rate", DEFAULT_RATE, 0, FLB_TRUE, offsetof(struct flb_dummy, rate), - "set a number of events per time interval." + "set a number of events per second." + }, + { + FLB_CONFIG_MAP_INT, "interval_sec", DEFAULT_INTERVAL_SEC, + 0, FLB_TRUE, offsetof(struct flb_dummy, interval_sec), + "set seconds of interval to generate events. overrides rate setting." }, { - FLB_CONFIG_MAP_INT, "time_interval", "1", - 0, FLB_TRUE, offsetof(struct flb_dummy, time_interval), - "set time interval to generate logs at the set rate." + FLB_CONFIG_MAP_INT, "interval_nsec", DEFAULT_INTERVAL_NSEC, + 0, FLB_TRUE, offsetof(struct flb_dummy, interval_nsec), + "set nanoseconds of interval to generate events. overrides rate setting." }, { FLB_CONFIG_MAP_INT, "copies", "1", diff --git a/plugins/in_dummy/in_dummy.h b/plugins/in_dummy/in_dummy.h index fdf3b2de871..da6c578e743 100644 --- a/plugins/in_dummy/in_dummy.h +++ b/plugins/in_dummy/in_dummy.h @@ -26,6 +26,9 @@ #define DEFAULT_DUMMY_MESSAGE "{\"message\":\"dummy\"}" #define DEFAULT_DUMMY_METADATA "{}" +#define DEFAULT_RATE "1" +#define DEFAULT_INTERVAL_SEC "0" +#define DEFAULT_INTERVAL_NSEC "0" struct flb_dummy { int coll_fd; @@ -34,7 +37,8 @@ struct flb_dummy { int copies; int samples; int samples_count; - int time_interval; + int interval_sec; + int interval_nsec; int dummy_timestamp_set; struct flb_time base_timestamp; diff --git a/tests/runtime/in_simple_systems.c b/tests/runtime/in_simple_systems.c index 32099a6e15a..9f1a9d2e49f 100644 --- a/tests/runtime/in_simple_systems.c +++ b/tests/runtime/in_simple_systems.c @@ -533,14 +533,19 @@ void flb_test_dummy_records_message_copies_100(struct callback_records *records) TEST_CHECK(records->num_records >= 100); } -void flb_test_dummy_records_message_time_interval_2_rate_5(struct callback_records *records) +void flb_test_dummy_records_message_rate(struct callback_records *records) { - TEST_CHECK(records->num_records >= 5); + TEST_CHECK(records->num_records >= 20); } -void flb_test_dummy_records_message_time_interval_3_rate_100(struct callback_records *records) +void flb_test_dummy_records_message_interval_sec(struct callback_records *records) { - TEST_CHECK(records->num_records >= 100); + TEST_CHECK(records->num_records >= 1); +} + +void flb_test_dummy_records_message_interval_nsec(struct callback_records *records) +{ + TEST_CHECK(records->num_records >= 1); } void flb_test_in_dummy_flush() @@ -573,13 +578,16 @@ void flb_test_in_dummy_flush() do_test_records_single("dummy", flb_test_dummy_records_message_copies_100, "copies", "100", NULL); - do_test_records_wait_time("dummy", 2, flb_test_dummy_records_message_time_interval_2_rate_5, - "time_interval", "2", - "rate", "5", + do_test_records_wait_time("dummy", 1, flb_test_dummy_records_message_rate, + "rate", "20", + NULL); + do_test_records_wait_time("dummy", 2, flb_test_dummy_records_message_interval_sec, + "interval_sec", "2", + "interval_nsec", "0", NULL); - do_test_records_wait_time("dummy", 3, flb_test_dummy_records_message_time_interval_3_rate_100, - "time_interval", "3", - "rate", "100", + do_test_records_wait_time("dummy", 1, flb_test_dummy_records_message_interval_nsec, + "interval_sec", "0", + "interval_nsec", "700000000", NULL); }