| 371 | * fsync. */ |
| 372 | #define AOF_WRITE_LOG_ERROR_RATE 30 /* Seconds between errors logging. */ |
| 373 | void flushAppendOnlyFile(int force) { |
| 374 | ssize_t nwritten; |
| 375 | int sync_in_progress = 0; |
| 376 | mstime_t latency; |
| 377 | |
| 378 | if (serverTL->fRetrySetAofEvent) |
| 379 | installAofRewriteEvent(); |
| 380 | |
| 381 | if (sdslen(g_pserver->aof_buf) == 0) { |
| 382 | /* Check if we need to do fsync even the aof buffer is empty, |
| 383 | * because previously in AOF_FSYNC_EVERYSEC mode, fsync is |
| 384 | * called only when aof buffer is not empty, so if users |
| 385 | * stop write commands before fsync called in one second, |
| 386 | * the data in page cache cannot be flushed in time. */ |
| 387 | if (g_pserver->aof_fsync == AOF_FSYNC_EVERYSEC && |
| 388 | g_pserver->aof_fsync_offset != g_pserver->aof_current_size && |
| 389 | g_pserver->unixtime > g_pserver->aof_last_fsync && |
| 390 | !(sync_in_progress = aofFsyncInProgress())) { |
| 391 | goto try_fsync; |
| 392 | } else { |
| 393 | return; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (g_pserver->aof_fsync == AOF_FSYNC_EVERYSEC) |
| 398 | sync_in_progress = aofFsyncInProgress(); |
| 399 | |
| 400 | if (g_pserver->aof_fsync == AOF_FSYNC_EVERYSEC && !force) { |
| 401 | /* With this append fsync policy we do background fsyncing. |
| 402 | * If the fsync is still in progress we can try to delay |
| 403 | * the write for a couple of seconds. */ |
| 404 | if (sync_in_progress) { |
| 405 | if (g_pserver->aof_flush_postponed_start == 0) { |
| 406 | /* No previous write postponing, remember that we are |
| 407 | * postponing the flush and return. */ |
| 408 | g_pserver->aof_flush_postponed_start = g_pserver->unixtime; |
| 409 | return; |
| 410 | } else if (g_pserver->unixtime - g_pserver->aof_flush_postponed_start < 2) { |
| 411 | /* We were already waiting for fsync to finish, but for less |
| 412 | * than two seconds this is still ok. Postpone again. */ |
| 413 | return; |
| 414 | } |
| 415 | /* Otherwise fall trough, and go write since we can't wait |
| 416 | * over two seconds. */ |
| 417 | g_pserver->aof_delayed_fsync++; |
| 418 | serverLog(LL_NOTICE,"Asynchronous AOF fsync is taking too long (disk is busy?). Writing the AOF buffer without waiting for fsync to complete, this may slow down KeyDB."); |
| 419 | } |
| 420 | } |
| 421 | /* We want to perform a single write. This should be guaranteed atomic |
| 422 | * at least if the filesystem we are writing is a real physical one. |
| 423 | * While this will save us against the server being killed I don't think |
| 424 | * there is much to do about the whole server stopping for power problems |
| 425 | * or alike */ |
| 426 | |
| 427 | if (g_pserver->aof_flush_sleep && sdslen(g_pserver->aof_buf)) { |
| 428 | usleep(g_pserver->aof_flush_sleep); |
| 429 | } |
| 430 |
no test coverage detected