Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
2ecc7f22
Unverified
Commit
2ecc7f22
authored
Apr 06, 2020
by
Dan Cech
Committed by
GitHub
Apr 06, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
delete old dashboard versions in multiple batches (#23348)
parent
d94796a0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
38 deletions
+60
-38
pkg/services/sqlstore/dashboard_version.go
+51
-32
pkg/services/sqlstore/dashboard_version_test.go
+9
-6
No files found.
pkg/services/sqlstore/dashboard_version.go
View file @
2ecc7f22
...
@@ -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
}
}
pkg/services/sqlstore/dashboard_version_test.go
View file @
2ecc7f22
...
@@ -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
a
re 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
)
})
})
})
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment