Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit e27c1e5

Browse files
committed
Java surface refactoring on GAX
Java surface refactoring on GAX - Settings classes (ApiCallSettings, ServiceApiSettings) are converted from mutable classes to immutable classes with builders. - The builders in ApiCallable become builders in individual top-level settings classes. - ServiceApiSettings.allCallSettings removed, replaced with applyToAllMethods(ApiCallSettings.Builder) - Renamed getChannel (now in ServiceApiSettings.Builder) to getOrBuildChannel; also getExecutor -> getOrBuildExecutor - Rename RetryParams to RetrySettings for consistency.
1 parent 8862d42 commit e27c1e5

10 files changed

Lines changed: 757 additions & 434 deletions

File tree

src/main/java/com/google/api/gax/core/RetryParams.java renamed to src/main/java/com/google/api/gax/core/RetrySettings.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@
3939

4040
/**
4141
* {@code RetryParams} encapsulates a retry strategy used by
42-
* {@link com.google.api.gax.grpc.ApiCallable#retrying(RetryParams, ScheduledExecutorService)}.
42+
* {@link com.google.api.gax.grpc.ApiCallable#retrying(RetrySettings, ScheduledExecutorService)}.
4343
*/
4444
@AutoValue
45-
public abstract class RetryParams {
46-
45+
public abstract class RetrySettings {
4746

4847
public abstract Duration getTotalTimeout();
4948

@@ -56,11 +55,11 @@ public abstract class RetryParams {
5655
public abstract Duration getMaxRpcTimeout();
5756

5857
public static Builder newBuilder() {
59-
return new AutoValue_RetryParams.Builder();
58+
return new AutoValue_RetrySettings.Builder();
6059
}
6160

6261
public Builder toBuilder() {
63-
return new AutoValue_RetryParams.Builder(this);
62+
return new AutoValue_RetrySettings.Builder(this);
6463
}
6564

6665
@AutoValue.Builder
@@ -76,10 +75,20 @@ public abstract static class Builder {
7675

7776
public abstract Builder setTotalTimeout(Duration totalTimeout);
7877

79-
abstract RetryParams autoBuild();
78+
public abstract Duration getInitialRetryDelay();
79+
public abstract double getRetryDelayMultiplier();
80+
public abstract Duration getMaxRetryDelay();
81+
82+
public abstract Duration getInitialRpcTimeout();
83+
public abstract double getRpcTimeoutMultiplier();
84+
public abstract Duration getMaxRpcTimeout();
85+
86+
public abstract Duration getTotalTimeout();
8087

81-
public RetryParams build() {
82-
RetryParams params = autoBuild();
88+
abstract RetrySettings autoBuild();
89+
90+
public RetrySettings build() {
91+
RetrySettings params = autoBuild();
8392
if (params.getTotalTimeout().getMillis() < 0) {
8493
throw new IllegalStateException("total timeout must not be negative");
8594
}
@@ -100,5 +109,27 @@ public RetryParams build() {
100109
}
101110
return params;
102111
}
112+
113+
public RetrySettings.Builder merge(RetrySettings.Builder newSettings) {
114+
if (newSettings.getInitialRetryDelay() != null) {
115+
setInitialRetryDelay(newSettings.getInitialRetryDelay());
116+
}
117+
if (newSettings.getRetryDelayMultiplier() >= 1) {
118+
setRetryDelayMultiplier(newSettings.getRetryDelayMultiplier());
119+
}
120+
if (newSettings.getMaxRetryDelay() != null) {
121+
setMaxRetryDelay(newSettings.getMaxRetryDelay());
122+
}
123+
if (newSettings.getInitialRpcTimeout() != null) {
124+
setInitialRpcTimeout(newSettings.getInitialRpcTimeout());
125+
}
126+
if (newSettings.getRpcTimeoutMultiplier() >= 1) {
127+
setRpcTimeoutMultiplier(newSettings.getRpcTimeoutMultiplier());
128+
}
129+
if (newSettings.getMaxRpcTimeout() != null) {
130+
setMaxRpcTimeout(newSettings.getMaxRpcTimeout());
131+
}
132+
return this;
133+
}
103134
}
104135
}

src/main/java/com/google/api/gax/grpc/ApiCallSettings.java

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,85 @@
3131

3232
package com.google.api.gax.grpc;
3333

34-
import com.google.api.gax.core.RetryParams;
34+
import com.google.api.gax.core.RetrySettings;
35+
import com.google.common.collect.ImmutableSet;
3536
import com.google.common.collect.Sets;
3637

3738
import io.grpc.Status;
3839

39-
import java.util.HashSet;
4040
import java.util.Set;
4141

4242
/**
4343
* A settings class to configure an API method.
4444
*/
4545
public class ApiCallSettings {
4646

47-
private Set<Status.Code> retryableCodes = new HashSet<>();
48-
private RetryParams retryParams = null;
47+
private final ImmutableSet<Status.Code> retryableCodes;
48+
private final RetrySettings retrySettings;
4949

50-
/**
51-
* Sets the retryable codes.
52-
*/
53-
public ApiCallSettings setRetryableCodes(Set<Status.Code> retryableCodes) {
54-
this.retryableCodes = retryableCodes;
55-
return this;
50+
public ImmutableSet<Status.Code> getRetryableCodes() {
51+
return retryableCodes;
5652
}
5753

58-
/**
59-
* Sets the retryable codes.
60-
*/
61-
public ApiCallSettings setRetryableCodes(Status.Code... codes) {
62-
this.retryableCodes = Sets.newHashSet(codes);
63-
return this;
54+
public RetrySettings getRetrySettings() {
55+
return retrySettings;
6456
}
6557

66-
/**
67-
* Gets the retryable codes.
68-
*/
69-
public Set<Status.Code> getRetryableCodes() {
70-
return retryableCodes;
58+
public static Builder newBuilder() {
59+
return new Builder();
60+
}
61+
62+
public Builder toBuilder() {
63+
return new Builder(this);
7164
}
7265

73-
/**
74-
* Sets the retry params.
75-
*/
76-
public ApiCallSettings setRetryParams(RetryParams retryParams) {
77-
this.retryParams = retryParams;
78-
return this;
66+
protected ApiCallSettings(ImmutableSet<Status.Code> retryableCodes,
67+
RetrySettings retrySettings) {
68+
this.retryableCodes = ImmutableSet.<Status.Code>copyOf(retryableCodes);
69+
this.retrySettings = retrySettings;
7970
}
8071

81-
/**
82-
* Returns the retry params.
83-
*/
84-
public RetryParams getRetryParams() {
85-
return retryParams;
72+
public static class Builder {
73+
74+
private Set<Status.Code> retryableCodes;
75+
private RetrySettings.Builder retrySettingsBuilder;
76+
77+
public Builder() {
78+
retryableCodes = Sets.newHashSet();
79+
retrySettingsBuilder = RetrySettings.newBuilder();
80+
}
81+
82+
public Builder(ApiCallSettings apiCallSettings) {
83+
setRetryableCodes(apiCallSettings.retryableCodes);
84+
setRetrySettingsBuilder(apiCallSettings.getRetrySettings().toBuilder());
85+
}
86+
87+
public Builder setRetryableCodes(Set<Status.Code> retryableCodes) {
88+
this.retryableCodes = Sets.newHashSet(retryableCodes);
89+
return this;
90+
}
91+
92+
public Builder setRetryableCodes(Status.Code... codes) {
93+
this.setRetryableCodes(Sets.newHashSet(codes));
94+
return this;
95+
}
96+
97+
public Builder setRetrySettingsBuilder(RetrySettings.Builder retrySettingsBuilder) {
98+
this.retrySettingsBuilder = retrySettingsBuilder;
99+
return this;
100+
}
101+
102+
public Set<Status.Code> getRetryableCodes() {
103+
return this.retryableCodes;
104+
}
105+
106+
public RetrySettings.Builder getRetrySettingsBuilder() {
107+
return this.retrySettingsBuilder;
108+
}
109+
110+
public ApiCallSettings build() {
111+
return new ApiCallSettings(ImmutableSet.<Status.Code>copyOf(retryableCodes),
112+
retrySettingsBuilder.build());
113+
}
86114
}
87115
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.google.api.gax.grpc;
2+
3+
import com.google.api.gax.core.RetrySettings;
4+
import com.google.common.collect.ImmutableSet;
5+
6+
import io.grpc.ManagedChannel;
7+
import io.grpc.MethodDescriptor;
8+
import io.grpc.Status;
9+
10+
import java.io.IOException;
11+
import java.util.concurrent.ScheduledExecutorService;
12+
13+
/**
14+
* Package-private: Use other concrete settings classes instead of this class from outside of
15+
* the package.
16+
* A settings class with generic typing which can be used to configure an API method or create
17+
* the method callable object. This class can be used as the base class that other concrete
18+
* call settings classes inherit from.
19+
*/
20+
class ApiCallSettingsTyped<RequestT, ResponseT> extends ApiCallSettings {
21+
22+
private final MethodDescriptor<RequestT, ResponseT> methodDescriptor;
23+
24+
public MethodDescriptor<RequestT, ResponseT> getMethodDescriptor() {
25+
return methodDescriptor;
26+
}
27+
28+
protected ApiCallSettingsTyped(ImmutableSet<Status.Code> retryableCodes,
29+
RetrySettings retrySettings,
30+
MethodDescriptor<RequestT, ResponseT> methodDescriptor) {
31+
super(retryableCodes, retrySettings);
32+
this.methodDescriptor = methodDescriptor;
33+
}
34+
35+
protected ApiCallable<RequestT, ResponseT> createBaseCallable(
36+
ServiceApiSettings.Builder serviceSettingsBuilder) throws IOException {
37+
ClientCallFactory<RequestT, ResponseT> clientCallFactory =
38+
new DescriptorClientCallFactory<>(methodDescriptor);
39+
ApiCallable<RequestT, ResponseT> callable =
40+
new ApiCallable<>(new DirectCallable<>(clientCallFactory), this);
41+
ManagedChannel channel = serviceSettingsBuilder.getOrBuildChannel();
42+
ScheduledExecutorService executor = serviceSettingsBuilder.getOrBuildExecutor();
43+
if (getRetryableCodes() != null) {
44+
callable = callable.retryableOn(ImmutableSet.copyOf(getRetryableCodes()));
45+
}
46+
if (getRetrySettings() != null) {
47+
callable = callable.retrying(getRetrySettings(), executor);
48+
}
49+
return callable.bind(channel);
50+
}
51+
52+
public static class Builder<RequestT, ResponseT> extends ApiCallSettings.Builder {
53+
private MethodDescriptor<RequestT, ResponseT> grpcMethodDescriptor;
54+
55+
public Builder(MethodDescriptor<RequestT, ResponseT> grpcMethodDescriptor) {
56+
this.grpcMethodDescriptor = grpcMethodDescriptor;
57+
}
58+
59+
public MethodDescriptor<RequestT, ResponseT> getMethodDescriptor() {
60+
return grpcMethodDescriptor;
61+
}
62+
63+
@Override
64+
public ApiCallSettingsTyped<RequestT, ResponseT> build() {
65+
return new ApiCallSettingsTyped<RequestT, ResponseT>(
66+
ImmutableSet.<Status.Code>copyOf(getRetryableCodes()),
67+
getRetrySettingsBuilder().build(),
68+
grpcMethodDescriptor);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)