This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
store_queue.cpp
365 lines (310 loc) · 9.83 KB
/
store_queue.cpp
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
// Copyright (c) 2007-2008 Facebook
//
// 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.
//
// See accompanying file LICENSE or visit the Scribe site at:
// http://developers.facebook.com/scribe/
//
// @author Bobby Johnson
// @author James Wang
// @author Jason Sobel
// @author Anthony Giardullo
// @author John Song
#include "common.h"
#include "scribe_server.h"
using namespace std;
using namespace boost;
using namespace scribe::thrift;
#define DEFAULT_TARGET_WRITE_SIZE 16384LL
#define DEFAULT_MAX_WRITE_INTERVAL 1
void* threadStatic(void *this_ptr) {
StoreQueue *queue_ptr = (StoreQueue*)this_ptr;
queue_ptr->threadMember();
return NULL;
}
StoreQueue::StoreQueue(const string& type, const string& category,
unsigned check_period, bool is_model, bool multi_category)
: msgQueueSize(0),
hasWork(false),
stopping(false),
isModel(is_model),
multiCategory(multi_category),
categoryHandled(category),
checkPeriod(check_period),
targetWriteSize(DEFAULT_TARGET_WRITE_SIZE),
maxWriteInterval(DEFAULT_MAX_WRITE_INTERVAL),
mustSucceed(true) {
store = Store::createStore(this, type, category,
false, multiCategory);
if (!store) {
throw std::runtime_error("createStore failed in StoreQueue constructor. Invalid type?");
}
storeInitCommon();
}
StoreQueue::StoreQueue(const boost::shared_ptr<StoreQueue> example,
const std::string &category)
: msgQueueSize(0),
hasWork(false),
stopping(false),
isModel(false),
multiCategory(example->multiCategory),
categoryHandled(category),
checkPeriod(example->checkPeriod),
targetWriteSize(example->targetWriteSize),
maxWriteInterval(example->maxWriteInterval),
mustSucceed(example->mustSucceed) {
store = example->copyStore(category);
if (!store) {
throw std::runtime_error("createStore failed copying model store");
}
storeInitCommon();
}
StoreQueue::~StoreQueue() {
if (!isModel) {
pthread_mutex_destroy(&cmdMutex);
pthread_mutex_destroy(&msgMutex);
pthread_mutex_destroy(&hasWorkMutex);
pthread_cond_destroy(&hasWorkCond);
}
}
void StoreQueue::addMessage(boost::shared_ptr<LogEntry> entry) {
if (isModel) {
LOG_OPER("ERROR: called addMessage on model store");
} else {
bool waitForWork = false;
pthread_mutex_lock(&msgMutex);
msgQueue->push_back(entry);
msgQueueSize += entry->message.size();
waitForWork = (msgQueueSize >= targetWriteSize) ? true : false;
pthread_mutex_unlock(&msgMutex);
// Wake up store thread if we have enough messages
if (waitForWork == true) {
// signal that there is work to do if not already signaled
pthread_mutex_lock(&hasWorkMutex);
if (!hasWork) {
hasWork = true;
pthread_cond_signal(&hasWorkCond);
}
pthread_mutex_unlock(&hasWorkMutex);
}
}
}
void StoreQueue::configureAndOpen(pStoreConf configuration) {
// model store has to handle this inline since it has no queue
if (isModel) {
configureInline(configuration);
} else {
pthread_mutex_lock(&cmdMutex);
StoreCommand cmd(CMD_CONFIGURE, configuration);
cmdQueue.push(cmd);
pthread_mutex_unlock(&cmdMutex);
// signal that there is work to do if not already signaled
pthread_mutex_lock(&hasWorkMutex);
if (!hasWork) {
hasWork = true;
pthread_cond_signal(&hasWorkCond);
}
pthread_mutex_unlock(&hasWorkMutex);
}
}
void StoreQueue::stop() {
if (isModel) {
LOG_OPER("ERROR: called stop() on model store");
} else if(!stopping) {
pthread_mutex_lock(&cmdMutex);
StoreCommand cmd(CMD_STOP);
cmdQueue.push(cmd);
stopping = true;
pthread_mutex_unlock(&cmdMutex);
// signal that there is work to do if not already signaled
pthread_mutex_lock(&hasWorkMutex);
if (!hasWork) {
hasWork = true;
pthread_cond_signal(&hasWorkCond);
}
pthread_mutex_unlock(&hasWorkMutex);
pthread_join(storeThread, NULL);
}
}
void StoreQueue::open() {
if (isModel) {
LOG_OPER("ERROR: called open() on model store");
} else {
pthread_mutex_lock(&cmdMutex);
StoreCommand cmd(CMD_OPEN);
cmdQueue.push(cmd);
pthread_mutex_unlock(&cmdMutex);
// signal that there is work to do if not already signaled
pthread_mutex_lock(&hasWorkMutex);
if (!hasWork) {
hasWork = true;
pthread_cond_signal(&hasWorkCond);
}
pthread_mutex_unlock(&hasWorkMutex);
}
}
shared_ptr<Store> StoreQueue::copyStore(const std::string &category) {
return store->copy(category);
}
std::string StoreQueue::getCategoryHandled() {
return categoryHandled;
}
std::string StoreQueue::getStatus() {
return store->getStatus();
}
std::string StoreQueue::getBaseType() {
return store->getType();
}
void StoreQueue::threadMember() {
LOG_OPER("store thread starting");
if (isModel) {
LOG_OPER("ERROR: store thread starting on model store, exiting");
return;
}
if (!store) {
LOG_OPER("store is NULL, store thread exiting");
return;
}
// init time of last periodic check to time of 0
time_t last_periodic_check = 0;
time_t last_handle_messages;
time(&last_handle_messages);
struct timespec abs_timeout;
bool stop = false;
bool open = false;
while (!stop) {
// handle commands
//
pthread_mutex_lock(&cmdMutex);
while (!cmdQueue.empty()) {
StoreCommand cmd = cmdQueue.front();
cmdQueue.pop();
switch (cmd.command) {
case CMD_CONFIGURE:
configureInline(cmd.configuration);
openInline();
open = true;
break;
case CMD_OPEN:
openInline();
open = true;
break;
case CMD_STOP:
stop = true;
break;
default:
LOG_OPER("LOGIC ERROR: unknown command to store queue");
break;
}
}
// handle periodic tasks
time_t this_loop;
time(&this_loop);
if (!stop && ((this_loop - last_periodic_check) >= checkPeriod)) {
if (open) store->periodicCheck();
last_periodic_check = this_loop;
}
pthread_mutex_lock(&msgMutex);
pthread_mutex_unlock(&cmdMutex);
boost::shared_ptr<logentry_vector_t> messages;
// handle messages if stopping, enough time has passed, or queue is large
//
if (stop ||
(this_loop - last_handle_messages >= maxWriteInterval) ||
msgQueueSize >= targetWriteSize) {
if (failedMessages) {
// process any messages we were not able to process last time
messages = failedMessages;
failedMessages = boost::shared_ptr<logentry_vector_t>();
} else if (msgQueueSize > 0) {
// process message in queue
messages = msgQueue;
msgQueue = boost::shared_ptr<logentry_vector_t>(new logentry_vector_t);
msgQueueSize = 0;
}
// reset timer
last_handle_messages = this_loop;
}
pthread_mutex_unlock(&msgMutex);
if (messages) {
if (!store->handleMessages(messages)) {
// Store could not handle these messages
processFailedMessages(messages);
}
store->flush();
}
if (!stop) {
// set timeout to when we need to handle messages or do a periodic check
abs_timeout.tv_sec = min(last_periodic_check + checkPeriod,
last_handle_messages + maxWriteInterval);
abs_timeout.tv_nsec = 0;
// wait until there's some work to do or we timeout
pthread_mutex_lock(&hasWorkMutex);
if (!hasWork) {
pthread_cond_timedwait(&hasWorkCond, &hasWorkMutex, &abs_timeout);
}
hasWork = false;
pthread_mutex_unlock(&hasWorkMutex);
}
} // while (!stop)
store->close();
}
void StoreQueue::processFailedMessages(shared_ptr<logentry_vector_t> messages) {
// If the store was not able to process these messages, we will either
// requeue them or give up depending on the value of mustSucceed
if (mustSucceed) {
// Save failed messages
failedMessages = messages;
LOG_OPER("[%s] WARNING: Re-queueing %lu messages!",
categoryHandled.c_str(), messages->size());
g_Handler->incCounter(categoryHandled, "requeue", messages->size());
} else {
// record messages as being lost
LOG_OPER("[%s] WARNING: Lost %lu messages!",
categoryHandled.c_str(), messages->size());
g_Handler->incCounter(categoryHandled, "lost", messages->size());
}
}
void StoreQueue::storeInitCommon() {
// model store doesn't need this stuff
if (!isModel) {
msgQueue = boost::shared_ptr<logentry_vector_t>(new logentry_vector_t);
pthread_mutex_init(&cmdMutex, NULL);
pthread_mutex_init(&msgMutex, NULL);
pthread_mutex_init(&hasWorkMutex, NULL);
pthread_cond_init(&hasWorkCond, NULL);
pthread_create(&storeThread, NULL, threadStatic, (void*) this);
}
}
void StoreQueue::configureInline(pStoreConf configuration) {
// Constructor defaults are fine if these don't exist
configuration->getUnsignedLongLong("target_write_size", targetWriteSize);
configuration->getUnsigned("max_write_interval",
(unsigned long&) maxWriteInterval);
if (maxWriteInterval == 0) {
maxWriteInterval = 1;
}
string tmp;
if (configuration->getString("must_succeed", tmp) && tmp == "no") {
mustSucceed = false;
}
store->configure(configuration, pStoreConf());
}
void StoreQueue::openInline() {
if (store->isOpen()) {
store->close();
}
if (!isModel) {
store->open();
}
}