Skip to content

Commit

Permalink
xds: fix hash policy header to skip bin headers and use extra metadata (
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley authored Sep 7, 2023
1 parent 1e0d82e commit b0a946c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
22 changes: 17 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,30 @@ 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 (e.g. the "content-type" header) takes
// precedence over the user's metadata.
values = md.Get(policy.HeaderName)
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 takes precedence over the user's metadata.
{
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

0 comments on commit b0a946c

Please sign in to comment.