• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.examples.orca;
18 
19 import static io.grpc.examples.orca.CustomBackendMetricsLoadBalancerProvider.EXAMPLE_LOAD_BALANCER;
20 
21 import io.grpc.Channel;
22 import io.grpc.Grpc;
23 import io.grpc.InsecureChannelCredentials;
24 import io.grpc.LoadBalancerRegistry;
25 import io.grpc.ManagedChannel;
26 import io.grpc.StatusRuntimeException;
27 import io.grpc.examples.helloworld.GreeterGrpc;
28 import io.grpc.examples.helloworld.HelloReply;
29 import io.grpc.examples.helloworld.HelloRequest;
30 import java.util.concurrent.TimeUnit;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33 
34 /**
35  * A simple xDS client that requests a greeting from {@link CustomBackendMetricsServer}.
36  * The client channel is configured to use an example load balancer policy
37  * {@link CustomBackendMetricsLoadBalancerProvider} which integrates with ORCA metrics reporting.
38  */
39 public class CustomBackendMetricsClient {
40   private static final Logger logger = Logger.getLogger(CustomBackendMetricsClient.class.getName());
41 
42   private final GreeterGrpc.GreeterBlockingStub blockingStub;
43 
44   /** Construct client for accessing HelloWorld server using the existing channel. */
CustomBackendMetricsClient(Channel channel)45   public CustomBackendMetricsClient(Channel channel) {
46     blockingStub = GreeterGrpc.newBlockingStub(channel);
47   }
48 
49   /** Say hello to server. */
greet(String name)50   public void greet(String name) {
51     logger.info("Will try to greet " + name + " ...");
52     HelloRequest request = HelloRequest.newBuilder().setName(name).build();
53     HelloReply response;
54     try {
55       response = blockingStub.sayHello(request);
56     } catch (StatusRuntimeException e) {
57       logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
58       return;
59     }
60     logger.info("Greeting: " + response.getMessage());
61   }
62 
63   /**
64    * Greet server. If provided, the first element of {@code args} is the name to use in the
65    * greeting. The second argument is the target server.
66    */
main(String[] args)67   public static void main(String[] args) throws Exception {
68     String user = "orca tester";
69     // The example defaults to the same behavior as the hello world example.
70     // To receive more periodic OOB metrics reports, use duration argument to a longer value.
71     String target = "localhost:50051";
72     long timeBeforeShutdown = 1500;
73     if (args.length > 0) {
74       if ("--help".equals(args[0])) {
75         System.err.println("Usage: [name [duration [target]]]");
76         System.err.println("");
77         System.err.println("  name    The name you wish to be greeted by. Defaults to " + user);
78         System.err.println("  duration  The time period in milliseconds that the client application " +
79             "wait until shutdown. Defaults to " + timeBeforeShutdown);
80         System.err.println("  target  The server to connect to. Defaults to " + target);
81         System.exit(1);
82       }
83       user = args[0];
84     }
85     if (args.length > 1) {
86       timeBeforeShutdown = Long.parseLong(args[1]);
87     }
88 
89     if (args.length > 2) {
90       target = args[2];
91     }
92 
93     LoadBalancerRegistry.getDefaultRegistry().register(
94         new CustomBackendMetricsLoadBalancerProvider());
95     ManagedChannel channel = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create())
96         .defaultLoadBalancingPolicy(EXAMPLE_LOAD_BALANCER)
97         .build();
98     try {
99       CustomBackendMetricsClient client = new CustomBackendMetricsClient(channel);
100       client.greet(user);
101       Thread.sleep(timeBeforeShutdown);
102     } finally {
103       channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
104     }
105   }
106 }
107