diff options
| author | Stewart Miles <stewart@agentic.ai> | 2023-03-15 17:49:46 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-15 17:49:46 -0700 |
| commit | 57ab1f851656276f62124c3dc5e3fc0d15eed646 (patch) | |
| tree | e9dcc6fbf41c450a8e841a005105386a502876d2 /weed | |
| parent | dd71f54c6b8355a35cda134173d9ebb76a53b62c (diff) | |
| download | seaweedfs-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.go | 19 |
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) { |
