Skip to content
Newer
Older
100644 158 lines (132 sloc) 4.43 KB
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
3 #include "flusher.hh"
4
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
5 bool Flusher::stop(void) {
6 return transition_state(stopping);
7 }
8
9 bool Flusher::pause(void) {
10 return transition_state(pausing);
11 }
12
13 bool Flusher::resume(void) {
14 return transition_state(running);
15 }
16
17 static bool validTransition(enum flusher_state from,
18 enum flusher_state to)
19 {
20 bool rv(true);
67f0337 @dustin Added initializing state to flusher.
dustin authored Apr 30, 2010
21 if (from == initializing && to == running) {
22 } else if (from == running && to == pausing) {
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
23 } else if (from == running && to == stopping) {
24 } else if (from == pausing && to == paused) {
25 } else if (from == stopping && to == stopped) {
26 } else if (from == paused && to == running) {
27 } else if (from == paused && to == stopping) {
28 } else if (from == pausing && to == stopping) {
29 } else {
30 rv = false;
31 }
32 return rv;
33 }
34
35 const char * const Flusher::stateName(enum flusher_state st) const {
36 static const char * const stateNames[] = {
67f0337 @dustin Added initializing state to flusher.
dustin authored Apr 30, 2010
37 "initializing", "running", "pausing", "paused", "stopping", "stopped"
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
38 };
67f0337 @dustin Added initializing state to flusher.
dustin authored Apr 30, 2010
39 assert(st >= initializing && st <= stopped);
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
40 return stateNames[st];
41 }
42
43 bool Flusher::transition_state(enum flusher_state to) {
44
45 getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
46 "Attempting transition from %s to %s\n",
47 stateName(_state), stateName(to));
48
49 if (!validTransition(_state, to)) {
50 return false;
51 }
52
53 getLogger()->log(EXTENSION_LOG_DEBUG, NULL, "Transitioning from %s to %s\n",
54 stateName(_state), stateName(to));
55
56 _state = to;
57 return true;
58 }
59
60 const char * const Flusher::stateName() const {
61 return stateName(_state);
62 }
63
64 enum flusher_state Flusher::state() const {
65 return _state;
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
66 }
67
68 void Flusher::initialize(void) {
4487e12 @dustin Fix time tracking during flusher warmup (and log it).
dustin authored Apr 30, 2010
69 getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
70 "Initializing flusher; warming up\n");
71
72 time_t start = time(NULL);
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
73 store->warmup();
4487e12 @dustin Fix time tracking during flusher warmup (and log it).
dustin authored Apr 30, 2010
74 store->stats.warmupTime = time(NULL) - start;
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
75 store->stats.warmupComplete = true;
4487e12 @dustin Fix time tracking during flusher warmup (and log it).
dustin authored Apr 30, 2010
76
77 getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
78 "Warmup completed in %ds\n", store->stats.warmupTime);
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
79 hasInitialized = true;
67f0337 @dustin Added initializing state to flusher.
dustin authored Apr 30, 2010
80 transition_state(running);
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
81 }
82
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
83 void Flusher::maybePause(void) {
84 if (_state == pausing) {
85 transition_state(paused);
86 while (_state == paused) {
87 sleep(1);
88 }
89 }
90 }
91
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
92 void Flusher::run(void) {
93 if (!hasInitialized) {
94 initialize();
95 }
96 try {
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
97 while (_state != stopping) {
98 maybePause();
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
99
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
100 rel_time_t start = ep_current_time();
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
101 int n = doFlush(true);
102 if (n > 0) {
103 rel_time_t sleep_end = start + n;
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
104 while (_state == running && ep_current_time() < sleep_end) {
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
105 sleep(1);
106 }
107 }
108
109 }
110 std::stringstream ss;
111 ss << "Shutting down flusher (Write of all dirty items)"
112 << std::endl;
113 getLogger()->log(EXTENSION_LOG_DEBUG, NULL, "%s",
114 ss.str().c_str());
115 store->stats.min_data_age = 0;
116 doFlush(false);
117 getLogger()->log(EXTENSION_LOG_DEBUG, NULL, "Flusher stopped\n");
118 } catch(std::runtime_error &e) {
119 std::stringstream ss;
3329250 @dustin Typo fix.
dustin authored Apr 29, 2010
120 ss << "Exception in flusher loop: " << e.what() << std::endl;
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
121 getLogger()->log(EXTENSION_LOG_WARNING, NULL, "%s",
122 ss.str().c_str());
123 assert(false);
124 }
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
125 transition_state(stopped);
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
126 }
127
128 int Flusher::doFlush(bool shouldWait) {
933ca26 @dustin Control flusher loops from within flusher.
dustin authored Apr 27, 2010
129 int rv(0);
130 std::queue<std::string> *q = store->beginFlush(shouldWait);
131 getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
132 "Looking for something to flush.\n");
133 if (q) {
134 getLogger()->log(EXTENSION_LOG_DEBUG, NULL,
135 "Flushing a write queue.\n");
136 std::queue<std::string> *rejectQueue = new std::queue<std::string>();
137 rel_time_t flush_start = ep_current_time();
138 rv = store->stats.min_data_age;
139
140 while (!q->empty()) {
78a613c @dustin Refactored flusher to not send a callback all the way down.
dustin authored Apr 29, 2010
141 int n = store->flushSome(q, rejectQueue);
2a1c9f8 @dustin Maintain flusher status within the flusher.
dustin authored Apr 27, 2010
142 maybePause();
933ca26 @dustin Control flusher loops from within flusher.
dustin authored Apr 27, 2010
143 if (n < rv) {
144 rv = n;
145 }
146 }
147
148 store->completeFlush(rejectQueue, flush_start);
149
150 getLogger()->log(EXTENSION_LOG_INFO, NULL,
151 "Completed a flush, age of oldest item was %ds\n",
152 rv);
153
154 delete rejectQueue;
155 }
156 return rv;
dbd2dff @dustin Moved flusher implementation out of header file.
dustin authored Apr 27, 2010
157 }
Something went wrong with that request. Please try again.