Skip to content

Commit

Permalink
Fix/delete old snapshot (SigNoz#1621)
Browse files Browse the repository at this point in the history
* fix: remove old snapshots
  • Loading branch information
nityanandagohain authored Oct 7, 2022
1 parent f2f2069 commit fe0a4ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ee/query-service/usage/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func (lm *Manager) UsageExporter(ctx context.Context) {
lm.CollectAndStoreUsage(ctx)
case <-uploadTicker.C:
lm.UploadUsage(ctx)
// remove the old snapshots
lm.repository.DropOldSnapshots(ctx)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions ee/query-service/usage/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

const (
MaxFailedSyncCount = 9 // a snapshot will be ignored if the max failed count is greater than or equal to 9
SnapShotLife = 3 * 24 * time.Hour
)

// Repository is usage Repository which stores usage snapshot in a secured DB
Expand Down Expand Up @@ -111,6 +112,18 @@ func (r *Repository) GetSnapshotsNotSynced(ctx context.Context) ([]*model.Usage,
return snapshots, nil
}

func (r *Repository) DropOldSnapshots(ctx context.Context) error {
query := `delete from usage where created_at <= $1`

_, err := r.db.ExecContext(ctx, query, time.Now().Add(-(SnapShotLife)))
if err != nil {
zap.S().Errorf("failed to remove old snapshots from db: %v", zap.Error(err))
return err
}

return nil
}

// CheckSnapshotGtCreatedAt checks if there is any snapshot greater than the provided timestamp
func (r *Repository) CheckSnapshotGtCreatedAt(ctx context.Context, ts time.Time) (bool, error) {

Expand Down

0 comments on commit fe0a4ab

Please sign in to comment.