From 60f9297e3f7f68381bec48ef1eccdbc6dd12624a Mon Sep 17 00:00:00 2001 From: Aleksey Myasnikov Date: Mon, 9 Sep 2024 13:04:15 +0300 Subject: [PATCH] * Added `balancers.PreferNearestDC[WithFallback]` balancers * Marked as deprecated `balancers.PreferLocalDC[WithFallback]` balancers because `local` word is ambiguous for balancer idea --- CHANGELOG.md | 3 ++ balancers/balancers.go | 34 +++++++++++++++++----- balancers/balancers_test.go | 4 +-- balancers/config.go | 17 +++++++++-- balancers/config_test.go | 36 ++++++++++++++++++++++-- internal/balancer/balancer.go | 4 +-- internal/balancer/config/routerconfig.go | 12 ++++---- internal/balancer/local_dc_test.go | 2 +- 8 files changed, 88 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89b2cd889..67edcd595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +* Added `balancers.PreferNearestDC[WithFallback]` balancers +* Marked as deprecated `balancers.PreferLocalDC[WithFallback]` balancers because `local` word is ambiguous for balancer idea + ## v3.80.1 * Added `lastErr` from previous attempt in `retry.RetryWithResult` diff --git a/balancers/balancers.go b/balancers/balancers.go index 9232c8657..a2b5593fc 100644 --- a/balancers/balancers.go +++ b/balancers/balancers.go @@ -37,21 +37,41 @@ func (filterLocalDC) String() string { return "LocalDC" } -// PreferLocalDC creates balancer which use endpoints only in location such as initial endpoint location -// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location -// PreferLocalDC balancer try to autodetect local DC from client side. +// Deprecated: use PreferNearestDC instead +// Will be removed after March 2025. +// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated func PreferLocalDC(balancer *balancerConfig.Config) *balancerConfig.Config { balancer.Filter = filterLocalDC{} - balancer.DetectLocalDC = true + balancer.DetectNearestDC = true return balancer } -// PreferLocalDCWithFallBack creates balancer which use endpoints only in location such as initial endpoint location +// PreferNearestDC creates balancer which use endpoints only in location such as initial endpoint location // Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location -// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead +// PreferNearestDC balancer try to autodetect local DC from client side. +func PreferNearestDC(balancer *balancerConfig.Config) *balancerConfig.Config { + balancer.Filter = filterLocalDC{} + balancer.DetectNearestDC = true + + return balancer +} + +// Deprecated: use PreferNearestDCWithFallBack instead +// Will be removed after March 2025. +// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated func PreferLocalDCWithFallBack(balancer *balancerConfig.Config) *balancerConfig.Config { - balancer = PreferLocalDC(balancer) + balancer = PreferNearestDC(balancer) + balancer.AllowFallback = true + + return balancer +} + +// PreferNearestDCWithFallBack creates balancer which use endpoints only in location such as initial endpoint location +// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location +// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead +func PreferNearestDCWithFallBack(balancer *balancerConfig.Config) *balancerConfig.Config { + balancer = PreferNearestDC(balancer) balancer.AllowFallback = true return balancer diff --git a/balancers/balancers_test.go b/balancers/balancers_test.go index e5ad00ef6..6f59f0006 100644 --- a/balancers/balancers_test.go +++ b/balancers/balancers_test.go @@ -17,7 +17,7 @@ func TestPreferLocalDC(t *testing.T) { &mock.Conn{AddrField: "2", State: conn.Online, LocationField: "2"}, &mock.Conn{AddrField: "3", State: conn.Online, LocationField: "2"}, } - rr := PreferLocalDC(RandomChoice()) + rr := PreferNearestDC(RandomChoice()) require.False(t, rr.AllowFallback) require.Equal(t, []conn.Conn{conns[1], conns[2]}, applyPreferFilter(balancerConfig.Info{SelfLocation: "2"}, rr, conns)) } @@ -28,7 +28,7 @@ func TestPreferLocalDCWithFallBack(t *testing.T) { &mock.Conn{AddrField: "2", State: conn.Online, LocationField: "2"}, &mock.Conn{AddrField: "3", State: conn.Online, LocationField: "2"}, } - rr := PreferLocalDCWithFallBack(RandomChoice()) + rr := PreferNearestDCWithFallBack(RandomChoice()) require.True(t, rr.AllowFallback) require.Equal(t, []conn.Conn{conns[1], conns[2]}, applyPreferFilter(balancerConfig.Info{SelfLocation: "2"}, rr, conns)) } diff --git a/balancers/config.go b/balancers/config.go index 8bc38199c..6ea79c6c2 100644 --- a/balancers/config.go +++ b/balancers/config.go @@ -20,8 +20,13 @@ const ( type preferType string const ( - preferTypeLocalDC = preferType("local_dc") + preferTypeNearestDC = preferType("nearest_dc") preferTypeLocations = preferType("locations") + + // Deprecated + // Will be removed after March 2025. + // Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated + preferTypeLocalDC = preferType("local_dc") ) type balancersConfig struct { @@ -90,10 +95,16 @@ func CreateFromConfig(s string) (*balancerConfig.Config, error) { switch c.Prefer { case preferTypeLocalDC: if c.Fallback { - return PreferLocalDCWithFallBack(b), nil + return PreferNearestDCWithFallBack(b), nil + } + + return PreferNearestDC(b), nil + case preferTypeNearestDC: + if c.Fallback { + return PreferNearestDCWithFallBack(b), nil } - return PreferLocalDC(b), nil + return PreferNearestDC(b), nil case preferTypeLocations: if len(c.Locations) == 0 { return nil, xerrors.WithStackTrace(fmt.Errorf("empty locations list in balancer '%s' config", c.Type)) diff --git a/balancers/config_test.go b/balancers/config_test.go index b14a843ae..587e6a997 100644 --- a/balancers/config_test.go +++ b/balancers/config_test.go @@ -70,7 +70,21 @@ func TestFromConfig(t *testing.T) { "prefer": "local_dc" }`, res: balancerConfig.Config{ - DetectLocalDC: true, + DetectNearestDC: true, + Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { + // some non nil func + return false + }), + }, + }, + { + name: "prefer_nearest_dc", + config: `{ + "type": "random_choice", + "prefer": "nearest_dc" + }`, + res: balancerConfig.Config{ + DetectNearestDC: true, Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { // some non nil func return false @@ -93,8 +107,24 @@ func TestFromConfig(t *testing.T) { "fallback": true }`, res: balancerConfig.Config{ - AllowFallback: true, - DetectLocalDC: true, + AllowFallback: true, + DetectNearestDC: true, + Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { + // some non nil func + return false + }), + }, + }, + { + name: "prefer_nearest_dc_with_fallback", + config: `{ + "type": "random_choice", + "prefer": "nearest_dc", + "fallback": true + }`, + res: balancerConfig.Config{ + AllowFallback: true, + DetectNearestDC: true, Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool { // some non nil func return false diff --git a/internal/balancer/balancer.go b/internal/balancer/balancer.go index f9cdeebf2..08c927540 100644 --- a/internal/balancer/balancer.go +++ b/internal/balancer/balancer.go @@ -111,7 +111,7 @@ func (b *Balancer) clusterDiscoveryAttempt(ctx context.Context) (err error) { return xerrors.WithStackTrace(err) } - if b.config.DetectLocalDC { + if b.config.DetectNearestDC { localDC, err = b.localDCDetector(ctx, endpoints) if err != nil { return xerrors.WithStackTrace(err) @@ -129,7 +129,7 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi b.driverConfig.Trace(), &ctx, stack.FunctionID( "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer.(*Balancer).applyDiscoveredEndpoints"), - b.config.DetectLocalDC, + b.config.DetectNearestDC, ) previous = b.connections().All() ) diff --git a/internal/balancer/config/routerconfig.go b/internal/balancer/config/routerconfig.go index 220926c20..92b1258fa 100644 --- a/internal/balancer/config/routerconfig.go +++ b/internal/balancer/config/routerconfig.go @@ -10,10 +10,10 @@ import ( // Dedicated package need for prevent cyclo dependencies config -> balancer -> config type Config struct { - Filter Filter - AllowFallback bool - SingleConn bool - DetectLocalDC bool + Filter Filter + AllowFallback bool + SingleConn bool + DetectNearestDC bool } func (c Config) String() string { @@ -26,8 +26,8 @@ func (c Config) String() string { buffer.WriteString("RandomChoice{") - buffer.WriteString("DetectLocalDC=") - fmt.Fprintf(buffer, "%t", c.DetectLocalDC) + buffer.WriteString("DetectNearestDC=") + fmt.Fprintf(buffer, "%t", c.DetectNearestDC) buffer.WriteString(",AllowFallback=") fmt.Fprintf(buffer, "%t", c.AllowFallback) diff --git a/internal/balancer/local_dc_test.go b/internal/balancer/local_dc_test.go index 2eab1e9a8..5fbcac1b8 100644 --- a/internal/balancer/local_dc_test.go +++ b/internal/balancer/local_dc_test.go @@ -131,7 +131,7 @@ func TestDetectLocalDC(t *testing.T) { func TestLocalDCDiscovery(t *testing.T) { ctx := context.Background() cfg := config.New( - config.WithBalancer(balancers.PreferLocalDC(balancers.Default())), + config.WithBalancer(balancers.PreferNearestDC(balancers.Default())), ) r := &Balancer{ driverConfig: cfg,