-
Notifications
You must be signed in to change notification settings - Fork 95
close dropped connection on balancer.Update() #1694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
qrort
wants to merge
4
commits into
master
Choose a base branch
from
fix-driver-conns-metric
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,6 +175,35 @@ func (b *Balancer) clusterDiscoveryAttempt(ctx context.Context, cc *grpc.ClientC | |
return nil | ||
} | ||
|
||
func buildConnectionsState(ctx context.Context, | ||
pool interface { | ||
GetIfPresent(endpoint endpoint.Endpoint) conn.Conn | ||
Allow(ctx context.Context, cc conn.Conn) | ||
EndpointsToConnections(endpoints []endpoint.Endpoint) []conn.Conn | ||
}, | ||
newest []endpoint.Endpoint, | ||
dropped []endpoint.Endpoint, | ||
balancerConfig *balancerConfig.Config, | ||
selfLocation balancerConfig.Info, | ||
) *connectionsState { | ||
connections := pool.EndpointsToConnections(newest) | ||
for _, c := range connections { | ||
pool.Allow(ctx, c) | ||
c.Endpoint().Touch() | ||
} | ||
|
||
state := newConnectionsState(connections, balancerConfig.Filter, selfLocation, balancerConfig.AllowFallback) | ||
|
||
for _, e := range dropped { | ||
c := pool.GetIfPresent(e) | ||
if c != nil { | ||
_ = c.Close(ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the connection is closed, requests that are performing long-running stream reading may be interrupted. Required graceful termination of requests. |
||
} | ||
} | ||
|
||
return state | ||
} | ||
|
||
func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoint.Endpoint, localDC string) { | ||
var ( | ||
onDone = trace.DriverOnBalancerUpdate( | ||
|
@@ -186,10 +215,12 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi | |
) | ||
previous = b.connections().All() | ||
) | ||
|
||
kprokopenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_, added, dropped := xslices.Diff(previous, newest, func(lhs, rhs endpoint.Endpoint) int { | ||
return strings.Compare(lhs.Address(), rhs.Address()) | ||
}) | ||
|
||
defer func() { | ||
_, added, dropped := xslices.Diff(previous, newest, func(lhs, rhs endpoint.Endpoint) int { | ||
return strings.Compare(lhs.Address(), rhs.Address()) | ||
}) | ||
onDone( | ||
xslices.Transform(newest, func(t endpoint.Endpoint) trace.EndpointInfo { return t }), | ||
xslices.Transform(added, func(t endpoint.Endpoint) trace.EndpointInfo { return t }), | ||
|
@@ -198,21 +229,8 @@ func (b *Balancer) applyDiscoveredEndpoints(ctx context.Context, newest []endpoi | |
) | ||
}() | ||
|
||
connections := endpointsToConnections(b.pool, newest) | ||
for _, c := range connections { | ||
b.pool.Allow(ctx, c) | ||
c.Endpoint().Touch() | ||
} | ||
|
||
info := balancerConfig.Info{SelfLocation: localDC} | ||
state := newConnectionsState(connections, b.balancerConfig.Filter, info, b.balancerConfig.AllowFallback) | ||
|
||
endpointsInfo := make([]endpoint.Info, len(newest)) | ||
for i, e := range newest { | ||
endpointsInfo[i] = e | ||
} | ||
|
||
b.connectionsState.Store(state) | ||
b.connectionsState.Store(buildConnectionsState(ctx, b.pool, newest, dropped, &b.balancerConfig, info)) | ||
} | ||
|
||
func (b *Balancer) Close(ctx context.Context) (err error) { | ||
|
@@ -444,12 +462,3 @@ func (b *Balancer) nextConn(ctx context.Context) (c conn.Conn, err error) { | |
|
||
return c, nil | ||
} | ||
|
||
func endpointsToConnections(p *conn.Pool, endpoints []endpoint.Endpoint) []conn.Conn { | ||
conns := make([]conn.Conn, 0, len(endpoints)) | ||
for _, e := range endpoints { | ||
conns = append(conns, p.Get(e)) | ||
} | ||
|
||
return conns | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package balancer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
balancerConfig "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/config" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/internal/conn" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/internal/mock" | ||
) | ||
|
||
type fakePool struct { | ||
connections map[string]*mock.Conn | ||
} | ||
|
||
func (fp *fakePool) EndpointsToConnections(eps []endpoint.Endpoint) []conn.Conn { | ||
var conns []conn.Conn | ||
for _, ep := range eps { | ||
if c, ok := fp.connections[ep.Address()]; ok { | ||
conns = append(conns, c) | ||
} | ||
} | ||
|
||
return conns | ||
} | ||
|
||
func (fp *fakePool) Allow(_ context.Context, c conn.Conn) { | ||
if c, ok := fp.connections[c.Endpoint().Address()]; ok { | ||
c.Allowed.Store(true) | ||
} | ||
} | ||
|
||
func (fp *fakePool) GetIfPresent(ep endpoint.Endpoint) conn.Conn { | ||
if c, ok := fp.connections[ep.Address()]; ok { | ||
return c | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestBuildConnectionsState(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
tests := []struct { | ||
name string | ||
newEndpoints []endpoint.Endpoint | ||
oldEndpoints []endpoint.Endpoint | ||
initialConns map[string]*mock.Conn | ||
conf balancerConfig.Config | ||
selfLoc balancerConfig.Info | ||
expectAllowed []string | ||
expectClosed []string | ||
}{ | ||
{ | ||
newEndpoints: []endpoint.Endpoint{&mock.Endpoint{AddrField: "a1"}, &mock.Endpoint{AddrField: "a2"}}, | ||
oldEndpoints: []endpoint.Endpoint{&mock.Endpoint{AddrField: "a3"}, &mock.Endpoint{AddrField: "a4"}}, | ||
initialConns: map[string]*mock.Conn{ | ||
"a1": { | ||
AddrField: "a1", | ||
LocationField: "local", | ||
State: conn.Offline, | ||
}, | ||
"a2": { | ||
AddrField: "a2", | ||
State: conn.Offline, | ||
PingErr: ErrNoEndpoints, | ||
}, | ||
"a3": { | ||
AddrField: "a3", | ||
State: conn.Online, | ||
}, | ||
"a4": { | ||
AddrField: "a4", | ||
State: conn.Online, | ||
}, | ||
}, | ||
conf: balancerConfig.Config{ | ||
AllowFallback: true, | ||
DetectNearestDC: true, | ||
}, | ||
selfLoc: balancerConfig.Info{SelfLocation: "local"}, | ||
expectAllowed: []string{"a1", "a2"}, | ||
expectClosed: []string{"a3", "a4"}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fp := &fakePool{connections: make(map[string]*mock.Conn)} | ||
for addr, c := range tt.initialConns { | ||
fp.connections[addr] = c | ||
} | ||
|
||
state := buildConnectionsState(ctx, fp, tt.newEndpoints, tt.oldEndpoints, &tt.conf, tt.selfLoc) | ||
assert.NotNil(t, state) | ||
for _, addr := range tt.expectAllowed { | ||
c := fp.connections[addr] | ||
assert.True(t, c.Allowed.Load(), "connection %s should be allowed", addr) | ||
} | ||
for _, addr := range tt.expectClosed { | ||
c := fp.connections[addr] | ||
assert.True(t, c.Closed.Load(), "connection %s should be closed", addr) | ||
assert.True(t, c.State == conn.Offline, "connection %s should be offline", addr) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package integration | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/metrics" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/query" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/trace" | ||
) | ||
|
||
func TestYDBConnMetrics(t *testing.T) { | ||
var ( | ||
scope = newScope(t) | ||
registry = ®istryConfig{ | ||
details: trace.DriverConnEvents, | ||
gauges: newVec[gaugeVec](), | ||
counters: newVec[counterVec](), | ||
timers: newVec[timerVec](), | ||
histograms: newVec[histogramVec](), | ||
} | ||
) | ||
|
||
db, err := ydb.Open(scope.Ctx, "grpc://localhost:2136/local", metrics.WithTraces(registry)) | ||
scope.Require.NoError(err) | ||
defer db.Close(scope.Ctx) // cleanup resources | ||
|
||
db.Query().Do(scope.Ctx, func(ctx context.Context, s query.Session) error { | ||
return s.Exec(ctx, "SELECT 42") | ||
}) | ||
|
||
metric := "ydb.driver.conns" | ||
labelsNames := []string{"endpoint", "node_id"} | ||
labels := map[string]string{"endpoint": "localhost:2136", "node_id": "1"} | ||
|
||
gauges := registry.gauges.data[nameLabelNamesToString(metric, labelsNames)] | ||
scope.Require.NotNil(gauges) | ||
|
||
labeledGauges := gauges.gauges[nameLabelValuesToString(metric, labels)] | ||
scope.Require.NotNil(labeledGauges) | ||
|
||
scope.Require.EqualValues(1, labeledGauges.value) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.