Skip to content

Commit

Permalink
Garbage-collect timer and fix timer data race
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Pavlovic <matopavlovic@gmail.com>
  • Loading branch information
matejpavlovic committed Jun 24, 2022
1 parent aa0a2e8 commit a7be1f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/iss/iss.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,9 @@ func (iss *ISS) applyStableCheckpoint(stableCheckpoint *isspb.StableCheckpoint)
// stable checkpoint is associated with.
eventsOut.PushBack(events.WALTruncate(walModuleName, t.WALRetIndex(stableCheckpoint.Epoch)))

// Prune old Timer tasks
eventsOut.PushBack(events.TimerGarbageCollect(timerModuleName, t.TimerRetIndex(stableCheckpoint.Epoch)))

// Clean up the global ISS state from all the epoch
// instances that are associated with epoch numbers
// less than the epoch number of the new stable
Expand Down
15 changes: 13 additions & 2 deletions pkg/timer/timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/filecoin-project/mir/pkg/events"
"github.com/filecoin-project/mir/pkg/pb/eventpb"
t "github.com/filecoin-project/mir/pkg/types"
"sync"
"time"
)

Expand All @@ -14,7 +15,8 @@ import (
type Timer struct {
eventsOut chan *events.EventList

retIndex t.TimerRetIndex
retIndexMutex sync.RWMutex
retIndex t.TimerRetIndex
}

func New() *Timer {
Expand Down Expand Up @@ -107,7 +109,7 @@ func (tm *Timer) Repeat(
defer ticker.Stop()

// Repeat as long as this repetition task has not been garbage-collected.
for retIndex >= tm.retIndex {
for retIndex >= tm.getRetIndex() {

// Try to write events to the output channel until the context is canceled.
select {
Expand Down Expand Up @@ -135,9 +137,18 @@ func (tm *Timer) Repeat(
// smaller than retIndex.
// If GarbageCollect already has been invoked with the same or higher retention index, the call has no effect.
func (tm *Timer) GarbageCollect(retIndex t.TimerRetIndex) {
tm.retIndexMutex.Lock()
defer tm.retIndexMutex.Unlock()

// Retention index must be monotonically increasing over calls to GarbageCollect.
if retIndex > tm.retIndex {
tm.retIndex = retIndex
}
}

func (tm *Timer) getRetIndex() t.TimerRetIndex {
tm.retIndexMutex.RLock()
defer tm.retIndexMutex.RUnlock()

return tm.retIndex
}

0 comments on commit a7be1f1

Please sign in to comment.