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

Commit

Permalink
Java surface refactoring on GAX
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shinfan committed Apr 6, 2016
1 parent 8862d42 commit e27c1e5
Show file tree
Hide file tree
Showing 10 changed files with 757 additions and 434 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@

/**
* {@code RetryParams} encapsulates a retry strategy used by
* {@link com.google.api.gax.grpc.ApiCallable#retrying(RetryParams, ScheduledExecutorService)}.
* {@link com.google.api.gax.grpc.ApiCallable#retrying(RetrySettings, ScheduledExecutorService)}.
*/
@AutoValue
public abstract class RetryParams {

public abstract class RetrySettings {

public abstract Duration getTotalTimeout();

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

public static Builder newBuilder() {
return new AutoValue_RetryParams.Builder();
return new AutoValue_RetrySettings.Builder();
}

public Builder toBuilder() {
return new AutoValue_RetryParams.Builder(this);
return new AutoValue_RetrySettings.Builder(this);
}

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

public abstract Builder setTotalTimeout(Duration totalTimeout);

abstract RetryParams autoBuild();
public abstract Duration getInitialRetryDelay();
public abstract double getRetryDelayMultiplier();
public abstract Duration getMaxRetryDelay();

public abstract Duration getInitialRpcTimeout();
public abstract double getRpcTimeoutMultiplier();
public abstract Duration getMaxRpcTimeout();

public abstract Duration getTotalTimeout();

public RetryParams build() {
RetryParams params = autoBuild();
abstract RetrySettings autoBuild();

public RetrySettings build() {
RetrySettings params = autoBuild();
if (params.getTotalTimeout().getMillis() < 0) {
throw new IllegalStateException("total timeout must not be negative");
}
Expand All @@ -100,5 +109,27 @@ public RetryParams build() {
}
return params;
}

public RetrySettings.Builder merge(RetrySettings.Builder newSettings) {
if (newSettings.getInitialRetryDelay() != null) {
setInitialRetryDelay(newSettings.getInitialRetryDelay());
}
if (newSettings.getRetryDelayMultiplier() >= 1) {
setRetryDelayMultiplier(newSettings.getRetryDelayMultiplier());
}
if (newSettings.getMaxRetryDelay() != null) {
setMaxRetryDelay(newSettings.getMaxRetryDelay());
}
if (newSettings.getInitialRpcTimeout() != null) {
setInitialRpcTimeout(newSettings.getInitialRpcTimeout());
}
if (newSettings.getRpcTimeoutMultiplier() >= 1) {
setRpcTimeoutMultiplier(newSettings.getRpcTimeoutMultiplier());
}
if (newSettings.getMaxRpcTimeout() != null) {
setMaxRpcTimeout(newSettings.getMaxRpcTimeout());
}
return this;
}
}
}
92 changes: 60 additions & 32 deletions src/main/java/com/google/api/gax/grpc/ApiCallSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,85 @@

package com.google.api.gax.grpc;

import com.google.api.gax.core.RetryParams;
import com.google.api.gax.core.RetrySettings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import io.grpc.Status;

import java.util.HashSet;
import java.util.Set;

/**
* A settings class to configure an API method.
*/
public class ApiCallSettings {

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

/**
* Sets the retryable codes.
*/
public ApiCallSettings setRetryableCodes(Set<Status.Code> retryableCodes) {
this.retryableCodes = retryableCodes;
return this;
public ImmutableSet<Status.Code> getRetryableCodes() {
return retryableCodes;
}

/**
* Sets the retryable codes.
*/
public ApiCallSettings setRetryableCodes(Status.Code... codes) {
this.retryableCodes = Sets.newHashSet(codes);
return this;
public RetrySettings getRetrySettings() {
return retrySettings;
}

/**
* Gets the retryable codes.
*/
public Set<Status.Code> getRetryableCodes() {
return retryableCodes;
public static Builder newBuilder() {
return new Builder();
}

public Builder toBuilder() {
return new Builder(this);
}

/**
* Sets the retry params.
*/
public ApiCallSettings setRetryParams(RetryParams retryParams) {
this.retryParams = retryParams;
return this;
protected ApiCallSettings(ImmutableSet<Status.Code> retryableCodes,
RetrySettings retrySettings) {
this.retryableCodes = ImmutableSet.<Status.Code>copyOf(retryableCodes);
this.retrySettings = retrySettings;
}

/**
* Returns the retry params.
*/
public RetryParams getRetryParams() {
return retryParams;
public static class Builder {

private Set<Status.Code> retryableCodes;
private RetrySettings.Builder retrySettingsBuilder;

public Builder() {
retryableCodes = Sets.newHashSet();
retrySettingsBuilder = RetrySettings.newBuilder();
}

public Builder(ApiCallSettings apiCallSettings) {
setRetryableCodes(apiCallSettings.retryableCodes);
setRetrySettingsBuilder(apiCallSettings.getRetrySettings().toBuilder());
}

public Builder setRetryableCodes(Set<Status.Code> retryableCodes) {
this.retryableCodes = Sets.newHashSet(retryableCodes);
return this;
}

public Builder setRetryableCodes(Status.Code... codes) {
this.setRetryableCodes(Sets.newHashSet(codes));
return this;
}

public Builder setRetrySettingsBuilder(RetrySettings.Builder retrySettingsBuilder) {
this.retrySettingsBuilder = retrySettingsBuilder;
return this;
}

public Set<Status.Code> getRetryableCodes() {
return this.retryableCodes;
}

public RetrySettings.Builder getRetrySettingsBuilder() {
return this.retrySettingsBuilder;
}

public ApiCallSettings build() {
return new ApiCallSettings(ImmutableSet.<Status.Code>copyOf(retryableCodes),
retrySettingsBuilder.build());
}
}
}
71 changes: 71 additions & 0 deletions src/main/java/com/google/api/gax/grpc/ApiCallSettingsTyped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.google.api.gax.grpc;

import com.google.api.gax.core.RetrySettings;
import com.google.common.collect.ImmutableSet;

import io.grpc.ManagedChannel;
import io.grpc.MethodDescriptor;
import io.grpc.Status;

import java.io.IOException;
import java.util.concurrent.ScheduledExecutorService;

/**
* Package-private: Use other concrete settings classes instead of this class from outside of
* the package.
* A settings class with generic typing which can be used to configure an API method or create
* the method callable object. This class can be used as the base class that other concrete
* call settings classes inherit from.
*/
class ApiCallSettingsTyped<RequestT, ResponseT> extends ApiCallSettings {

private final MethodDescriptor<RequestT, ResponseT> methodDescriptor;

public MethodDescriptor<RequestT, ResponseT> getMethodDescriptor() {
return methodDescriptor;
}

protected ApiCallSettingsTyped(ImmutableSet<Status.Code> retryableCodes,
RetrySettings retrySettings,
MethodDescriptor<RequestT, ResponseT> methodDescriptor) {
super(retryableCodes, retrySettings);
this.methodDescriptor = methodDescriptor;
}

protected ApiCallable<RequestT, ResponseT> createBaseCallable(
ServiceApiSettings.Builder serviceSettingsBuilder) throws IOException {
ClientCallFactory<RequestT, ResponseT> clientCallFactory =
new DescriptorClientCallFactory<>(methodDescriptor);
ApiCallable<RequestT, ResponseT> callable =
new ApiCallable<>(new DirectCallable<>(clientCallFactory), this);
ManagedChannel channel = serviceSettingsBuilder.getOrBuildChannel();
ScheduledExecutorService executor = serviceSettingsBuilder.getOrBuildExecutor();
if (getRetryableCodes() != null) {
callable = callable.retryableOn(ImmutableSet.copyOf(getRetryableCodes()));
}
if (getRetrySettings() != null) {
callable = callable.retrying(getRetrySettings(), executor);
}
return callable.bind(channel);
}

public static class Builder<RequestT, ResponseT> extends ApiCallSettings.Builder {
private MethodDescriptor<RequestT, ResponseT> grpcMethodDescriptor;

public Builder(MethodDescriptor<RequestT, ResponseT> grpcMethodDescriptor) {
this.grpcMethodDescriptor = grpcMethodDescriptor;
}

public MethodDescriptor<RequestT, ResponseT> getMethodDescriptor() {
return grpcMethodDescriptor;
}

@Override
public ApiCallSettingsTyped<RequestT, ResponseT> build() {
return new ApiCallSettingsTyped<RequestT, ResponseT>(
ImmutableSet.<Status.Code>copyOf(getRetryableCodes()),
getRetrySettingsBuilder().build(),
grpcMethodDescriptor);
}
}
}
Loading

0 comments on commit e27c1e5

Please sign in to comment.