mirror of https://github.com/grafana/grafana
Chore: Move annotations cleanup to the annotations service (#55618)
parent
4739ff06a5
commit
2f14575dd3
@ -0,0 +1,62 @@ |
|||||||
|
package annotationsimpl |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/infra/log" |
||||||
|
"github.com/grafana/grafana/pkg/services/sqlstore/db" |
||||||
|
"github.com/grafana/grafana/pkg/setting" |
||||||
|
) |
||||||
|
|
||||||
|
// CleanupServiceImpl is responsible for cleaning old annotations.
|
||||||
|
type CleanupServiceImpl struct { |
||||||
|
store store |
||||||
|
} |
||||||
|
|
||||||
|
func ProvideCleanupService(db db.DB, cfg *setting.Cfg) *CleanupServiceImpl { |
||||||
|
return &CleanupServiceImpl{ |
||||||
|
store: &xormRepositoryImpl{ |
||||||
|
cfg: cfg, |
||||||
|
db: db, |
||||||
|
log: log.New("annotations"), |
||||||
|
}, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const ( |
||||||
|
alertAnnotationType = "alert_id <> 0" |
||||||
|
dashboardAnnotationType = "dashboard_id <> 0 AND alert_id = 0" |
||||||
|
apiAnnotationType = "alert_id = 0 AND dashboard_id = 0" |
||||||
|
) |
||||||
|
|
||||||
|
// Run deletes old annotations created by alert rules, API
|
||||||
|
// requests and human made in the UI. It subsequently deletes orphaned rows
|
||||||
|
// from the annotation_tag table. Cleanup actions are performed in batches
|
||||||
|
// so that no query takes too long to complete.
|
||||||
|
//
|
||||||
|
// Returns the number of annotation and annotation_tag rows deleted. If an
|
||||||
|
// error occurs, it returns the number of rows affected so far.
|
||||||
|
func (cs *CleanupServiceImpl) Run(ctx context.Context, cfg *setting.Cfg) (int64, int64, error) { |
||||||
|
var totalCleanedAnnotations int64 |
||||||
|
affected, err := cs.store.CleanAnnotations(ctx, cfg.AlertingAnnotationCleanupSetting, alertAnnotationType) |
||||||
|
totalCleanedAnnotations += affected |
||||||
|
if err != nil { |
||||||
|
return totalCleanedAnnotations, 0, err |
||||||
|
} |
||||||
|
|
||||||
|
affected, err = cs.store.CleanAnnotations(ctx, cfg.APIAnnotationCleanupSettings, apiAnnotationType) |
||||||
|
totalCleanedAnnotations += affected |
||||||
|
if err != nil { |
||||||
|
return totalCleanedAnnotations, 0, err |
||||||
|
} |
||||||
|
|
||||||
|
affected, err = cs.store.CleanAnnotations(ctx, cfg.DashboardAnnotationCleanupSettings, dashboardAnnotationType) |
||||||
|
totalCleanedAnnotations += affected |
||||||
|
if err != nil { |
||||||
|
return totalCleanedAnnotations, 0, err |
||||||
|
} |
||||||
|
if totalCleanedAnnotations > 0 { |
||||||
|
affected, err = cs.store.CleanOrphanedAnnotationTags(ctx) |
||||||
|
} |
||||||
|
return totalCleanedAnnotations, affected, err |
||||||
|
} |
||||||
@ -0,0 +1,18 @@ |
|||||||
|
package annotationstest |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/setting" |
||||||
|
) |
||||||
|
|
||||||
|
type fakeCleaner struct { |
||||||
|
} |
||||||
|
|
||||||
|
func NewFakeCleaner() *fakeCleaner { |
||||||
|
return &fakeCleaner{} |
||||||
|
} |
||||||
|
|
||||||
|
func (f *fakeCleaner) Run(ctx context.Context, cfg *setting.Cfg) (int64, int64, error) { |
||||||
|
return 0, 0, nil |
||||||
|
} |
||||||
@ -1,116 +0,0 @@ |
|||||||
package sqlstore |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"fmt" |
|
||||||
"time" |
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/infra/log" |
|
||||||
"github.com/grafana/grafana/pkg/setting" |
|
||||||
) |
|
||||||
|
|
||||||
// AnnotationCleanupService is responsible for cleaning old annotations.
|
|
||||||
type AnnotationCleanupService struct { |
|
||||||
batchSize int64 |
|
||||||
log log.Logger |
|
||||||
sqlstore *SQLStore |
|
||||||
} |
|
||||||
|
|
||||||
const ( |
|
||||||
alertAnnotationType = "alert_id <> 0" |
|
||||||
dashboardAnnotationType = "dashboard_id <> 0 AND alert_id = 0" |
|
||||||
apiAnnotationType = "alert_id = 0 AND dashboard_id = 0" |
|
||||||
) |
|
||||||
|
|
||||||
// CleanAnnotations deletes old annotations created by alert rules, API
|
|
||||||
// requests and human made in the UI. It subsequently deletes orphaned rows
|
|
||||||
// from the annotation_tag table. Cleanup actions are performed in batches
|
|
||||||
// so that no query takes too long to complete.
|
|
||||||
//
|
|
||||||
// Returns the number of annotation and annotation_tag rows deleted. If an
|
|
||||||
// error occurs, it returns the number of rows affected so far.
|
|
||||||
func (acs *AnnotationCleanupService) CleanAnnotations(ctx context.Context, cfg *setting.Cfg) (int64, int64, error) { |
|
||||||
var totalCleanedAnnotations int64 |
|
||||||
affected, err := acs.cleanAnnotations(ctx, cfg.AlertingAnnotationCleanupSetting, alertAnnotationType) |
|
||||||
totalCleanedAnnotations += affected |
|
||||||
if err != nil { |
|
||||||
return totalCleanedAnnotations, 0, err |
|
||||||
} |
|
||||||
|
|
||||||
affected, err = acs.cleanAnnotations(ctx, cfg.APIAnnotationCleanupSettings, apiAnnotationType) |
|
||||||
totalCleanedAnnotations += affected |
|
||||||
if err != nil { |
|
||||||
return totalCleanedAnnotations, 0, err |
|
||||||
} |
|
||||||
|
|
||||||
affected, err = acs.cleanAnnotations(ctx, cfg.DashboardAnnotationCleanupSettings, dashboardAnnotationType) |
|
||||||
totalCleanedAnnotations += affected |
|
||||||
if err != nil { |
|
||||||
return totalCleanedAnnotations, 0, err |
|
||||||
} |
|
||||||
if totalCleanedAnnotations > 0 { |
|
||||||
affected, err = acs.cleanOrphanedAnnotationTags(ctx) |
|
||||||
} |
|
||||||
return totalCleanedAnnotations, affected, err |
|
||||||
} |
|
||||||
|
|
||||||
func (acs *AnnotationCleanupService) cleanAnnotations(ctx context.Context, cfg setting.AnnotationCleanupSettings, annotationType string) (int64, error) { |
|
||||||
var totalAffected int64 |
|
||||||
if cfg.MaxAge > 0 { |
|
||||||
cutoffDate := time.Now().Add(-cfg.MaxAge).UnixNano() / int64(time.Millisecond) |
|
||||||
deleteQuery := `DELETE FROM annotation WHERE id IN (SELECT id FROM (SELECT id FROM annotation WHERE %s AND created < %v ORDER BY id DESC %s) a)` |
|
||||||
sql := fmt.Sprintf(deleteQuery, annotationType, cutoffDate, dialect.Limit(acs.batchSize)) |
|
||||||
|
|
||||||
affected, err := acs.executeUntilDoneOrCancelled(ctx, sql) |
|
||||||
totalAffected += affected |
|
||||||
if err != nil { |
|
||||||
return totalAffected, err |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
if cfg.MaxCount > 0 { |
|
||||||
deleteQuery := `DELETE FROM annotation WHERE id IN (SELECT id FROM (SELECT id FROM annotation WHERE %s ORDER BY id DESC %s) a)` |
|
||||||
sql := fmt.Sprintf(deleteQuery, annotationType, dialect.LimitOffset(acs.batchSize, cfg.MaxCount)) |
|
||||||
affected, err := acs.executeUntilDoneOrCancelled(ctx, sql) |
|
||||||
totalAffected += affected |
|
||||||
return totalAffected, err |
|
||||||
} |
|
||||||
|
|
||||||
return totalAffected, nil |
|
||||||
} |
|
||||||
|
|
||||||
func (acs *AnnotationCleanupService) cleanOrphanedAnnotationTags(ctx context.Context) (int64, error) { |
|
||||||
deleteQuery := `DELETE FROM annotation_tag WHERE id IN ( SELECT id FROM (SELECT id FROM annotation_tag WHERE NOT EXISTS (SELECT 1 FROM annotation a WHERE annotation_id = a.id) %s) a)` |
|
||||||
sql := fmt.Sprintf(deleteQuery, dialect.Limit(acs.batchSize)) |
|
||||||
return acs.executeUntilDoneOrCancelled(ctx, sql) |
|
||||||
} |
|
||||||
|
|
||||||
func (acs *AnnotationCleanupService) executeUntilDoneOrCancelled(ctx context.Context, sql string) (int64, error) { |
|
||||||
var totalAffected int64 |
|
||||||
for { |
|
||||||
select { |
|
||||||
case <-ctx.Done(): |
|
||||||
return totalAffected, ctx.Err() |
|
||||||
default: |
|
||||||
var affected int64 |
|
||||||
err := withDbSession(ctx, acs.sqlstore.engine, func(session *DBSession) error { |
|
||||||
res, err := session.Exec(sql) |
|
||||||
if err != nil { |
|
||||||
return err |
|
||||||
} |
|
||||||
|
|
||||||
affected, err = res.RowsAffected() |
|
||||||
totalAffected += affected |
|
||||||
|
|
||||||
return err |
|
||||||
}) |
|
||||||
if err != nil { |
|
||||||
return totalAffected, err |
|
||||||
} |
|
||||||
|
|
||||||
if affected == 0 { |
|
||||||
return totalAffected, nil |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue