aboutsummaryrefslogtreecommitdiff
path: root/weed
diff options
context:
space:
mode:
authorStewart Miles <stewart@agentic.ai>2023-03-15 17:49:46 -0700
committerGitHub <noreply@github.com>2023-03-15 17:49:46 -0700
commit57ab1f851656276f62124c3dc5e3fc0d15eed646 (patch)
treee9dcc6fbf41c450a8e841a005105386a502876d2 /weed
parentdd71f54c6b8355a35cda134173d9ebb76a53b62c (diff)
downloadseaweedfs-57ab1f851656276f62124c3dc5e3fc0d15eed646.tar.xz
seaweedfs-57ab1f851656276f62124c3dc5e3fc0d15eed646.zip
Use exponential backoff to query leader. (#4313)
`topology.Leader()` was using a backoff that typically resulted in at least a 5s delay when initially starting a master and raft server. This changes the backoff algorithm to use exponential backoff starting with 100ms and waiting up to 20s for leader selection. Related to #4307
Diffstat (limited to 'weed')
-rw-r--r--weed/topology/topology.go19
1 files changed, 6 insertions, 13 deletions
diff --git a/weed/topology/topology.go b/weed/topology/topology.go
index c7b492267..825c02961 100644
--- a/weed/topology/topology.go
+++ b/weed/topology/topology.go
@@ -11,6 +11,8 @@ import (
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/storage/types"
+ backoff "github.com/cenkalti/backoff/v4"
+
hashicorpRaft "github.com/hashicorp/raft"
"github.com/seaweedfs/raft"
@@ -96,19 +98,10 @@ func (t *Topology) IsLeader() bool {
}
func (t *Topology) Leader() (l pb.ServerAddress, err error) {
- for count := 0; count < 3; count++ {
- l, err = t.MaybeLeader()
- if err != nil {
- return
- }
- if l != "" {
- break
- }
-
- time.Sleep(time.Duration(5+count) * time.Second)
- }
-
- return
+ exponentialBackoff := backoff.NewExponentialBackOff()
+ exponentialBackoff.InitialInterval = 100 * time.Millisecond
+ exponentialBackoff.MaxElapsedTime = 20 * time.Second
+ return backoff.RetryWithData(t.MaybeLeader, exponentialBackoff)
}
func (t *Topology) MaybeLeader() (l pb.ServerAddress, err error) {