Commit 2ecc7f22 by Dan Cech Committed by GitHub

delete old dashboard versions in multiple batches (#23348)

parent d94796a0
...@@ -67,49 +67,68 @@ func GetDashboardVersions(query *models.GetDashboardVersionsQuery) error { ...@@ -67,49 +67,68 @@ func GetDashboardVersions(query *models.GetDashboardVersionsQuery) error {
return nil return nil
} }
const MAX_VERSIONS_TO_DELETE = 100 const MAX_VERSIONS_TO_DELETE_PER_BATCH = 100
const MAX_VERSION_DELETION_BATCHES = 50
func DeleteExpiredVersions(cmd *models.DeleteExpiredVersionsCommand) error { func DeleteExpiredVersions(cmd *models.DeleteExpiredVersionsCommand) error {
return inTransaction(func(sess *DBSession) error { return deleteExpiredVersions(cmd, MAX_VERSIONS_TO_DELETE_PER_BATCH, MAX_VERSION_DELETION_BATCHES)
versionsToKeep := setting.DashboardVersionsToKeep }
if versionsToKeep < 1 {
versionsToKeep = 1
}
// Idea of this query is finding version IDs to delete based on formula: func deleteExpiredVersions(cmd *models.DeleteExpiredVersionsCommand, perBatch int, maxBatches int) error {
// min_version_to_keep = min_version + (versions_count - versions_to_keep) versionsToKeep := setting.DashboardVersionsToKeep
// where version stats is processed for each dashboard. This guarantees that we keep at least versions_to_keep if versionsToKeep < 1 {
// versions, but in some cases (when versions are sparse) this number may be more. versionsToKeep = 1
versionIdsToDeleteQuery := `SELECT id }
FROM dashboard_version, (
SELECT dashboard_id, count(version) as count, min(version) as min
FROM dashboard_version
GROUP BY dashboard_id
) AS vtd
WHERE dashboard_version.dashboard_id=vtd.dashboard_id
AND version < vtd.min + vtd.count - ?`
var versionIdsToDelete []interface{}
err := sess.SQL(versionIdsToDeleteQuery, versionsToKeep).Find(&versionIdsToDelete)
if err != nil {
return err
}
// Don't delete more than MAX_VERSIONS_TO_DELETE version per time for batch := 0; batch < maxBatches; batch++ {
if len(versionIdsToDelete) > MAX_VERSIONS_TO_DELETE { deleted := int64(0)
versionIdsToDelete = versionIdsToDelete[:MAX_VERSIONS_TO_DELETE]
} batchErr := inTransaction(func(sess *DBSession) error {
// Idea of this query is finding version IDs to delete based on formula:
// min_version_to_keep = min_version + (versions_count - versions_to_keep)
// where version stats is processed for each dashboard. This guarantees that we keep at least versions_to_keep
// versions, but in some cases (when versions are sparse) this number may be more.
versionIdsToDeleteQuery := `SELECT id
FROM dashboard_version, (
SELECT dashboard_id, count(version) as count, min(version) as min
FROM dashboard_version
GROUP BY dashboard_id
) AS vtd
WHERE dashboard_version.dashboard_id=vtd.dashboard_id
AND version < vtd.min + vtd.count - ?
LIMIT ?`
var versionIdsToDelete []interface{}
err := sess.SQL(versionIdsToDeleteQuery, versionsToKeep, perBatch).Find(&versionIdsToDelete)
if err != nil {
return err
}
if len(versionIdsToDelete) < 1 {
return nil
}
if len(versionIdsToDelete) > 0 {
deleteExpiredSql := `DELETE FROM dashboard_version WHERE id IN (?` + strings.Repeat(",?", len(versionIdsToDelete)-1) + `)` deleteExpiredSql := `DELETE FROM dashboard_version WHERE id IN (?` + strings.Repeat(",?", len(versionIdsToDelete)-1) + `)`
sqlOrArgs := append([]interface{}{deleteExpiredSql}, versionIdsToDelete...) sqlOrArgs := append([]interface{}{deleteExpiredSql}, versionIdsToDelete...)
expiredResponse, err := sess.Exec(sqlOrArgs...) expiredResponse, err := sess.Exec(sqlOrArgs...)
if err != nil { if err != nil {
return err return err
} }
cmd.DeletedRows, _ = expiredResponse.RowsAffected()
deleted, err = expiredResponse.RowsAffected()
return err
})
if batchErr != nil {
return batchErr
} }
return nil cmd.DeletedRows += deleted
})
if deleted < int64(perBatch) {
break
}
}
return nil
} }
...@@ -131,7 +131,7 @@ func TestDeleteExpiredVersions(t *testing.T) { ...@@ -131,7 +131,7 @@ func TestDeleteExpiredVersions(t *testing.T) {
So(query.Result[0].Version, ShouldEqual, versionsToWrite) So(query.Result[0].Version, ShouldEqual, versionsToWrite)
}) })
Convey("Don't delete anything if there're no expired versions", func() { Convey("Don't delete anything if there are no expired versions", func() {
setting.DashboardVersionsToKeep = versionsToWrite setting.DashboardVersionsToKeep = versionsToWrite
err := DeleteExpiredVersions(&models.DeleteExpiredVersionsCommand{}) err := DeleteExpiredVersions(&models.DeleteExpiredVersionsCommand{})
...@@ -144,15 +144,18 @@ func TestDeleteExpiredVersions(t *testing.T) { ...@@ -144,15 +144,18 @@ func TestDeleteExpiredVersions(t *testing.T) {
So(len(query.Result), ShouldEqual, versionsToWrite) So(len(query.Result), ShouldEqual, versionsToWrite)
}) })
Convey("Don't delete more than MAX_VERSIONS_TO_DELETE per iteration", func() { Convey("Don't delete more than MAX_VERSIONS_TO_DELETE_PER_BATCH * MAX_VERSION_DELETION_BATCHES per iteration", func() {
versionsToWriteBigNumber := MAX_VERSIONS_TO_DELETE + versionsToWrite perBatch := 10
maxBatches := 10
versionsToWriteBigNumber := perBatch*maxBatches + versionsToWrite
for i := 0; i < versionsToWriteBigNumber-versionsToWrite; i++ { for i := 0; i < versionsToWriteBigNumber-versionsToWrite; i++ {
updateTestDashboard(savedDash, map[string]interface{}{ updateTestDashboard(savedDash, map[string]interface{}{
"tags": "different-tag", "tags": "different-tag",
}) })
} }
err := DeleteExpiredVersions(&models.DeleteExpiredVersionsCommand{}) err := deleteExpiredVersions(&models.DeleteExpiredVersionsCommand{}, perBatch, maxBatches)
So(err, ShouldBeNil) So(err, ShouldBeNil)
query := models.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1, Limit: versionsToWriteBigNumber} query := models.GetDashboardVersionsQuery{DashboardId: savedDash.Id, OrgId: 1, Limit: versionsToWriteBigNumber}
...@@ -161,8 +164,8 @@ func TestDeleteExpiredVersions(t *testing.T) { ...@@ -161,8 +164,8 @@ func TestDeleteExpiredVersions(t *testing.T) {
// Ensure we have at least versionsToKeep versions // Ensure we have at least versionsToKeep versions
So(len(query.Result), ShouldBeGreaterThanOrEqualTo, versionsToKeep) So(len(query.Result), ShouldBeGreaterThanOrEqualTo, versionsToKeep)
// Ensure we haven't deleted more than MAX_VERSIONS_TO_DELETE rows // Ensure we haven't deleted more than perBatch * maxBatches rows
So(versionsToWriteBigNumber-len(query.Result), ShouldBeLessThanOrEqualTo, MAX_VERSIONS_TO_DELETE) So(versionsToWriteBigNumber-len(query.Result), ShouldBeLessThanOrEqualTo, perBatch*maxBatches)
}) })
}) })
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment