• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #343

31 Oct 2024 06:31PM UTC coverage: 75.148% (-3.6%) from 78.794%
#343

push

github

web-flow
Fix jacoco coverage (#2304)

5139 of 8240 branches covered (62.37%)

Branch coverage included in aggregate %.

22841 of 28993 relevant lines covered (78.78%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.27
/temporal-serviceclient/src/main/java/io/temporal/serviceclient/WorkflowServiceStubsOptions.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.serviceclient;
22

23
import io.grpc.health.v1.HealthCheckResponse;
24
import io.temporal.serviceclient.rpcretry.DefaultStubServiceOperationRpcRetryOptions;
25
import java.time.Duration;
26
import java.util.*;
27

28
public final class WorkflowServiceStubsOptions extends ServiceStubsOptions {
29
  /**
30
   * RPC timeout used for all long poll calls on Temporal Server side. Long poll returns with an
31
   * empty result after this server timeout.
32
   */
33
  public static final Duration DEFAULT_SERVER_LONG_POLL_RPC_TIMEOUT = Duration.ofSeconds(60);
1✔
34

35
  /** Default RPC timeout used for all long poll calls. */
36
  public static final Duration DEFAULT_POLL_RPC_TIMEOUT =
1✔
37
      DEFAULT_SERVER_LONG_POLL_RPC_TIMEOUT.plus(Duration.ofSeconds(10));
1✔
38

39
  /** Default RPC timeout for workflow queries */
40
  public static final Duration DEFAULT_QUERY_RPC_TIMEOUT = Duration.ofSeconds(10);
1✔
41

42
  private static final WorkflowServiceStubsOptions DEFAULT_INSTANCE =
1✔
43
      newBuilder().validateAndBuildWithDefaults();
1✔
44

45
  /**
46
   * Asks client to perform a health check after gRPC connection to the Server is created by making
47
   * a request to endpoint to make sure that the server is accessible.
48
   */
49
  private final boolean disableHealthCheck;
50

51
  /** The gRPC timeout for long poll calls */
52
  private final Duration rpcLongPollTimeout;
53

54
  /** The gRPC timeout for query workflow call */
55
  private final Duration rpcQueryTimeout;
56

57
  /** Retry options for outgoing RPC calls */
58
  private final RpcRetryOptions rpcRetryOptions;
59

60
  public static Builder newBuilder() {
61
    return new Builder();
1✔
62
  }
63

64
  public static Builder newBuilder(ServiceStubsOptions options) {
65
    return new Builder(options);
1✔
66
  }
67

68
  public static WorkflowServiceStubsOptions getDefaultInstance() {
69
    return DEFAULT_INSTANCE;
1✔
70
  }
71

72
  private WorkflowServiceStubsOptions(
73
      ServiceStubsOptions serviceStubsOptions,
74
      boolean disableHealthCheck,
75
      Duration rpcLongPollTimeout,
76
      Duration rpcQueryTimeout,
77
      RpcRetryOptions rpcRetryOptions) {
78
    super(serviceStubsOptions);
1✔
79
    this.disableHealthCheck = disableHealthCheck;
1✔
80
    this.rpcLongPollTimeout = rpcLongPollTimeout;
1✔
81
    this.rpcQueryTimeout = rpcQueryTimeout;
1✔
82
    this.rpcRetryOptions = rpcRetryOptions;
1✔
83
  }
1✔
84

85
  /**
86
   * @return false when client checks endpoint to make sure that the server is accessible.
87
   * @deprecated ServiceStubs don't perform health check on start anymore to allow lazy
88
   *     connectivity. Users that prefer old behavior should explicitly call {@link
89
   *     ServiceStubs#healthCheck()} on client/stubs start and wait until it returns {@link
90
   *     HealthCheckResponse.ServingStatus#SERVING}
91
   */
92
  @Deprecated
93
  public boolean getDisableHealthCheck() {
94
    return disableHealthCheck;
×
95
  }
96

97
  /**
98
   * @return Returns the rpc timout for long poll requests.
99
   */
100
  public Duration getRpcLongPollTimeout() {
101
    return rpcLongPollTimeout;
1✔
102
  }
103

104
  /**
105
   * @return Returns the rpc timout for query workflow requests.
106
   */
107
  public Duration getRpcQueryTimeout() {
108
    return rpcQueryTimeout;
1✔
109
  }
110

111
  /**
112
   * @return Returns rpc retry options for outgoing requests to the temporal server that supposed to
113
   *     be processed and returned fast, like start workflow (not long polls or awaits for workflow
114
   *     finishing).
115
   */
116
  public RpcRetryOptions getRpcRetryOptions() {
117
    return rpcRetryOptions;
1✔
118
  }
119

120
  @Override
121
  public boolean equals(Object o) {
122
    if (this == o) return true;
×
123
    if (o == null || getClass() != o.getClass()) return false;
×
124
    WorkflowServiceStubsOptions that = (WorkflowServiceStubsOptions) o;
×
125
    return disableHealthCheck == that.disableHealthCheck
×
126
        && Objects.equals(rpcLongPollTimeout, that.rpcLongPollTimeout)
×
127
        && Objects.equals(rpcQueryTimeout, that.rpcQueryTimeout)
×
128
        && Objects.equals(rpcRetryOptions, that.rpcRetryOptions);
×
129
  }
130

131
  @Override
132
  public int hashCode() {
133
    return Objects.hash(disableHealthCheck, rpcLongPollTimeout, rpcQueryTimeout, rpcRetryOptions);
×
134
  }
135

136
  @Override
137
  public String toString() {
138
    return "WorkflowServiceStubsOptions{"
×
139
        + "disableHealthCheck="
140
        + disableHealthCheck
141
        + ", rpcLongPollTimeout="
142
        + rpcLongPollTimeout
143
        + ", rpcQueryTimeout="
144
        + rpcQueryTimeout
145
        + ", rpcRetryOptions="
146
        + rpcRetryOptions
147
        + '}';
148
  }
149

150
  /** Builder is the builder for ClientOptions. */
151
  public static class Builder extends ServiceStubsOptions.Builder<Builder> {
152
    private boolean disableHealthCheck = true;
1✔
153
    private Duration rpcLongPollTimeout = DEFAULT_POLL_RPC_TIMEOUT;
1✔
154
    private Duration rpcQueryTimeout = DEFAULT_QUERY_RPC_TIMEOUT;
1✔
155
    private RpcRetryOptions rpcRetryOptions = DefaultStubServiceOperationRpcRetryOptions.INSTANCE;
1✔
156

157
    private Builder() {}
1✔
158

159
    private Builder(ServiceStubsOptions options) {
160
      super(options);
1✔
161
      if (options instanceof WorkflowServiceStubsOptions) {
1!
162
        WorkflowServiceStubsOptions castedOptions = (WorkflowServiceStubsOptions) options;
1✔
163
        this.rpcLongPollTimeout = castedOptions.rpcLongPollTimeout;
1✔
164
        this.rpcQueryTimeout = castedOptions.rpcQueryTimeout;
1✔
165
        this.rpcRetryOptions = castedOptions.rpcRetryOptions;
1✔
166
      }
167
    }
1✔
168

169
    /**
170
     * If false, enables client to make a request to health check endpoint to make sure that the
171
     * server is accessible.
172
     *
173
     * @deprecated Use more explicit {@link
174
     *     WorkflowServiceStubs#newServiceStubs(WorkflowServiceStubsOptions)} that doesn't perform
175
     *     an explicit connection and health check.
176
     */
177
    @Deprecated
178
    public Builder setDisableHealthCheck(boolean disableHealthCheck) {
179
      this.disableHealthCheck = disableHealthCheck;
×
180
      return this;
×
181
    }
182

183
    /**
184
     * Sets the rpc timeout value for non-query and non-long-poll calls. Default is 10 seconds.
185
     *
186
     * <p>This timeout is applied to only a single rpc server call, not a complete client-server
187
     * interaction. In case of failure, the requests are automatically retried according to {@link
188
     * #setRpcRetryOptions(RpcRetryOptions)}. The full interaction is limited by {@link
189
     * RpcRetryOptions.Builder#setExpiration(Duration)} or {@link
190
     * RpcRetryOptions.Builder#setMaximumAttempts(int)}}, whichever happens first.
191
     *
192
     * <p>For example, let's consider you've called {@code WorkflowClient#start}, and this timeout
193
     * is set to 10s, while {@link RpcRetryOptions.Builder#setExpiration(Duration)} is set to 60s,
194
     * and the server is responding slowly. The first two RPC calls may time out and be retried, but
195
     * if the third one completes fast, the overall {@code WorkflowClient#start} call will
196
     * successfully resolve.
197
     */
198
    @Override
199
    public Builder setRpcTimeout(Duration timeout) {
200
      return super.setRpcTimeout(timeout);
1✔
201
    }
202

203
    /**
204
     * Sets the rpc timeout value for the following long poll based operations:
205
     * PollWorkflowTaskQueue, PollActivityTaskQueue, GetWorkflowExecutionHistory. Defaults to 70
206
     * seconds.
207
     *
208
     * <p>Server always responds below this timeout. Most users should never modify the default
209
     * value of 70s. The only reasonable reason to modify this timeout it if there is a reversed
210
     * proxy in the network that cuts the gRPC requests really short and there is no way to adjust
211
     * it.
212
     */
213
    public Builder setRpcLongPollTimeout(Duration timeout) {
214
      this.rpcLongPollTimeout = Objects.requireNonNull(timeout);
1✔
215
      return this;
1✔
216
    }
217

218
    /**
219
     * Sets the rpc timeout value for query calls. Default is 10 seconds.
220
     *
221
     * <p>This timeout is applied to only a single rpc server call, not a complete client-server
222
     * interaction. In case of failure, the requests are automatically retried according to {@link
223
     * #setRpcRetryOptions(RpcRetryOptions)}. The full interaction is limited by {@link
224
     * RpcRetryOptions.Builder#setExpiration(Duration)} or {@link
225
     * RpcRetryOptions.Builder#setMaximumAttempts(int)}}, whichever happens first.
226
     *
227
     * <p>For example, let's consider you've called {@code WorkflowStub#query}, and this timeout is
228
     * set to 10s, while {@link RpcRetryOptions.Builder#setExpiration(Duration)} is set to 60s, and
229
     * the server is responding slowly or the query is not getting picked up by the worker for any
230
     * reason. The first two RPC calls may time out and be retried, but if the third one completes
231
     * fast, the overall {@code WorkflowStub#query} call will successfully resolve.
232
     */
233
    public Builder setRpcQueryTimeout(Duration rpcQueryTimeout) {
234
      this.rpcQueryTimeout = rpcQueryTimeout;
1✔
235
      return this;
1✔
236
    }
237

238
    /**
239
     * Allows customization of retry options for the outgoing RPC calls to temporal service.
240
     *
241
     * <p>Note that default values should be reasonable for most users, be cautious when changing
242
     * these values as it may result in increased load to the temporal backend or bad network
243
     * instability tolerance.
244
     *
245
     * <p>Defaults are:
246
     *
247
     * <ul>
248
     *   <li>Retries are limited by the maximum period of 1 minute
249
     *   <li>Initial period between retries: 50ms
250
     *   <li>Exponential Backoff Coefficient (exponential rate) for the retry period is 2
251
     * </ul>
252
     *
253
     * @see <a href="http://backoffcalculator.com">Backoff Calculator</a> to get a grasp on an
254
     *     Exponential Backoff as a retry strategy
255
     * @see #setRpcTimeout(Duration)
256
     * @see #setRpcQueryTimeout(Duration)
257
     */
258
    public Builder setRpcRetryOptions(RpcRetryOptions rpcRetryOptions) {
259
      this.rpcRetryOptions = rpcRetryOptions;
1✔
260
      return this;
1✔
261
    }
262

263
    /**
264
     * Sets the rpc timeout value for query calls. Default is 10 seconds.
265
     *
266
     * @param timeout timeout.
267
     * @deprecated use {{@link #setRpcQueryTimeout(Duration)}}
268
     */
269
    public Builder setQueryRpcTimeout(Duration timeout) {
270
      this.rpcQueryTimeout = Objects.requireNonNull(timeout);
×
271
      return this;
×
272
    }
273

274
    /**
275
     * Builds and returns a ClientOptions object.
276
     *
277
     * @return ClientOptions object with the specified params.
278
     */
279
    public WorkflowServiceStubsOptions build() {
280
      return new WorkflowServiceStubsOptions(
1✔
281
          super.build(),
1✔
282
          this.disableHealthCheck,
283
          this.rpcLongPollTimeout,
284
          this.rpcQueryTimeout,
285
          this.rpcRetryOptions);
286
    }
287

288
    public WorkflowServiceStubsOptions validateAndBuildWithDefaults() {
289
      ServiceStubsOptions serviceStubsOptions = super.validateAndBuildWithDefaults();
1✔
290
      RpcRetryOptions retryOptions =
1✔
291
          RpcRetryOptions.newBuilder(this.rpcRetryOptions).validateBuildWithDefaults();
1✔
292
      return new WorkflowServiceStubsOptions(
1✔
293
          serviceStubsOptions,
294
          this.disableHealthCheck,
295
          this.rpcLongPollTimeout,
296
          this.rpcQueryTimeout,
297
          retryOptions);
298
    }
299
  }
300
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc