1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Fluent Bit
4  *  ==========
5  *  Copyright (C) 2019-2021 The Fluent Bit Authors
6  *  Copyright (C) 2015-2018 Treasure Data Inc.
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 #ifndef FLB_OUTPUT_THREAD_H
22 #define FLB_OUTPUT_THREAD_H
23 
24 #include <fluent-bit/flb_info.h>
25 #include <fluent-bit/flb_upstream.h>
26 #include <fluent-bit/flb_upstream_queue.h>
27 
28 /*
29  * For every 'upstream' registered in the output plugin initialization, we create
30  * a local entry so we can manage the connections queues locally, on this way we
31  * avoid sharing a single list with the other threads.
32  */
33 struct flb_out_thread_upstream {
34     /* output instance upstream connection context */
35     struct flb_upstream *u;
36 
37     /*
38      * Local implementation of upstream queues: same as in the co-routines case, we
39      * implement our own lists of upstream connections so they can be only reused inside
40      * the same thread.
41      *
42      * The flb_upstream_queue structure have the following queues:
43      *
44      * - av_queue: connections in a persistent state (keepalive) ready to be
45      *             used.
46      *
47      * - busy_queue: connections doing I/O, being used by a co-routine.
48      *
49      * - destroy_queue: connections that cannot be longer used, the connections linked
50      *                  to this list will be destroyed in the event loop once there is
51      *                  no pending events associated.
52      *
53      * note: in single-thread mode, the same fields are in 'struct flb_upstream'
54      */
55     struct flb_upstream_queue queue;
56 
57     /* Link to struct flb_out_thread_instance->upstreams */
58     struct mk_list _head;
59 };
60 
61 struct flb_out_thread_instance {
62     struct mk_event event;               /* event context to associate events */
63     struct mk_event_loop *evl;           /* thread event loop context */
64     flb_pipefd_t ch_parent_events[2];    /* channel to receive parent notifications */
65     flb_pipefd_t ch_thread_events[2];    /* channel to send messages local event loop */
66     struct flb_output_instance *ins;     /* output plugin instance */
67     struct flb_config *config;
68     struct flb_tp_thread *th;
69     struct mk_list _head;
70 
71     /*
72      * In multithread mode, we move some contexts to independent references per thread
73      * so we can avoid to have shared resources and mutexes.
74      *
75      * The following 'coro' fields maintains a state of co-routines inside the thread
76      * event loop.
77      *
78      * note: in single-thread mode, the same fields are in 'struct flb_output_instance'.
79      */
80     int coro_id;                         /* coroutine id counter */
81     struct mk_list coros;                /* list of co-routines */
82     struct mk_list coros_destroy;        /* list of co-routines */
83 
84     /*
85      * If the main engine (parent thread) needs to query the number of active
86      * coroutines being used by a threaded instance, the access to the 'coros'
87      * list must be protected: we use 'coro_mutex for that purpose.
88      */
89      pthread_mutex_t coro_mutex;         /* mutex for 'coros' list */
90 
91     /* List of mapped 'upstream' contexts */
92     struct mk_list upstreams;
93 };
94 
95 int flb_output_thread_pool_create(struct flb_config *config,
96                                   struct flb_output_instance *ins);
97 int flb_output_thread_pool_coros_size(struct flb_output_instance *ins);
98 void flb_output_thread_pool_destroy(struct flb_output_instance *ins);
99 int flb_output_thread_pool_start(struct flb_output_instance *ins);
100 int flb_output_thread_pool_flush(struct flb_task *task,
101                                  struct flb_output_instance *out_ins,
102                                  struct flb_config *config);
103 
104 
105 void flb_output_thread_instance_init();
106 struct flb_out_thread_instance *flb_output_thread_instance_get();
107 void flb_output_thread_instance_set(struct flb_out_thread_instance *th_ins);
108 
109 #endif
110