diff --git a/seaweed-block/cmd/blockvolume/chunk_maintenance.go b/seaweed-block/cmd/blockvolume/chunk_maintenance.go index 943619d0dd..766e6dd30f 100644 --- a/seaweed-block/cmd/blockvolume/chunk_maintenance.go +++ b/seaweed-block/cmd/blockvolume/chunk_maintenance.go @@ -26,7 +26,7 @@ func newChunkMaintenance(cat *chunkstore.Catalog, blobs chunkstore.BlobDeleter, if defaultChunkGCGrace <= max(defaultChunkCutTimeout, chunkstore.DefaultOpDeadline)+defaultChunkMaintenanceTimeout { return nil, closeFn, errors.New("automatic GC grace does not cover cut/copy and mark bounds") } - conn, err := link.Dial(master) + conn, err := link.Dial(master, link.MasterReconnect()) if err != nil { return nil, closeFn, err } diff --git a/seaweed-block/core/host/volume/host.go b/seaweed-block/core/host/volume/host.go index 78b5613748..0e1797403b 100644 --- a/seaweed-block/core/host/volume/host.go +++ b/seaweed-block/core/host/volume/host.go @@ -267,7 +267,7 @@ func New(cfg Config) (*Host, error) { lg = log.Default() } - conn, err := link.Dial(cfg.MasterAddr) + conn, err := link.Dial(cfg.MasterAddr, link.MasterReconnect()) if err != nil { return nil, fmt.Errorf("volume.New: dial master %q: %w", cfg.MasterAddr, err) } diff --git a/seaweed-block/core/host/volume/master_reconnect_test.go b/seaweed-block/core/host/volume/master_reconnect_test.go index 296871e02d..26ae72814c 100644 --- a/seaweed-block/core/host/volume/master_reconnect_test.go +++ b/seaweed-block/core/host/volume/master_reconnect_test.go @@ -69,8 +69,9 @@ func TestMasterLinkReconnectsWithinTwoSeconds(t *testing.T) { case <-sink.got: lag := time.Since(back) t.Logf("first heartbeat %v after the master returned", lag.Round(10*time.Millisecond)) - if lag > 2400*time.Millisecond { - t.Fatalf("master link reconnected %v after the master returned; want <= 2.4 s", lag.Round(10*time.Millisecond)) + // 2 s cap + 20 % jitter, plus one 200 ms heartbeat tick once connected. + if lag > 2600*time.Millisecond { + t.Fatalf("master link reconnected %v after the master returned; want <= 2.6 s", lag.Round(10*time.Millisecond)) } case <-time.After(60 * time.Second): t.Fatal("no heartbeat within 60 s of the master's return") diff --git a/seaweed-block/core/rpc/link/link.go b/seaweed-block/core/rpc/link/link.go index b67617a799..d115893da8 100644 --- a/seaweed-block/core/rpc/link/link.go +++ b/seaweed-block/core/rpc/link/link.go @@ -6,7 +6,10 @@ package link import ( + "time" + "google.golang.org/grpc" + "google.golang.org/grpc/backoff" "google.golang.org/grpc/credentials/insecure" ) @@ -45,3 +48,18 @@ func NewServer(cfg ServerConfig, opts ...grpc.ServerOption) *grpc.Server { all = append(all, opts...) return grpc.NewServer(all...) } + +// MasterReconnect caps the reconnect back-off of a long-lived link to +// blockmaster. A volume process waits for an unreachable master instead of +// exiting, so one channel can live through a long outage; with gRPC's default +// back-off (base 1 s, x1.6, jitter 0.2, max 120 s: grpc@v1.80.0 +// backoff/backoff.go:48-51) the first call after the master returns could lag +// by up to two minutes. Capped at 2 s, the lag stays about 2 s. The connect +// timeout must be set: WithConnectParams otherwise makes it equal to the +// back-off step (dialoptions.go:313-314), too short for a TLS handshake. +func MasterReconnect() grpc.DialOption { + return grpc.WithConnectParams(grpc.ConnectParams{ + Backoff: backoff.Config{BaseDelay: time.Second, Multiplier: 1.6, Jitter: 0.2, MaxDelay: 2 * time.Second}, + MinConnectTimeout: 5 * time.Second, + }) +}