Skip to content

Commit

Permalink
Add finalizers to delete trackers
Browse files Browse the repository at this point in the history
For safety, set a finalizer to make sure delete trackers are always
closed.

Signed-off-by: Jussi Maki <[email protected]>
  • Loading branch information
joamaki committed Apr 17, 2024
1 parent 48c2e61 commit ca1a89c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions deletetracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package statedb

import (
"runtime"
"sync/atomic"

"github.com/cilium/statedb/index"
Expand Down Expand Up @@ -53,8 +54,14 @@ func (dt *deleteTracker[Obj]) mark(upTo Revision) {
}

func (dt *deleteTracker[Obj]) close() {
if dt.db == nil {
return
}
runtime.SetFinalizer(dt, nil)

// Remove the delete tracker from the table.
txn := dt.db.WriteTxn(dt.table).getTxn()
dt.db = nil
db := txn.db
table := txn.modifiedTables[dt.table.tablePos()]
if table == nil {
Expand All @@ -71,6 +78,7 @@ func (dt *deleteTracker[Obj]) close() {
case db.gcTrigger <- struct{}{}:
default:
}

}

var closedWatchChannel = func() <-chan struct{} {
Expand Down
5 changes: 4 additions & 1 deletion txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,11 @@ func (txn *txn) addDeleteTracker(meta TableMeta, trackerName string, dt anyDelet
table := txn.modifiedTables[meta.tablePos()]
_, _, table.deleteTrackers = table.deleteTrackers.Insert([]byte(trackerName), dt)
txn.db.metrics.DeleteTrackerCount(meta.Name(), table.deleteTrackers.Len())
return nil

// Add a finalizer to make sure delete trackers are always closed.
runtime.SetFinalizer(dt, func(dt anyDeleteTracker) { dt.close() })

return nil
}

func (txn *txn) delete(meta TableMeta, guardRevision Revision, data any) (object, bool, error) {
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ type anyIndexer struct {
type anyDeleteTracker interface {
setRevision(uint64)
getRevision() uint64
close()
}

type indexEntry struct {
Expand Down

0 comments on commit ca1a89c

Please sign in to comment.