Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.paging.Page;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallSettings.Builder;
import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.admin.database.v1.MockDatabaseAdminImpl;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.Any;
Expand Down Expand Up @@ -183,22 +181,17 @@ public boolean isRetryable() {
private static LocalChannelProvider channelProvider;

@Parameter(0)
public boolean enableGaxRetries;

@Parameter(1)
public int exceptionAtCall;

@Parameter(2)
@Parameter(1)
public ExceptionType exceptionType;

@Parameters(name = "enable GAX retries = {0}, exception at call = {1}, exception type = {2}")
@Parameters(name = "exception at call = {0}, exception type = {1}")
public static Collection<Object[]> data() {
List<Object[]> params = new ArrayList<>();
for (boolean enableRetries : new boolean[] {true, false}) {
for (int exceptionAtCall : new int[] {0, 1}) {
for (ExceptionType exceptionType : ExceptionType.values()) {
params.add(new Object[] {enableRetries, exceptionAtCall, exceptionType});
}
for (int exceptionAtCall : new int[] {0, 1}) {
for (ExceptionType exceptionType : ExceptionType.values()) {
params.add(new Object[] {exceptionAtCall, exceptionType});
}
}
return params;
Expand Down Expand Up @@ -272,43 +265,6 @@ public Void apply(Builder<?, ?> input) {
.toBuilder()
.setRetrySettings(retrySettings)
.build());
if (!enableGaxRetries) {
// Disable retries by removing all retryable codes.
builder
.getDatabaseAdminStubSettingsBuilder()
.applyToAllUnaryMethods(
new ApiFunction<UnaryCallSettings.Builder<?, ?>, Void>() {
@Override
public Void apply(Builder<?, ?> input) {
input.setRetryableCodes(ImmutableSet.<StatusCode.Code>of());
return null;
}
});
builder
.getDatabaseAdminStubSettingsBuilder()
.createDatabaseOperationSettings()
.setInitialCallSettings(
builder
.getDatabaseAdminStubSettingsBuilder()
.createDatabaseOperationSettings()
.getInitialCallSettings()
.toBuilder()
.setRetrySettings(retrySettings)
.setRetryableCodes(ImmutableSet.<StatusCode.Code>of())
.build());
builder
.getDatabaseAdminStubSettingsBuilder()
.updateDatabaseDdlOperationSettings()
.setInitialCallSettings(
builder
.getDatabaseAdminStubSettingsBuilder()
.updateDatabaseDdlOperationSettings()
.getInitialCallSettings()
.toBuilder()
.setRetrySettings(retrySettings)
.setRetryableCodes(ImmutableSet.<StatusCode.Code>of())
.build());
}
spanner = builder.build().getService();
client = spanner.getDatabaseAdminClient();
}
Expand All @@ -319,12 +275,7 @@ public void tearDown() throws Exception {
}

private Exception setupException() {
if (exceptionType.isRetryable()) {
if (!enableGaxRetries) {
expectedException.expect(
SpannerMatchers.isSpannerException(exceptionType.getExpectedErrorCodeWithoutGax()));
}
} else {
if (!exceptionType.isRetryable()) {
expectedException.expect(
SpannerMatchers.isSpannerException(exceptionType.getExpectedErrorCodeWithGax()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import com.google.api.gax.longrunning.OperationFuture;
import com.google.api.gax.paging.Page;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallSettings.Builder;
import com.google.cloud.NoCredentials;
import com.google.cloud.spanner.admin.instance.v1.MockInstanceAdminImpl;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.Any;
Expand Down Expand Up @@ -186,22 +184,17 @@ public boolean isRetryable() {
private static LocalChannelProvider channelProvider;

@Parameter(0)
public boolean enableGaxRetries;

@Parameter(1)
public int exceptionAtCall;

@Parameter(2)
@Parameter(1)
public ExceptionType exceptionType;

@Parameters(name = "enable GAX retries = {0}, exception at call = {1}, exception type = {2}")
@Parameters(name = "exception at call = {0}, exception type = {1}")
public static Collection<Object[]> data() {
List<Object[]> params = new ArrayList<>();
for (boolean enableRetries : new boolean[] {true, false}) {
for (int exceptionAtCall : new int[] {0, 1}) {
for (ExceptionType exceptionType : ExceptionType.values()) {
params.add(new Object[] {enableRetries, exceptionAtCall, exceptionType});
}
for (int exceptionAtCall : new int[] {0, 1}) {
for (ExceptionType exceptionType : ExceptionType.values()) {
params.add(new Object[] {exceptionAtCall, exceptionType});
}
}
return params;
Expand Down Expand Up @@ -275,43 +268,6 @@ public Void apply(Builder<?, ?> input) {
.toBuilder()
.setRetrySettings(retrySettings)
.build());
if (!enableGaxRetries) {
// Disable retries by removing all retryable codes.
builder
.getInstanceAdminStubSettingsBuilder()
.applyToAllUnaryMethods(
new ApiFunction<UnaryCallSettings.Builder<?, ?>, Void>() {
@Override
public Void apply(Builder<?, ?> input) {
input.setRetryableCodes(ImmutableSet.<StatusCode.Code>of());
return null;
}
});
builder
.getInstanceAdminStubSettingsBuilder()
.createInstanceOperationSettings()
.setInitialCallSettings(
builder
.getInstanceAdminStubSettingsBuilder()
.createInstanceOperationSettings()
.getInitialCallSettings()
.toBuilder()
.setRetrySettings(retrySettings)
.setRetryableCodes(ImmutableSet.<StatusCode.Code>of())
.build());
builder
.getInstanceAdminStubSettingsBuilder()
.updateInstanceOperationSettings()
.setInitialCallSettings(
builder
.getInstanceAdminStubSettingsBuilder()
.updateInstanceOperationSettings()
.getInitialCallSettings()
.toBuilder()
.setRetrySettings(retrySettings)
.setRetryableCodes(ImmutableSet.<StatusCode.Code>of())
.build());
}
spanner = builder.build().getService();
client = spanner.getInstanceAdminClient();
}
Expand All @@ -322,12 +278,7 @@ public void tearDown() throws Exception {
}

private Exception setupException() {
if (exceptionType.isRetryable()) {
if (!enableGaxRetries) {
expectedException.expect(
SpannerMatchers.isSpannerException(exceptionType.getExpectedErrorCodeWithoutGax()));
}
} else {
if (!exceptionType.isRetryable()) {
expectedException.expect(
SpannerMatchers.isSpannerException(exceptionType.getExpectedErrorCodeWithGax()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.testing.LocalChannelProvider;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallSettings.Builder;
import com.google.cloud.NoCredentials;
Expand All @@ -33,7 +32,6 @@
import com.google.cloud.spanner.TransactionRunner.TransactionCallable;
import com.google.cloud.spanner.v1.SpannerClient;
import com.google.cloud.spanner.v1.SpannerSettings;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.ListValue;
import com.google.spanner.v1.ResultSetMetadata;
import com.google.spanner.v1.StructType;
Expand All @@ -43,26 +41,22 @@
import io.grpc.StatusRuntimeException;
import io.grpc.inprocess.InProcessServerBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.JUnit4;
import org.threeten.bp.Duration;

@RunWith(Parameterized.class)
@RunWith(JUnit4.class)
public class SpannerGaxRetryTest {
private static final ResultSetMetadata READ_METADATA =
ResultSetMetadata.newBuilder()
Expand Down Expand Up @@ -144,17 +138,6 @@ public class SpannerGaxRetryTest {
private static Spanner spanner;
private static DatabaseClient client;

@Parameter(0)
public boolean enableGaxRetries;

@Parameters(name = "enable GAX retries = {0}")
public static Collection<Object[]> data() {
List<Object[]> params = new ArrayList<>();
params.add(new Object[] {true});
params.add(new Object[] {false});
return params;
}

@Rule public ExpectedException expectedException = ExpectedException.none();

@BeforeClass
Expand Down Expand Up @@ -232,27 +215,6 @@ public Void apply(Builder<?, ?> input) {
.executeStreamingSqlSettings()
.setRetrySettings(retrySettings);
builder.getSpannerStubSettingsBuilder().streamingReadSettings().setRetrySettings(retrySettings);
if (!enableGaxRetries) {
// Disable retries by removing all retryable codes.
builder
.getSpannerStubSettingsBuilder()
.applyToAllUnaryMethods(
new ApiFunction<UnaryCallSettings.Builder<?, ?>, Void>() {
@Override
public Void apply(Builder<?, ?> input) {
input.setRetryableCodes(ImmutableSet.<StatusCode.Code>of());
return null;
}
});
builder
.getSpannerStubSettingsBuilder()
.executeStreamingSqlSettings()
.setRetryableCodes(ImmutableSet.<StatusCode.Code>of());
builder
.getSpannerStubSettingsBuilder()
.streamingReadSettings()
.setRetryableCodes(ImmutableSet.<StatusCode.Code>of());
}
spanner = builder.build().getService();
client = spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
}
Expand Down Expand Up @@ -297,9 +259,7 @@ public Long run(TransactionContext transaction) throws Exception {

@Test
public void singleUseTimeout() {
if (enableGaxRetries) {
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
}
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
mockSpanner.setCreateSessionExecutionTime(ONE_SECOND);
try (ResultSet rs = client.singleUse().executeQuery(SELECT1AND2)) {
while (rs.next()) {}
Expand All @@ -308,9 +268,6 @@ public void singleUseTimeout() {

@Test
public void singleUseUnavailable() {
if (!enableGaxRetries) {
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.UNAVAILABLE));
}
mockSpanner.addException(UNAVAILABLE);
try (ResultSet rs = client.singleUse().executeQuery(SELECT1AND2)) {
while (rs.next()) {}
Expand Down Expand Up @@ -346,9 +303,7 @@ public void singleUseInternal() {

@Test
public void singleUseReadOnlyTransactionTimeout() {
if (enableGaxRetries) {
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
}
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
mockSpanner.setCreateSessionExecutionTime(ONE_SECOND);
try (ResultSet rs = client.singleUseReadOnlyTransaction().executeQuery(SELECT1AND2)) {
while (rs.next()) {}
Expand All @@ -357,28 +312,24 @@ public void singleUseReadOnlyTransactionTimeout() {

@Test
public void singleUseReadOnlyTransactionUnavailable() {
if (!enableGaxRetries) {
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.UNAVAILABLE));
}
mockSpanner.addException(UNAVAILABLE);
try (ResultSet rs = client.singleUseReadOnlyTransaction().executeQuery(SELECT1AND2)) {
while (rs.next()) {}
}
}

@Test
@Ignore("enable once gax 1.47 is released")
public void singleUseExecuteStreamingSqlTimeout() {
// Streaming calls do not timeout.
mockSpanner.setExecuteStreamingSqlExecutionTime(ONE_SECOND);
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
try (ResultSet rs = client.singleUse().executeQuery(SELECT1AND2)) {
mockSpanner.setExecuteStreamingSqlExecutionTime(ONE_SECOND);
while (rs.next()) {}
}
}

@Test
public void singleUseExecuteStreamingSqlUnavailable() {
// executeStreamingSql is always retried by the Spanner library, even if gax retries have been
// disabled.
try (ResultSet rs = client.singleUse().executeQuery(SELECT1AND2)) {
mockSpanner.addException(UNAVAILABLE);
while (rs.next()) {}
Expand All @@ -388,9 +339,7 @@ public void singleUseExecuteStreamingSqlUnavailable() {
@Test
public void readWriteTransactionTimeout() {
warmUpSessionPool();
if (enableGaxRetries) {
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
}
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
mockSpanner.setBeginTransactionExecutionTime(ONE_SECOND);
TransactionRunner runner = client.readWriteTransaction();
long updateCount =
Expand All @@ -407,9 +356,6 @@ public Long run(TransactionContext transaction) throws Exception {
@Test
public void readWriteTransactionUnavailable() {
warmUpSessionPool();
if (!enableGaxRetries) {
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.UNAVAILABLE));
}
mockSpanner.addException(UNAVAILABLE);
TransactionRunner runner = client.readWriteTransaction();
long updateCount =
Expand Down Expand Up @@ -492,9 +438,7 @@ public Long run(TransactionContext transaction) throws Exception {
@Test
public void transactionManagerTimeout() {
warmUpSessionPool();
if (enableGaxRetries) {
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
}
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.DEADLINE_EXCEEDED));
mockSpanner.setBeginTransactionExecutionTime(ONE_SECOND);
try (TransactionManager txManager = client.transactionManager()) {
TransactionContext tx = txManager.begin();
Expand All @@ -514,9 +458,6 @@ public void transactionManagerTimeout() {
@Test
public void transactionManagerUnavailable() {
warmUpSessionPool();
if (!enableGaxRetries) {
expectedException.expect(SpannerMatchers.isSpannerException(ErrorCode.UNAVAILABLE));
}
mockSpanner.addException(UNAVAILABLE);
try (TransactionManager txManager = client.transactionManager()) {
TransactionContext tx = txManager.begin();
Expand Down