Revision 80d08166 include/LogDB.h
include/LogDB.h | ||
---|---|---|
19 | 19 |
|
20 | 20 |
#include <string> |
21 | 21 |
#include <sstream> |
22 |
#include <set> |
|
22 | 23 |
|
23 | 24 |
#include "SqlDB.h" |
24 | 25 |
|
... | ... | |
53 | 54 |
time_t timestamp; |
54 | 55 |
|
55 | 56 |
/** |
57 |
* The index in the federation, -1 if the log entry is not federated. |
|
58 |
* At master fed_index is equal to index. |
|
59 |
*/ |
|
60 |
int fed_index; |
|
61 |
|
|
62 |
/** |
|
56 | 63 |
* Sets callback to load register from DB |
57 | 64 |
*/ |
58 | 65 |
void set_callback() |
... | ... | |
72 | 79 |
* This class implements a generic DB interface with replication. The associated |
73 | 80 |
* DB stores a log to replicate on followers. |
74 | 81 |
*/ |
75 |
class LogDB : public SqlDB |
|
82 |
class LogDB : public SqlDB, Callbackable
|
|
76 | 83 |
{ |
77 | 84 |
public: |
78 | 85 |
LogDB(SqlDB * _db, bool solo, unsigned int log_retention); |
... | ... | |
111 | 118 |
* @param term for the record |
112 | 119 |
* @param sql command of the record |
113 | 120 |
* @param timestamp associated to this record |
121 |
* @param fed_index index in the federation -1 if not federated |
|
114 | 122 |
* |
115 | 123 |
* @return -1 on failure, index of the inserted record on success |
116 | 124 |
*/ |
117 | 125 |
int insert_log_record(unsigned int index, unsigned int term, |
118 |
std::ostringstream& sql, time_t timestamp); |
|
126 |
std::ostringstream& sql, time_t timestamp, int fed_index);
|
|
119 | 127 |
|
120 | 128 |
//-------------------------------------------------------------------------- |
121 | 129 |
// Functions to manage the Raft state. Log record 0, term -1 |
... | ... | |
148 | 156 |
* This function replicates the DB changes on followers before updating |
149 | 157 |
* the DB state |
150 | 158 |
*/ |
151 |
int exec_wr(ostringstream& cmd); |
|
159 |
int exec_wr(ostringstream& cmd) |
|
160 |
{ |
|
161 |
return _exec_wr(cmd, -1); |
|
162 |
} |
|
163 |
|
|
164 |
int exec_federated_wr(ostringstream& cmd) |
|
165 |
{ |
|
166 |
return _exec_wr(cmd, 0); |
|
167 |
} |
|
168 |
|
|
169 |
int exec_federated_wr(ostringstream& cmd, int index) |
|
170 |
{ |
|
171 |
return _exec_wr(cmd, index); |
|
172 |
} |
|
152 | 173 |
|
153 | 174 |
int exec_local_wr(ostringstream& cmd) |
154 | 175 |
{ |
... | ... | |
201 | 222 |
*/ |
202 | 223 |
void get_last_record_index(unsigned int& _i, unsigned int& _t); |
203 | 224 |
|
225 |
// ------------------------------------------------------------------------- |
|
226 |
// Federate log methods |
|
227 |
// ------------------------------------------------------------------------- |
|
228 |
/** |
|
229 |
* Get last federated index, and previous |
|
230 |
*/ |
|
231 |
int last_federated(); |
|
232 |
|
|
233 |
int previous_federated(int index); |
|
234 |
|
|
235 |
int next_federated(int index); |
|
236 |
|
|
204 | 237 |
protected: |
205 | 238 |
int exec(std::ostringstream& cmd, Callbackable* obj, bool quiet) |
206 | 239 |
{ |
... | ... | |
246 | 279 |
unsigned int log_retention; |
247 | 280 |
|
248 | 281 |
// ------------------------------------------------------------------------- |
282 |
// Federated Log |
|
283 |
// ------------------------------------------------------------------------- |
|
284 |
/** |
|
285 |
* The federated log stores a map with the federated log index and its |
|
286 |
* corresponding local index. For the master both are the same |
|
287 |
*/ |
|
288 |
std::set<int> fed_log; |
|
289 |
|
|
290 |
/** |
|
291 |
* Generates the federated index, it should be called whenever a server |
|
292 |
* takes leadership. |
|
293 |
*/ |
|
294 |
void build_federated_index(); |
|
295 |
|
|
296 |
// ------------------------------------------------------------------------- |
|
249 | 297 |
// DataBase implementation |
250 | 298 |
// ------------------------------------------------------------------------- |
251 | 299 |
static const char * table; |
... | ... | |
255 | 303 |
static const char * db_bootstrap; |
256 | 304 |
|
257 | 305 |
/** |
306 |
* Replicates writes in the followers and apply changes to DB state once |
|
307 |
* it is safe to do so. |
|
308 |
* |
|
309 |
* @param federated -1 not federated (fed_index = -1), 0 generate fed index |
|
310 |
* (fed_index = index), > 0 set (fed_index = federated) |
|
311 |
*/ |
|
312 |
int _exec_wr(ostringstream& cmd, int federated); |
|
313 |
|
|
314 |
/** |
|
315 |
* Callback to store the IDs of federated records in the federated log. |
|
316 |
*/ |
|
317 |
int index_cb(void *null, int num, char **values, char **names); |
|
318 |
|
|
319 |
/** |
|
258 | 320 |
* Applies the SQL command of the given record to the database. The |
259 | 321 |
* timestamp of the record is updated. |
260 | 322 |
* @param lr the log record |
... | ... | |
267 | 329 |
* @param term for the log entry |
268 | 330 |
* @param sql command to modify DB state |
269 | 331 |
* @param ts timestamp of record application to DB state |
332 |
* @param fi the federated index -1 if none |
|
270 | 333 |
* |
271 | 334 |
* @return 0 on success |
272 | 335 |
*/ |
273 |
int insert(int index, int term, const std::string& sql, time_t ts); |
|
336 |
int insert(int index, int term, const std::string& sql, time_t ts, int fi);
|
|
274 | 337 |
|
275 | 338 |
/** |
276 | 339 |
* Inserts a new log record in the database. If the record is successfully |
... | ... | |
278 | 341 |
* @param term for the record |
279 | 342 |
* @param sql command of the record |
280 | 343 |
* @param timestamp associated to this record |
344 |
* @param federated, if true it will set fed_index == index, -1 otherwise |
|
281 | 345 |
* |
282 | 346 |
* @return -1 on failure, index of the inserted record on success |
283 | 347 |
*/ |
284 | 348 |
int insert_log_record(unsigned int term, std::ostringstream& sql, |
285 |
time_t timestamp); |
|
349 |
time_t timestamp, int federated);
|
|
286 | 350 |
}; |
287 | 351 |
|
288 | 352 |
// ----------------------------------------------------------------------------- |
Also available in: Unified diff