|
| 1 | +/* |
| 2 | +Copyright 2025 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package database |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "errors" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/dgraph-io/badger/v3" |
| 24 | + "github.com/go-logr/logr" |
| 25 | + ctrl "sigs.k8s.io/controller-runtime" |
| 26 | +) |
| 27 | + |
| 28 | +// BadgerGarbageCollector implements controller runtime's Runnable |
| 29 | +type BadgerGarbageCollector struct { |
| 30 | + // DiscardRatio must be a float between 0.0 and 1.0, inclusive |
| 31 | + // See badger.DB.RunValueLogGC for more info |
| 32 | + DiscardRatio float64 |
| 33 | + Interval time.Duration |
| 34 | + |
| 35 | + name string |
| 36 | + db *badger.DB |
| 37 | + log logr.Logger |
| 38 | +} |
| 39 | + |
| 40 | +// NewBadgerGarbageCollector creates and returns a new BadgerGarbageCollector |
| 41 | +func NewBadgerGarbageCollector(name string, db *badger.DB, interval time.Duration, discardRatio float64) *BadgerGarbageCollector { |
| 42 | + return &BadgerGarbageCollector{ |
| 43 | + DiscardRatio: discardRatio, |
| 44 | + Interval: interval, |
| 45 | + |
| 46 | + name: name, |
| 47 | + db: db, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Start repeatedly runs the BadgerDB garbage collector with a delay inbetween |
| 52 | +// runs. |
| 53 | +// |
| 54 | +// Start blocks until the context is cancelled. The database is expected to |
| 55 | +// already be open and not be closed while this context is active. |
| 56 | +// |
| 57 | +// ctx should be a logr.Logger context. |
| 58 | +func (gc *BadgerGarbageCollector) Start(ctx context.Context) error { |
| 59 | + gc.log = ctrl.LoggerFrom(ctx).WithName(gc.name) |
| 60 | + |
| 61 | + gc.log.Info("Starting Badger GC") |
| 62 | + timer := time.NewTimer(gc.Interval) |
| 63 | + for { |
| 64 | + select { |
| 65 | + case <-timer.C: |
| 66 | + gc.discardValueLogFiles() |
| 67 | + timer.Reset(gc.Interval) |
| 68 | + case <-ctx.Done(): |
| 69 | + timer.Stop() |
| 70 | + gc.log.Info("Stopped Badger GC") |
| 71 | + return nil |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// upper bound for loop |
| 77 | +const maxDiscards = 1000 |
| 78 | + |
| 79 | +func (gc *BadgerGarbageCollector) discardValueLogFiles() { |
| 80 | + for c := 0; c < maxDiscards; c++ { |
| 81 | + err := gc.db.RunValueLogGC(gc.DiscardRatio) |
| 82 | + if errors.Is(err, badger.ErrNoRewrite) { |
| 83 | + // there is no more garbage to discard |
| 84 | + gc.log.Info("Ran Badger GC", "discarded_vlogs", c) |
| 85 | + return |
| 86 | + } |
| 87 | + if err != nil { |
| 88 | + gc.log.Error(err, "Badger GC Error", "discarded_vlogs", c) |
| 89 | + return |
| 90 | + } |
| 91 | + } |
| 92 | + gc.log.Info("Ran Badger GC for maximum discards", "discarded_vlogs", maxDiscards) |
| 93 | +} |
0 commit comments