-
Notifications
You must be signed in to change notification settings - Fork 4
Fix: Hold Leader Election #60
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e2229c9
hold leader election
zedimytch 7064af0
include secrets and update config
zedimytch 1a05d0f
use session-scoped lock
zedimytch 802161d
pass uint32 instead of byte
zedimytch 01ccf18
separate tryAdvisoryLock function
zedimytch a70bcf2
remove code artifact
zedimytch 20d6e6e
remove elector config
zedimytch a0d1f8c
address comments
zedimytch 08eb337
functions above where they're used
zedimytch 50a3962
remove unused env vars
zedimytch eb3d256
use transaction level lock with tests
zedimytch 6d13fd2
fix test
zedimytch 566b1fa
30 min period
zedimytch 6d221e7
use context with timeout
zedimytch 5689627
address comments
zedimytch 714625e
add log messages
zedimytch ea91862
timeout test after 2 seconds
zedimytch 0b5ac00
fix logging nil error
zedimytch 2108845
delay after performing work
zedimytch 8adb984
wait until next boundary
zedimytch 10171c0
address comments
zedimytch 94cd342
defer and wrap in closure
zedimytch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package ingestors | ||
|
|
||
| type Ingestor interface { | ||
| Start() | ||
| Stop() | ||
| } | ||
|
|
||
| type Manager interface { | ||
| StartIngestors() | ||
| StopIngestors() | ||
| } | ||
|
|
||
| type IngestorType string | ||
|
|
||
| const ( | ||
| IngestorTypeCSFloat IngestorType = "csfloat" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package leader | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
| ) | ||
|
|
||
| type Elector interface { | ||
| Run(ctx context.Context, lockKey uint32, period time.Duration, onWork func(ctx context.Context)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package leader | ||
|
zedimytch marked this conversation as resolved.
|
||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "time" | ||
|
|
||
| "reverse-watch/domain/leader" | ||
| "reverse-watch/domain/repository" | ||
|
|
||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type elector struct { | ||
| log *zap.SugaredLogger | ||
| factory repository.Factory | ||
| } | ||
|
|
||
| var _ leader.Elector = (*elector)(nil) | ||
|
|
||
| func New(factory repository.Factory, log *zap.SugaredLogger) leader.Elector { | ||
| return &elector{ | ||
| log: log, | ||
| factory: factory, | ||
| } | ||
| } | ||
|
|
||
| func (e *elector) tryAdvisoryLock(ctx context.Context, lockKey uint32) (*sql.Tx, bool) { | ||
| db, err := e.factory.PublicDB().DB() | ||
| if err != nil { | ||
| e.log.Errorf("failed to get database connection: %v", err) | ||
| return nil, false | ||
| } | ||
|
|
||
| // Use background context so transaction isn't auto-cancelled. | ||
| // Very unlikely, but we don't want another instance to acquire the lock and begin work before we have exited. | ||
| tx, err := db.BeginTx(context.Background(), nil) | ||
| if err != nil { | ||
| e.log.Errorf("failed to begin transaction: %v", err) | ||
| return nil, false | ||
| } | ||
|
|
||
| var hasLock bool | ||
| err = tx.QueryRowContext(ctx, "SELECT pg_try_advisory_xact_lock($1)", lockKey).Scan(&hasLock) | ||
| if err != nil { | ||
| tx.Rollback() | ||
| e.log.Errorf("failed to acquire advisory lock: %v", err) | ||
| return nil, false | ||
| } | ||
|
|
||
| if !hasLock { | ||
| tx.Rollback() | ||
| e.log.Errorf("failed to acquire advisory lock") | ||
| return nil, false | ||
| } | ||
| return tx, true | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| // Run will run the elector in a loop, acquiring the leader lock and calling the onWork function when the leader is acquired. | ||
| // If the leader lock is not acquired, it will wait for the next period and try again. | ||
| // If the context is done, it will return. | ||
| func (e *elector) Run(ctx context.Context, lockKey uint32, period time.Duration, onWork func(ctx context.Context)) { | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| default: | ||
| } | ||
|
zedimytch marked this conversation as resolved.
|
||
|
|
||
| func() { | ||
| workCtx, workCancel := context.WithCancel(ctx) | ||
| defer workCancel() | ||
| defer waitUntilNextBoundary(ctx, period) | ||
| tx, acquired := e.tryAdvisoryLock(workCtx, lockKey) | ||
| if !acquired { | ||
| return | ||
| } | ||
| defer tx.Rollback() | ||
|
|
||
| onWork(workCtx) | ||
| }() | ||
|
|
||
| } | ||
| } | ||
|
|
||
| func waitUntilNextBoundary(ctx context.Context, period time.Duration) { | ||
| nextBoundary := time.Now().Truncate(period).Add(period) | ||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-time.After(time.Until(nextBoundary)): | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.