forked from cadence-workflow/cadence-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkflowServiceTChannel.java
More file actions
2987 lines (2790 loc) · 112 KB
/
Copy pathWorkflowServiceTChannel.java
File metadata and controls
2987 lines (2790 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.uber.cadence.serviceclient;
import static com.uber.cadence.internal.metrics.MetricsTagValue.REQUEST_TYPE_LONG_POLL;
import static com.uber.cadence.internal.metrics.MetricsTagValue.REQUEST_TYPE_NORMAL;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.uber.cadence.*;
import com.uber.cadence.WorkflowService.GetWorkflowExecutionHistory_result;
import com.uber.cadence.internal.Version;
import com.uber.cadence.internal.common.CheckedExceptionWrapper;
import com.uber.cadence.internal.common.InternalUtils;
import com.uber.cadence.internal.metrics.MetricsTag;
import com.uber.cadence.internal.metrics.MetricsType;
import com.uber.cadence.internal.metrics.ServiceMethod;
import com.uber.cadence.internal.tracing.TracingPropagator;
import com.uber.m3.tally.Scope;
import com.uber.m3.tally.Stopwatch;
import com.uber.tchannel.api.ResponseCode;
import com.uber.tchannel.api.SubChannel;
import com.uber.tchannel.api.TChannel;
import com.uber.tchannel.api.TFuture;
import com.uber.tchannel.api.errors.TChannelError;
import com.uber.tchannel.errors.ErrorType;
import com.uber.tchannel.messages.ThriftRequest;
import com.uber.tchannel.messages.ThriftResponse;
import com.uber.tchannel.messages.generated.Meta;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentracing.Span;
import io.opentracing.Tracer;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WorkflowServiceTChannel implements IWorkflowService {
private static final Logger log = LoggerFactory.getLogger(WorkflowServiceTChannel.class);
private static final String INTERFACE_NAME = "WorkflowService";
private final ClientOptions options;
private final Map<String, String> thriftHeaders;
private final TChannel tChannel;
private final TracingPropagator tracingPropagator;
private final Tracer tracer;
private final SubChannel subChannel;
/**
* Creates Cadence client that connects to the specified host and port using specified options.
*
* @param options configuration options like rpc timeouts.
*/
public WorkflowServiceTChannel(ClientOptions options) {
this.options = options;
this.thriftHeaders = getThriftHeaders(options);
this.tChannel = new TChannel.Builder(options.getClientAppName()).build();
this.tracingPropagator = new TracingPropagator(options.getTracer());
this.tracer = options.getTracer();
InetAddress address;
try {
address = InetAddress.getByName(options.getHost());
} catch (UnknownHostException e) {
tChannel.shutdown();
throw new RuntimeException("Unable to get name of host " + options.getHost(), e);
}
ArrayList<InetSocketAddress> peers = new ArrayList<>();
peers.add(new InetSocketAddress(address, options.getPort()));
this.subChannel = tChannel.makeSubChannel(options.getServiceName()).setPeers(peers);
log.info(
"Initialized TChannel for service "
+ this.subChannel.getServiceName()
+ ", LibraryVersion: "
+ Version.LIBRARY_VERSION
+ ", FeatureVersion: "
+ Version.FEATURE_VERSION);
}
public void resetSubchannelPeers() throws UnknownHostException {
InetAddress address = InetAddress.getByName(options.getHost());
ArrayList<InetSocketAddress> peers = new ArrayList<>();
peers.add(new InetSocketAddress(address, options.getPort()));
this.subChannel.setPeers(peers);
}
/**
* Creates Cadence client with specified sub channel and options.
*
* @param subChannel sub channel for communicating with cadence frontend service.
* @param options configuration options like rpc timeouts.
*/
public WorkflowServiceTChannel(SubChannel subChannel, ClientOptions options) {
this.options = options;
this.thriftHeaders = getThriftHeaders(options);
this.tChannel = null;
this.subChannel = subChannel;
this.tracingPropagator = new TracingPropagator(options.getTracer());
this.tracer = options.getTracer();
}
private static Map<String, String> getThriftHeaders(ClientOptions options) {
String envUserName = System.getProperty("user.name");
String envHostname;
try {
envHostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
envHostname = "localhost";
}
ImmutableMap.Builder<String, String> builder =
ImmutableMap.<String, String>builder()
.put("user-name", envUserName)
.put("host-name", envHostname)
.put("cadence-client-library-version", Version.LIBRARY_VERSION)
.put("cadence-client-feature-version", Version.FEATURE_VERSION)
.put("cadence-client-name", "uber-java");
if (options.getHeaders() != null) {
for (Map.Entry<String, String> entry : options.getHeaders().entrySet()) {
builder.put(entry.getKey(), entry.getValue());
}
}
if (options.getFeatureFlags() != null) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
String serialized = gson.toJson(options.getFeatureFlags());
builder.put("cadence-client-feature-flags", serialized);
}
if (!Strings.isNullOrEmpty(options.getIsolationGroup())) {
builder.put("cadence-client-isolation-group", options.getIsolationGroup());
}
return builder.build();
}
/** Returns the endpoint in the format service::method" */
private static String getEndpoint(String service, String method) {
return String.format("%s::%s", service, method);
}
private <T> ThriftRequest<T> buildThriftRequest(String apiName, T body) {
return buildThriftRequest(apiName, body, null);
}
@Override
public ClientOptions getOptions() {
return options;
}
/**
* Checks if we have a valid connection to the Cadence cluster, and potentially resets the peer
* list
*/
@Override
public CompletableFuture<Boolean> isHealthy() {
final ThriftRequest<Meta.health_args> req =
new ThriftRequest.Builder<Meta.health_args>(options.getServiceName(), "Meta::health")
.setBody(new Meta.health_args())
.build();
final CompletableFuture<Boolean> result = new CompletableFuture<>();
try {
final TFuture<ThriftResponse<Meta.health_result>> future = this.subChannel.send(req);
future.addCallback(
response -> {
req.releaseQuietly();
if (response.isError()) {
try {
this.resetSubchannelPeers();
} catch (final Exception inner_e) {
}
result.completeExceptionally(new TException("Rpc error:" + response.getError()));
} else {
result.complete(response.getBody(Meta.health_result.class).getSuccess().isOk());
}
try {
response.release();
} catch (final Exception e) {
// ignore
}
});
} catch (final TChannelError e) {
req.releaseQuietly();
try {
this.resetSubchannelPeers();
} catch (final Exception inner_e) {
}
result.complete(Boolean.FALSE);
}
return result;
}
protected <T> ThriftRequest<T> buildThriftRequest(
String apiName, T body, Long rpcTimeoutOverride) {
String endpoint = getEndpoint(INTERFACE_NAME, apiName);
ThriftRequest.Builder<T> builder =
new ThriftRequest.Builder<>(options.getServiceName(), endpoint);
// Create a mutable hashmap for headers, as tchannel.tracing.PrefixedHeadersCarrier assumes
// that it can call put directly to add new stuffs (e.g. traces).
final HashMap<String, String> headers = new HashMap<>(thriftHeaders);
TextMapPropagator textMapPropagator =
GlobalOpenTelemetry.getPropagators().getTextMapPropagator();
String tracingHeadersPrefix = "$tracing$";
TextMapSetter<Map<String, String>> setter =
(carrier, key, value) -> {
if (carrier != null) {
carrier.put(tracingHeadersPrefix + key, value);
}
};
textMapPropagator.inject(Context.current(), headers, setter);
if (this.options.getAuthProvider() != null) {
headers.put(
"cadence-authorization",
new String(options.getAuthProvider().getAuthToken(), StandardCharsets.UTF_8));
}
builder.setHeaders(headers);
if (rpcTimeoutOverride != null) {
builder.setTimeout(rpcTimeoutOverride);
} else {
builder.setTimeout(this.options.getRpcTimeoutMillis());
}
for (Map.Entry<String, String> header : this.options.getTransportHeaders().entrySet()) {
builder.setTransportHeader(header.getKey(), header.getValue());
}
builder.setBody(body);
return builder.build();
}
private <T> ThriftResponse<T> doRemoteCall(ThriftRequest<?> request) throws TException {
ThriftResponse<T> response = null;
try {
TFuture<ThriftResponse<T>> future = subChannel.send(request);
response = future.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new TException(e);
} catch (ExecutionException e) {
throw new TException(e);
} catch (TChannelError e) {
throw new TException("Rpc error", e);
}
this.throwOnRpcError(response);
return response;
}
private <T> CompletableFuture<ThriftResponse<T>> doRemoteCallAsync(ThriftRequest<?> request) {
final CompletableFuture<ThriftResponse<T>> result = new CompletableFuture<>();
TFuture<ThriftResponse<T>> future = null;
try {
future = subChannel.send(request);
} catch (TChannelError tChannelError) {
result.completeExceptionally(new TException(tChannelError));
}
future.addCallback(
response -> {
if (response.isError()) {
result.completeExceptionally(new TException("Rpc error:" + response.getError()));
} else {
result.complete(response);
}
});
return result;
}
private void throwOnRpcError(ThriftResponse<?> response) throws TException {
if (response.isError()) {
if (response.getError().getErrorType() == ErrorType.Timeout) {
throw new TTransportException(
TTransportException.TIMED_OUT, response.getError().getMessage());
} else {
throw new TException("Rpc error:" + response.getError());
}
}
}
@Override
public void close() {
if (tChannel != null) {
tChannel.shutdown();
}
}
interface RemoteCall<T> {
T apply() throws TException;
}
private <T> T measureRemoteCall(String scopeName, RemoteCall<T> call) throws TException {
return measureRemoteCallWithTags(scopeName, call, null);
}
private <T> T measureRemoteCallWithTags(
String scopeName, RemoteCall<T> call, Map<String, String> tags) throws TException {
Scope scope = options.getMetricsScope().subScope(scopeName);
if (tags != null) {
scope = scope.tagged(tags);
}
scope.counter(MetricsType.CADENCE_REQUEST).inc(1);
Stopwatch sw = scope.timer(MetricsType.CADENCE_LATENCY).start();
Span span = tracingPropagator.spanByServiceMethod(scopeName);
try (io.opentracing.Scope tracingScope = tracer.activateSpan(span)) {
T resp = call.apply();
sw.stop();
return resp;
} catch (EntityNotExistsError
| WorkflowExecutionAlreadyCompletedError
| BadRequestError
| DomainAlreadyExistsError
| WorkflowExecutionAlreadyStartedError
| QueryFailedError e) {
sw.stop();
scope.counter(MetricsType.CADENCE_INVALID_REQUEST).inc(1);
throw e;
} catch (TException e) {
sw.stop();
scope.counter(MetricsType.CADENCE_ERROR).inc(1);
throw e;
} finally {
span.finish();
}
}
interface RemoteProc {
void apply() throws TException;
}
private void measureRemoteProc(String scopeName, RemoteProc proc) throws TException {
measureRemoteCall(
scopeName,
() -> {
proc.apply();
return null;
});
}
@Override
public void RegisterDomain(RegisterDomainRequest request) throws TException {
measureRemoteProc(ServiceMethod.REGISTER_DOMAIN, () -> registerDomain(request));
}
private void registerDomain(RegisterDomainRequest registerRequest) throws TException {
ThriftResponse<WorkflowService.RegisterDomain_result> response = null;
try {
ThriftRequest<WorkflowService.RegisterDomain_args> request =
buildThriftRequest(
"RegisterDomain", new WorkflowService.RegisterDomain_args(registerRequest));
response = doRemoteCall(request);
WorkflowService.RegisterDomain_result result =
response.getBody(WorkflowService.RegisterDomain_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return;
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetDomainExistsError()) {
throw result.getDomainExistsError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
throw new TException("RegisterDomain failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public DescribeDomainResponse DescribeDomain(DescribeDomainRequest describeRequest)
throws TException {
return measureRemoteCall(ServiceMethod.DESCRIBE_DOMAIN, () -> describeDomain(describeRequest));
}
private DescribeDomainResponse describeDomain(DescribeDomainRequest describeRequest)
throws TException {
ThriftResponse<WorkflowService.DescribeDomain_result> response = null;
try {
ThriftRequest<WorkflowService.DescribeDomain_args> request =
buildThriftRequest(
"DescribeDomain", new WorkflowService.DescribeDomain_args(describeRequest));
response = doRemoteCall(request);
WorkflowService.DescribeDomain_result result =
response.getBody(WorkflowService.DescribeDomain_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
throw new TException("DescribeDomain failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public DiagnoseWorkflowExecutionResponse DiagnoseWorkflowExecution(
DiagnoseWorkflowExecutionRequest diagnoseRequest)
throws DomainNotActiveError, ServiceBusyError, EntityNotExistsError,
ClientVersionNotSupportedError, TException {
throw new UnsupportedOperationException("DiagnoseWorkflowExecution is not implemented");
}
@Override
public ListDomainsResponse ListDomains(ListDomainsRequest listRequest)
throws BadRequestError, InternalServiceError, EntityNotExistsError, ServiceBusyError,
TException {
return measureRemoteCall(ServiceMethod.LIST_DOMAINS, () -> listDomains(listRequest));
}
private ListDomainsResponse listDomains(ListDomainsRequest describeRequest) throws TException {
ThriftResponse<WorkflowService.ListDomains_result> response = null;
try {
ThriftRequest<WorkflowService.ListDomains_args> request =
buildThriftRequest("ListDomains", new WorkflowService.ListDomains_args(describeRequest));
response = doRemoteCall(request);
WorkflowService.ListDomains_result result =
response.getBody(WorkflowService.ListDomains_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
throw new TException("ListDomains failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public UpdateDomainResponse UpdateDomain(UpdateDomainRequest updateRequest) throws TException {
return measureRemoteCall(ServiceMethod.UPDATE_DOMAIN, () -> updateDomain(updateRequest));
}
private UpdateDomainResponse updateDomain(UpdateDomainRequest updateRequest) throws TException {
ThriftResponse<WorkflowService.UpdateDomain_result> response = null;
try {
ThriftRequest<WorkflowService.UpdateDomain_args> request =
buildThriftRequest("UpdateDomain", new WorkflowService.UpdateDomain_args(updateRequest));
response = doRemoteCall(request);
WorkflowService.UpdateDomain_result result =
response.getBody(WorkflowService.UpdateDomain_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetDomainNotActiveError()) {
throw result.getDomainNotActiveError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("UpdateDomain failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public void DeprecateDomain(DeprecateDomainRequest deprecateRequest) throws TException {
measureRemoteProc(ServiceMethod.DEPRECATE_DOMAIN, () -> deprecateDomain(deprecateRequest));
}
@Override
public RestartWorkflowExecutionResponse RestartWorkflowExecution(
RestartWorkflowExecutionRequest restartRequest)
throws BadRequestError, ServiceBusyError, DomainNotActiveError, LimitExceededError,
EntityNotExistsError, ClientVersionNotSupportedError, TException {
throw new UnsupportedOperationException("unimplemented");
}
private void deprecateDomain(DeprecateDomainRequest deprecateRequest) throws TException {
ThriftResponse<WorkflowService.DeprecateDomain_result> response = null;
try {
ThriftRequest<WorkflowService.DeprecateDomain_args> request =
buildThriftRequest(
"DeprecateDomain", new WorkflowService.DeprecateDomain_args(deprecateRequest));
response = doRemoteCall(request);
WorkflowService.DeprecateDomain_result result =
response.getBody(WorkflowService.DeprecateDomain_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return;
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetDomainNotActiveError()) {
throw result.getDomainNotActiveError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("DeprecateDomain failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public GetTaskListsByDomainResponse GetTaskListsByDomain(
GetTaskListsByDomainRequest getTaskListsByDomainRequest) throws TException {
return measureRemoteCall(
ServiceMethod.GET_TASK_LISTS_BY_DOMAIN,
() -> getTaskListsByDomain(getTaskListsByDomainRequest));
}
private GetTaskListsByDomainResponse getTaskListsByDomain(
GetTaskListsByDomainRequest getTaskListsByDomainRequest) throws TException {
ThriftResponse<WorkflowService.GetTaskListsByDomain_result> response = null;
try {
ThriftRequest<WorkflowService.GetTaskListsByDomain_args> request =
buildThriftRequest(
"GetTaskListsByDomain",
new WorkflowService.GetTaskListsByDomain_args(getTaskListsByDomainRequest));
response = doRemoteCall(request);
WorkflowService.GetTaskListsByDomain_result result =
response.getBody(WorkflowService.GetTaskListsByDomain_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetLimitExceededError()) {
throw result.getLimitExceededError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("GetTaskListsByDomain failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public StartWorkflowExecutionResponse StartWorkflowExecution(
StartWorkflowExecutionRequest request) throws TException {
return measureRemoteCall(
ServiceMethod.START_WORKFLOW_EXECUTION, () -> startWorkflowExecution(request));
}
private StartWorkflowExecutionResponse startWorkflowExecution(
StartWorkflowExecutionRequest startRequest) throws TException {
ThriftResponse<WorkflowService.StartWorkflowExecution_result> response = null;
try {
initializeStartWorkflowRequest(startRequest);
ThriftRequest<WorkflowService.StartWorkflowExecution_args> request =
buildThriftRequest(
"StartWorkflowExecution",
new WorkflowService.StartWorkflowExecution_args(startRequest));
response = doRemoteCall(request);
WorkflowService.StartWorkflowExecution_result result =
response.getBody(WorkflowService.StartWorkflowExecution_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetSessionAlreadyExistError()) {
throw result.getSessionAlreadyExistError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetDomainNotActiveError()) {
throw result.getDomainNotActiveError();
}
if (result.isSetLimitExceededError()) {
throw result.getLimitExceededError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("StartWorkflowExecution failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public StartWorkflowExecutionAsyncResponse StartWorkflowExecutionAsync(
StartWorkflowExecutionAsyncRequest startAsyncRequest) throws TException {
return measureRemoteCall(
ServiceMethod.START_WORKFLOW_EXECUTION_ASYNC,
() -> startWorkflowExecutionAsync(startAsyncRequest));
}
private StartWorkflowExecutionAsyncResponse startWorkflowExecutionAsync(
StartWorkflowExecutionAsyncRequest startAsyncRequest) throws TException {
ThriftResponse<WorkflowService.StartWorkflowExecutionAsync_result> response = null;
try {
initializeStartWorkflowRequest(startAsyncRequest.getRequest());
ThriftRequest<WorkflowService.StartWorkflowExecutionAsync_args> request =
buildThriftRequest(
"StartWorkflowExecutionAsync",
new WorkflowService.StartWorkflowExecutionAsync_args(startAsyncRequest));
response = doRemoteCall(request);
WorkflowService.StartWorkflowExecutionAsync_result result =
response.getBody(WorkflowService.StartWorkflowExecutionAsync_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetSessionAlreadyExistError()) {
throw result.getSessionAlreadyExistError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetDomainNotActiveError()) {
throw result.getDomainNotActiveError();
}
if (result.isSetLimitExceededError()) {
throw result.getLimitExceededError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("StartWorkflowExecution failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
private void initializeStartWorkflowRequest(StartWorkflowExecutionRequest startRequest) {
if (!startRequest.isSetRequestId()) {
startRequest.setRequestId(UUID.randomUUID().toString());
}
// Write span context to header
if (!startRequest.isSetHeader()) {
startRequest.setHeader(new Header());
}
tracingPropagator.inject(startRequest.getHeader());
}
@Override
public GetWorkflowExecutionHistoryResponse GetWorkflowExecutionHistoryWithTimeout(
GetWorkflowExecutionHistoryRequest request, Long timeoutInMillis) throws TException {
Map<String, String> tags =
ImmutableMap.of(
MetricsTag.REQUEST_TYPE,
request.isWaitForNewEvent() ? REQUEST_TYPE_LONG_POLL : REQUEST_TYPE_NORMAL);
return measureRemoteCallWithTags(
ServiceMethod.GET_WORKFLOW_EXECUTION_HISTORY,
() -> getWorkflowExecutionHistory(request, timeoutInMillis),
tags);
}
@Override
public GetWorkflowExecutionHistoryResponse GetWorkflowExecutionHistory(
GetWorkflowExecutionHistoryRequest request) throws TException {
Map<String, String> tags =
ImmutableMap.of(
MetricsTag.REQUEST_TYPE,
request.isWaitForNewEvent() ? REQUEST_TYPE_LONG_POLL : REQUEST_TYPE_NORMAL);
return measureRemoteCallWithTags(
ServiceMethod.GET_WORKFLOW_EXECUTION_HISTORY,
() -> getWorkflowExecutionHistory(request, null),
tags);
}
private GetWorkflowExecutionHistoryResponse getWorkflowExecutionHistory(
GetWorkflowExecutionHistoryRequest getRequest, Long timeoutInMillis) throws TException {
ThriftResponse<WorkflowService.GetWorkflowExecutionHistory_result> response = null;
try {
ThriftRequest<WorkflowService.GetWorkflowExecutionHistory_args> request =
buildGetWorkflowExecutionHistoryThriftRequest(getRequest, timeoutInMillis);
response = doRemoteCall(request);
WorkflowService.GetWorkflowExecutionHistory_result result =
response.getBody(WorkflowService.GetWorkflowExecutionHistory_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
GetWorkflowExecutionHistoryResponse res = result.getSuccess();
if (res.getRawHistory() != null) {
History history =
InternalUtils.DeserializeFromBlobDataToHistory(
res.getRawHistory(), getRequest.getHistoryEventFilterType());
res.setHistory(history);
}
return res;
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("GetWorkflowExecutionHistory failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
private ThriftRequest<WorkflowService.GetWorkflowExecutionHistory_args>
buildGetWorkflowExecutionHistoryThriftRequest(
GetWorkflowExecutionHistoryRequest getRequest, Long timeoutInMillis) {
if (getRequest.isWaitForNewEvent()) {
timeoutInMillis =
validateAndUpdateTimeout(timeoutInMillis, options.getRpcLongPollTimeoutMillis());
} else {
timeoutInMillis = validateAndUpdateTimeout(timeoutInMillis, options.getRpcTimeoutMillis());
}
return buildThriftRequest(
"GetWorkflowExecutionHistory",
new WorkflowService.GetWorkflowExecutionHistory_args(getRequest),
timeoutInMillis);
}
@Override
public PollForDecisionTaskResponse PollForDecisionTask(PollForDecisionTaskRequest request)
throws TException {
return measureRemoteCall(
ServiceMethod.POLL_FOR_DECISION_TASK, () -> pollForDecisionTask(request));
}
private PollForDecisionTaskResponse pollForDecisionTask(PollForDecisionTaskRequest pollRequest)
throws TException {
ThriftResponse<WorkflowService.PollForDecisionTask_result> response = null;
try {
ThriftRequest<WorkflowService.PollForDecisionTask_args> request =
buildThriftRequest(
"PollForDecisionTask",
new WorkflowService.PollForDecisionTask_args(pollRequest),
options.getRpcLongPollTimeoutMillis());
response = doRemoteCall(request);
WorkflowService.PollForDecisionTask_result result =
response.getBody(WorkflowService.PollForDecisionTask_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetDomainNotActiveError()) {
throw result.getDomainNotActiveError();
}
if (result.isSetLimitExceededError()) {
throw result.getLimitExceededError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("PollForDecisionTask failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public RespondDecisionTaskCompletedResponse RespondDecisionTaskCompleted(
RespondDecisionTaskCompletedRequest completedRequest) throws TException {
return measureRemoteCall(
ServiceMethod.RESPOND_DECISION_TASK_COMPLETED,
() -> respondDecisionTaskCompleted(completedRequest));
}
private RespondDecisionTaskCompletedResponse respondDecisionTaskCompleted(
RespondDecisionTaskCompletedRequest completedRequest) throws TException {
ThriftResponse<WorkflowService.RespondDecisionTaskCompleted_result> response = null;
try {
ThriftRequest<WorkflowService.RespondDecisionTaskCompleted_args> request =
buildThriftRequest(
"RespondDecisionTaskCompleted",
new WorkflowService.RespondDecisionTaskCompleted_args(completedRequest));
response = doRemoteCall(request);
WorkflowService.RespondDecisionTaskCompleted_result result =
response.getBody(WorkflowService.RespondDecisionTaskCompleted_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetDomainNotActiveError()) {
throw result.getDomainNotActiveError();
}
if (result.isSetLimitExceededError()) {
throw result.getLimitExceededError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetWorkflowExecutionAlreadyCompletedError()) {
throw result.getWorkflowExecutionAlreadyCompletedError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("RespondDecisionTaskCompleted failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public void RespondDecisionTaskFailed(RespondDecisionTaskFailedRequest request)
throws TException {
measureRemoteProc(
ServiceMethod.RESPOND_DECISION_TASK_FAILED, () -> respondDecisionTaskFailed(request));
}
private void respondDecisionTaskFailed(RespondDecisionTaskFailedRequest failedRequest)
throws TException {
ThriftResponse<WorkflowService.RespondDecisionTaskFailed_result> response = null;
try {
ThriftRequest<WorkflowService.RespondDecisionTaskFailed_args> request =
buildThriftRequest(
"RespondDecisionTaskFailed",
new WorkflowService.RespondDecisionTaskFailed_args(failedRequest));
response = doRemoteCall(request);
WorkflowService.RespondDecisionTaskFailed_result result =
response.getBody(WorkflowService.RespondDecisionTaskFailed_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return;
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}
if (result.isSetWorkflowExecutionAlreadyCompletedError()) {
throw result.getWorkflowExecutionAlreadyCompletedError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetDomainNotActiveError()) {
throw result.getDomainNotActiveError();
}
if (result.isSetLimitExceededError()) {
throw result.getLimitExceededError();
}
if (result.isSetClientVersionNotSupportedError()) {
throw result.getClientVersionNotSupportedError();
}
throw new TException("RespondDecisionTaskFailed failed with unknown error:" + result);
} finally {
if (response != null) {
response.release();
}
}
}
@Override
public PollForActivityTaskResponse PollForActivityTask(PollForActivityTaskRequest request)
throws TException {
return measureRemoteCall(
ServiceMethod.POLL_FOR_ACTIVITY_TASK, () -> pollForActivityTask(request));
}
private PollForActivityTaskResponse pollForActivityTask(PollForActivityTaskRequest pollRequest)
throws TException {
ThriftResponse<WorkflowService.PollForActivityTask_result> response = null;
try {
ThriftRequest<WorkflowService.PollForActivityTask_args> request =
buildThriftRequest(
"PollForActivityTask",
new WorkflowService.PollForActivityTask_args(pollRequest),
options.getRpcLongPollTimeoutMillis());
response = doRemoteCall(request);
WorkflowService.PollForActivityTask_result result =
response.getBody(WorkflowService.PollForActivityTask_result.class);
if (response.getResponseCode() == ResponseCode.OK) {
return result.getSuccess();
}
if (result.isSetBadRequestError()) {
throw result.getBadRequestError();
}
if (result.isSetServiceBusyError()) {
throw result.getServiceBusyError();
}
if (result.isSetEntityNotExistError()) {
throw result.getEntityNotExistError();
}