diff --git a/include/FedReplicaManager.h b/include/FedReplicaManager.h index c881844765..9a7e7e4bcd 100644 --- a/include/FedReplicaManager.h +++ b/include/FedReplicaManager.h @@ -38,7 +38,7 @@ public: * @param d pointer to underlying DB (LogDB) * @param l log_retention length (num records) */ - FedReplicaManager(time_t _t, time_t _p, SqlDB * d, const std::string& l); + FedReplicaManager(time_t _t, time_t _p, SqlDB * d, unsigned int l); virtual ~FedReplicaManager(); @@ -203,7 +203,7 @@ private: SqlDB * logdb; - std::string log_retention; + unsigned int log_retention; // ------------------------------------------------------------------------- // Action Listener interface diff --git a/include/LogDB.h b/include/LogDB.h index b68f2157ba..166a11df5d 100644 --- a/include/LogDB.h +++ b/include/LogDB.h @@ -93,7 +93,7 @@ private: class LogDB : public SqlDB { public: - LogDB(SqlDB * _db, bool solo, const std::string& log_retention); + LogDB(SqlDB * _db, bool solo, unsigned int log_retention); virtual ~LogDB(); @@ -267,7 +267,7 @@ private: /** * Max number of records to keep in the database */ - std::string log_retention; + unsigned int log_retention; // ------------------------------------------------------------------------- // DataBase implementation diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index ddf10f1cb3..c5f4c1a2fd 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -231,12 +231,13 @@ void Nebula::start(bool bootstrap_only) time_t xmlrpc_ms; time_t log_purge; - string log_retention = vatt->vector_value("LOG_RETENTION"); + unsigned int log_retention; vatt->vector_value("LOG_PURGE_TIMEOUT", log_purge); vatt->vector_value("ELECTION_TIMEOUT_MS", election_ms); vatt->vector_value("BROADCAST_TIMEOUT_MS", bcast_ms); vatt->vector_value("XMLRPC_TIMEOUT_MS", xmlrpc_ms); + vatt->vector_value("LOG_RETENTION", log_retention); Log::set_zone_id(zone_id); diff --git a/src/raft/FedReplicaManager.cc b/src/raft/FedReplicaManager.cc index c7a7cae9c0..86aa9f6016 100644 --- a/src/raft/FedReplicaManager.cc +++ b/src/raft/FedReplicaManager.cc @@ -35,7 +35,7 @@ const time_t FedReplicaManager::xmlrpc_timeout_ms = 10000; /* -------------------------------------------------------------------------- */ FedReplicaManager::FedReplicaManager(time_t _t, time_t _p, SqlDB * d, - const std::string& l): ReplicaManager(), timer_period(_t), purge_period(_p), + unsigned int l): ReplicaManager(), timer_period(_t), purge_period(_p), last_index(-1), logdb(d), log_retention(l) { pthread_mutex_init(&mutex, 0); @@ -303,12 +303,16 @@ void FedReplicaManager::timer_action(const ActionRequest& ar) pthread_mutex_lock(&mutex); - // keep the last "log_retention" records - oss << "DELETE FROM fed_logdb WHERE log_index NOT IN (SELECT " - << "log_index FROM fed_logdb ORDER BY log_index DESC LIMIT " - << log_retention <<")"; + if ( last_index > log_retention ) + { + unsigned int delete_index = last_index - log_retention; - logdb->exec_wr(oss); + // keep the last "log_retention" records + oss << "DELETE FROM fed_logdb WHERE log_index >= 0 AND " + << "log_index < " << delete_index; + + logdb->exec_wr(oss); + } pthread_mutex_unlock(&mutex); } diff --git a/src/sql/LogDB.cc b/src/sql/LogDB.cc index fa5237e251..3e45253f52 100644 --- a/src/sql/LogDB.cc +++ b/src/sql/LogDB.cc @@ -33,8 +33,8 @@ const char * LogDB::db_bootstrap = "CREATE TABLE IF NOT EXISTS " /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ -LogDB::LogDB(SqlDB * _db, bool _solo, const std::string& _lret):solo(_solo), - db(_db), next_index(0), last_applied(-1), last_index(-1), last_term(-1), +LogDB::LogDB(SqlDB * _db, bool _solo, unsigned int _lret):solo(_solo), db(_db), + next_index(0), last_applied(-1), last_index(-1), last_term(-1), log_retention(_lret) { int r, i; @@ -419,12 +419,27 @@ int LogDB::purge_log() { std::ostringstream oss; - // keep the last "log_retention" records as well as those not applied to DB - oss << "DELETE FROM logdb WHERE timestamp >0 AND log_index NOT IN " - << "(SELECT log_index FROM logdb ORDER BY log_index DESC LIMIT " - << log_retention <<")"; + pthread_mutex_lock(&mutex); - return db->exec_wr(oss); + if ( last_index < log_retention ) + { + pthread_mutex_unlock(&mutex); + return 0; + } + + unsigned int delete_index = last_index - log_retention; + + oss.str(""); + + // keep the last "log_retention" records as well as those not applied to DB + oss << "DELETE FROM logdb WHERE timestamp > 0 AND log_index >= 0 " + << "AND log_index < " << delete_index; + + int rc = db->exec_wr(oss); + + pthread_mutex_unlock(&mutex); + + return rc; } /* -------------------------------------------------------------------------- */