Skip to content

Adding dead-man's switch #6853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"math/rand"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/dgryski/go-farm"
Expand Down Expand Up @@ -142,6 +143,22 @@ func AwaitWaitGroup(wg *sync.WaitGroup, timeout time.Duration) bool {
}
}

func WatchMissedDeadline(shouldBeDoneBy time.Duration, deadMansSwitch func()) (isDone func()) {
done := atomic.Bool{}
go func() {
time.Sleep(shouldBeDoneBy)
isDone := done.Load()
if isDone {
return
}
deadMansSwitch()
}()

return func() {
done.Swap(true)
}
}

// CreatePersistenceRetryPolicy creates a retry policy for persistence layer operations
func CreatePersistenceRetryPolicy() backoff.RetryPolicy {
policy := backoff.NewExponentialRetryPolicy(retryPersistenceOperationInitialInterval)
Expand Down
19 changes: 19 additions & 0 deletions common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1721,3 +1721,22 @@ func TestCheckEventBlobSizeLimit(t *testing.T) {
})
}
}

func TestWatchMissedDeadlineDoesNotFireWhenDone(t *testing.T) {
fired := false
isDone := WatchMissedDeadline(time.Millisecond*10, func() {
fired = true
})
isDone()
assert.False(t, fired)
}

func TestWatchMissedDeadlineDoesFireWhenNotDone(t *testing.T) {
fired := false
isDone := WatchMissedDeadline(time.Millisecond*10, func() {
fired = true
})
time.Sleep(time.Millisecond * 11)
isDone()
assert.True(t, fired)
}
4 changes: 4 additions & 0 deletions service/history/queue/transfer_queue_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,17 @@ func (t *transferQueueProcessor) Stop() {
defer t.logger.Info("Transfer queue processor stopped")

if !t.shard.GetConfig().QueueProcessorEnableGracefulSyncShutdown() {
isDone := common.WatchMissedDeadline(time.Minute, func() {
t.logger.Error("Graceful shutdown of transfer queue processor has failed")
})
t.activeQueueProcessor.Stop()
for _, standbyQueueProcessor := range t.standbyQueueProcessors {
standbyQueueProcessor.Stop()
}

close(t.shutdownChan)
common.AwaitWaitGroup(&t.shutdownWG, time.Minute)
isDone()
return
}

Expand Down
Loading