-
Notifications
You must be signed in to change notification settings - Fork 135
refactor: removes double loop of pdml transaction #361
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
Merged
thiagotnunes
merged 1 commit into
googleapis:master
from
thiagotnunes:pdml-transaction-refactoring
Jul 23, 2020
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
177 changes: 0 additions & 177 deletions
177
google-cloud-spanner/src/main/java/com/google/cloud/spanner/PartitionedDMLTransaction.java
This file was deleted.
Oops, something went wrong.
196 changes: 196 additions & 0 deletions
196
google-cloud-spanner/src/main/java/com/google/cloud/spanner/PartitionedDmlTransaction.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| /* | ||
| * Copyright 2019 Google LLC | ||
| * | ||
| * Licensed 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 com.google.cloud.spanner; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkState; | ||
|
|
||
| import com.google.api.gax.grpc.GrpcStatusCode; | ||
| import com.google.api.gax.rpc.AbortedException; | ||
| import com.google.api.gax.rpc.DeadlineExceededException; | ||
| import com.google.api.gax.rpc.ServerStream; | ||
| import com.google.api.gax.rpc.UnavailableException; | ||
| import com.google.cloud.spanner.spi.v1.SpannerRpc; | ||
| import com.google.common.base.Stopwatch; | ||
| import com.google.common.base.Ticker; | ||
| import com.google.protobuf.ByteString; | ||
| import com.google.spanner.v1.BeginTransactionRequest; | ||
| import com.google.spanner.v1.ExecuteSqlRequest; | ||
| import com.google.spanner.v1.PartialResultSet; | ||
| import com.google.spanner.v1.Transaction; | ||
| import com.google.spanner.v1.TransactionOptions; | ||
| import com.google.spanner.v1.TransactionSelector; | ||
| import io.grpc.Status; | ||
| import io.opencensus.trace.Span; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import org.threeten.bp.Duration; | ||
| import org.threeten.bp.temporal.ChronoUnit; | ||
|
|
||
| public class PartitionedDmlTransaction implements SessionImpl.SessionTransaction { | ||
| private static final Logger LOGGER = Logger.getLogger(PartitionedDmlTransaction.class.getName()); | ||
|
|
||
| private final SessionImpl session; | ||
| private final SpannerRpc rpc; | ||
| private final Ticker ticker; | ||
| private volatile boolean isValid = true; | ||
|
|
||
| PartitionedDmlTransaction(SessionImpl session, SpannerRpc rpc, Ticker ticker) { | ||
| this.session = session; | ||
| this.rpc = rpc; | ||
| this.ticker = ticker; | ||
| } | ||
|
|
||
| /** | ||
| * Executes the {@link Statement} using a partitioned dml transaction with automatic retry if the | ||
| * transaction was aborted. The update method uses the ExecuteStreamingSql RPC to execute the | ||
| * statement, and will retry the stream if an {@link UnavailableException} is thrown, using the | ||
| * last seen resume token if the server returns any. | ||
| */ | ||
| long executeStreamingPartitionedUpdate(final Statement statement, final Duration timeout) { | ||
| checkState(isValid, "Partitioned DML has been invalidated by a new operation on the session"); | ||
| LOGGER.log(Level.FINER, "Starting PartitionedUpdate statement"); | ||
|
|
||
| ByteString resumeToken = ByteString.EMPTY; | ||
| boolean foundStats = false; | ||
| long updateCount = 0L; | ||
| Stopwatch stopwatch = Stopwatch.createStarted(ticker); | ||
|
|
||
| try { | ||
| ExecuteSqlRequest request = newTransactionRequestFrom(statement); | ||
|
|
||
| while (true) { | ||
| final Duration remainingTimeout = tryUpdateTimeout(timeout, stopwatch); | ||
|
|
||
| try { | ||
| ServerStream<PartialResultSet> stream = | ||
| rpc.executeStreamingPartitionedDml(request, session.getOptions(), remainingTimeout); | ||
|
|
||
| for (PartialResultSet rs : stream) { | ||
| if (rs.getResumeToken() != null && !rs.getResumeToken().isEmpty()) { | ||
| resumeToken = rs.getResumeToken(); | ||
| } | ||
| if (rs.hasStats()) { | ||
| foundStats = true; | ||
| updateCount += rs.getStats().getRowCountLowerBound(); | ||
| } | ||
| } | ||
| break; | ||
| } catch (UnavailableException e) { | ||
| LOGGER.log( | ||
| Level.FINER, "Retrying PartitionedDml transaction after UnavailableException", e); | ||
| request = resumeOrRestartRequest(resumeToken, statement, request); | ||
| } catch (AbortedException e) { | ||
| LOGGER.log(Level.FINER, "Retrying PartitionedDml transaction after AbortedException", e); | ||
| resumeToken = ByteString.EMPTY; | ||
| foundStats = false; | ||
| updateCount = 0L; | ||
| request = newTransactionRequestFrom(statement); | ||
| } | ||
| } | ||
| if (!foundStats) { | ||
| throw SpannerExceptionFactory.newSpannerException( | ||
| ErrorCode.INVALID_ARGUMENT, | ||
| "Partitioned DML response missing stats possibly due to non-DML statement as input"); | ||
| } | ||
| LOGGER.log(Level.FINER, "Finished PartitionedUpdate statement"); | ||
| return updateCount; | ||
| } catch (Exception e) { | ||
| throw SpannerExceptionFactory.newSpannerException(e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void invalidate() { | ||
| isValid = false; | ||
| } | ||
|
|
||
| // No-op method needed to implement SessionTransaction interface. | ||
| @Override | ||
| public void setSpan(Span span) {} | ||
|
|
||
| private Duration tryUpdateTimeout(final Duration timeout, final Stopwatch stopwatch) { | ||
| final Duration remainingTimeout = | ||
| timeout.minus(stopwatch.elapsed(TimeUnit.MILLISECONDS), ChronoUnit.MILLIS); | ||
| if (remainingTimeout.isNegative() || remainingTimeout.isZero()) { | ||
| // The total deadline has been exceeded while retrying. | ||
| throw new DeadlineExceededException( | ||
| null, GrpcStatusCode.of(Status.Code.DEADLINE_EXCEEDED), false); | ||
| } | ||
| return remainingTimeout; | ||
| } | ||
|
|
||
| private ExecuteSqlRequest resumeOrRestartRequest( | ||
| final ByteString resumeToken, | ||
| final Statement statement, | ||
| final ExecuteSqlRequest originalRequest) { | ||
| if (resumeToken.isEmpty()) { | ||
| return newTransactionRequestFrom(statement); | ||
| } else { | ||
| return ExecuteSqlRequest.newBuilder(originalRequest).setResumeToken(resumeToken).build(); | ||
| } | ||
| } | ||
|
|
||
| private ExecuteSqlRequest newTransactionRequestFrom(final Statement statement) { | ||
| ByteString transactionId = initTransaction(); | ||
|
|
||
| final TransactionSelector transactionSelector = | ||
| TransactionSelector.newBuilder().setId(transactionId).build(); | ||
| final ExecuteSqlRequest.Builder builder = | ||
| ExecuteSqlRequest.newBuilder() | ||
| .setSql(statement.getSql()) | ||
| .setQueryMode(ExecuteSqlRequest.QueryMode.NORMAL) | ||
| .setSession(session.getName()) | ||
| .setTransaction(transactionSelector); | ||
|
|
||
| setParameters(builder, statement.getParameters()); | ||
|
|
||
| builder.setResumeToken(ByteString.EMPTY); | ||
|
|
||
| return builder.build(); | ||
| } | ||
|
|
||
| private ByteString initTransaction() { | ||
| final BeginTransactionRequest request = | ||
| BeginTransactionRequest.newBuilder() | ||
| .setSession(session.getName()) | ||
| .setOptions( | ||
| TransactionOptions.newBuilder() | ||
| .setPartitionedDml(TransactionOptions.PartitionedDml.getDefaultInstance())) | ||
| .build(); | ||
| Transaction tx = rpc.beginTransaction(request, session.getOptions()); | ||
| if (tx.getId().isEmpty()) { | ||
| throw SpannerExceptionFactory.newSpannerException( | ||
| ErrorCode.INTERNAL, | ||
| "Failed to init transaction, missing transaction id\n" + session.getName()); | ||
| } | ||
| return tx.getId(); | ||
| } | ||
|
|
||
| private void setParameters( | ||
| final ExecuteSqlRequest.Builder requestBuilder, | ||
| final Map<String, Value> statementParameters) { | ||
| if (!statementParameters.isEmpty()) { | ||
| com.google.protobuf.Struct.Builder paramsBuilder = requestBuilder.getParamsBuilder(); | ||
| for (Map.Entry<String, Value> param : statementParameters.entrySet()) { | ||
| paramsBuilder.putFields(param.getKey(), param.getValue().toProto()); | ||
| requestBuilder.putParamTypes(param.getKey(), param.getValue().getType().toProto()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
nit: 2020
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.
Will fix on the next PR (#360).