|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2023 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 |
| 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 | +// Binary client is an example client. |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "flag" |
| 25 | + "fmt" |
| 26 | + "log" |
| 27 | + "time" |
| 28 | + |
| 29 | + "google.golang.org/grpc" |
| 30 | + "google.golang.org/grpc/balancer" |
| 31 | + "google.golang.org/grpc/connectivity" |
| 32 | + "google.golang.org/grpc/credentials/insecure" |
| 33 | + "google.golang.org/grpc/orca" |
| 34 | + |
| 35 | + v3orcapb "github.com/cncf/xds/go/xds/data/orca/v3" |
| 36 | + pb "google.golang.org/grpc/examples/features/proto/echo" |
| 37 | +) |
| 38 | + |
| 39 | +var addr = flag.String("addr", "localhost:50051", "the address to connect to") |
| 40 | +var test = flag.Bool("test", false, "if set, only 1 RPC is performed before exiting") |
| 41 | + |
| 42 | +func main() { |
| 43 | + flag.Parse() |
| 44 | + |
| 45 | + // Set up a connection to the server. Configure to use our custom LB |
| 46 | + // policy which will receive all the ORCA load reports. |
| 47 | + conn, err := grpc.Dial(*addr, |
| 48 | + grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 49 | + grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"orca_example":{}}]}`), |
| 50 | + ) |
| 51 | + if err != nil { |
| 52 | + log.Fatalf("did not connect: %v", err) |
| 53 | + } |
| 54 | + defer conn.Close() |
| 55 | + |
| 56 | + c := pb.NewEchoClient(conn) |
| 57 | + |
| 58 | + // Perform RPCs once per second. |
| 59 | + ticker := time.NewTicker(time.Second) |
| 60 | + for range ticker.C { |
| 61 | + func() { |
| 62 | + // Use an anonymous function to ensure context cancelation via defer. |
| 63 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 64 | + defer cancel() |
| 65 | + if _, err := c.UnaryEcho(ctx, &pb.EchoRequest{Message: "test echo message"}); err != nil { |
| 66 | + log.Fatalf("Error from UnaryEcho call: %v", err) |
| 67 | + } |
| 68 | + }() |
| 69 | + if *test { |
| 70 | + return |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +// Register an ORCA load balancing policy to receive per-call metrics and |
| 77 | +// out-of-band metrics. |
| 78 | +func init() { |
| 79 | + balancer.Register(orcaLBBuilder{}) |
| 80 | +} |
| 81 | + |
| 82 | +type orcaLBBuilder struct{} |
| 83 | + |
| 84 | +func (orcaLBBuilder) Name() string { return "orca_example" } |
| 85 | +func (orcaLBBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer { |
| 86 | + return &orcaLB{cc: cc} |
| 87 | +} |
| 88 | + |
| 89 | +// orcaLB is an incomplete LB policy designed to show basic ORCA load reporting |
| 90 | +// functionality. It collects per-call metrics in the `Done` callback returned |
| 91 | +// by its picker, and it collects out-of-band metrics by registering a listener |
| 92 | +// when its SubConn is created. It does not follow general LB policy best |
| 93 | +// practices and makes assumptions about the simple test environment it is |
| 94 | +// designed to run within. |
| 95 | +type orcaLB struct { |
| 96 | + cc balancer.ClientConn |
| 97 | +} |
| 98 | + |
| 99 | +func (o *orcaLB) UpdateClientConnState(ccs balancer.ClientConnState) error { |
| 100 | + // We assume only one update, ever, containing exactly one address, given |
| 101 | + // the use of the "passthrough" (default) name resolver. |
| 102 | + |
| 103 | + addrs := ccs.ResolverState.Addresses |
| 104 | + if len(addrs) != 1 { |
| 105 | + return fmt.Errorf("orcaLB: expected 1 address; received: %v", addrs) |
| 106 | + } |
| 107 | + |
| 108 | + // Create one SubConn for the address and connect it. |
| 109 | + sc, err := o.cc.NewSubConn(addrs, balancer.NewSubConnOptions{}) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("orcaLB: error creating SubConn: %v", err) |
| 112 | + } |
| 113 | + sc.Connect() |
| 114 | + |
| 115 | + // Register a simple ORCA OOB listener on the SubConn. We request a 1 |
| 116 | + // second report interval, but in this example the server indicated the |
| 117 | + // minimum interval it will allow is 3 seconds, so reports will only be |
| 118 | + // sent that often. |
| 119 | + orca.RegisterOOBListener(sc, orcaLis{}, orca.OOBListenerOptions{ReportInterval: time.Second}) |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (o *orcaLB) ResolverError(error) {} |
| 125 | + |
| 126 | +func (o *orcaLB) UpdateSubConnState(sc balancer.SubConn, scs balancer.SubConnState) { |
| 127 | + if scs.ConnectivityState == connectivity.Ready { |
| 128 | + o.cc.UpdateState(balancer.State{ConnectivityState: connectivity.Ready, Picker: &picker{sc}}) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func (o *orcaLB) Close() {} |
| 133 | + |
| 134 | +type picker struct { |
| 135 | + sc balancer.SubConn |
| 136 | +} |
| 137 | + |
| 138 | +func (p *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) { |
| 139 | + return balancer.PickResult{ |
| 140 | + SubConn: p.sc, |
| 141 | + Done: func(di balancer.DoneInfo) { |
| 142 | + fmt.Println("Per-call load report received:", di.ServerLoad.(*v3orcapb.OrcaLoadReport).GetRequestCost()) |
| 143 | + }, |
| 144 | + }, nil |
| 145 | +} |
| 146 | + |
| 147 | +// orcaLis is the out-of-band load report listener that we pass to |
| 148 | +// orca.RegisterOOBListener to receive periodic load report information. |
| 149 | +type orcaLis struct{} |
| 150 | + |
| 151 | +func (orcaLis) OnLoadReport(lr *v3orcapb.OrcaLoadReport) { |
| 152 | + fmt.Println("Out-of-band load report received:", lr) |
| 153 | +} |
0 commit comments