summaryrefslogtreecommitdiffstats
path: root/fluent-bit/src/stream_processor/flb_sp_snapshot.c
blob: edef823b43240dcce9198072bc7457a428df8590 (plain)
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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <fluent-bit/flb_mem.h>
#include <fluent-bit/stream_processor/flb_sp.h>
#include <fluent-bit/stream_processor/flb_sp_parser.h>
#include <fluent-bit/stream_processor/flb_sp_snapshot.h>

static struct flb_sp_snapshot_page *snapshot_page_create()
{
    struct flb_sp_snapshot_page *page;

    page = (struct flb_sp_snapshot_page *)
           flb_calloc(1, sizeof(struct flb_sp_snapshot_page));
    if (!page) {
        flb_errno();
        return NULL;
    }

    page->snapshot_page = (char *) flb_malloc(SNAPSHOT_PAGE_SIZE);
    if (!page->snapshot_page) {
        flb_errno();
        flb_free(page);
        return NULL;
    }

    return page;
}

static int snapshot_cleanup(struct flb_sp_snapshot *snapshot, struct flb_time *tms)
{
    int ok;
    size_t off;
    size_t off_copy;
    msgpack_unpacked result;
    msgpack_object *obj;
    struct flb_time tms0;
    struct flb_sp_snapshot_page *page;

    ok = MSGPACK_UNPACK_SUCCESS;
    off = 0;

    while (mk_list_is_empty(&snapshot->pages) != 0) {
        page = mk_list_entry_first(&snapshot->pages, struct flb_sp_snapshot_page,
                                   _head);
        off = page->start_pos;
        off_copy = off;

        msgpack_unpacked_init(&result);

        while (msgpack_unpack_next(&result, page->snapshot_page, page->end_pos,
                                   &off) == ok) {

            if (snapshot->record_limit > 0 &&
                snapshot->records > snapshot->record_limit) {
                page->start_pos = off;
                snapshot->records--;
                snapshot->size = snapshot->size - (off - off_copy);
                off_copy = off;

                continue;
            }

            /* extract timestamp */
            flb_time_pop_from_msgpack(&tms0, &result, &obj);

            if (snapshot->time_limit > 0 &&
                tms->tm.tv_sec - tms0.tm.tv_sec > snapshot->time_limit) {
                page->start_pos = off;
                snapshot->records--;
                snapshot->size = snapshot->size - (off - off_copy);
                off_copy = off;

                continue;
            }

            break;
        }

        msgpack_unpacked_destroy(&result);

        /* If page is empty, free the page and move to the next one */
        if (page->start_pos != page->end_pos) {
            break;
        }

        mk_list_del(&page->_head);
        flb_free(page->snapshot_page);
        flb_free(page);
    }

    return 0;
}

static bool snapshot_page_is_full(struct flb_sp_snapshot_page *page, size_t buf_size)
{
    return SNAPSHOT_PAGE_SIZE - page->end_pos < buf_size;
}

char *flb_sp_snapshot_name_from_flush(flb_sds_t name)
{
    return name + sizeof("__flush_") - 1;
}

int flb_sp_snapshot_update(struct flb_sp_task *task, const char *buf_data,
                           size_t buf_size, struct flb_time *tms)
{
    int ok;
    size_t off = 0;
    struct flb_time tm;
    struct flb_sp_snapshot *snapshot;
    struct flb_sp_snapshot_page *page;
    msgpack_unpacked result;
    msgpack_object *obj;

    ok = MSGPACK_UNPACK_SUCCESS;
    msgpack_unpacked_init(&result);

    if (buf_size <= 0) {
        return -1;
    }

    snapshot = (struct flb_sp_snapshot *) task->snapshot;

    /* Create a snapshot pgae if the list is empty */
    if (mk_list_is_empty(&snapshot->pages) == 0) {
        page = snapshot_page_create();
        if (!page) {
            flb_errno();
            return -1;
        }

        mk_list_add(&page->_head, &snapshot->pages);
    }
    else {
        page = mk_list_entry_last(&snapshot->pages, struct flb_sp_snapshot_page, _head);

        if (snapshot_page_is_full(page, buf_size)) {
            page = snapshot_page_create();
            if (!page) {
                flb_errno();
                return -1;
            }

            mk_list_add(&page->_head, &snapshot->pages);
        }
    }

    memcpy(page->snapshot_page + page->end_pos, buf_data, buf_size);
    page->end_pos = page->end_pos + buf_size;

    /* Get the last timestamp */
    while (msgpack_unpack_next(&result, page->snapshot_page,
                               page->end_pos - page->start_pos, &off) == ok) {
        flb_time_pop_from_msgpack(&tm, &result, &obj);
    }

    msgpack_unpacked_destroy(&result);

    snapshot->records++;
    snapshot->size = snapshot->size + buf_size;

    /* Remove records from snapshot pages based on time/length window */
    snapshot_cleanup(snapshot, tms);

    return 0;
}

int flb_sp_snapshot_flush(struct flb_sp *sp, struct flb_sp_task *task,
                          char **out_buf_data, size_t *out_buf_size)
{
    size_t off;
    size_t page_size;
    char *snapshot_name;
    char *out_buf_data_tmp;
    struct flb_sp_cmd *cmd;
    struct mk_list *tmp;
    struct mk_list *head;
    struct mk_list *snapshot_head;
    struct flb_sp_task *snapshot_task;
    struct flb_sp_snapshot *snapshot;
    struct flb_sp_snapshot_page *page;

    off = 0;
    cmd = task->cmd;
    snapshot_name = flb_sp_snapshot_name_from_flush(cmd->stream_name);

    /* Lookup Tasks that matches the incoming instance data */
    mk_list_foreach(head, &sp->tasks) {
        snapshot_task = mk_list_entry(head, struct flb_sp_task, _head);
        cmd = snapshot_task->cmd;

        if (cmd->type == FLB_SP_CREATE_SNAPSHOT &&
            flb_sds_cmp(cmd->stream_name, snapshot_name,
                        strlen(snapshot_name)) == 0) {

            snapshot = (struct flb_sp_snapshot *) snapshot_task->snapshot;

            if (snapshot->size == 0) {
                break;
            }

            if (*out_buf_data == NULL) {
                *out_buf_data = (char *) flb_malloc(snapshot->size);
                if (!*out_buf_data) {
                    flb_errno();
                    return -1;
                }
                *out_buf_size = snapshot->size;
            }
            else {
                out_buf_data_tmp = (char *) flb_realloc(*out_buf_data,
                                                        *out_buf_size + snapshot->size);
                if (!out_buf_data_tmp) {
                    flb_errno();
                    return -1;
                }
                *out_buf_data = out_buf_data_tmp;
                *out_buf_size = *out_buf_size + snapshot->size;
            }

            mk_list_foreach_safe(snapshot_head, tmp, &snapshot->pages) {
                page = mk_list_entry_first(&snapshot->pages,
                                           struct flb_sp_snapshot_page, _head);
                page_size = page->end_pos - page->start_pos;
                memcpy(*out_buf_data + off,
                       page->snapshot_page + page->start_pos, page_size);
                off = off + page_size;

                /* Remove page from list */
                mk_list_del(&page->_head);
                flb_free(page->snapshot_page);
                flb_free(page);
            }

            mk_list_init(&snapshot->pages);

            snapshot->records = 0;
            snapshot->size = 0;
        }
    }

    return 0;
}

void flb_sp_snapshot_destroy(struct flb_sp_snapshot *snapshot)
{
    struct mk_list *head;
    struct mk_list *tmp;
    struct flb_sp_snapshot_page *page;

    if (snapshot != NULL) {
        mk_list_foreach_safe(head, tmp, &snapshot->pages) {
            page = mk_list_entry(head, struct flb_sp_snapshot_page, _head);
            mk_list_del(&page->_head);
            flb_free(page->snapshot_page);
            flb_free(page);
        }
        flb_free(snapshot);
    }
}