thiagotnunes commented on a change in pull request #16846: URL: https://github.com/apache/beam/pull/16846#discussion_r806467293
########## File path: sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/SpannerChangeStreamErrorTest.java ########## @@ -0,0 +1,494 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.apache.beam.sdk.io.gcp.spanner.changestreams; + +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_CREATED_AT; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_END_TIMESTAMP; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_FINISHED_AT; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_HEARTBEAT_MILLIS; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_PARENT_TOKENS; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_PARTITION_TOKEN; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_RUNNING_AT; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_SCHEDULED_AT; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_START_TIMESTAMP; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_STATE; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_WATERMARK; +import static org.hamcrest.MatcherAssert.assertThat; + +import com.google.api.gax.grpc.testing.MockServiceHelper; +import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.Timestamp; +import com.google.cloud.spanner.ErrorCode; +import com.google.cloud.spanner.MockSpannerServiceImpl; +import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; +import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; +import com.google.cloud.spanner.Statement; +import com.google.protobuf.ListValue; +import com.google.protobuf.NullValue; +import com.google.protobuf.Value; +import com.google.spanner.v1.ExecuteSqlRequest; +import com.google.spanner.v1.ResultSet; +import com.google.spanner.v1.ResultSetMetadata; +import com.google.spanner.v1.StructType; +import com.google.spanner.v1.StructType.Field; +import com.google.spanner.v1.Type; +import com.google.spanner.v1.TypeCode; +import io.grpc.Status; +import java.io.Serializable; +import java.util.Collections; +import org.apache.beam.sdk.Pipeline.PipelineExecutionException; +import org.apache.beam.sdk.io.gcp.spanner.SpannerConfig; +import org.apache.beam.sdk.io.gcp.spanner.SpannerIO; +import org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.DaoFactory; +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata.State; +import org.apache.beam.sdk.options.ValueProvider.StaticValueProvider; +import org.apache.beam.sdk.testing.TestPipeline; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableSet; +import org.hamcrest.Matchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class SpannerChangeStreamErrorTest implements Serializable { + + public static final String SPANNER_HOST = "my-host"; + private static final String TEST_PROJECT = "my-project"; + private static final String TEST_INSTANCE = "my-instance"; + private static final String TEST_DATABASE = "my-database"; + private static final String TEST_TABLE = "my-metadata-table"; + private static final String TEST_CHANGE_STREAM = "my-change-stream"; + + @Rule + public final transient TestPipeline pipeline = + TestPipeline.create().enableAbandonedNodeEnforcement(false); + + @Rule public final transient ExpectedException thrown = ExpectedException.none(); + + private MockSpannerServiceImpl mockSpannerService; + private MockServiceHelper serviceHelper; + + @Before + public void setUp() throws Exception { + mockSpannerService = new MockSpannerServiceImpl(); + serviceHelper = + new MockServiceHelper(SPANNER_HOST, Collections.singletonList(mockSpannerService)); + serviceHelper.start(); + serviceHelper.reset(); + } + + @After + public void tearDown() throws NoSuchFieldException, IllegalAccessException { + serviceHelper.reset(); + serviceHelper.stop(); + mockSpannerService.reset(); + resetDaoFactoryFields(); + } + + @Test + public void testResourceExhaustedDoesNotRetry() { + mockSpannerService.setExecuteStreamingSqlExecutionTime( + SimulatedExecutionTime.ofStickyException(Status.RESOURCE_EXHAUSTED.asRuntimeException())); + + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + try { + pipeline.apply( + SpannerIO.readChangeStream() + .withSpannerConfig(getSpannerConfig()) + .withChangeStreamName(TEST_CHANGE_STREAM) + .withMetadataDatabase(TEST_DATABASE) + .withInclusiveStartAt(now) + .withInclusiveEndAt(after3Seconds)); + pipeline.run().waitUntilFinish(); + } finally { + thrown.expect(PipelineExecutionException.class); + thrown.expectMessage(ErrorCode.RESOURCE_EXHAUSTED.name()); + } + } + + @Test + public void testUnavailableExceptionRetries() { + mockSpannerService.setExecuteStreamingSqlExecutionTime( + SimulatedExecutionTime.ofExceptions( + ImmutableSet.of( + Status.UNAVAILABLE.asRuntimeException(), + Status.RESOURCE_EXHAUSTED.asRuntimeException()))); + + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + try { + pipeline.apply( + SpannerIO.readChangeStream() + .withSpannerConfig(getSpannerConfig()) + .withChangeStreamName(TEST_CHANGE_STREAM) + .withMetadataDatabase(TEST_DATABASE) + .withInclusiveStartAt(now) + .withInclusiveEndAt(after3Seconds)); + pipeline.run().waitUntilFinish(); + } finally { + assertThat( + mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), Matchers.greaterThan(1)); + thrown.expect(PipelineExecutionException.class); + thrown.expectMessage(ErrorCode.RESOURCE_EXHAUSTED.name()); + } + } + + @Test + public void testAbortedExceptionRetries() { + mockSpannerService.setExecuteStreamingSqlExecutionTime( + SimulatedExecutionTime.ofStickyException(Status.ABORTED.asRuntimeException())); + + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + try { + pipeline.apply( + SpannerIO.readChangeStream() + .withSpannerConfig(getSpannerConfig()) + .withChangeStreamName(TEST_CHANGE_STREAM) + .withMetadataDatabase(TEST_DATABASE) + .withInclusiveStartAt(now) + .withInclusiveEndAt(after3Seconds)); + pipeline.run().waitUntilFinish(); + } finally { + assertThat( + mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), Matchers.greaterThan(1)); + thrown.expect(PipelineExecutionException.class); + thrown.expectMessage(ErrorCode.ABORTED.name()); + } + } + + @Test + public void testUnknownExceptionDoesNotRetry() { + mockSpannerService.setExecuteStreamingSqlExecutionTime( + SimulatedExecutionTime.ofStickyException(Status.UNKNOWN.asRuntimeException())); + + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + try { + pipeline.apply( + SpannerIO.readChangeStream() + .withSpannerConfig(getSpannerConfig()) + .withChangeStreamName(TEST_CHANGE_STREAM) + .withMetadataDatabase(TEST_DATABASE) + .withInclusiveStartAt(now) + .withInclusiveEndAt(after3Seconds)); + pipeline.run().waitUntilFinish(); + } finally { + assertThat( + mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), Matchers.equalTo(1)); + thrown.expect(PipelineExecutionException.class); + thrown.expectMessage(ErrorCode.UNKNOWN.name()); + } + } + + @Test + public void testInvalidRecordReceived() { + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + + mockTableExists(); + ResultSet getPartitionResultSet = mockGetParentPartition(now, after3Seconds); + mockGetWatermark(now); + mockGetPartitionsAfter( + Timestamp.ofTimeSecondsAndNanos(now.getSeconds(), now.getNanos() + 1000), + getPartitionResultSet); + mockGetPartitionsAfter( + Timestamp.ofTimeSecondsAndNanos(now.getSeconds(), now.getNanos() - 1000), + getPartitionResultSet); + + Statement changeStreamQueryStatement = + Statement.newBuilder( + "SELECT * FROM READ_my-change-stream( start_timestamp => @startTimestamp, end_timestamp => @endTimestamp, partition_token => @partitionToken, read_options => null, heartbeat_milliseconds => @heartbeatMillis)") Review comment: nit: we could remove the extra spacing here (probably spotless added it when merging lines into one) ########## File path: buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy ########## @@ -454,8 +454,12 @@ class BeamModulePlugin implements Plugin<Project> { def checkerframework_version = "3.10.0" def classgraph_version = "4.8.104" def errorprone_version = "2.10.0" + // Try to keep gax_version consistent with gax-grpc version in google_cloud_platform_libraries_bom + def gax_version = "2.8.1" Review comment: I think that this one will work without the version as the BOM defines a version for it (https://mvnrepository.com/artifact/com.google.cloud/libraries-bom/24.2.0) ########## File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerAccessor.java ########## @@ -107,34 +108,56 @@ public static SpannerAccessor getOrCreate(SpannerConfig spannerConfig) { private static SpannerAccessor createAndConnect(SpannerConfig spannerConfig) { SpannerOptions.Builder builder = SpannerOptions.newBuilder(); - ValueProvider<Duration> commitDeadline = spannerConfig.getCommitDeadline(); - if (commitDeadline != null && commitDeadline.get().getMillis() > 0) { + // Set retryable codes + if (spannerConfig.getRetryableCodes() != null) { Review comment: Makes sense, thanks for trying. I think this approach is nice. ########## File path: sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/SpannerChangeStreamErrorTest.java ########## @@ -0,0 +1,494 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 org.apache.beam.sdk.io.gcp.spanner.changestreams; + +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_CREATED_AT; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_END_TIMESTAMP; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_FINISHED_AT; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_HEARTBEAT_MILLIS; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_PARENT_TOKENS; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_PARTITION_TOKEN; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_RUNNING_AT; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_SCHEDULED_AT; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_START_TIMESTAMP; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_STATE; +import static org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.PartitionMetadataAdminDao.COLUMN_WATERMARK; +import static org.hamcrest.MatcherAssert.assertThat; + +import com.google.api.gax.grpc.testing.MockServiceHelper; +import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.Timestamp; +import com.google.cloud.spanner.ErrorCode; +import com.google.cloud.spanner.MockSpannerServiceImpl; +import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; +import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; +import com.google.cloud.spanner.Statement; +import com.google.protobuf.ListValue; +import com.google.protobuf.NullValue; +import com.google.protobuf.Value; +import com.google.spanner.v1.ExecuteSqlRequest; +import com.google.spanner.v1.ResultSet; +import com.google.spanner.v1.ResultSetMetadata; +import com.google.spanner.v1.StructType; +import com.google.spanner.v1.StructType.Field; +import com.google.spanner.v1.Type; +import com.google.spanner.v1.TypeCode; +import io.grpc.Status; +import java.io.Serializable; +import java.util.Collections; +import org.apache.beam.sdk.Pipeline.PipelineExecutionException; +import org.apache.beam.sdk.io.gcp.spanner.SpannerConfig; +import org.apache.beam.sdk.io.gcp.spanner.SpannerIO; +import org.apache.beam.sdk.io.gcp.spanner.changestreams.dao.DaoFactory; +import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata.State; +import org.apache.beam.sdk.options.ValueProvider.StaticValueProvider; +import org.apache.beam.sdk.testing.TestPipeline; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableSet; +import org.hamcrest.Matchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class SpannerChangeStreamErrorTest implements Serializable { + + public static final String SPANNER_HOST = "my-host"; + private static final String TEST_PROJECT = "my-project"; + private static final String TEST_INSTANCE = "my-instance"; + private static final String TEST_DATABASE = "my-database"; + private static final String TEST_TABLE = "my-metadata-table"; + private static final String TEST_CHANGE_STREAM = "my-change-stream"; + + @Rule + public final transient TestPipeline pipeline = + TestPipeline.create().enableAbandonedNodeEnforcement(false); + + @Rule public final transient ExpectedException thrown = ExpectedException.none(); + + private MockSpannerServiceImpl mockSpannerService; + private MockServiceHelper serviceHelper; + + @Before + public void setUp() throws Exception { + mockSpannerService = new MockSpannerServiceImpl(); + serviceHelper = + new MockServiceHelper(SPANNER_HOST, Collections.singletonList(mockSpannerService)); + serviceHelper.start(); + serviceHelper.reset(); + } + + @After + public void tearDown() throws NoSuchFieldException, IllegalAccessException { + serviceHelper.reset(); + serviceHelper.stop(); + mockSpannerService.reset(); + resetDaoFactoryFields(); + } + + @Test + public void testResourceExhaustedDoesNotRetry() { + mockSpannerService.setExecuteStreamingSqlExecutionTime( + SimulatedExecutionTime.ofStickyException(Status.RESOURCE_EXHAUSTED.asRuntimeException())); + + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + try { + pipeline.apply( + SpannerIO.readChangeStream() + .withSpannerConfig(getSpannerConfig()) + .withChangeStreamName(TEST_CHANGE_STREAM) + .withMetadataDatabase(TEST_DATABASE) + .withInclusiveStartAt(now) + .withInclusiveEndAt(after3Seconds)); + pipeline.run().waitUntilFinish(); + } finally { + thrown.expect(PipelineExecutionException.class); + thrown.expectMessage(ErrorCode.RESOURCE_EXHAUSTED.name()); + } + } + + @Test + public void testUnavailableExceptionRetries() { + mockSpannerService.setExecuteStreamingSqlExecutionTime( + SimulatedExecutionTime.ofExceptions( + ImmutableSet.of( + Status.UNAVAILABLE.asRuntimeException(), + Status.RESOURCE_EXHAUSTED.asRuntimeException()))); + + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + try { + pipeline.apply( + SpannerIO.readChangeStream() + .withSpannerConfig(getSpannerConfig()) + .withChangeStreamName(TEST_CHANGE_STREAM) + .withMetadataDatabase(TEST_DATABASE) + .withInclusiveStartAt(now) + .withInclusiveEndAt(after3Seconds)); + pipeline.run().waitUntilFinish(); + } finally { + assertThat( + mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), Matchers.greaterThan(1)); + thrown.expect(PipelineExecutionException.class); + thrown.expectMessage(ErrorCode.RESOURCE_EXHAUSTED.name()); + } + } + + @Test + public void testAbortedExceptionRetries() { + mockSpannerService.setExecuteStreamingSqlExecutionTime( + SimulatedExecutionTime.ofStickyException(Status.ABORTED.asRuntimeException())); + + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + try { + pipeline.apply( + SpannerIO.readChangeStream() + .withSpannerConfig(getSpannerConfig()) + .withChangeStreamName(TEST_CHANGE_STREAM) + .withMetadataDatabase(TEST_DATABASE) + .withInclusiveStartAt(now) + .withInclusiveEndAt(after3Seconds)); + pipeline.run().waitUntilFinish(); + } finally { + assertThat( + mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), Matchers.greaterThan(1)); + thrown.expect(PipelineExecutionException.class); + thrown.expectMessage(ErrorCode.ABORTED.name()); + } + } + + @Test + public void testUnknownExceptionDoesNotRetry() { + mockSpannerService.setExecuteStreamingSqlExecutionTime( + SimulatedExecutionTime.ofStickyException(Status.UNKNOWN.asRuntimeException())); + + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + try { + pipeline.apply( + SpannerIO.readChangeStream() + .withSpannerConfig(getSpannerConfig()) + .withChangeStreamName(TEST_CHANGE_STREAM) + .withMetadataDatabase(TEST_DATABASE) + .withInclusiveStartAt(now) + .withInclusiveEndAt(after3Seconds)); + pipeline.run().waitUntilFinish(); + } finally { + assertThat( + mockSpannerService.countRequestsOfType(ExecuteSqlRequest.class), Matchers.equalTo(1)); + thrown.expect(PipelineExecutionException.class); + thrown.expectMessage(ErrorCode.UNKNOWN.name()); + } + } + + @Test + public void testInvalidRecordReceived() { + final Timestamp now = Timestamp.now(); + final Timestamp after3Seconds = + Timestamp.ofTimeSecondsAndNanos(now.getSeconds() + 3, now.getNanos()); + + mockTableExists(); + ResultSet getPartitionResultSet = mockGetParentPartition(now, after3Seconds); + mockGetWatermark(now); + mockGetPartitionsAfter( + Timestamp.ofTimeSecondsAndNanos(now.getSeconds(), now.getNanos() + 1000), + getPartitionResultSet); + mockGetPartitionsAfter( + Timestamp.ofTimeSecondsAndNanos(now.getSeconds(), now.getNanos() - 1000), + getPartitionResultSet); + + Statement changeStreamQueryStatement = + Statement.newBuilder( + "SELECT * FROM READ_my-change-stream( start_timestamp => @startTimestamp, end_timestamp => @endTimestamp, partition_token => @partitionToken, read_options => null, heartbeat_milliseconds => @heartbeatMillis)") + .bind("startTimestamp") + .to(now) + .bind("endTimestamp") + .to(after3Seconds) + .bind("partitionToken") + .to((String) null) + .bind("heartbeatMillis") + .to(500) + .build(); + ResultSetMetadata readChangeStreamResultSetMetadata = Review comment: nit: could we extract these to methods as well? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
