1
0
Fork 0

Better wait for function

master
Philip O'Toole 7 months ago
parent 1cb7d05285
commit c41799fc8d

@ -713,23 +713,18 @@ func (s *Store) WaitForAppliedIndex(idx uint64, timeout time.Duration) error {
}
}
// WaitCommitIndex blocks until the local Raft commit index is equal to
// or greater than the Leader Commit Index at the time this function
// is called, or the timeout expires.
func (s *Store) WaitCommitIndex(timeout time.Duration) error {
// WaitForCommitIndex blocks until the local Raft commit index is equal to
// or greater the given index, or the timeout expires.
func (s *Store) WaitForCommitIndex(idx uint64, timeout time.Duration) error {
tck := time.NewTicker(commitEquivalenceDelay)
defer tck.Stop()
tmr := time.NewTimer(timeout)
defer tmr.Stop()
lci, err := s.LeaderCommitIndex()
if err != nil {
return err
}
for {
select {
case <-tck.C:
if lci <= s.raft.CommitIndex() {
if s.raft.CommitIndex() >= idx {
return nil
}
case <-tmr.C:

@ -76,9 +76,12 @@ func Test_MultiNodeSimple(t *testing.T) {
t.Fatalf("wrong leader commit index, got: %d, exp: %d", got, exp)
}
if err := s0.WaitCommitIndex(time.Second); err != nil {
if err := s0.WaitForCommitIndex(4, time.Second); err != nil {
t.Fatalf("failed to wait for commit index: %s", err.Error())
}
if err := s0.WaitForCommitIndex(5, 500*time.Millisecond); err == nil {
t.Fatalf("unexpectedly waited successfully for commit index")
}
// Now, do a NONE consistency query on each node, to actually confirm the data
// has been replicated.
@ -112,9 +115,12 @@ func Test_MultiNodeSimple(t *testing.T) {
t.Fatalf("wrong leader commit index, got: %d, exp: %d", got, exp)
}
if err := s1.WaitCommitIndex(time.Second); err != nil {
if err := s1.WaitForCommitIndex(4, time.Second); err != nil {
t.Fatalf("failed to wait for commit index: %s", err.Error())
}
if err := s1.WaitForCommitIndex(5, 500*time.Millisecond); err == nil {
t.Fatalf("unexpectedly waited successfully for commit index")
}
// Write another row using Request
rr := executeQueryRequestFromString("INSERT INTO foo(id, name) VALUES(2, 'fiona')", proto.QueryRequest_QUERY_REQUEST_LEVEL_STRONG, false, false)

Loading…
Cancel
Save