Binlog: call fsync only if it is necessary

GitOrigin-RevId: 2081799ad27c62950d029ebeb13d19f343be024d
This commit is contained in:
Arseny Smirnov 2018-02-16 12:13:04 +03:00
parent ccb6553a5d
commit 33a4d428f9
2 changed files with 20 additions and 9 deletions

View File

@ -258,10 +258,12 @@ Status Binlog::close(bool need_sync) {
path_ = ""; path_ = "";
info_.is_opened = false; info_.is_opened = false;
fd_.close(); fd_.close();
need_sync_ = false;
}; };
flush();
if (need_sync) { if (need_sync) {
TRY_STATUS(fd_.sync()); sync();
} else {
flush();
} }
return Status::OK(); return Status::OK();
} }
@ -353,7 +355,11 @@ void Binlog::do_event(BinlogEvent &&event) {
void Binlog::sync() { void Binlog::sync() {
flush(); flush();
fd_.sync().ensure(); if (need_sync_) {
auto status = fd_.sync();
LOG_IF(FATAL, status.is_error()) << "Failed to sync binlog: " << status;
need_sync_ = false;
}
} }
void Binlog::flush() { void Binlog::flush() {
@ -365,8 +371,14 @@ void Binlog::flush() {
if (byte_flow_flag_) { if (byte_flow_flag_) {
byte_flow_source_.wakeup(); byte_flow_source_.wakeup();
} }
fd_.flush_write().ensure(); auto r_written = fd_.flush_write();
r_written.ensure();
auto written = r_written.ok();
if (written > 0) {
need_sync_ = true;
}
need_flush_since_ = 0; need_flush_since_ = 0;
LOG_IF(FATAL, fd_.need_flush_write()) << "Failed to flush binlog";
} }
void Binlog::lazy_flush() { void Binlog::lazy_flush() {
@ -584,13 +596,11 @@ void Binlog::do_reindex() {
processor_->for_each([&](BinlogEvent &event) { processor_->for_each([&](BinlogEvent &event) {
do_event(std::move(event)); // NB: no move is actually happens do_event(std::move(event)); // NB: no move is actually happens
}); });
flush(); need_sync_ = true; // must sync creation of the file
LOG_IF(FATAL, fd_.need_flush_write()) << "Reindex failed: failed to flush everything on disk"; sync();
auto status = fd_.sync();
LOG_IF(FATAL, status.is_error()) << "Failed to sync binlog: " << status;
// finish_reindex // finish_reindex
status = unlink(path_); auto status = unlink(path_);
LOG_IF(FATAL, status.is_error()) << "Failed to unlink old binlog: " << status; LOG_IF(FATAL, status.is_error()) << "Failed to unlink old binlog: " << status;
status = rename(new_path, path_); status = rename(new_path, path_);
LOG_IF(FATAL, status.is_error()) << "Failed to rename binlog: " << status; LOG_IF(FATAL, status.is_error()) << "Failed to rename binlog: " << status;

View File

@ -123,6 +123,7 @@ class Binlog {
bool in_flush_events_buffer_{false}; bool in_flush_events_buffer_{false};
uint64 last_id_{0}; uint64 last_id_{0};
double need_flush_since_ = 0; double need_flush_since_ = 0;
bool need_sync_{false};
enum class State { Empty, Load, Reindex, Run } state_{State::Empty}; enum class State { Empty, Load, Reindex, Run } state_{State::Empty};
static constexpr uint32 MAX_EVENT_SIZE = 65536; static constexpr uint32 MAX_EVENT_SIZE = 65536;