Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions pkg/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,34 @@ type longOperationLocker interface {
type longOperation struct {
lock longOperationLocker
mu sync.Mutex
tick *time.Ticker
done chan struct{}
timeout time.Duration
}

func (l *longOperation) Lock() error {
if err := l.lock.Lock(); err != nil {
return err
}
l.tick = time.NewTicker(l.timeout / 3)
l.mu.Lock()
done := make(chan struct{})
l.done = done
l.mu.Unlock()
go func() {
defer l.mu.Unlock()
tick := time.NewTicker(l.timeout / 3)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it change to move the tick instantiation to the goroutine?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, nothing, just have it in the same place where it is used, there no need to have in the struct

defer tick.Stop()
for {
l.mu.Lock()
if l.tick == nil {
return
}
ch := l.tick.C
l.mu.Unlock()
<-ch
l.mu.Lock()
if l.tick == nil {
select {
case <-done:
return
case <-tick.C:
l.mu.Lock()
if l.done != done {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which situations do we replace l.done? 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for example, when you call lock after unlock

l.mu.Unlock()
return
}
l.lock.Extend()
l.mu.Unlock()
}
l.lock.Extend()
l.mu.Unlock()
}
}()
return nil
Expand All @@ -82,9 +85,9 @@ func (l *longOperation) Lock() error {
func (l *longOperation) Unlock() {
l.mu.Lock()
defer l.mu.Unlock()
if l.tick != nil {
l.tick.Stop()
l.tick = nil
if l.done != nil {
close(l.done)
l.done = nil
}
l.lock.Unlock()
}
61 changes: 61 additions & 0 deletions pkg/lock/long_operation_shutdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package lock

import (
"sync/atomic"
"testing"
"testing/synctest"
"time"

"github.com/stretchr/testify/require"
)

type shutdownLeaseStub struct {
renewals atomic.Int32
unlocked atomic.Bool
renewalsAfterUnlock atomic.Int32
}

func (s *shutdownLeaseStub) Lock() error { s.unlocked.Store(false); return nil }
func (s *shutdownLeaseStub) Unlock() { s.unlocked.Store(true) }
func (s *shutdownLeaseStub) Extend() {
s.renewals.Add(1)
if s.unlocked.Load() {
s.renewalsAfterUnlock.Add(1)
}
}

func TestLongOperationStopsRenewingOnUnlock(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
s := &shutdownLeaseStub{}
l := &longOperation{lock: s, timeout: 3 * time.Second}
for range 2 {
require.NoError(t, l.Lock())
synctest.Wait()
before := s.renewals.Load()
time.Sleep(time.Second)
synctest.Wait()
require.Equal(t, before+1, s.renewals.Load())

l.Unlock()
synctest.Wait()
time.Sleep(time.Second)
synctest.Wait()
require.Equal(t, before+1, s.renewals.Load())
}
})
}

func TestLongOperationDoesNotRenewAfterUnlock(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
s := &shutdownLeaseStub{}
l := &longOperation{lock: s, timeout: 3 * time.Second}
for range 100 {
require.NoError(t, l.Lock())
synctest.Wait()
time.Sleep(time.Second)
l.Unlock()
synctest.Wait()
}
require.Zero(t, s.renewalsAfterUnlock.Load())
})
}
9 changes: 2 additions & 7 deletions pkg/lock/simple_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ func (i *InMemoryLockGetter) ReadWrite(_ prefixer.Prefixer, name string) ErrorRW
return lock.(*memLock)
}

// LongOperation returns a lock suitable for long operations. It will refresh
// the lock in redis to avoid its automatic expiration.
// LongOperation returns an in-memory lock, which does not expire.
func (i *InMemoryLockGetter) LongOperation(db prefixer.Prefixer, name string) ErrorLocker {
return &longOperation{
lock: i.ReadWrite(db, name).(*memLock),
timeout: LockTimeout,
}
return i.ReadWrite(db, name)
}

type memLock struct {
Expand All @@ -35,6 +31,5 @@ type memLock struct {

func (ml *memLock) Lock() error { ml.RWMutex.Lock(); return nil }
func (ml *memLock) RLock() error { ml.RWMutex.RLock(); return nil }
func (ml *memLock) Extend() {}
func (ml *memLock) Unlock() { ml.RWMutex.Unlock() }
func (ml *memLock) RUnlock() { ml.RWMutex.RUnlock() }
Loading