mirror of
https://github.com/OpenNebula/one.git
synced 2024-12-23 17:33:56 +03:00
F #4809: Better query to purge log records
This commit is contained in:
parent
75b8889e08
commit
234734a3c7
@ -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
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
Loading…
Reference in New Issue
Block a user