From 7fc9eaa5868cd6f6032f383ea8bb1bbf66d99cd1 Mon Sep 17 00:00:00 2001 From: Vinaykumar Bhat Date: Mon, 4 May 2020 11:02:16 -0700 Subject: [PATCH] remove trimmed gtid from retrieved gtid set Summary: When raft logs are trimmed as part of TruncateOpsAfter(), the gtid of the trimmed trxs are also removed from executed/logged gtids. However, when binlog files are rotated, the previous gtid written to the new file depends on what files are rotated. During binlog rotation the prev gtid are the logged/executed gtids of the instance. During relay log rotation prev gtids are the retrieved gtids of the instance. Hence, in addition to trimming logged gtids/executed gtids, we should also trim retrieved gtids. This prevents creation of holes in the instances executed_gtid set as the replicaset goes through multiple promotions over its lifetime. Reviewed By: anirbanr-fb Differential Revision: D21375044 fbshipit-source-id: 96fb274cca7 --- sql/binlog.cc | 15 +++++++++++++++ sql/rpl_gtid_state.cc | 17 +++++++++++++++-- sql/rpl_rli.cc | 41 +++++++++++++++++++++++++++++++++++++++++ sql/rpl_rli.h | 3 +++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/sql/binlog.cc b/sql/binlog.cc index 1ff4ad29fd78..0180378b1a00 100644 --- a/sql/binlog.cc +++ b/sql/binlog.cc @@ -12577,6 +12577,21 @@ int trim_logged_gtid(const std::vector& trimmed_gtids) int error = gtid_state->remove_logged_gtid_on_trim(trimmed_gtids); +#ifdef HAVE_REPLICATION + if (active_mi && active_mi->rli) + { + // Remove rli logged gtids. Note that retrieved gtid is not cleared here + // since it is going to be updated when the next gtid is fetched + error = active_mi->rli->remove_logged_gtids(trimmed_gtids); + } + else + { + // NO_LINT_DEBUG + sql_print_information("active_mi or rli is not set. Hence not trimming " + "logged gtids from rli"); + } +#endif + global_sid_lock->unlock(); return error; diff --git a/sql/rpl_gtid_state.cc b/sql/rpl_gtid_state.cc index 27f77a3c6ffa..0b1f956af110 100644 --- a/sql/rpl_gtid_state.cc +++ b/sql/rpl_gtid_state.cc @@ -456,13 +456,26 @@ enum_return_status Gtid_state::remove_logged_gtid_on_trim( const auto& first_gtid= trimmed_gtids.front(); Gtid gtid; - gtid.parse(global_sid_map, first_gtid.c_str()); + if (gtid.parse(global_sid_map, first_gtid.c_str()) != RETURN_STATUS_OK) + { + // NO_LINT_DEBUG + sql_print_error("Failed to parse gtid %s", first_gtid.c_str()); + RETURN_REPORTED_ERROR; + } + rpl_sidno first_sidno= gtid.sidno; sid_locks.lock(first_sidno); for (const auto& trimmed_gtid : trimmed_gtids) { - gtid.parse(global_sid_map, trimmed_gtid.c_str()); + if (gtid.parse(global_sid_map, trimmed_gtid.c_str()) != RETURN_STATUS_OK) + { + // NO_LINT_DEBUG + sql_print_error("Failed to parse gtid %s", trimmed_gtid.c_str()); + sid_locks.unlock(first_sidno); + RETURN_REPORTED_ERROR; + } + DBUG_ASSERT(first_sidno == gtid.sidno); if (gtid.sidno > 0) { diff --git a/sql/rpl_rli.cc b/sql/rpl_rli.cc index 6c85157003a8..b1b753e9b102 100644 --- a/sql/rpl_rli.cc +++ b/sql/rpl_rli.cc @@ -2225,6 +2225,47 @@ a file name for --relay-log-index option.", opt_relaylog_index_name); DBUG_RETURN(error); } +int Relay_log_info::remove_logged_gtids( + const std::vector& trimmed_gtids) +{ + DBUG_ENTER("Relay_log_info::remove_logged_gtid"); + global_sid_lock->assert_some_lock(); + + if (trimmed_gtids.empty()) + RETURN_OK; + + + Gtid gtid; + for (const auto& trimmed_gtid : trimmed_gtids) + { + if (gtid.parse(global_sid_map, trimmed_gtid.c_str()) != RETURN_STATUS_OK) + { + // NO_LINT_DEBUG + sql_print_error("Failed to parse gtid %s", trimmed_gtid.c_str()); + RETURN_REPORTED_ERROR; + } + + if (gtid.sidno > 0) + { + /* Remove Gtid from logged_gtid set. */ + DBUG_PRINT( + "info", ("Removing gtid(sidno:%d, gno:%lld) from rli logged gtids", + gtid.sidno, gtid.gno)); + + if (gtid_set._remove_gtid(gtid) != RETURN_STATUS_OK) + { + // NO_LINT_DEBUG + sql_print_error("Failed to remove gtid(sidno:%d, gno: %lld) from " + "rli logged gtids. ", gtid.sidno, gtid.gno); + RETURN_REPORTED_ERROR; + + } + } + } + + RETURN_OK; +} + void Relay_log_info::end_info() { DBUG_ENTER("Relay_log_info::end_info"); diff --git a/sql/rpl_rli.h b/sql/rpl_rli.h index 0b2c6f247c0a..12e3d8fb29b5 100644 --- a/sql/rpl_rli.h +++ b/sql/rpl_rli.h @@ -299,6 +299,9 @@ class Relay_log_info : public Rpl_info ret= 1; return ret; } + + int remove_logged_gtids(const std::vector& trimmed_gtids); + const Gtid_set *get_gtid_set() const { return >id_set; } Gtid_set *get_gtid_set_nc() { return >id_set; }