1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-16 22:50:10 +03:00

B #2144: Better purge for federated records

(cherry picked from commit 6ba048c9d370ded7ee365e45262fa50e09a54605)
This commit is contained in:
Ruben S. Montero 2018-06-17 12:43:09 +02:00
parent 16b264d1f2
commit aaf5e444d5
3 changed files with 46 additions and 67 deletions

View File

@ -155,7 +155,7 @@ public:
/**
* Purge log records. Delete old records applied to database upto the
* LOG_RETENTION configuration variable.
* @return 0 on success
* @return number of records deleted from DB
*/
int purge_log();

View File

@ -811,18 +811,11 @@ void RaftManager::timer_action(const ActionRequest& ar)
int rc = logdb->purge_log();
if ( rc != 0 ) //logs removed, wakeup again in 60s to purge reaming
purge_tics = 0;
if (rc > 0 && purge_period_ms > 60000) //logs removed, wakeup in 60s
{
purge_tics = (int) ((purge_period_ms - 60000)/timer_period_ms);
if ( purge_tics < 0 )
{
purge_tics = 0;
}
}
else
{
purge_tics = 0;
}
oss << rc << " records purged";

View File

@ -563,85 +563,71 @@ int LogDB::purge_log()
{
std::ostringstream oss;
int rc = 0;
empty_cb cb;
int fed_records_delete = 0;
int records_delete = 0 ;
int rc = 0;
pthread_mutex_lock(&mutex);
/* ---------------------------------------------------------------------- */
/* Non-federated records */
/* Non-federated records. Keep last log_retention records */
/* ---------------------------------------------------------------------- */
int delete_index = last_applied - log_retention;
oss << "DELETE FROM logdb WHERE timestamp > 0 AND log_index >= 0 "
<< "AND fed_index = -1 AND log_index < ("
<< " SELECT MIN(i.log_index) FROM ("
<< " SELECT log_index FROM logdb WHERE fed_index = -1 AND"
<< " timestamp > 0 AND log_index >= 0 "
<< " ORDER BY log_index DESC LIMIT " << log_retention
<< " ) AS i"
<< ")";
if ( delete_index > 0 )
if ( db->limit_support() )
{
empty_cb cb;
oss << " LIMIT " << limit_purge;
}
// keep the last "log_retention" records as well as those not applied
oss << "DELETE FROM logdb WHERE timestamp > 0 AND log_index >= 0 "
<< "AND fed_index = -1 AND log_index < ("
<< " SELECT MIN(i.log_index) FROM ("
<< " SELECT log_index FROM logdb WHERE fed_index = -1 AND"
<< " timestamp > 0 AND log_index >= 0 "
<< " ORDER BY log_index DESC LIMIT " << log_retention
<< " ) AS i"
<< ")";
if ( db->limit_support() )
{
oss << " LIMIT " << limit_purge;
}
rc = db->exec_wr(oss, &cb);
if ( rc != -1 )
{
records_delete = cb.get_affected_rows();
}
if ( db->exec_wr(oss, &cb) != -1 )
{
rc = cb.get_affected_rows();
}
/* ---------------------------------------------------------------------- */
/* Federated records */
/* Federated records. Keep last log_retention federated records */
/* ---------------------------------------------------------------------- */
if ( fed_log.size() < log_retention)
if ( fed_log.size() < log_retention )
{
fed_records_delete = 0;
}
else
{
fed_records_delete = fed_log.size() - log_retention;
pthread_mutex_unlock(&mutex);
if ( fed_records_delete > limit_purge )
{
fed_records_delete = limit_purge;
}
return rc;
}
std::set<int>::iterator it = fed_log.begin();
cb.set_affected_rows(0);
for (int i=0; i < fed_records_delete; ++i)
oss.str("");
oss << "DELETE FROM logdb WHERE timestamp > 0 AND log_index >= 0 "
<< "AND fed_index != -1 AND log_index < ("
<< " SELECT MIN(i.log_index) FROM ("
<< " SELECT log_index FROM logdb WHERE fed_index != -1 AND"
<< " timestamp > 0 AND log_index >= 0 "
<< " ORDER BY log_index DESC LIMIT " << log_retention
<< " ) AS i"
<< ")";
if ( db->limit_support() )
{
oss.str("");
oss << "DELETE FROM logdb WHERE timestamp > 0 AND log_index >= 0 "
<< "AND fed_index = " << *it;
db->exec_wr(oss);
it = fed_log.erase(it);
oss << " LIMIT " << limit_purge;
}
if ( db->exec_wr(oss, &cb) != -1 )
{
rc += cb.get_affected_rows();
}
build_federated_index();
pthread_mutex_unlock(&mutex);
records_delete += fed_records_delete ;
if ( records_delete > 0 )
{
return records_delete;
}
return rc;
}