@@ -96,43 +96,12 @@ public void primeChannel(ManagedChannel managedChannel) {
9696 }
9797
9898 private void primeChannelUnsafe (ManagedChannel managedChannel ) throws IOException {
99- sendPrimeRequests (managedChannel );
99+ sendPrimeRequestsBlocking (managedChannel );
100100 }
101101
102- private void sendPrimeRequests (ManagedChannel managedChannel ) {
102+ private void sendPrimeRequestsBlocking (ManagedChannel managedChannel ) {
103103 try {
104- ClientCall <PingAndWarmRequest , PingAndWarmResponse > clientCall =
105- managedChannel .newCall (
106- BigtableGrpc .getPingAndWarmMethod (),
107- CallOptions .DEFAULT
108- .withCallCredentials (callCredentials )
109- .withDeadline (Deadline .after (1 , TimeUnit .MINUTES )));
110-
111- SettableApiFuture <PingAndWarmResponse > future = SettableApiFuture .create ();
112- clientCall .start (
113- new ClientCall .Listener <PingAndWarmResponse >() {
114- PingAndWarmResponse response ;
115-
116- @ Override
117- public void onMessage (PingAndWarmResponse message ) {
118- response = message ;
119- }
120-
121- @ Override
122- public void onClose (Status status , Metadata trailers ) {
123- if (status .isOk ()) {
124- future .set (response );
125- } else {
126- future .setException (status .asException ());
127- }
128- }
129- },
130- createMetadata (headers , request ));
131- clientCall .sendMessage (request );
132- clientCall .halfClose ();
133- clientCall .request (Integer .MAX_VALUE );
134-
135- future .get (1 , TimeUnit .MINUTES );
104+ sendPrimeRequestsAsync (managedChannel ).get (1 , TimeUnit .MINUTES );
136105 } catch (Throwable e ) {
137106 // TODO: Not sure if we should swallow the error here. We are pre-emptively swapping
138107 // channels if the new
@@ -141,6 +110,59 @@ public void onClose(Status status, Metadata trailers) {
141110 }
142111 }
143112
113+ /**
114+ * Asynchronously sends a PingAndWarm request.
115+ *
116+ * @param managedChannel The channel to send the request on.
117+ * @return A ListenableFuture that will be completed with the PingAndWarmResponse or an exception.
118+ */
119+ public SettableApiFuture <PingAndWarmResponse > sendPrimeRequestsAsync (
120+ ManagedChannel managedChannel ) {
121+ ClientCall <PingAndWarmRequest , PingAndWarmResponse > clientCall =
122+ managedChannel .newCall (
123+ BigtableGrpc .getPingAndWarmMethod (),
124+ CallOptions .DEFAULT
125+ .withCallCredentials (callCredentials )
126+ .withDeadline (Deadline .after (1 , TimeUnit .MINUTES )));
127+
128+ SettableApiFuture <PingAndWarmResponse > future = SettableApiFuture .create ();
129+ clientCall .start (
130+ new ClientCall .Listener <PingAndWarmResponse >() {
131+ private PingAndWarmResponse response ;
132+
133+ @ Override
134+ public void onMessage (PingAndWarmResponse message ) {
135+ response = message ;
136+ }
137+
138+ @ Override
139+ public void onClose (Status status , Metadata trailers ) {
140+ if (status .isOk ()) {
141+ future .set (response );
142+ } else {
143+ // Propagate the gRPC error to the future.
144+ future .setException (status .asException (trailers ));
145+ }
146+ }
147+ },
148+ createMetadata (headers , request ));
149+
150+ try {
151+ // Send the request message.
152+ clientCall .sendMessage (request );
153+ // Signal that no more messages will be sent.
154+ clientCall .halfClose ();
155+ // Request the response from the server.
156+ clientCall .request (Integer .MAX_VALUE );
157+ } catch (Throwable t ) {
158+ // If sending fails, cancel the call and notify the future.
159+ clientCall .cancel ("Failed to send priming request" , t );
160+ future .setException (t );
161+ }
162+
163+ return future ;
164+ }
165+
144166 private static Metadata createMetadata (Map <String , String > headers , PingAndWarmRequest request ) {
145167 Metadata metadata = new Metadata ();
146168
0 commit comments