Skip to content

Implement candidacy#46

Merged
benbjohnson merged 1 commit intomainfrom
candidate
Aug 11, 2022
Merged

Implement candidacy#46
benbjohnson merged 1 commit intomainfrom
candidate

Conversation

@benbjohnson
Copy link
Collaborator

@benbjohnson benbjohnson commented Aug 11, 2022

This pull request adds a candidate flag to the config which tells the node whether it can become the primary or not. It defaults to true.

Original PR: jwhear#1

Fixes #16

This adds a `candidate` flag to the config which tells the node whether it
can become the primary or not. It defaults to true.

Co-authored-by: Justin Whear <justin.whear@gmail.com>
Co-authored-by: Ben Johnson <benbjohnson@yahoo.com>
@benbjohnson benbjohnson merged commit c29eae9 into main Aug 11, 2022
@benbjohnson benbjohnson deleted the candidate branch August 11, 2022 16:56
@benbjohnson
Copy link
Collaborator Author

@jwhear Thanks for the implementation. You're marked as the author and Co-authored-by in the commit but it's still showing my avatar in the commit on GitHub. You're also not listed in the Contributor list on the main repo page. I'm not really sure why. Maybe it's because I cherry-picked the commit? Sorry about that. I'll try rebasing from your commits in the future.

I mostly made small tweaks from your commit and added a functional test.

Copy link
Collaborator Author

@benbjohnson benbjohnson left a comment

Choose a reason for hiding this comment

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

@jwhear I marked the changes in the commit in comments in this review.

MountDir string `yaml:"mount-dir"`
Exec string `yaml:"exec"`
Debug bool `yaml:"debug"`
Candidate bool `yaml:"candidate"`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I renamed this from IsPrimaryCandidate to simply Candidate.

Comment on lines +168 to +202
func TestMultiNode_Candidate(t *testing.T) {
m0 := runMain(t, newMain(t, t.TempDir(), nil))
waitForPrimary(t, m0)
m1 := newMain(t, t.TempDir(), m0)
m1.Config.Candidate = false
runMain(t, m1)
db0 := testingutil.OpenSQLDB(t, filepath.Join(m0.Config.MountDir, "db"))

// Create a database and wait for sync.
if _, err := db0.Exec(`CREATE TABLE t (x)`); err != nil {
t.Fatal(err)
}
waitForSync(t, 1, m0, m1)

// Stop the primary.
t.Log("shutting down primary node")
if err := db0.Close(); err != nil {
t.Fatal(err)
} else if err := m0.Close(); err != nil {
t.Fatal(err)
}

// Second node is NOT a candidate so it should not become primary.
t.Log("waiting to ensure replica is not promoted...")
time.Sleep(3 * time.Second)
if m1.Store.IsPrimary() {
t.Fatalf("replica should not have been promoted to primary")
}

// Reopen first node and ensure it can become primary again.
t.Log("restarting first node as replica")
m0 = runMain(t, newMain(t, m0.Config.MountDir, m1))
waitForPrimary(t, m0)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a test to make sure it works.

Comment on lines +154 to +158
// Candidate returns true if store is eligible to be the primary.
func (s *Store) Candidate() bool {
return s.candidate
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This previously had a lock around it but the field is only written once on initialization so it doesn't need one.

Comment on lines +320 to +323
if err == ErrNoPrimary && !s.candidate {
log.Printf("cannot find primary & ineligible to become primary, retrying: %s", err)
time.Sleep(1 * time.Second)
continue
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I propagated the logging up a level so it wouldn't do duplicate logging.

@benbjohnson benbjohnson added this to the v0.2.0 milestone Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Candidates

2 participants