forked from axoflow/axosyslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogthrdestdrv.c
1508 lines (1253 loc) · 47.6 KB
/
logthrdestdrv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2013, 2014 Balabit
* Copyright (c) 2013, 2014 Gergely Nagy <algernon@balabit.hu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "stats/stats-cluster-logpipe.h"
#include "stats/stats-cluster-single.h"
#include "stats/aggregator/stats-aggregator-registry.h"
#include "logthrdestdrv.h"
#include "seqnum.h"
#include "scratch-buffers.h"
#include "template/eval.h"
#include "mainloop-threaded-worker.h"
#include <string.h>
#define MAX_RETRIES_ON_ERROR_DEFAULT 3
#define MAX_RETRIES_BEFORE_SUSPEND_DEFAULT 3
const gchar *
log_threaded_result_to_str(LogThreadedResult self)
{
g_assert(self <= LTR_MAX);
static const gchar *as_str[] = { "DROP",
"ERROR",
"EXPLICIT_ACK_MGMT",
"SUCCESS",
"QUEUED",
"NOT_CONNECTED",
"RETRY",
"MAX"
};
return as_str[self];
}
void
log_threaded_dest_driver_set_batch_lines(LogDriver *s, gint batch_lines)
{
LogThreadedDestDriver *self = (LogThreadedDestDriver *) s;
self->batch_lines = batch_lines;
}
void
log_threaded_dest_driver_set_batch_timeout(LogDriver *s, gint batch_timeout)
{
LogThreadedDestDriver *self = (LogThreadedDestDriver *) s;
self->batch_timeout = batch_timeout;
}
void
log_threaded_dest_driver_set_time_reopen(LogDriver *s, time_t time_reopen)
{
LogThreadedDestDriver *self = (LogThreadedDestDriver *) s;
self->time_reopen = time_reopen;
}
CfgFlagHandler log_threaded_dest_driver_flag_handlers[] =
{
/* seqnum-all turns on seqnums */
{ "seqnum-all", CFH_SET, offsetof(LogThreadedDestDriver, flags), LTDF_SEQNUM_ALL + LTDF_SEQNUM },
{ "no-seqnum-all", CFH_CLEAR, offsetof(LogThreadedDestDriver, flags), LTDF_SEQNUM_ALL },
{ "seqnum", CFH_SET, offsetof(LogThreadedDestDriver, flags), LTDF_SEQNUM },
{ "no-seqnum", CFH_CLEAR, offsetof(LogThreadedDestDriver, flags), LTDF_SEQNUM },
{ NULL },
};
gboolean
log_threaded_dest_driver_process_flag(LogDriver *s, const gchar *flag)
{
LogThreadedDestDriver *self = (LogThreadedDestDriver *) s;
return cfg_process_flag(log_threaded_dest_driver_flag_handlers, self, flag);
}
/* LogThreadedDestWorker */
/* this should be used in combination with LTR_EXPLICIT_ACK_MGMT to actually confirm message delivery. */
void
log_threaded_dest_worker_ack_messages(LogThreadedDestWorker *self, gint batch_size)
{
log_queue_ack_backlog(self->queue, batch_size);
stats_counter_add(self->owner->metrics.written_messages, batch_size);
self->retries_on_error_counter = 0;
self->batch_size -= batch_size;
}
void
log_threaded_dest_worker_drop_messages(LogThreadedDestWorker *self, gint batch_size)
{
log_queue_ack_backlog(self->queue, batch_size);
stats_counter_add(self->owner->metrics.dropped_messages, batch_size);
self->retries_on_error_counter = 0;
self->batch_size -= batch_size;
}
void
log_threaded_dest_worker_rewind_messages(LogThreadedDestWorker *self, gint batch_size)
{
log_queue_rewind_backlog(self->queue, batch_size);
self->rewound_batch_size = self->batch_size;
self->batch_size -= batch_size;
}
static gchar *
_format_queue_persist_name(LogThreadedDestWorker *self)
{
LogPipe *owner = &self->owner->super.super.super;
if (self->worker_index == 0)
{
/* the first worker uses the legacy persist name, e.g. to be able to
* recover the queue previously used. */
return g_strdup(log_pipe_get_persist_name(owner));
}
else
{
return g_strdup_printf("%s.%d.queue",
log_pipe_get_persist_name(owner),
self->worker_index);
}
}
static gboolean
_should_flush_now(LogThreadedDestWorker *self)
{
struct timespec now;
glong diff;
if (self->owner->batch_timeout <= 0 ||
self->owner->batch_lines <= 1 ||
!self->enable_batching)
return TRUE;
iv_validate_now();
now = iv_now;
diff = timespec_diff_msec(&now, &self->last_flush_time);
return (diff >= self->owner->batch_timeout);
}
static void
_stop_watches(LogThreadedDestWorker *self)
{
if (iv_task_registered(&self->do_work))
{
iv_task_unregister(&self->do_work);
}
if (iv_timer_registered(&self->timer_reopen))
{
iv_timer_unregister(&self->timer_reopen);
}
if (iv_timer_registered(&self->timer_throttle))
{
iv_timer_unregister(&self->timer_throttle);
}
if (iv_timer_registered(&self->timer_flush))
{
iv_timer_unregister(&self->timer_flush);
}
}
/* NOTE: runs in the worker thread in response to a wakeup event being
* posted, which happens if a new element is added to our queue while we
* were sleeping */
static void
_wakeup_event_callback(gpointer data)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) data;
if (!iv_task_registered(&self->do_work))
{
iv_task_register(&self->do_work);
}
}
/* NOTE: runs in the worker thread in response to the shutdown event being
* posted. The shutdown event is initiated by the mainloop when the
* configuration is deinited */
static void
_shutdown_event_callback(gpointer data)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) data;
log_queue_reset_parallel_push(self->queue);
_stop_watches(self);
iv_quit();
}
/* NOTE: runs in the worker thread */
static void
_suspend(LogThreadedDestWorker *self)
{
self->suspended = TRUE;
}
/* NOTE: runs in the worker thread */
static void
_connect(LogThreadedDestWorker *self)
{
if (!log_threaded_dest_worker_connect(self))
{
msg_debug("Error establishing connection to server",
evt_tag_str("driver", self->owner->super.super.id),
evt_tag_int("worker_index", self->worker_index),
log_expr_node_location_tag(self->owner->super.super.super.expr_node));
_suspend(self);
}
}
/* NOTE: runs in the worker thread */
static void
_disconnect(LogThreadedDestWorker *self)
{
log_threaded_dest_worker_disconnect(self);
}
/* NOTE: runs in the worker thread */
static void
_disconnect_and_suspend(LogThreadedDestWorker *self)
{
_disconnect(self);
_suspend(self);
}
/* Accepts the current batch including the current message by acking it back
* to the source.
*
* NOTE: runs in the worker thread */
static void
_accept_batch(LogThreadedDestWorker *self)
{
log_threaded_dest_worker_ack_messages(self, self->batch_size);
}
/* NOTE: runs in the worker thread */
static void
_drop_batch(LogThreadedDestWorker *self)
{
log_threaded_dest_worker_drop_messages(self, self->batch_size);
}
/* NOTE: runs in the worker thread */
static void
_rewind_batch(LogThreadedDestWorker *self)
{
stats_counter_inc(self->owner->metrics.output_event_retries);
log_threaded_dest_worker_rewind_messages(self, self->batch_size);
}
static void
_process_result_drop(LogThreadedDestWorker *self)
{
msg_error("Message(s) dropped while sending message to destination",
evt_tag_str("driver", self->owner->super.super.id),
evt_tag_int("worker_index", self->worker_index),
evt_tag_int("time_reopen", self->time_reopen),
evt_tag_int("batch_size", self->batch_size));
_drop_batch(self);
_disconnect_and_suspend(self);
}
static void
_process_result_error(LogThreadedDestWorker *self)
{
self->retries_on_error_counter++;
if (self->retries_on_error_counter >= self->owner->retries_on_error_max)
{
msg_error("Multiple failures while sending message(s) to destination, message(s) dropped",
evt_tag_str("driver", self->owner->super.super.id),
log_expr_node_location_tag(self->owner->super.super.super.expr_node),
evt_tag_int("worker_index", self->worker_index),
evt_tag_int("retries", self->retries_on_error_counter),
evt_tag_int("batch_size", self->batch_size));
_drop_batch(self);
}
else
{
msg_error("Error occurred while trying to send a message, trying again",
evt_tag_str("driver", self->owner->super.super.id),
log_expr_node_location_tag(self->owner->super.super.super.expr_node),
evt_tag_int("worker_index", self->worker_index),
evt_tag_int("retries", self->retries_on_error_counter),
evt_tag_int("time_reopen", self->time_reopen),
evt_tag_int("batch_size", self->batch_size));
_rewind_batch(self);
_disconnect_and_suspend(self);
}
}
static void
_process_result_not_connected(LogThreadedDestWorker *self)
{
msg_info("Server disconnected while preparing messages for sending, trying again",
evt_tag_str("driver", self->owner->super.super.id),
log_expr_node_location_tag(self->owner->super.super.super.expr_node),
evt_tag_int("worker_index", self->worker_index),
evt_tag_int("time_reopen", self->time_reopen),
evt_tag_int("batch_size", self->batch_size));
self->retries_counter = 0;
_rewind_batch(self);
_disconnect_and_suspend(self);
}
static void
_process_result_success(LogThreadedDestWorker *self)
{
_accept_batch(self);
}
static void
_process_result_queued(LogThreadedDestWorker *self)
{
self->enable_batching = TRUE;
}
static void
_process_result_retry(LogThreadedDestWorker *self)
{
self->retries_counter++;
if (self->retries_counter >= self->owner->retries_max)
_process_result_not_connected(self);
else
_rewind_batch(self);
}
static void
_process_result(LogThreadedDestWorker *self, gint result)
{
switch (result)
{
case LTR_DROP:
_process_result_drop(self);
break;
case LTR_ERROR:
_process_result_error(self);
break;
case LTR_NOT_CONNECTED:
_process_result_not_connected(self);
break;
case LTR_EXPLICIT_ACK_MGMT:
/* we require the instance to use explicit calls to ack_messages/rewind_messages */
break;
case LTR_SUCCESS:
_process_result_success(self);
break;
case LTR_QUEUED:
_process_result_queued(self);
break;
case LTR_RETRY:
_process_result_retry(self);
break;
default:
break;
}
}
static LogThreadedResult
_perform_flush(LogThreadedDestWorker *self)
{
LogThreadedResult result = LTR_SUCCESS;
/* NOTE: earlier we had a condition on only calling flush() if batch_size
* is non-zero. This was removed, as the language bindings that were done
* _before_ the batching support in LogThreadedDestDriver relies on
* flush() being called always, even if LTR_SUCCESS is
* returned, in which case batch_size is already zero at this point.
*/
if (!self->suspended)
{
msg_trace("Flushing batch",
evt_tag_str("driver", self->owner->super.super.id),
evt_tag_int("worker_index", self->worker_index),
evt_tag_int("batch_size", self->batch_size));
result = log_threaded_dest_worker_flush(self, LTF_FLUSH_NORMAL);
_process_result(self, result);
}
iv_invalidate_now();
return result;
}
static inline gboolean
_flush_on_worker_partition_key_change_enabled(LogThreadedDestWorker *self)
{
return self->owner->flush_on_key_change && self->owner->worker_partition_key;
}
static inline gboolean
_should_flush_due_to_partition_key_change(LogThreadedDestWorker *self, LogMessage *msg)
{
GString *buffer = scratch_buffers_alloc();
LogTemplateEvalOptions options = DEFAULT_TEMPLATE_EVAL_OPTIONS;
log_template_format(self->owner->worker_partition_key, msg, &options, buffer);
gboolean should_flush = self->batch_size != 0 && strcmp(self->partitioning.last_key->str, buffer->str) != 0;
g_string_assign(self->partitioning.last_key, buffer->str);
return should_flush;
}
/* NOTE: runs in the worker thread, whenever items on our queue are
* available. It iterates all elements on the queue, however will terminate
* if the mainloop requests that we exit. */
static void
_perform_inserts(LogThreadedDestWorker *self)
{
LogThreadedResult result;
LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
if (self->batch_size == 0)
{
/* first message in the batch sets the last_flush_time, so we
* won't expedite the flush even if the previous one was a long
* time ago */
iv_validate_now();
self->last_flush_time = iv_now;
}
while (G_LIKELY(!self->owner->under_termination) && !self->suspended)
{
ScratchBuffersMarker mark;
scratch_buffers_mark(&mark);
if (G_UNLIKELY(_flush_on_worker_partition_key_change_enabled(self)))
{
LogMessage *msg = log_queue_peek_head(self->queue);
if (!msg)
{
scratch_buffers_reclaim_marked(mark);
break;
}
if (_should_flush_due_to_partition_key_change(self, msg))
{
gboolean flush_result = _perform_flush(self);
if (flush_result != LTR_SUCCESS && flush_result != LTR_EXPLICIT_ACK_MGMT)
goto flush_error;
}
}
LogMessage *msg = log_queue_pop_head(self->queue, &path_options);
if (!msg)
{
scratch_buffers_reclaim_marked(mark);
break;
}
msg_set_context(msg);
log_msg_refcache_start_consumer(msg, &path_options);
self->batch_size++;
result = log_threaded_dest_worker_insert(self, msg);
_process_result(self, result);
if (self->enable_batching && self->batch_size >= self->owner->batch_lines)
_perform_flush(self);
log_msg_unref(msg);
msg_set_context(NULL);
log_msg_refcache_stop();
flush_error:
scratch_buffers_reclaim_marked(mark);
if (self->rewound_batch_size)
{
self->rewound_batch_size--;
if (self->rewound_batch_size == 0)
break;
}
iv_invalidate_now();
}
self->rewound_batch_size = 0;
}
/* this callback is invoked by LogQueue and is registered using
* log_queue_check_items(). This only gets registered if at that point
* we've decided to wait for the queue, e.g. the work_task is not running.
*
* This callback is invoked from the source thread, e.g. it is not safe to
* do anything, but ensure that our thread is woken up in response.
*/
static void
_message_became_available_callback(gpointer user_data)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) user_data;
if (!self->owner->under_termination)
iv_event_post(&self->wake_up_event);
}
static void
_schedule_restart_on_suspend_timeout(LogThreadedDestWorker *self)
{
iv_validate_now();
self->timer_reopen.expires = iv_now;
self->timer_reopen.expires.tv_sec += self->time_reopen;
iv_timer_register(&self->timer_reopen);
}
static void
_schedule_restart_on_batch_timeout(LogThreadedDestWorker *self)
{
self->timer_flush.expires = self->last_flush_time;
timespec_add_msec(&self->timer_flush.expires, self->owner->batch_timeout);
iv_timer_register(&self->timer_flush);
}
static void
_schedule_restart(LogThreadedDestWorker *self)
{
if (self->suspended)
_schedule_restart_on_suspend_timeout(self);
else
iv_task_register(&self->do_work);
}
static void
_schedule_restart_on_next_flush(LogThreadedDestWorker *self)
{
if (self->suspended)
_schedule_restart_on_suspend_timeout(self);
else if (!_should_flush_now(self))
_schedule_restart_on_batch_timeout(self);
else
iv_task_register(&self->do_work);
}
static void
_schedule_restart_on_throttle_timeout(LogThreadedDestWorker *self, gint timeout_msec)
{
iv_validate_now();
self->timer_throttle.expires = iv_now;
timespec_add_msec(&self->timer_throttle.expires, timeout_msec);
iv_timer_register(&self->timer_throttle);
}
static void
_perform_work(gpointer data)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) data;
gint timeout_msec = 0;
self->suspended = FALSE;
main_loop_worker_run_gc();
_stop_watches(self);
if (!self->connected)
{
/* try to connect and come back if successful, would be suspended otherwise. */
_connect(self);
_schedule_restart(self);
}
else if (log_queue_check_items(self->queue, &timeout_msec,
_message_became_available_callback,
self, NULL))
{
msg_trace("Message(s) available in queue, starting inserts",
evt_tag_str("driver", self->owner->super.super.id),
evt_tag_int("worker_index", self->worker_index));
/* Something is in the queue, buffer them up and flush (if needed) */
_perform_inserts(self);
if (_should_flush_now(self))
_perform_flush(self);
_schedule_restart(self);
}
else if (self->batch_size > 0)
{
/* nothing in the queue, but there are pending elements in the buffer
* (e.g. batch size != 0). perform a round of flushing. We might
* get back here, as the flush() routine doesn't have to flush
* everything. We are awoken either by the
* _message_became_available_callback() or if the next flush time has
* arrived. */
gboolean should_flush = _should_flush_now(self);
msg_trace("Queue empty, flushing previously buffered data if needed",
evt_tag_str("should_flush", should_flush ? "YES" : "NO"),
evt_tag_str("driver", self->owner->super.super.id),
evt_tag_int("worker_index", self->worker_index),
evt_tag_int("batch_size", self->batch_size));
if (should_flush)
_perform_flush(self);
_schedule_restart_on_next_flush(self);
}
else if (timeout_msec != 0)
{
/* We probably have some items in the queue, but timeout_msec is set,
* indicating a throttle being active.
* _message_became_available_callback() is not set up in this case.
* we need to wake up after timeout_msec time.
*
* We are processing throttle delays _after_ we finished flushing, as
* items in the queue were already accepted by throttling, so they can
* be flushed.
*/
msg_trace("Delaying output due to throttling",
evt_tag_int("timeout_msec", timeout_msec),
evt_tag_str("driver", self->owner->super.super.id),
evt_tag_int("worker_index", self->worker_index));
_schedule_restart_on_throttle_timeout(self, timeout_msec);
}
else
{
/* NOTE: at this point we are not doing anything but keep the
* parallel_push callback alive, which will call
* _message_became_available_callback(), which in turn wakes us up by
* posting an event which causes this function to be run again
*
* NOTE/2: the parallel_push callback may need to be cancelled if we
* need to exit. That happens in the shutdown_event_callback(), or
* here in this very function, as log_queue_check_items() will cancel
* outstanding parallel push callbacks automatically.
*/
}
}
void
log_threaded_dest_worker_wakeup_when_suspended(LogThreadedDestWorker *self)
{
if (self->suspended)
_perform_work(self);
}
static void
_flush_timer_cb(gpointer data)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) data;
msg_trace("Flush timer expired",
evt_tag_str("driver", self->owner->super.super.id),
evt_tag_int("worker_index", self->worker_index),
evt_tag_int("batch_size", self->batch_size));
_perform_work(data);
}
/* these are events of the _worker_ thread and are not registered to the
* actual main thread. We basically run our workload in the handler of the
* do_work task, which might be invoked in a number of ways.
*
* Basic states:
* 1) disconnected state: _perform_work() will try to connect periodically
* using the suspend() mechanism, which uses a timer to get up periodically.
*
* 2) once connected:
* - if messages are already on the queue: flush them
*
* - if no messages are on the queue: schedule
* _message_became_available_callback() to be called by the LogQueue.
*
* - if there's an error, disconnect go back to the #1 state above.
*
*/
static void
_init_watches(LogThreadedDestWorker *self)
{
IV_EVENT_INIT(&self->wake_up_event);
self->wake_up_event.cookie = self;
self->wake_up_event.handler = _wakeup_event_callback;
IV_EVENT_INIT(&self->shutdown_event);
self->shutdown_event.cookie = self;
self->shutdown_event.handler = _shutdown_event_callback;
IV_TIMER_INIT(&self->timer_reopen);
self->timer_reopen.cookie = self;
self->timer_reopen.handler = _perform_work;
IV_TIMER_INIT(&self->timer_throttle);
self->timer_throttle.cookie = self;
self->timer_throttle.handler = _perform_work;
IV_TIMER_INIT(&self->timer_flush);
self->timer_flush.cookie = self;
self->timer_flush.handler = _flush_timer_cb;
IV_TASK_INIT(&self->do_work);
self->do_work.cookie = self;
self->do_work.handler = _perform_work;
}
static void
_perform_final_flush(LogThreadedDestWorker *self)
{
GlobalConfig *cfg = log_pipe_get_config(&self->owner->super.super.super);
LogThreadedResult result;
LogThreadedFlushMode mode = LTF_FLUSH_NORMAL;
if (!cfg_is_shutting_down(cfg))
mode = LTF_FLUSH_EXPEDITE;
result = log_threaded_dest_worker_flush(self, mode);
_process_result(self, result);
log_queue_rewind_backlog_all(self->queue);
}
static gboolean
_worker_thread_init(MainLoopThreadedWorker *s)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) s->data;
iv_event_register(&self->wake_up_event);
iv_event_register(&self->shutdown_event);
return log_threaded_dest_worker_init(self);
}
static void
_worker_thread_deinit(MainLoopThreadedWorker *s)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) s->data;
log_threaded_dest_worker_deinit(self);
iv_event_unregister(&self->wake_up_event);
iv_event_unregister(&self->shutdown_event);
}
static void
_worker_thread(MainLoopThreadedWorker *s)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) s->data;
msg_debug("Dedicated worker thread started",
evt_tag_int("worker_index", self->worker_index),
evt_tag_str("driver", self->owner->super.super.id),
log_expr_node_location_tag(self->owner->super.super.super.expr_node));
/* if we have anything on the backlog, that was a partial, potentially
* not-flushed batch. Rewind it, so we start with that */
log_queue_rewind_backlog_all(self->queue);
_schedule_restart(self);
iv_main();
_perform_final_flush(self);
_disconnect(self);
msg_debug("Dedicated worker thread finished",
evt_tag_int("worker_index", self->worker_index),
evt_tag_str("driver", self->owner->super.super.id),
log_expr_node_location_tag(self->owner->super.super.super.expr_node));
}
static void
_request_worker_exit(MainLoopThreadedWorker *s)
{
LogThreadedDestWorker *self = (LogThreadedDestWorker *) s->data;
msg_debug("Shutting down dedicated worker thread",
evt_tag_int("worker_index", self->worker_index),
evt_tag_str("driver", self->owner->super.super.id),
log_expr_node_location_tag(self->owner->super.super.super.expr_node));
self->owner->under_termination = TRUE;
iv_event_post(&self->shutdown_event);
}
static gboolean
log_threaded_dest_worker_start(LogThreadedDestWorker *self)
{
msg_debug("Starting dedicated worker thread",
evt_tag_int("worker_index", self->worker_index),
evt_tag_str("driver", self->owner->super.super.id),
log_expr_node_location_tag(self->owner->super.super.super.expr_node));
return main_loop_threaded_worker_start(&self->thread);
}
static void
_format_stats_key(LogThreadedDestDriver *self, StatsClusterKeyBuilder *kb)
{
self->format_stats_key(self, kb);
}
static const gchar *
_format_legacy_stats_instance(LogThreadedDestDriver *self, StatsClusterKeyBuilder *kb)
{
const gchar *legacy_stats_instance = self->format_stats_key(self, kb);
if (legacy_stats_instance)
return legacy_stats_instance;
static gchar stats_instance[1024];
stats_cluster_key_builder_format_legacy_stats_instance(kb, stats_instance, sizeof(stats_instance));
return stats_instance;
}
static void
_init_worker_sck_builder(LogThreadedDestWorker *self, StatsClusterKeyBuilder *builder)
{
stats_cluster_key_builder_add_label(builder, stats_cluster_label("id", self->owner->super.super.id ? : ""));
_format_stats_key(self->owner, builder);
gchar worker_index_str[8];
g_snprintf(worker_index_str, sizeof(worker_index_str), "%d", self->worker_index);
stats_cluster_key_builder_add_label(builder, stats_cluster_label("worker", worker_index_str));
}
static gboolean
_acquire_worker_queue(LogThreadedDestWorker *self, gint stats_level, StatsClusterKeyBuilder *driver_sck_builder)
{
gchar *persist_name = _format_queue_persist_name(self);
StatsClusterKeyBuilder *queue_sck_builder = stats_cluster_key_builder_new();
_init_worker_sck_builder(self, queue_sck_builder);
self->queue = log_dest_driver_acquire_queue(&self->owner->super, persist_name, stats_level, driver_sck_builder,
queue_sck_builder);
stats_cluster_key_builder_free(queue_sck_builder);
g_free(persist_name);
if (!self->queue)
return FALSE;
return TRUE;
}
static void
_register_worker_stats(LogThreadedDestWorker *self)
{
gint level = log_pipe_is_internal(&self->owner->super.super.super) ? STATS_LEVEL3 : STATS_LEVEL1;
StatsClusterKeyBuilder *kb = stats_cluster_key_builder_new();
stats_cluster_key_builder_push(kb);
{
stats_cluster_key_builder_add_label(kb, stats_cluster_label("id", self->owner->super.super.id ? : ""));
_format_stats_key(self->owner, kb);
stats_cluster_key_builder_set_name(kb, "output_event_bytes_total");
self->metrics.output_event_bytes_sc_key = stats_cluster_key_builder_build_single(kb);
stats_byte_counter_init(&self->metrics.written_bytes, self->metrics.output_event_bytes_sc_key, level, SBCP_KIB);
}
stats_cluster_key_builder_pop(kb);
stats_cluster_key_builder_push(kb);
{
_init_worker_sck_builder(self, kb);
stats_lock();
{
stats_cluster_key_builder_set_name(kb, "output_unreachable");
self->metrics.output_unreachable_key = stats_cluster_key_builder_build_single(kb);
stats_register_counter(level, self->metrics.output_unreachable_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.output_unreachable);
/* Up to 49 days and 17 hours on 32 bit machines. */
stats_cluster_key_builder_set_name(kb, "output_event_delay_sample_seconds");
stats_cluster_key_builder_set_unit(kb, SCU_MILLISECONDS);
self->metrics.message_delay_sample_key = stats_cluster_key_builder_build_single(kb);
stats_register_counter(level, self->metrics.message_delay_sample_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.message_delay_sample);
stats_cluster_key_builder_set_name(kb, "output_event_delay_sample_age_seconds");
stats_cluster_key_builder_set_unit(kb, SCU_SECONDS);
stats_cluster_key_builder_set_frame_of_reference(kb, SCFOR_RELATIVE_TO_TIME_OF_QUERY);
self->metrics.message_delay_sample_age_key = stats_cluster_key_builder_build_single(kb);
stats_register_counter(level, self->metrics.message_delay_sample_age_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.message_delay_sample_age);
}
stats_unlock();
}
stats_cluster_key_builder_pop(kb);
UnixTime now;
unix_time_set_now(&now);
stats_counter_set_time(self->metrics.message_delay_sample_age, now.ut_sec);
self->metrics.last_delay_update = now.ut_sec;
stats_cluster_key_builder_free(kb);
}
static void
_unregister_worker_stats(LogThreadedDestWorker *self)
{
if (self->metrics.output_event_bytes_sc_key)
{
stats_byte_counter_deinit(&self->metrics.written_bytes, self->metrics.output_event_bytes_sc_key);
stats_cluster_key_free(self->metrics.output_event_bytes_sc_key);
self->metrics.output_event_bytes_sc_key = NULL;
}
stats_lock();
{
if (self->metrics.output_unreachable_key)
{
stats_unregister_counter(self->metrics.output_unreachable_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.output_unreachable);
stats_cluster_key_free(self->metrics.output_unreachable_key);
self->metrics.output_unreachable_key = NULL;
}
if (self->metrics.message_delay_sample_key)
{
stats_unregister_counter(self->metrics.message_delay_sample_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.message_delay_sample);
stats_cluster_key_free(self->metrics.message_delay_sample_key);
self->metrics.message_delay_sample_key = NULL;
}
if (self->metrics.message_delay_sample_age_key)
{
stats_unregister_counter(self->metrics.message_delay_sample_age_key, SC_TYPE_SINGLE_VALUE,
&self->metrics.message_delay_sample_age);
stats_cluster_key_free(self->metrics.message_delay_sample_age_key);
self->metrics.message_delay_sample_age_key = NULL;
}
}
stats_unlock();
}
gboolean
log_threaded_dest_worker_init_method(LogThreadedDestWorker *self)
{
if (self->time_reopen == -1)
self->time_reopen = self->owner->time_reopen;
if (self->owner->flush_on_key_change)
self->partitioning.last_key = g_string_sized_new(128);
return TRUE;
}
void
log_threaded_dest_worker_deinit_method(LogThreadedDestWorker *self)
{
if (self->partitioning.last_key)
g_string_free(self->partitioning.last_key, TRUE);
}
void
log_threaded_dest_worker_free_method(LogThreadedDestWorker *self)
{
_unregister_worker_stats(self);
main_loop_threaded_worker_clear(&self->thread);
}
void
log_threaded_dest_worker_init_instance(LogThreadedDestWorker *self, LogThreadedDestDriver *owner, gint worker_index)
{
main_loop_threaded_worker_init(&self->thread, MLW_THREADED_OUTPUT_WORKER, self);
self->thread.thread_init = _worker_thread_init;
self->thread.thread_deinit = _worker_thread_deinit;
self->thread.run = _worker_thread;
self->thread.request_exit = _request_worker_exit;
self->worker_index = worker_index;
self->init = log_threaded_dest_worker_init_method;
self->deinit = log_threaded_dest_worker_deinit_method;
self->free_fn = log_threaded_dest_worker_free_method;
self->owner = owner;
self->time_reopen = -1;
self->partitioning.last_key = NULL;