Skip to content

Commit 57df25c

Browse files
authored
Merge pull request #1450 from ydb-platform/prefer-nearest-dc
Added `balancers.PreferNearestDC[WithFallback]` balancers + Marked as deprecated `balancers.PreferLocalDC[WithFallback]` balancers
2 parents ab4aa77 + 60f9297 commit 57df25c

File tree

8 files changed

+88
-24
lines changed

8 files changed

+88
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Added `balancers.PreferNearestDC[WithFallback]` balancers
2+
* Marked as deprecated `balancers.PreferLocalDC[WithFallback]` balancers because `local` word is ambiguous for balancer idea
3+
14
## v3.80.1
25
* Added `lastErr` from previous attempt in `retry.RetryWithResult`
36

balancers/balancers.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,41 @@ func (filterLocalDC) String() string {
3737
return "LocalDC"
3838
}
3939

40-
// PreferLocalDC creates balancer which use endpoints only in location such as initial endpoint location
41-
// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location
42-
// PreferLocalDC balancer try to autodetect local DC from client side.
40+
// Deprecated: use PreferNearestDC instead
41+
// Will be removed after March 2025.
42+
// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
4343
func PreferLocalDC(balancer *balancerConfig.Config) *balancerConfig.Config {
4444
balancer.Filter = filterLocalDC{}
45-
balancer.DetectLocalDC = true
45+
balancer.DetectNearestDC = true
4646

4747
return balancer
4848
}
4949

50-
// PreferLocalDCWithFallBack creates balancer which use endpoints only in location such as initial endpoint location
50+
// PreferNearestDC creates balancer which use endpoints only in location such as initial endpoint location
5151
// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location
52-
// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead
52+
// PreferNearestDC balancer try to autodetect local DC from client side.
53+
func PreferNearestDC(balancer *balancerConfig.Config) *balancerConfig.Config {
54+
balancer.Filter = filterLocalDC{}
55+
balancer.DetectNearestDC = true
56+
57+
return balancer
58+
}
59+
60+
// Deprecated: use PreferNearestDCWithFallBack instead
61+
// Will be removed after March 2025.
62+
// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
5363
func PreferLocalDCWithFallBack(balancer *balancerConfig.Config) *balancerConfig.Config {
54-
balancer = PreferLocalDC(balancer)
64+
balancer = PreferNearestDC(balancer)
65+
balancer.AllowFallback = true
66+
67+
return balancer
68+
}
69+
70+
// PreferNearestDCWithFallBack creates balancer which use endpoints only in location such as initial endpoint location
71+
// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location
72+
// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead
73+
func PreferNearestDCWithFallBack(balancer *balancerConfig.Config) *balancerConfig.Config {
74+
balancer = PreferNearestDC(balancer)
5575
balancer.AllowFallback = true
5676

5777
return balancer

balancers/balancers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestPreferLocalDC(t *testing.T) {
1717
&mock.Conn{AddrField: "2", State: conn.Online, LocationField: "2"},
1818
&mock.Conn{AddrField: "3", State: conn.Online, LocationField: "2"},
1919
}
20-
rr := PreferLocalDC(RandomChoice())
20+
rr := PreferNearestDC(RandomChoice())
2121
require.False(t, rr.AllowFallback)
2222
require.Equal(t, []conn.Conn{conns[1], conns[2]}, applyPreferFilter(balancerConfig.Info{SelfLocation: "2"}, rr, conns))
2323
}
@@ -28,7 +28,7 @@ func TestPreferLocalDCWithFallBack(t *testing.T) {
2828
&mock.Conn{AddrField: "2", State: conn.Online, LocationField: "2"},
2929
&mock.Conn{AddrField: "3", State: conn.Online, LocationField: "2"},
3030
}
31-
rr := PreferLocalDCWithFallBack(RandomChoice())
31+
rr := PreferNearestDCWithFallBack(RandomChoice())
3232
require.True(t, rr.AllowFallback)
3333
require.Equal(t, []conn.Conn{conns[1], conns[2]}, applyPreferFilter(balancerConfig.Info{SelfLocation: "2"}, rr, conns))
3434
}

balancers/config.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ const (
2020
type preferType string
2121

2222
const (
23-
preferTypeLocalDC = preferType("local_dc")
23+
preferTypeNearestDC = preferType("nearest_dc")
2424
preferTypeLocations = preferType("locations")
25+
26+
// Deprecated
27+
// Will be removed after March 2025.
28+
// Read about versioning policy: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#deprecated
29+
preferTypeLocalDC = preferType("local_dc")
2530
)
2631

2732
type balancersConfig struct {
@@ -90,10 +95,16 @@ func CreateFromConfig(s string) (*balancerConfig.Config, error) {
9095
switch c.Prefer {
9196
case preferTypeLocalDC:
9297
if c.Fallback {
93-
return PreferLocalDCWithFallBack(b), nil
98+
return PreferNearestDCWithFallBack(b), nil
99+
}
100+
101+
return PreferNearestDC(b), nil
102+
case preferTypeNearestDC:
103+
if c.Fallback {
104+
return PreferNearestDCWithFallBack(b), nil
94105
}
95106

96-
return PreferLocalDC(b), nil
107+
return PreferNearestDC(b), nil
97108
case preferTypeLocations:
98109
if len(c.Locations) == 0 {
99110
return nil, xerrors.WithStackTrace(fmt.Errorf("empty locations list in balancer '%s' config", c.Type))

balancers/config_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,21 @@ func TestFromConfig(t *testing.T) {
7070
"prefer": "local_dc"
7171
}`,
7272
res: balancerConfig.Config{
73-
DetectLocalDC: true,
73+
DetectNearestDC: true,
74+
Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool {
75+
// some non nil func
76+
return false
77+
}),
78+
},
79+
},
80+
{
81+
name: "prefer_nearest_dc",
82+
config: `{
83+
"type": "random_choice",
84+
"prefer": "nearest_dc"
85+
}`,
86+
res: balancerConfig.Config{
87+
DetectNearestDC: true,
7488
Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool {
7589
// some non nil func
7690
return false
@@ -93,8 +107,24 @@ func TestFromConfig(t *testing.T) {
93107
"fallback": true
94108
}`,
95109
res: balancerConfig.Config{
96-
AllowFallback: true,
97-
DetectLocalDC: true,
110+
AllowFallback: true,
111+
DetectNearestDC: true,
112+
Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool {
113+
// some non nil func
114+
return false
115+
}),
116+
},
117+
},
118+
{
119+
name: "prefer_nearest_dc_with_fallback",
120+
config: `{
121+
"type": "random_choice",
122+
"prefer": "nearest_dc",
123+
"fallback": true
124+
}`,
125+
res: balancerConfig.Config{
126+
AllowFallback: true,
127+
DetectNearestDC: true,
98128
Filter: filterFunc(func(info balancerConfig.Info, e endpoint.Info) bool {
99129
// some non nil func
100130
return false

internal/balancer/balancer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (b *Balancer) clusterDiscoveryAttempt(ctx context.Context) (err error) {
111111
return xerrors.WithStackTrace(err)
112112
}
113113

114-
if b.config.DetectLocalDC {
114+
if b.config.DetectNearestDC {
115115
localDC, err = b.localDCDetector(ctx, endpoints)
116116
if err != nil {
117117
return xerrors.WithStackTrace(err)
@@ -129,7 +129,7 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi
129129
b.driverConfig.Trace(), &ctx,
130130
stack.FunctionID(
131131
"github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer.(*Balancer).applyDiscoveredEndpoints"),
132-
b.config.DetectLocalDC,
132+
b.config.DetectNearestDC,
133133
)
134134
previous = b.connections().All()
135135
)

internal/balancer/config/routerconfig.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
// Dedicated package need for prevent cyclo dependencies config -> balancer -> config
1111

1212
type Config struct {
13-
Filter Filter
14-
AllowFallback bool
15-
SingleConn bool
16-
DetectLocalDC bool
13+
Filter Filter
14+
AllowFallback bool
15+
SingleConn bool
16+
DetectNearestDC bool
1717
}
1818

1919
func (c Config) String() string {
@@ -26,8 +26,8 @@ func (c Config) String() string {
2626

2727
buffer.WriteString("RandomChoice{")
2828

29-
buffer.WriteString("DetectLocalDC=")
30-
fmt.Fprintf(buffer, "%t", c.DetectLocalDC)
29+
buffer.WriteString("DetectNearestDC=")
30+
fmt.Fprintf(buffer, "%t", c.DetectNearestDC)
3131

3232
buffer.WriteString(",AllowFallback=")
3333
fmt.Fprintf(buffer, "%t", c.AllowFallback)

internal/balancer/local_dc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestDetectLocalDC(t *testing.T) {
131131
func TestLocalDCDiscovery(t *testing.T) {
132132
ctx := context.Background()
133133
cfg := config.New(
134-
config.WithBalancer(balancers.PreferLocalDC(balancers.Default())),
134+
config.WithBalancer(balancers.PreferNearestDC(balancers.Default())),
135135
)
136136
r := &Balancer{
137137
driverConfig: cfg,

0 commit comments

Comments
 (0)