-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spanner: Add specific timeout for Partitioned DML with default of 2 hours #5709
Merged
kolea2
merged 1 commit into
googleapis:master
from
olavloite:spanner-set-2hr-timeout-for-pdml-using-call-context
Jul 10, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,14 @@ | |
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import com.google.api.gax.grpc.testing.LocalChannelProvider; | ||
import com.google.api.gax.retrying.RetrySettings; | ||
import com.google.cloud.NoCredentials; | ||
import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; | ||
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; | ||
import com.google.cloud.spanner.TransactionRunner.TransactionCallable; | ||
import com.google.protobuf.ListValue; | ||
import com.google.spanner.v1.ResultSetMetadata; | ||
import com.google.spanner.v1.StructType; | ||
|
@@ -32,13 +36,15 @@ | |
import io.grpc.Status; | ||
import io.grpc.inprocess.InProcessServerBuilder; | ||
import java.io.IOException; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.threeten.bp.Duration; | ||
|
||
@RunWith(JUnit4.class) | ||
public class DatabaseClientImplTest { | ||
|
@@ -89,21 +95,24 @@ public static void startStaticServer() throws IOException { | |
String uniqueName = InProcessServerBuilder.generateName(); | ||
server = | ||
InProcessServerBuilder.forName(uniqueName) | ||
.directExecutor() | ||
// We need to use a real executor for timeouts to occur. | ||
.scheduledExecutorService(new ScheduledThreadPoolExecutor(1)) | ||
.addService(mockSpanner) | ||
.build() | ||
.start(); | ||
channelProvider = LocalChannelProvider.create(uniqueName); | ||
} | ||
|
||
@AfterClass | ||
public static void stopServer() { | ||
public static void stopServer() throws InterruptedException { | ||
server.shutdown(); | ||
server.awaitTermination(); | ||
} | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
mockSpanner.reset(); | ||
mockSpanner.removeAllExecutionTimes(); | ||
spanner = | ||
SpannerOptions.newBuilder() | ||
.setProjectId("[PROJECT]") | ||
|
@@ -158,4 +167,89 @@ public void testExecutePartitionedDmlWithException() { | |
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]")); | ||
client.executePartitionedUpdate(INVALID_UPDATE_STATEMENT); | ||
} | ||
|
||
@Test | ||
public void testPartitionedDmlDoesNotTimeout() throws Exception { | ||
mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofMinimumAndRandomTime(10, 0)); | ||
final RetrySettings retrySettings = | ||
RetrySettings.newBuilder() | ||
.setInitialRpcTimeout(Duration.ofMillis(1L)) | ||
.setMaxRpcTimeout(Duration.ofMillis(1L)) | ||
.setMaxAttempts(1) | ||
.setTotalTimeout(Duration.ofMillis(1L)) | ||
.build(); | ||
SpannerOptions.Builder builder = | ||
SpannerOptions.newBuilder() | ||
.setProjectId("[PROJECT]") | ||
.setChannelProvider(channelProvider) | ||
.setCredentials(NoCredentials.getInstance()); | ||
// Set normal DML timeout value. | ||
builder.getSpannerStubSettingsBuilder().executeSqlSettings().setRetrySettings(retrySettings); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to add an assertion that the PDML timeout is 2 hrs? |
||
try (Spanner spanner = builder.build().getService()) { | ||
DatabaseClient client = | ||
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]")); | ||
|
||
// PDML should not timeout with these settings. | ||
long updateCount = client.executePartitionedUpdate(UPDATE_STATEMENT); | ||
assertThat(updateCount, is(equalTo(UPDATE_COUNT))); | ||
|
||
// Normal DML should timeout. | ||
try { | ||
client | ||
.readWriteTransaction() | ||
.run( | ||
new TransactionCallable<Void>() { | ||
@Override | ||
public Void run(TransactionContext transaction) throws Exception { | ||
transaction.executeUpdate(UPDATE_STATEMENT); | ||
return null; | ||
} | ||
}); | ||
fail("expected DEADLINE_EXCEEDED"); | ||
} catch (SpannerException e) { | ||
if (e.getErrorCode() != ErrorCode.DEADLINE_EXCEEDED) { | ||
fail("expected DEADLINE_EXCEEDED"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testPartitionedDmlWithTimeout() throws Exception { | ||
mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofMinimumAndRandomTime(10, 0)); | ||
SpannerOptions.Builder builder = | ||
SpannerOptions.newBuilder() | ||
.setProjectId("[PROJECT]") | ||
.setChannelProvider(channelProvider) | ||
.setCredentials(NoCredentials.getInstance()); | ||
// Set PDML timeout value. | ||
builder.setPartitionedDmlTimeout(Duration.ofMillis(1L)); | ||
try (Spanner spanner = builder.build().getService()) { | ||
DatabaseClient client = | ||
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]")); | ||
|
||
// PDML should timeout with these settings. | ||
try { | ||
client.executePartitionedUpdate(UPDATE_STATEMENT); | ||
fail("expected DEADLINE_EXCEEDED"); | ||
} catch (SpannerException e) { | ||
if (e.getErrorCode() != ErrorCode.DEADLINE_EXCEEDED) { | ||
fail("expected DEADLINE_EXCEEDED"); | ||
} | ||
} | ||
|
||
// Normal DML should not timeout. | ||
long updateCount = | ||
client | ||
.readWriteTransaction() | ||
.run( | ||
new TransactionCallable<Long>() { | ||
@Override | ||
public Long run(TransactionContext transaction) throws Exception { | ||
return transaction.executeUpdate(UPDATE_STATEMENT); | ||
} | ||
}); | ||
assertThat(updateCount, is(equalTo(UPDATE_COUNT))); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very minor - I would move this above the try-catch, after sessionLabels.