104
104
import com .uber .tchannel .errors .ErrorType ;
105
105
import com .uber .tchannel .messages .ThriftRequest ;
106
106
import com .uber .tchannel .messages .ThriftResponse ;
107
+ import com .uber .tchannel .messages .generated .Meta ;
107
108
import java .net .InetAddress ;
108
109
import java .net .InetSocketAddress ;
109
110
import java .net .UnknownHostException ;
@@ -127,7 +128,7 @@ public class WorkflowServiceTChannel implements IWorkflowService {
127
128
private final ClientOptions options ;
128
129
private final Map <String , String > thriftHeaders ;
129
130
private final TChannel tChannel ;
130
- private final SubChannel subChannel ;
131
+ private SubChannel subChannel ;
131
132
132
133
/**
133
134
* Creates Cadence client that connects to the specified host and port using specified options.
@@ -159,6 +160,13 @@ public WorkflowServiceTChannel(ClientOptions options) {
159
160
+ Version .FEATURE_VERSION );
160
161
}
161
162
163
+ public void resetSubchannelPeers () throws UnknownHostException {
164
+ InetAddress address = InetAddress .getByName (options .getHost ());
165
+ ArrayList <InetSocketAddress > peers = new ArrayList <>();
166
+ peers .add (new InetSocketAddress (address , options .getPort ()));
167
+ this .subChannel .setPeers (peers );
168
+ }
169
+
162
170
/**
163
171
* Creates Cadence client with specified sub channel and options.
164
172
*
@@ -207,6 +215,49 @@ private <T> ThriftRequest<T> buildThriftRequest(String apiName, T body) {
207
215
return buildThriftRequest (apiName , body , null );
208
216
}
209
217
218
+ /**
219
+ * Checks if we have a valid connection to the Cadence cluster, and potentially resets the peer
220
+ * list
221
+ */
222
+ @ Override
223
+ public CompletableFuture <Boolean > isHealthy () {
224
+ final ThriftRequest <Meta .health_args > req =
225
+ new ThriftRequest .Builder <Meta .health_args >(options .getServiceName (), "Meta::health" )
226
+ .setBody (new Meta .health_args ())
227
+ .build ();
228
+ final CompletableFuture <Boolean > result = new CompletableFuture <>();
229
+ try {
230
+
231
+ final TFuture <ThriftResponse <Meta .health_result >> future = this .subChannel .send (req );
232
+ future .addCallback (
233
+ response -> {
234
+ req .releaseQuietly ();
235
+ if (response .isError ()) {
236
+ try {
237
+ this .resetSubchannelPeers ();
238
+ } catch (final Exception inner_e ) {
239
+ }
240
+ result .completeExceptionally (new TException ("Rpc error:" + response .getError ()));
241
+ } else {
242
+ result .complete (response .getBody (Meta .health_result .class ).getSuccess ().isOk ());
243
+ }
244
+ try {
245
+ response .release ();
246
+ } catch (final Exception e ) {
247
+ // ignore
248
+ }
249
+ });
250
+ } catch (final TChannelError e ) {
251
+ req .releaseQuietly ();
252
+ try {
253
+ this .resetSubchannelPeers ();
254
+ } catch (final Exception inner_e ) {
255
+ }
256
+ result .complete (Boolean .FALSE );
257
+ }
258
+ return result ;
259
+ }
260
+
210
261
private <T > ThriftRequest <T > buildThriftRequest (String apiName , T body , Long rpcTimeoutOverride ) {
211
262
String endpoint = getEndpoint (INTERFACE_NAME , apiName );
212
263
ThriftRequest .Builder <T > builder =
0 commit comments