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 15, 2024
1 parent 06904cc commit 6df9ee6
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 @@ -54,8 +55,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 @@ -72,6 +79,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 @@ -251,8 +251,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 @@ -356,6 +356,7 @@ type anyIndexer struct {
type anyDeleteTracker interface {
setRevision(uint64)
getRevision() uint64
close()
}

type indexEntry struct {
Expand Down

0 comments on commit 6df9ee6

Please sign in to comment.