|
14 | 14 | #include "infoLoggerMessage.h" |
15 | 15 | #include <unistd.h> |
16 | 16 | #include <string.h> |
| 17 | +#include <Common/Timer.h> |
17 | 18 |
|
18 | 19 | #if LIBMYSQL_VERSION_ID >= 80000 |
19 | 20 | typedef bool my_bool; |
@@ -51,6 +52,12 @@ class InfoLoggerDispatchSQLImpl |
51 | 52 |
|
52 | 53 | int connectDB(); // function to connect to database |
53 | 54 | int disconnectDB(); // disconnect/cleanup DB connection |
| 55 | + |
| 56 | + int commitEnabled = 1; // flag to enable transactions |
| 57 | + int commitDebug = 0; // log transactions |
| 58 | + int commitTimeout = 1000000; // time between commits |
| 59 | + Timer commitTimer; // timer for transaction |
| 60 | + int commitNumberOfMsg; // number of messages since last commit |
54 | 61 | }; |
55 | 62 |
|
56 | 63 | void InfoLoggerDispatchSQLImpl::start() |
@@ -201,6 +208,9 @@ int InfoLoggerDispatchSQLImpl::connectDB() |
201 | 208 | disconnectDB(); |
202 | 209 | return -1; |
203 | 210 | } |
| 211 | + |
| 212 | + // reset transactions |
| 213 | + commitNumberOfMsg = 0; |
204 | 214 | } |
205 | 215 |
|
206 | 216 | return 0; |
@@ -229,7 +239,23 @@ int InfoLoggerDispatchSQLImpl::customLoop() |
229 | 239 | if (err) { |
230 | 240 | // temporization to avoid immediate retry |
231 | 241 | sleep(SQL_RETRY_CONNECT); |
| 242 | + } else if (commitEnabled) { |
| 243 | + // complete pending transactions |
| 244 | + if (commitNumberOfMsg) { |
| 245 | + if (commitTimer.isTimeout()) { |
| 246 | + if (mysql_query (db, "COMMIT")) { |
| 247 | + theLog->error("DB transaction commit failed: %s", mysql_error(db)); |
| 248 | + commitEnabled = 0; |
| 249 | + } else { |
| 250 | + if (commitDebug) { |
| 251 | + theLog->info("DB commit - %d msgs", commitNumberOfMsg); |
| 252 | + } |
| 253 | + } |
| 254 | + commitNumberOfMsg = 0; |
| 255 | + } |
| 256 | + } |
232 | 257 | } |
| 258 | + |
233 | 259 | return err; |
234 | 260 | } |
235 | 261 |
|
@@ -274,6 +300,21 @@ int InfoLoggerDispatchSQLImpl::customMessageProcess(std::shared_ptr<InfoLoggerMe |
274 | 300 | } |
275 | 301 | } |
276 | 302 |
|
| 303 | + if (commitEnabled) { |
| 304 | + if (commitNumberOfMsg == 0) { |
| 305 | + if (mysql_query (db, "START TRANSACTION")) { |
| 306 | + theLog->error("DB start transaction failed: %s", mysql_error(db)); |
| 307 | + commitEnabled = 0; |
| 308 | + return -1; |
| 309 | + } else { |
| 310 | + if (commitDebug) { |
| 311 | + theLog->info("DB transaction started"); |
| 312 | + } |
| 313 | + } |
| 314 | + commitTimer.reset(commitTimeout); |
| 315 | + } |
| 316 | + } |
| 317 | + |
277 | 318 | // re-format message with multiple line - assumes it is the LAST field in the protocol |
278 | 319 | for (msg = (char*)m->values[nFields - 1].value.vString; msg != NULL; msg = nl) { |
279 | 320 | nl = strchr(msg, '\f'); |
@@ -303,11 +344,14 @@ int InfoLoggerDispatchSQLImpl::customMessageProcess(std::shared_ptr<InfoLoggerMe |
303 | 344 | } |
304 | 345 |
|
305 | 346 | insertCount++; |
306 | | - /* |
307 | | -if (insertCount%1000==0) { |
308 | | - theLog->info("insert count = %llu",insertCount); |
309 | | - } |
310 | | - */ |
| 347 | + commitNumberOfMsg++; |
| 348 | + |
| 349 | + if (commitDebug) { |
| 350 | + if (insertCount%1000==0) { |
| 351 | + theLog->info("insert count = %llu",insertCount); |
| 352 | + } |
| 353 | + } |
| 354 | + |
311 | 355 | } |
312 | 356 | } |
313 | 357 | return 0; |
|
0 commit comments