diff --git a/include/LogDB.h b/include/LogDB.h index 3bb92e1bed..470d1fe509 100644 --- a/include/LogDB.h +++ b/include/LogDB.h @@ -161,7 +161,7 @@ public: /** * Purge log records. Delete old records applied to database upto the * LOG_RETENTION configuration variable. - * @return number of records deleted from DB + * @return number of records deleted from DB, -1 on failure */ int purge_log(); diff --git a/src/onedb/database_schema.rb b/src/onedb/database_schema.rb index c967ffabe4..1b6cbdb7ed 100644 --- a/src/onedb/database_schema.rb +++ b/src/onedb/database_schema.rb @@ -107,11 +107,7 @@ class OneDBBacKEnd "owner_u INTEGER, group_u INTEGER, other_u INTEGER", index_sql: [{ :name =>'state_oid_idx', :table => 'vm_pool', :columns => '(state, oid)' }, { :name =>'applied_idx', :table => 'logdb', :columns => '(applied)' }, - { :name =>'fed_index_idx', :table => 'logdb', :columns => '(fed_index)' }], - - index_sqlite: [{ :name =>'state_oid_idx', :table => 'vm_pool', :columns => '(state, oid)' }, - { :name =>'applied_idx', :table => 'logdb', :columns => '(applied)' }, - { :name =>'fed_index_idx', :table => 'logdb', :columns => '(fed_index)' }] + { :name =>'fed_index_idx', :table => 'logdb', :columns => '(fed_index)' }] }, "5.10.0" => { logdb: "log_index BIGINT UNSIGNED PRIMARY KEY, term INTEGER, sqlcmd MEDIUMTEXT, " << @@ -147,6 +143,15 @@ class OneDBBacKEnd }, "7.4.0" => { group_vlans: "group_oid INTEGER PRIMARY KEY, body MEDIUMTEXT" + }, + '7.6.0' => { + index_sql: [ + { :name => 'state_oid_idx', :table => 'vm_pool', :columns => '(state, oid)' }, + { :name => 'applied_idx', :table => 'logdb', :columns => '(applied)' }, + { :name => 'fed_index_idx', :table => 'logdb', :columns => '(fed_index)' }, + { :name => 'fed_applied_log_idx', :table => 'logdb', + :columns => '(fed_index, applied, log_index)' } + ] } } diff --git a/src/onedb/local/7.4.0_to_7.6.0.rb b/src/onedb/local/7.4.0_to_7.6.0.rb index 9106621afa..08ee5814a4 100644 --- a/src/onedb/local/7.4.0_to_7.6.0.rb +++ b/src/onedb/local/7.4.0_to_7.6.0.rb @@ -61,11 +61,21 @@ module Migrator oneform_provision_bodies + logdb_purge_index + log_time true end + # Cover the applied non-federated log window used by periodic purges. + def logdb_purge_index + return if @db.indexes(:logdb).key?(:fed_applied_log_idx) + + @db.add_index :logdb, [:fed_index, :applied, :log_index], + :name => :fed_applied_log_idx + end + # Migrate OneForm provision data to its ODS representation. def oneform_provision_bodies @db.transaction do diff --git a/src/onedb/onedb_backend.rb b/src/onedb/onedb_backend.rb index 28915faba0..f04e0b175d 100644 --- a/src/onedb/onedb_backend.rb +++ b/src/onedb/onedb_backend.rb @@ -392,11 +392,7 @@ class BackEndMySQL < OneDBBacKEnd end def create_idx(version = nil) - type = :index_sql - - type = :index_sqlite unless @db.server_version >= 50600 - - schema = get_schema(type, version) + schema = get_schema(:index_sql, version) schema.each do |idx| next if idx? idx @@ -411,11 +407,7 @@ class BackEndMySQL < OneDBBacKEnd end def delete_idx(version = nil) - type = :index_sql - - type = :index_sqlite unless @db.server_version >= 50600 - - schema = get_schema(type, version) + schema = get_schema(:index_sql, version) return unless schema @@ -564,9 +556,7 @@ class BackEndSQLite < OneDBBacKEnd end def create_idx(version = nil) - type = :index_sqlite - - schema = get_schema(type, version) + schema = get_schema(:index_sql, version) schema.each do |idx| next if idx? idx @@ -580,9 +570,7 @@ class BackEndSQLite < OneDBBacKEnd end def delete_idx(version = nil) - type = :index_sqlite - - schema = get_schema(type, version) + schema = get_schema(:index_sql, version) return unless schema diff --git a/src/raft/RaftManager.cc b/src/raft/RaftManager.cc index 426dd70b0d..54f2678a63 100644 --- a/src/raft/RaftManager.cc +++ b/src/raft/RaftManager.cc @@ -751,6 +751,9 @@ void RaftManager::timer_action() time_t sec = last_heartbeat.tv_sec + broadcast_timeout.tv_sec; long nsec = last_heartbeat.tv_nsec + broadcast_timeout.tv_nsec; + sec += nsec / 1000000000; + nsec %= 1000000000; + if ((sec < the_time.tv_sec) || (sec == the_time.tv_sec && nsec <= the_time.tv_nsec)) { @@ -764,6 +767,9 @@ void RaftManager::timer_action() time_t sec = last_heartbeat.tv_sec + election_timeout.tv_sec; long nsec = last_heartbeat.tv_nsec + election_timeout.tv_nsec; + sec += nsec / 1000000000; + nsec %= 1000000000; + if ((sec < the_time.tv_sec) || (sec == the_time.tv_sec && nsec <= the_time.tv_nsec)) { @@ -1105,6 +1111,8 @@ std::string& RaftManager::to_xml(std::string& raft_xml) logdb->get_last_record_index(lindex, lterm); + uint64_t findex = nd.is_federation_enabled() ? logdb->last_federated() : UINT64_MAX; + std::lock_guard lock(raft_mutex); oss << "" @@ -1127,7 +1135,7 @@ std::string& RaftManager::to_xml(std::string& raft_xml) if ( nd.is_federation_enabled() ) { - oss << "" << logdb->last_federated() << ""; + oss << "" << findex << ""; } else { diff --git a/src/sql/LogDB.cc b/src/sql/LogDB.cc index a35ba92249..c2533df88a 100644 --- a/src/sql/LogDB.cc +++ b/src/sql/LogDB.cc @@ -45,6 +45,10 @@ int LogDB::bootstrap(SqlDB *_db) rc += _db->exec_local_wr(oss); + oss.str("CREATE INDEX fed_applied_log_idx on logdb (fed_index, applied, log_index);"); + + rc += _db->exec_local_wr(oss); + return rc; }; @@ -590,6 +594,11 @@ int LogDB::apply_log_records(uint64_t commit_index) int LogDB::purge_log() { + if ( log_retention == 0 ) + { + return 0; + } + std::ostringstream oss, foss; empty_cb cb; @@ -604,7 +613,7 @@ int LogDB::purge_log() int frc = 0; uint64_t fed_min, fed_max = UINT64_MAX; - lock_guard lock(_mutex); + unique_lock lock(_mutex, std::defer_lock); /* ---------------- Record log state -------------------- */ @@ -622,18 +631,23 @@ int LogDB::purge_log() /* ---------------------------------------------------------------------- */ oss.str(""); - oss << " SELECT MIN(i.log_index) FROM (" - << " SELECT log_index FROM logdb WHERE fed_index = " << UINT64_MAX - << " AND applied = '1' " - << " ORDER BY log_index DESC " << db->limit_string(log_retention) - << " ) AS i"; + // Select the oldest retained record. + // No result means there are fewer than log_retention applied records. + oss << "SELECT log_index FROM logdb WHERE fed_index = " << UINT64_MAX + << " AND applied = '1' ORDER BY log_index DESC " + << db->limit_string(log_retention - 1, 1); cb_min_idx.set_callback(&min_idx); - db->exec_rd(oss, &cb_min_idx); + int read_rc = db->exec_rd(oss, &cb_min_idx); cb_min_idx.unset_callback(); + if ( read_rc != 0 ) + { + return -1; + } + cb.set_affected_rows(0); oss.str(""); @@ -645,9 +659,20 @@ int LogDB::purge_log() oss << " " << db->limit_string(limit_purge); } - if ( db->exec_wr(oss, &cb) != -1 ) + // Applied entries are immutable in Raft. Concurrent appends and applies + // can only increase the retained window after the boundary was selected. + if ( !min_idx.empty() ) { + lock.lock(); + + if ( db->exec_wr(oss, &cb) != 0 ) + { + return -1; + } + rc = cb.get_affected_rows(); + + lock.unlock(); } /* ---------------- Record log state -------------------- */ @@ -677,6 +702,8 @@ int LogDB::purge_log() foss << "Purging obsolete federated LogDB records: "; + lock.lock(); + if (!fed_log.empty()) { fed_min = *(fed_log.begin()); @@ -697,19 +724,26 @@ int LogDB::purge_log() return rc; } + lock.unlock(); + oss.str(""); - oss << " SELECT MIN(i.log_index) FROM (" - << " SELECT log_index FROM logdb WHERE fed_index != " << UINT64_MAX - << " AND applied = '1' " - << " ORDER BY log_index DESC " << db->limit_string(log_retention) - << " ) AS i"; + oss << "SELECT log_index FROM logdb WHERE fed_index != " << UINT64_MAX + << " AND applied = '1' ORDER BY log_index DESC " + << db->limit_string(log_retention - 1, 1); + + min_idx.clear(); cb_min_idx.set_callback(&min_idx); - db->exec_rd(oss, &cb_min_idx); + read_rc = db->exec_rd(oss, &cb_min_idx); cb_min_idx.unset_callback(); + if ( read_rc != 0 ) + { + return -1; + } + cb.set_affected_rows(0); oss.str(""); @@ -721,18 +755,29 @@ int LogDB::purge_log() oss << " " << db->limit_string(limit_purge); } - if ( db->exec_wr(oss, &cb) != -1 ) + lock.lock(); + + if ( !min_idx.empty() ) { + if ( db->exec_wr(oss, &cb) != 0 ) + { + return -1; + } + frc = cb.get_affected_rows(); rc += frc; + + build_federated_index(); } - build_federated_index(); - foss << frc << " records purged. Federated log size: " << fed_log.size() - << ". Federation log state: " << fed_min << "," << fed_max << " - " - << *(fed_log.begin()) << "," << *(fed_log.rbegin()); + << ". Federation log state: " << fed_min << "," << fed_max; + + if ( !fed_log.empty() ) + { + foss << " - " << *(fed_log.begin()) << "," << *(fed_log.rbegin()); + } NebulaLog::log("DBM", Log::INFO, foss);