-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbgpstream_reader.c
More file actions
332 lines (273 loc) · 10.6 KB
/
Copy pathbgpstream_reader.c
File metadata and controls
332 lines (273 loc) · 10.6 KB
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
/*
* Copyright (C) 2014 The Regents of the University of California.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "bgpstream_reader.h"
#include "bgpstream_record_int.h"
#include "bgpstream_log.h"
#include "utils.h"
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#define DUMP_OPEN_MAX_RETRIES 5
#define DUMP_OPEN_MIN_RETRY_WAIT 10
#define PREFETCH_IDX (reader->rec_buf_prefetch_idx)
#define EXPORTED_IDX ((reader->rec_buf_prefetch_idx + 1) % 2)
struct bgpstream_reader {
// borrowed pointer to the resource that we have opened
bgpstream_resource_t *res;
// borrowed pointer to a filter manager instance
bgpstream_filter_mgr_t *filter_mgr;
// internal flip-flop buffers for storing records
bgpstream_record_t *rec_buf[2];
int rec_buf_filled[2];
// which of the flip-flop buffers is currently holding the "prefetch" record
// the other ((this+1)%2) is holding the "exported" record
int rec_buf_prefetch_idx;
// status of the underlying reader
bgpstream_format_status_t status;
// handle for the thread that will do the actual opening
pthread_t opener_thread;
// ALL BELOW HERE MUST USE MUTEX
// format instance
bgpstream_format_t *format;
// has the thread opened the dump? (must use mutex)
int dump_ready;
pthread_cond_t dump_ready_cond;
pthread_mutex_t mutex;
// can the dump open check be skipped?
int skip_dump_check;
// what is the time of the next record (PREFETCH)
uint32_t next_time;
};
static int prefetch_record(bgpstream_reader_t *reader)
{
bgpstream_record_t *record;
assert(reader->status == BGPSTREAM_FORMAT_OK);
assert(reader->rec_buf_filled[PREFETCH_IDX] == 0);
record = reader->rec_buf[PREFETCH_IDX];
// first, clear up our record
// note that this only destroys the reader struct and resets the elem
// generator. it does not clear the collector name etc as we reuse that.
bgpstream_record_clear(record);
// try and get the next entry from the resource (will do filtering)
reader->status = bgpstream_format_populate_record(reader->format, record);
// exit with error in case of read error
if (reader->status == BGPSTREAM_FORMAT_READ_ERROR) {
return -1;
}
// if we got any of the non-error END_OF_DUMP messages but this is a stream
// resource, then pretend we're ok. but beware that now we'll be "OK", with
// an unfilled prefetch record
if (reader->res->duration == BGPSTREAM_FOREVER &&
(reader->status == BGPSTREAM_FORMAT_END_OF_DUMP ||
reader->status == BGPSTREAM_FORMAT_FILTERED_DUMP ||
reader->status == BGPSTREAM_FORMAT_EMPTY_DUMP ||
reader->status == BGPSTREAM_FORMAT_CORRUPTED_DUMP)) {
reader->status = BGPSTREAM_FORMAT_OK;
return 0;
}
// if we see corrupted or unsupported message, we still
// fill the buffer and should continue reading
if (reader->status == BGPSTREAM_FORMAT_CORRUPTED_MSG ||
reader->status == BGPSTREAM_FORMAT_UNSUPPORTED_MSG) {
reader->rec_buf_filled[PREFETCH_IDX] = 1;
reader->status = BGPSTREAM_FORMAT_OK;
return 0;
}
reader->next_time = record->time_sec;
// set the previous record position to END if we didn't skip any records. we
// know this because the format has set the position of the current record to
// END (if records were skipped, it would be set to MIDDLE)
if (reader->status == BGPSTREAM_FORMAT_END_OF_DUMP &&
record->dump_pos == BGPSTREAM_DUMP_END &&
reader->rec_buf_filled[EXPORTED_IDX] == 1) {
reader->rec_buf[EXPORTED_IDX]->dump_pos = BGPSTREAM_DUMP_END;
}
// we export a meta record for every status except end of dump
if (reader->status != BGPSTREAM_FORMAT_END_OF_DUMP) {
reader->rec_buf_filled[PREFETCH_IDX] = 1;
}
return 0;
}
// fills the record with resource-level info that doesn't change per-record
static int prepopulate_record(bgpstream_record_t *record,
bgpstream_resource_t *res)
{
// project
strncpy(record->project_name, res->project, BGPSTREAM_UTILS_STR_NAME_LEN);
record->project_name[BGPSTREAM_UTILS_STR_NAME_LEN - 1] = '\0';
// collector
strncpy(record->collector_name, res->collector, BGPSTREAM_UTILS_STR_NAME_LEN);
record->collector_name[BGPSTREAM_UTILS_STR_NAME_LEN - 1] = '\0';
// dump type
record->type = res->record_type;
// dump time
record->dump_time_sec = res->initial_time;
return 0;
}
static void *threaded_opener(void *user)
{
bgpstream_reader_t *reader = (bgpstream_reader_t *)user;
int retries = 0;
int delay = DUMP_OPEN_MIN_RETRY_WAIT;
int i;
/* all we do is open the dump */
/* but try a few times in case there is a transient failure */
while (retries < DUMP_OPEN_MAX_RETRIES && reader->format == NULL) {
if ((reader->format =
bgpstream_format_create(reader->res, reader->filter_mgr)) == NULL) {
bgpstream_log(BGPSTREAM_LOG_WARN, "Could not open (%s). Attempt %d of %d",
reader->res->url, retries + 1, DUMP_OPEN_MAX_RETRIES);
retries++;
if (retries < DUMP_OPEN_MAX_RETRIES) {
sleep(delay);
delay *= 2;
}
}
}
pthread_mutex_lock(&reader->mutex);
if (reader->format == NULL) {
bgpstream_log(BGPSTREAM_LOG_ERR,
"Could not open dumpfile (%s) after %d attempts. Giving up.",
reader->res->url, DUMP_OPEN_MAX_RETRIES);
reader->status = BGPSTREAM_FORMAT_CANT_OPEN_DUMP;
} else {
// create the pair of records
for (i = 0; i < 2; i++) {
if ((reader->rec_buf[i] = bgpstream_record_create(reader->format)) ==
NULL ||
prepopulate_record(reader->rec_buf[i], reader->res) != 0) {
reader->status = BGPSTREAM_FORMAT_CANT_OPEN_DUMP;
break;
}
reader->rec_buf_filled[i] = 0;
}
reader->rec_buf_prefetch_idx = 0;
if (reader->status != BGPSTREAM_FORMAT_CANT_OPEN_DUMP) {
// prefetch the first record (will set reader->status to error if needed)
prefetch_record(reader);
}
}
reader->dump_ready = 1;
pthread_cond_signal(&reader->dump_ready_cond);
pthread_mutex_unlock(&reader->mutex);
return NULL;
}
/* ========== PUBLIC FUNCTIONS BELOW ========== */
bgpstream_reader_t *bgpstream_reader_create(bgpstream_resource_t *resource,
bgpstream_filter_mgr_t *filter_mgr)
{
bgpstream_reader_t *reader;
if ((reader = malloc_zero(sizeof(bgpstream_reader_t))) == NULL) {
return NULL;
}
reader->res = resource;
reader->filter_mgr = filter_mgr;
reader->status = BGPSTREAM_FORMAT_OK;
// initialize and start the thread to open the resource
// this will also pre-fetch the first record
pthread_mutex_init(&reader->mutex, NULL);
pthread_cond_init(&reader->dump_ready_cond, NULL);
reader->dump_ready = 0;
reader->skip_dump_check = 0;
pthread_create(&reader->opener_thread, NULL, threaded_opener, reader);
return reader;
}
uint32_t bgpstream_reader_get_next_time(bgpstream_reader_t *reader)
{
assert(bgpstream_reader_open_wait(reader) == 0);
return reader->next_time;
}
void bgpstream_reader_destroy(bgpstream_reader_t *reader)
{
if (reader == NULL) {
return;
}
// Ensure the thread is done
pthread_join(reader->opener_thread, NULL);
pthread_mutex_destroy(&reader->mutex);
pthread_cond_destroy(&reader->dump_ready_cond);
int i;
for (i = 0; i < 2; i++) {
bgpstream_record_destroy(reader->rec_buf[i]);
reader->rec_buf[i] = NULL;
}
bgpstream_format_destroy(reader->format);
free(reader);
}
int bgpstream_reader_open_wait(bgpstream_reader_t *reader)
{
if (reader->skip_dump_check != 0) {
return 0;
}
pthread_mutex_lock(&reader->mutex);
while (reader->dump_ready == 0) {
pthread_cond_wait(&reader->dump_ready_cond, &reader->mutex);
}
pthread_mutex_unlock(&reader->mutex);
if (reader->status == BGPSTREAM_FORMAT_CANT_OPEN_DUMP) {
return -1;
}
reader->skip_dump_check = 1;
return 0;
}
int bgpstream_reader_get_next_record(bgpstream_reader_t *reader,
bgpstream_record_t **record)
{
// DO NOT use the prefetch record before open_wait!
if (bgpstream_reader_open_wait(reader) != 0) {
// cant even open the dump file
// we're not going to last long, but we should return the record saying
// we're a failure
*record = reader->rec_buf[PREFETCH_IDX];
(*record)->status = BGPSTREAM_RECORD_STATUS_CORRUPTED_SOURCE;
assert((*record)->__int->data == NULL);
return BGPSTREAM_READER_STATUS_EOS;
}
// mark the previous record as unfilled (about to become PREFETCH_IDX)
reader->rec_buf_filled[EXPORTED_IDX] = 0;
// the record contents will be cleared by the next prefetch
// flip-flop the buffers (EXPORTED will mean "about to export")
reader->rec_buf_prefetch_idx = EXPORTED_IDX;
// prefetch the next message (so we can see if the record we're about to
// export would be the last one)
if (reader->status == BGPSTREAM_FORMAT_OK && prefetch_record(reader) != 0) {
bgpstream_log(BGPSTREAM_LOG_ERR, "Prefetch failed");
return BGPSTREAM_READER_STATUS_ERROR;
}
// if the EXPORT record is not filled then we need to return EOS or AGAIN
if (reader->rec_buf_filled[EXPORTED_IDX] == 0) {
if (reader->res->duration == BGPSTREAM_FOREVER &&
reader->status == BGPSTREAM_FORMAT_OK) {
return BGPSTREAM_READER_STATUS_AGAIN;
} else {
return BGPSTREAM_READER_STATUS_EOS;
}
}
// we have something in our EXPORT record, so go ahead and copy that into the
// user's record
*record = reader->rec_buf[EXPORTED_IDX];
return BGPSTREAM_READER_STATUS_OK;
}