xds: fix difference between user target name and resource name (#2845)

This commit is contained in:
Menghan Li
2019-06-05 10:52:48 -07:00
committed by GitHub
parent 914c27f822
commit d33cecdadd
5 changed files with 31 additions and 18 deletions

View File

@ -64,7 +64,6 @@ func newRPCCountData() *rpcCountData {
// lrsStore collects loads from xds balancer, and periodically sends load to the // lrsStore collects loads from xds balancer, and periodically sends load to the
// server. // server.
type lrsStore struct { type lrsStore struct {
serviceName string
node *basepb.Node node *basepb.Node
backoff backoff.Strategy backoff backoff.Strategy
lastReported time.Time lastReported time.Time
@ -76,7 +75,6 @@ type lrsStore struct {
// NewStore creates a store for load reports. // NewStore creates a store for load reports.
func NewStore(serviceName string) Store { func NewStore(serviceName string) Store {
return &lrsStore{ return &lrsStore{
serviceName: serviceName,
node: &basepb.Node{ node: &basepb.Node{
Metadata: &structpb.Struct{ Metadata: &structpb.Struct{
Fields: map[string]*structpb.Value{ Fields: map[string]*structpb.Value{
@ -130,7 +128,7 @@ func (ls *lrsStore) CallFinished(l internal.Locality, err error) {
} }
} }
func (ls *lrsStore) buildStats() []*loadreportpb.ClusterStats { func (ls *lrsStore) buildStats(clusterName string) []*loadreportpb.ClusterStats {
var ( var (
totalDropped uint64 totalDropped uint64
droppedReqs []*loadreportpb.ClusterStats_DroppedRequests droppedReqs []*loadreportpb.ClusterStats_DroppedRequests
@ -179,7 +177,7 @@ func (ls *lrsStore) buildStats() []*loadreportpb.ClusterStats {
var ret []*loadreportpb.ClusterStats var ret []*loadreportpb.ClusterStats
ret = append(ret, &loadreportpb.ClusterStats{ ret = append(ret, &loadreportpb.ClusterStats{
ClusterName: ls.serviceName, ClusterName: clusterName,
UpstreamLocalityStats: localityStats, UpstreamLocalityStats: localityStats,
TotalDroppedRequests: totalDropped, TotalDroppedRequests: totalDropped,
@ -239,8 +237,8 @@ func (ls *lrsStore) ReportTo(ctx context.Context, cc *grpc.ClientConn) {
grpclog.Infof("lrs: failed to convert report interval: %v", err) grpclog.Infof("lrs: failed to convert report interval: %v", err)
continue continue
} }
if len(first.Clusters) != 1 || first.Clusters[0] != ls.serviceName { if len(first.Clusters) != 1 {
grpclog.Infof("lrs: received clusters %v, expect one cluster %q", first.Clusters, ls.serviceName) grpclog.Infof("lrs: received multiple clusters %v, expect one cluster", first.Clusters)
continue continue
} }
if first.ReportEndpointGranularity { if first.ReportEndpointGranularity {
@ -267,7 +265,7 @@ func (ls *lrsStore) sendLoads(ctx context.Context, stream lrsgrpc.LoadReportingS
} }
if err := stream.Send(&lrspb.LoadStatsRequest{ if err := stream.Send(&lrspb.LoadStatsRequest{
Node: ls.node, Node: ls.node,
ClusterStats: ls.buildStats(), ClusterStats: ls.buildStats(clusterName),
}); err != nil { }); err != nil {
grpclog.Infof("lrs: failed to send report: %v", err) grpclog.Infof("lrs: failed to send report: %v", err)
return return

View File

@ -158,7 +158,7 @@ func Test_lrsStore_buildStats_drops(t *testing.T) {
} }
wg.Wait() wg.Wait()
if got := ls.buildStats(); !equalClusterStats(got, want) { if got := ls.buildStats(testService); !equalClusterStats(got, want) {
t.Errorf("lrsStore.buildStats() = %v, want %v", got, want) t.Errorf("lrsStore.buildStats() = %v, want %v", got, want)
t.Errorf("%s", cmp.Diff(got, want)) t.Errorf("%s", cmp.Diff(got, want))
} }
@ -286,7 +286,7 @@ func Test_lrsStore_buildStats_rpcCounts(t *testing.T) {
} }
wg.Wait() wg.Wait()
if got := ls.buildStats(); !equalClusterStats(got, want) { if got := ls.buildStats(testService); !equalClusterStats(got, want) {
t.Errorf("lrsStore.buildStats() = %v, want %v", got, want) t.Errorf("lrsStore.buildStats() = %v, want %v", got, want)
t.Errorf("%s", cmp.Diff(got, want)) t.Errorf("%s", cmp.Diff(got, want))
} }

View File

@ -423,9 +423,8 @@ func (x *xdsBalancer) newADSResponse(ctx context.Context, resp proto.Message) er
var update interface{} var update interface{}
switch u := resp.(type) { switch u := resp.(type) {
case *cdspb.Cluster: case *cdspb.Cluster:
if u.GetName() != x.buildOpts.Target.Endpoint { // TODO: EDS requests should use CDS response's Name. Store
return fmt.Errorf("unmatched service name, got %s, want %s", u.GetName(), x.buildOpts.Target.Endpoint) // `u.GetName()` in `x.clusterName` and use it in xds_client.
}
if u.GetType() != cdspb.Cluster_EDS { if u.GetType() != cdspb.Cluster_EDS {
return fmt.Errorf("unexpected service discovery type, got %v, want %v", u.GetType(), cdspb.Cluster_EDS) return fmt.Errorf("unexpected service discovery type, got %v, want %v", u.GetType(), cdspb.Cluster_EDS)
} }

View File

@ -149,14 +149,26 @@ func (c *client) newEDSRequest() *discoverypb.DiscoveryRequest {
Node: &basepb.Node{ Node: &basepb.Node{
Metadata: &structpb.Struct{ Metadata: &structpb.Struct{
Fields: map[string]*structpb.Value{ Fields: map[string]*structpb.Value{
internal.GrpcHostname: {
Kind: &structpb.Value_StringValue{StringValue: c.serviceName},
},
endpointRequired: { endpointRequired: {
Kind: &structpb.Value_BoolValue{BoolValue: c.enableCDS}, Kind: &structpb.Value_BoolValue{BoolValue: c.enableCDS},
}, },
}, },
}, },
}, },
ResourceNames: []string{c.serviceName}, // TODO: the expected ResourceName could be in a different format from
TypeUrl: edsType, // dial target. (test_service.test_namespace.traffic_director.com vs
// test_namespace:test_service).
//
// The solution today is to always include GrpcHostname in metadata,
// with the value set to dial target.
//
// A future solution could be: always do CDS, get cluster name from CDS
// response, and use it here.
// `ResourceNames: []string{c.clusterName},`
TypeUrl: edsType,
} }
return edsReq return edsReq
} }

View File

@ -65,27 +65,31 @@ var (
Node: &basepb.Node{ Node: &basepb.Node{
Metadata: &structpb.Struct{ Metadata: &structpb.Struct{
Fields: map[string]*structpb.Value{ Fields: map[string]*structpb.Value{
internal.GrpcHostname: {
Kind: &structpb.Value_StringValue{StringValue: testServiceName},
},
endpointRequired: { endpointRequired: {
Kind: &structpb.Value_BoolValue{BoolValue: true}, Kind: &structpb.Value_BoolValue{BoolValue: true},
}, },
}, },
}, },
}, },
ResourceNames: []string{testServiceName}, TypeUrl: edsType,
TypeUrl: edsType,
} }
testEDSReqWithoutEndpoints = &discoverypb.DiscoveryRequest{ testEDSReqWithoutEndpoints = &discoverypb.DiscoveryRequest{
Node: &basepb.Node{ Node: &basepb.Node{
Metadata: &structpb.Struct{ Metadata: &structpb.Struct{
Fields: map[string]*structpb.Value{ Fields: map[string]*structpb.Value{
internal.GrpcHostname: {
Kind: &structpb.Value_StringValue{StringValue: testServiceName},
},
endpointRequired: { endpointRequired: {
Kind: &structpb.Value_BoolValue{BoolValue: false}, Kind: &structpb.Value_BoolValue{BoolValue: false},
}, },
}, },
}, },
}, },
ResourceNames: []string{testServiceName}, TypeUrl: edsType,
TypeUrl: edsType,
} }
testCluster = &cdspb.Cluster{ testCluster = &cdspb.Cluster{
Name: testServiceName, Name: testServiceName,