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: fix hash policy header to skip bin headers and use extra metadata #6609

Merged
merged 2 commits into from
Sep 7, 2023
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
21 changes: 16 additions & 5 deletions xds/internal/resolver/serviceconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/internal/envconfig"
"google.golang.org/grpc/internal/grpcrand"
"google.golang.org/grpc/internal/grpcutil"
iresolver "google.golang.org/grpc/internal/resolver"
"google.golang.org/grpc/internal/serviceconfig"
"google.golang.org/grpc/internal/wrr"
Expand Down Expand Up @@ -229,19 +230,29 @@ func retryConfigToPolicy(config *xdsresource.RetryConfig) *serviceconfig.RetryPo
func (cs *configSelector) generateHash(rpcInfo iresolver.RPCInfo, hashPolicies []*xdsresource.HashPolicy) uint64 {
var hash uint64
var generatedHash bool
var md, emd metadata.MD
var mdRead bool
for _, policy := range hashPolicies {
var policyHash uint64
var generatedPolicyHash bool
switch policy.HashPolicyType {
case xdsresource.HashPolicyTypeHeader:
md, ok := metadata.FromOutgoingContext(rpcInfo.Context)
if !ok {
if strings.HasSuffix(policy.HeaderName, "-bin") {
continue
}
values := md.Get(policy.HeaderName)
// If the header isn't present, no-op.
if !mdRead {
md, _ = metadata.FromOutgoingContext(rpcInfo.Context)
emd, _ = grpcutil.ExtraMetadata(rpcInfo.Context)
mdRead = true
}
values := emd.Get(policy.HeaderName)
if len(values) == 0 {
continue
// Extra metadata takes precedence over the user's metadata.
zasweq marked this conversation as resolved.
Show resolved Hide resolved
values = md.Get(policy.HeaderName)
Copy link
Contributor

Choose a reason for hiding this comment

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

So at this point in the RPC lifecycle, the only overwriting extra metadata is "content-type"? "However, we will support matching against the content-type header; if a gRPC implementation does not add that header until after xDS route selection is done, then xDS route selection will assume a hard-coded value of application/grpc."

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, the "extra metadata" is "content-type"... I'm not sure I like how this all was done (originally for route matching), but it works at least.

if len(values) == 0 {
// If the header isn't present at all, this policy is a no-op.
continue
}
}
joinedValues := strings.Join(values, ",")
if policy.Regex != nil {
Expand Down
30 changes: 30 additions & 0 deletions xds/internal/resolver/serviceconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

xxhash "github.com/cespare/xxhash/v2"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/internal/grpcutil"
iresolver "google.golang.org/grpc/internal/resolver"
"google.golang.org/grpc/metadata"
_ "google.golang.org/grpc/xds/internal/balancer/cdsbalancer" // To parse LB config
Expand Down Expand Up @@ -106,6 +107,35 @@ func (s) TestGenerateRequestHash(t *testing.T) {
Method: "/some-method",
},
},
// Tests that bin headers are skipped.
{
name: "skip-bin",
hashPolicies: []*xdsresource.HashPolicy{{
HashPolicyType: xdsresource.HashPolicyTypeHeader,
HeaderName: "something-bin",
}, {
HashPolicyType: xdsresource.HashPolicyTypeChannelID,
}},
requestHashWant: channelID,
rpcInfo: iresolver.RPCInfo{
Context: metadata.NewOutgoingContext(context.Background(), metadata.Pairs("something-bin", "xyz")),
},
},
// Tests that extra metadata is used first.
zasweq marked this conversation as resolved.
Show resolved Hide resolved
{
name: "extra-metadata",
hashPolicies: []*xdsresource.HashPolicy{{
HashPolicyType: xdsresource.HashPolicyTypeHeader,
HeaderName: "content-type",
}},
requestHashWant: xxhash.Sum64String("grpc value"),
rpcInfo: iresolver.RPCInfo{
Context: grpcutil.WithExtraMetadata(
metadata.NewOutgoingContext(context.Background(), metadata.Pairs("content-type", "user value")),
metadata.Pairs("content-type", "grpc value"),
),
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down