Skip to content
Closed
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 @@ -481,11 +481,18 @@ long checkRetryBudgetAndGetDelay(SpannerException spannerException) {
// Determine the retry delay: either the delay that the server included in the error, or
// otherwise a delay determined by the exponential backoff.
long delayMillis = spannerException.getRetryDelayInMillis();
if (delayMillis == -1L) {
if (delayMillis <= 0L) {
if (this.backOff == null) {
this.backOff = newBackOff();
}
delayMillis = nextBackOffMillis(this.backOff);
} else {
long configuredMaxDelayMillis =
streamingRetrySettings != null && streamingRetrySettings.getMaxRetryDelay() != null
? streamingRetrySettings.getMaxRetryDelay().toMillis()
: 0L;
long maxDelayMillis = configuredMaxDelayMillis > 0L ? configuredMaxDelayMillis : 32_000L;
delayMillis = Math.min(delayMillis, maxDelayMillis);
}
// The total timeout budget applies regardless of whether the delay came from the server
// or from the backoff.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,18 @@ public Statement of(String sql) {
* @see DatabaseClient#getStatementFactory
*/
public Statement withUnnamedParameters(String sql, Object... values) {
Map<String, Value> parameters = getUnnamedParametersMap(values);
Preconditions.checkNotNull(values);
AbstractStatementParser statementParser = AbstractStatementParser.getInstance(this.dialect);
ParametersInfo parametersInfo =
statementParser.convertPositionalParametersToNamedParameters('?', sql);
if (parametersInfo.numberOfParameters != values.length) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
String.format(
"The number of parameter values (%d) does not match the number of positional parameters (%d) in the SQL statement.",
values.length, parametersInfo.numberOfParameters));
}
Map<String, Value> parameters = getUnnamedParametersMap(values);
return new Statement(parametersInfo.sqlWithNamedParameters, parameters, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ ImmutableList<Savepoint> getSavepoints() {
return ImmutableList.copyOf(savepoints);
}

void clearSavepoints() {
savepoints.clear();
}

@Override
public void savepoint(@Nonnull String name, @Nonnull Dialect dialect) {
if (dialect != Dialect.POSTGRESQL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,15 @@ public <T> T runTransaction(TransactionCallable<T> callable) {
}

void resetForRetry(UnitOfWork retryUnitOfWork) {
this.connectionState.rollback();
if (isBatchActive()) {
abortBatch();
}
transactionBeginMarked = true;
if (isAutocommit()) {
inTransaction = true;
}
setConnectionPropertyValue(RETRY_ABORTS_INTERNALLY, false, true);
retryUnitOfWork.resetForRetry();
this.currentUnitOfWork = retryUnitOfWork;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,20 @@ private ApiFuture<Void> rollbackAsync(CallType callType, boolean updateStatusAnd

@Override
public void resetForRetry() {
txContextFuture = ApiFutures.immediateFuture(txManager.resetForRetry());
this.state = UnitOfWorkState.STARTED;
this.abortedException = null;
this.rolledBackToSavepointException = null;
this.timedOutOrCancelled = false;
this.commitResponseFuture = null;
this.mutations.clear();
this.statements.clear();
this.autoSavepoint = null;
clearSavepoints();
if (txManager != null) {
txContextFuture = ApiFutures.immediateFuture(txManager.resetForRetry());
} else {
txContextFuture = null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.cloud.spanner.connection.Connection.TransactionCallable;
import com.google.cloud.spanner.connection.ConnectionImpl.Caller;
import com.google.cloud.spanner.connection.UnitOfWork.CallType;
import com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState;
import java.util.concurrent.ThreadLocalRandom;

class TransactionRunnerImpl {
private final ConnectionImpl connection;
Expand All @@ -43,20 +45,34 @@ <T> T run(TransactionCallable<T> callable) {
return result;
} catch (AbortedException abortedException) {
try {
//noinspection BusyWait
Thread.sleep(abortedException.getRetryDelayInMillis());
long retryDelayMillis = abortedException.getRetryDelayInMillis();
if (retryDelayMillis > 0L) {
//noinspection BusyWait
Thread.sleep(retryDelayMillis);
} else if (abortedException.isEmulatorOnlySupportsOneTransactionException()) {
//noinspection BusyWait
Thread.sleep(ThreadLocalRandom.current().nextInt(1, 5));
}
connection.resetForRetry(transaction);
} catch (InterruptedException interruptedException) {
connection.rollbackAsync(CallType.SYNC, Caller.TRANSACTION_RUNNER);
rollbackIfActive(transaction);
throw SpannerExceptionFactory.propagateInterrupt(interruptedException);
} catch (Throwable t) {
connection.rollbackAsync(CallType.SYNC, Caller.TRANSACTION_RUNNER);
rollbackIfActive(transaction);
throw t;
}
} catch (Throwable t) {
connection.rollbackAsync(CallType.SYNC, Caller.TRANSACTION_RUNNER);
rollbackIfActive(transaction);
throw t;
}
}
}

private void rollbackIfActive(UnitOfWork transaction) {
if (connection.isInTransaction()
&& (transaction.getState() == UnitOfWorkState.STARTED
|| transaction.getState() == UnitOfWorkState.ABORTED)) {
connection.rollbackAsync(CallType.SYNC, Caller.TRANSACTION_RUNNER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -525,17 +525,18 @@ private boolean totalTimeoutExceeded(long timeoutMillis, long elapsedMillis, lon
initWithLimitAndRetrySettings(
Integer.MAX_VALUE,
RetrySettings.newBuilder()
.setMaxRetryDelayDuration(java.time.Duration.ofMillis(Long.MAX_VALUE))
.setTotalTimeoutDuration(java.time.Duration.ofMillis(timeoutMillis))
.build());
SpannerException exception =
Mockito.spy(new RetryableException(errorCodeParameter, "failed by test"));
when(exception.getRetryDelayInMillis()).thenReturn(0L);
when(exception.getRetryDelayInMillis()).thenReturn(1L);
nanoTime = () -> 0L;
assertEquals(0L, resumableStreamIterator.checkRetryBudgetAndGetDelay(exception));
assertEquals(1L, resumableStreamIterator.checkRetryBudgetAndGetDelay(exception));
nanoTime = () -> TimeUnit.MILLISECONDS.toNanos(elapsedMillis);
when(exception.getRetryDelayInMillis()).thenReturn(delayMillis);
try {
assertEquals(delayMillis, resumableStreamIterator.checkRetryBudgetAndGetDelay(exception));
resumableStreamIterator.checkRetryBudgetAndGetDelay(exception);
return false;
} catch (SpannerException e) {
assertThat(e).isSameInstanceAs(exception);
Expand All @@ -561,15 +562,94 @@ public void retryBudget_usesConfiguredBackoffWithoutRetryInfo() {
assertThat(delayMillis).isAtMost(150L);
}

@Test
public void retryInfoWithZeroOrNegativeDelay_usesExponentialBackoff() {
initWithLimitAndRetrySettings(
Integer.MAX_VALUE,
RetrySettings.newBuilder()
.setInitialRetryDelayDuration(java.time.Duration.ofMillis(100))
.setMaxRetryDelayDuration(java.time.Duration.ofMillis(100))
.build());
Metadata.Key<RetryInfo> key = ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());

Metadata zeroTrailers = new Metadata();
zeroTrailers.put(
key,
RetryInfo.newBuilder()
.setRetryDelay(Duration.newBuilder().setSeconds(0L).setNanos(0))
.build());
SpannerException zeroDelayException =
new RetryableException(
errorCodeParameter,
"zero retry delay",
errorCodeParameter.getGrpcStatus().asRuntimeException(zeroTrailers));
long zeroFallbackDelay =
resumableStreamIterator.checkRetryBudgetAndGetDelay(zeroDelayException);
assertThat(zeroFallbackDelay).isAtLeast(50L);
assertThat(zeroFallbackDelay).isAtMost(150L);

Metadata negativeTrailers = new Metadata();
negativeTrailers.put(
key,
RetryInfo.newBuilder()
.setRetryDelay(Duration.newBuilder().setSeconds(-1L).setNanos(0))
.build());
SpannerException negativeDelayException =
new RetryableException(
errorCodeParameter,
"negative retry delay",
errorCodeParameter.getGrpcStatus().asRuntimeException(negativeTrailers));
long negativeFallbackDelay =
resumableStreamIterator.checkRetryBudgetAndGetDelay(negativeDelayException);
assertThat(negativeFallbackDelay).isAtLeast(50L);
assertThat(negativeFallbackDelay).isAtMost(150L);
}

@Test
public void retryInfoWithMultiHourDelay_clampsToMaxRetryDelay() {
initWithLimitAndRetrySettings(
Integer.MAX_VALUE,
RetrySettings.newBuilder()
.setInitialRetryDelayDuration(java.time.Duration.ofMillis(10))
.setMaxRetryDelayDuration(java.time.Duration.ofSeconds(32))
.setTotalTimeoutDuration(java.time.Duration.ofHours(24))
.build());
Metadata.Key<RetryInfo> key = ProtoUtils.keyForProto(RetryInfo.getDefaultInstance());
Metadata trailers = new Metadata();
trailers.put(
key,
RetryInfo.newBuilder()
.setRetryDelay(
Duration.newBuilder().setSeconds(TimeUnit.HOURS.toSeconds(10L)).setNanos(0))
.build());
SpannerException multiHourException =
new RetryableException(
errorCodeParameter,
"multi-hour retry delay",
errorCodeParameter.getGrpcStatus().asRuntimeException(trailers));
long clampedDelay = resumableStreamIterator.checkRetryBudgetAndGetDelay(multiHourException);
assertEquals(TimeUnit.SECONDS.toMillis(32L), clampedDelay);

// Also verify default 32s cap when maxRetryDelay is not configured (0ms).
initWithLimitAndRetrySettings(
Integer.MAX_VALUE,
RetrySettings.newBuilder().setTotalTimeoutDuration(java.time.Duration.ofHours(24)).build());
long defaultClampedDelay =
resumableStreamIterator.checkRetryBudgetAndGetDelay(multiHourException);
assertEquals(TimeUnit.SECONDS.toMillis(32L), defaultClampedDelay);
}

@Test
public void rawGapicSettings_useConfiguredTimeoutWithoutSpecialCase() {
RetrySettings settings =
SpannerStubSettings.newBuilder().executeStreamingSqlSettings().getRetrySettings();
initWithLimitAndRetrySettings(Integer.MAX_VALUE, settings);
SpannerException exception =
Mockito.spy(new RetryableException(errorCodeParameter, "failed by test"));
when(exception.getRetryDelayInMillis())
.thenReturn(settings.getTotalTimeoutDuration().toMillis());
when(exception.getRetryDelayInMillis()).thenReturn(1L);
nanoTime = () -> 0L;
assertEquals(1L, resumableStreamIterator.checkRetryBudgetAndGetDelay(exception));
nanoTime = () -> settings.getTotalTimeoutDuration().toNanos();
assertThat(
assertThrows(
SpannerException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,41 @@ public void testGetParameterName() {
assertSame(Statement.getParameterName(i), Statement.getParameterName(i));
}
}

@Test
public void testWithUnnamedParametersCountMismatchThrowsInvalidArgument() {
for (Dialect dialect : Dialect.values()) {
Statement.StatementFactory factory = new Statement.StatementFactory(dialect);

// Matching positional parameter count succeeds, ignoring '?' inside literals and comments.
Statement valid =
factory.withUnnamedParameters(
"SELECT '?' AS q, /* ? */ * FROM Users WHERE Id = ? AND Status = ?", 1234L, "ACTIVE");
assertEquals(Value.toValue(1234L), valid.getParameters().get("p1"));
assertEquals(Value.toValue("ACTIVE"), valid.getParameters().get("p2"));

// Too few parameter values throws INVALID_ARGUMENT.
SpannerException tooFew =
assertThrows(
SpannerException.class,
() ->
factory.withUnnamedParameters(
"SELECT * FROM Users WHERE Id = ? AND Status = ?", 1234L));
assertEquals(ErrorCode.INVALID_ARGUMENT, tooFew.getErrorCode());

// Too many parameter values throws INVALID_ARGUMENT.
SpannerException tooMany =
assertThrows(
SpannerException.class,
() ->
factory.withUnnamedParameters(
"SELECT '?' AS q, /* ? */ * FROM Users WHERE Id = ?", 1234L, "EXTRA"));
assertEquals(ErrorCode.INVALID_ARGUMENT, tooMany.getErrorCode());

// Null values array throws NullPointerException.
assertThrows(
NullPointerException.class,
() -> factory.withUnnamedParameters("SELECT * FROM Users WHERE Id = ?", (Object[]) null));
}
}
}
Loading
Loading