Skip to content
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

xds: use locality from the connected address for load reporting #7378

Merged
merged 9 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove setting locality in NewSubConn.
  • Loading branch information
townba committed Jul 2, 2024
commit 18bce3623cd9b3daae6a1533cbe1dcadc5525670
16 changes: 16 additions & 0 deletions internal/testutils/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"google.golang.org/grpc/balancer"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/resolver"
)
Expand Down Expand Up @@ -76,6 +77,21 @@ func (tsc *TestSubConn) UpdateState(state balancer.SubConnState) {
}
}

// UpdateStateAndConnectedAddress saves the connected address to state if the
easwars marked this conversation as resolved.
Show resolved Hide resolved
// connectivity state is Ready and pushes the state to the listener if one is
// registered.
func (tsc *TestSubConn) UpdateStateAndConnectedAddress(state balancer.SubConnState, addr resolver.Address) {
<-tsc.connectCalled.Done()
if state.ConnectivityState == connectivity.Ready {
sca := internal.SetConnectedAddress.(func(*balancer.SubConnState, resolver.Address))
sca(&state, addr)
}
if tsc.stateListener != nil {
tsc.stateListener(state)
return
}
}

// Shutdown pushes the SubConn to the ShutdownSubConn channel in the parent
// TestClientConn.
func (tsc *TestSubConn) Shutdown() {
Expand Down
2 changes: 1 addition & 1 deletion xds/internal/balancer/clusterimpl/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ func (s) TestLoadReporting(t *testing.T) {
t.Fatal(err.Error())
}

sc1.UpdateState(balancer.SubConnState{ConnectivityState: connectivity.Ready})
sc1.UpdateStateAndConnectedAddress(balancer.SubConnState{ConnectivityState: connectivity.Ready}, addrs[0])
// Test pick with one backend.
const successCount = 5
const errorCount = 5
Expand Down
4 changes: 0 additions & 4 deletions xds/internal/balancer/clusterimpl/clusterimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,8 @@
func (b *clusterImplBalancer) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
clusterName := b.getClusterName()
newAddrs := make([]resolver.Address, len(addrs))
// TODO: Don't set the locality here. Let the StateListener handle it all.
var lID xdsinternal.LocalityID
for i, addr := range addrs {
newAddrs[i] = xds.SetXDSHandshakeClusterName(addr, clusterName)
lID = xdsinternal.GetLocalityID(newAddrs[i])
}
var sc balancer.SubConn
scw := &scWrapper{}
Expand All @@ -380,7 +377,7 @@
if !lID.Empty() {
scw.updateLocalityID(lID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go style: let's try to avoid all this nesting:

StateListener = func... {
	b.updateSubConnState(...)
	if state != Ready {
		return
	}
	locality := xdsinternal.GetLocalityID(getConnectedAddress(state))
	if locality.Empty() {
		if logger.V(2) { log }
		return
	}
	scw.updateLocalityID(locality)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

} else if b.logger.V(2) {
b.logger.Infof("Locality ID for %v unexpectedly empty", addr)

Check warning on line 380 in xds/internal/balancer/clusterimpl/clusterimpl.go

View check run for this annotation

Codecov / codecov/patch

xds/internal/balancer/clusterimpl/clusterimpl.go#L380

Added line #L380 was not covered by tests
easwars marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand All @@ -390,7 +387,6 @@
return nil, err
}
scw.SubConn = sc
scw.updateLocalityID(lID)
return scw, nil
}

Expand Down
Loading