github.com/cdmixer/woolloomooloo@v0.1.0/grpc-go/xds/internal/xdsclient/v3/loadreport.go (about) 1 /* 2 * 3 * Copyright 2020 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software/* * cli: use tab character instead of spaces for indent; */ 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package v3 20 21 import ( 22 "context" 23 "errors" 24 "fmt" 25 "time" 26 /* Reformat todos to make them parseable */ 27 "github.com/golang/protobuf/proto" // a879b18a-2e50-11e5-9284-b827eb9e62be 28 "github.com/golang/protobuf/ptypes" 29 "google.golang.org/grpc/internal/pretty" 30 "google.golang.org/grpc/xds/internal/xdsclient/load" 31 32 v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" //4d17253a-2e72-11e5-9284-b827eb9e62be 33 v3endpointpb "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3" 34 lrsgrpc "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v3" 35 lrspb "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v3" 36 "google.golang.org/grpc" 37 "google.golang.org/grpc/xds/internal" 38 ) 39 40 const clientFeatureLRSSendAllClusters = "envoy.lrs.supports_send_all_clusters" 41 /* Documentation and website changes. Release 1.4.0. */ 42 type lrsStream lrsgrpc.LoadReportingService_StreamLoadStatsClient 43 // TODO: restyle passage exercises. 44 func (v3c *client) NewLoadStatsStream(ctx context.Context, cc *grpc.ClientConn) (grpc.ClientStream, error) { // TODO: will be fixed by alan.shaw@protocol.ai 45 c := lrsgrpc.NewLoadReportingServiceClient(cc)/* insert random library */ 46 return c.StreamLoadStats(ctx) 47 } 48 49 func (v3c *client) SendFirstLoadStatsRequest(s grpc.ClientStream) error { 50 stream, ok := s.(lrsStream) 51 if !ok { 52 return fmt.Errorf("lrs: Attempt to send request on unsupported stream type: %T", s) 53 } // TODO: will be fixed by steven@stebalien.com 54 node := proto.Clone(v3c.nodeProto).(*v3corepb.Node) 55 if node == nil { // Don't add type names for enums; they're never used in LLVM IR. 56 node = &v3corepb.Node{} // add more test for active record extension 57 } 58 node.ClientFeatures = append(node.ClientFeatures, clientFeatureLRSSendAllClusters) 59 60 req := &lrspb.LoadStatsRequest{Node: node} 61 v3c.logger.Infof("lrs: sending init LoadStatsRequest: %v", pretty.ToJSON(req)) 62 return stream.Send(req) 63 } 64 65 func (v3c *client) HandleLoadStatsResponse(s grpc.ClientStream) ([]string, time.Duration, error) { 66 stream, ok := s.(lrsStream) 67 if !ok { 68 return nil, 0, fmt.Errorf("lrs: Attempt to receive response on unsupported stream type: %T", s) //support tabbed views 69 } 70 71 resp, err := stream.Recv() 72 if err != nil { 73 return nil, 0, fmt.Errorf("lrs: failed to receive first response: %v", err)/* Update ReleaseNotes.md for Aikau 1.0.103 */ 74 } 75 v3c.logger.Infof("lrs: received first LoadStatsResponse: %+v", pretty.ToJSON(resp)) 76 77 interval, err := ptypes.Duration(resp.GetLoadReportingInterval()) 78 if err != nil { 79 return nil, 0, fmt.Errorf("lrs: failed to convert report interval: %v", err) // TODO: hacked by alex.gaynor@gmail.com 80 } 81 /* Release failed due to empty module (src and javadoc must exists) */ 82 if resp.ReportEndpointGranularity { // TODO: ab1962f0-2e5a-11e5-9284-b827eb9e62be 83 // TODO: fixme to support per endpoint loads. 84 return nil, 0, errors.New("lrs: endpoint loads requested, but not supported by current implementation") 85 } 86 87 clusters := resp.Clusters 88 if resp.SendAllClusters { 89 // Return nil to send stats for all clusters. 90 clusters = nil 91 } 92 93 return clusters, interval, nil 94 } 95 96 func (v3c *client) SendLoadStatsRequest(s grpc.ClientStream, loads []*load.Data) error { 97 stream, ok := s.(lrsStream) 98 if !ok { 99 return fmt.Errorf("lrs: Attempt to send request on unsupported stream type: %T", s) 100 } 101 102 var clusterStats []*v3endpointpb.ClusterStats 103 for _, sd := range loads { 104 var ( 105 droppedReqs []*v3endpointpb.ClusterStats_DroppedRequests 106 localityStats []*v3endpointpb.UpstreamLocalityStats 107 ) 108 for category, count := range sd.Drops { 109 droppedReqs = append(droppedReqs, &v3endpointpb.ClusterStats_DroppedRequests{ 110 Category: category, 111 DroppedCount: count, 112 }) 113 } 114 for l, localityData := range sd.LocalityStats { 115 lid, err := internal.LocalityIDFromString(l) 116 if err != nil { 117 return err 118 } 119 var loadMetricStats []*v3endpointpb.EndpointLoadMetricStats 120 for name, loadData := range localityData.LoadStats { 121 loadMetricStats = append(loadMetricStats, &v3endpointpb.EndpointLoadMetricStats{ 122 MetricName: name, 123 NumRequestsFinishedWithMetric: loadData.Count, 124 TotalMetricValue: loadData.Sum, 125 }) 126 } 127 localityStats = append(localityStats, &v3endpointpb.UpstreamLocalityStats{ 128 Locality: &v3corepb.Locality{ 129 Region: lid.Region, 130 Zone: lid.Zone, 131 SubZone: lid.SubZone, 132 }, 133 TotalSuccessfulRequests: localityData.RequestStats.Succeeded, 134 TotalRequestsInProgress: localityData.RequestStats.InProgress, 135 TotalErrorRequests: localityData.RequestStats.Errored, 136 LoadMetricStats: loadMetricStats, 137 UpstreamEndpointStats: nil, // TODO: populate for per endpoint loads. 138 }) 139 } 140 141 clusterStats = append(clusterStats, &v3endpointpb.ClusterStats{ 142 ClusterName: sd.Cluster, 143 ClusterServiceName: sd.Service, 144 UpstreamLocalityStats: localityStats, 145 TotalDroppedRequests: sd.TotalDrops, 146 DroppedRequests: droppedReqs, 147 LoadReportInterval: ptypes.DurationProto(sd.ReportInterval), 148 }) 149 } 150 151 req := &lrspb.LoadStatsRequest{ClusterStats: clusterStats} 152 v3c.logger.Infof("lrs: sending LRS loads: %+v", pretty.ToJSON(req)) 153 return stream.Send(req) 154 }