-
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
Bigtable: add await replication #3658
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ecdd30
Bigtable: add await replication
igorbernstein2 9dd8722
remove consistency token model
igorbernstein2 2306532
fix tests
igorbernstein2 16f06c5
address feedback
igorbernstein2 0143123
Merge remote-tracking branch 'origin/master' into await-replication
igorbernstein2 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
54 changes: 0 additions & 54 deletions
54
...table-admin/src/main/java/com/google/cloud/bigtable/admin/v2/models/ConsistencyToken.java
This file was deleted.
Oops, something went wrong.
195 changes: 195 additions & 0 deletions
195
...admin/src/main/java/com/google/cloud/bigtable/admin/v2/stub/AwaitReplicationCallable.java
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 |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright 2018 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 | ||
* | ||
* https://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.bigtable.admin.v2.stub; | ||
|
||
import com.google.api.core.ApiAsyncFunction; | ||
import com.google.api.core.ApiFunction; | ||
import com.google.api.core.ApiFuture; | ||
import com.google.api.core.ApiFutures; | ||
import com.google.api.gax.retrying.ExponentialPollAlgorithm; | ||
import com.google.api.gax.retrying.NonCancellableFuture; | ||
import com.google.api.gax.retrying.ResultRetryAlgorithm; | ||
import com.google.api.gax.retrying.RetryAlgorithm; | ||
import com.google.api.gax.retrying.RetrySettings; | ||
import com.google.api.gax.retrying.RetryingExecutor; | ||
import com.google.api.gax.retrying.RetryingFuture; | ||
import com.google.api.gax.retrying.ScheduledRetryingExecutor; | ||
import com.google.api.gax.retrying.TimedAttemptSettings; | ||
import com.google.api.gax.rpc.ApiCallContext; | ||
import com.google.api.gax.rpc.ClientContext; | ||
import com.google.api.gax.rpc.UnaryCallable; | ||
import com.google.bigtable.admin.v2.CheckConsistencyRequest; | ||
import com.google.bigtable.admin.v2.CheckConsistencyResponse; | ||
import com.google.bigtable.admin.v2.GenerateConsistencyTokenRequest; | ||
import com.google.bigtable.admin.v2.GenerateConsistencyTokenResponse; | ||
import com.google.bigtable.admin.v2.TableName; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CancellationException; | ||
|
||
/** | ||
* Callable that waits until replication has caught up to the point it was called. | ||
* | ||
* <p>This callable wraps GenerateConsistencyToken and CheckConsistency RPCs. It will generate a | ||
* token then poll until isConsistent is true. | ||
*/ | ||
class AwaitReplicationCallable extends UnaryCallable<TableName, Void> { | ||
private final UnaryCallable<GenerateConsistencyTokenRequest, GenerateConsistencyTokenResponse> generateCallable; | ||
private final UnaryCallable<CheckConsistencyRequest, CheckConsistencyResponse> checkCallable; | ||
private final RetryingExecutor<CheckConsistencyResponse> executor; | ||
|
||
static AwaitReplicationCallable create( | ||
UnaryCallable<GenerateConsistencyTokenRequest, GenerateConsistencyTokenResponse> generateCallable, | ||
UnaryCallable<CheckConsistencyRequest, CheckConsistencyResponse> checkCallable, | ||
ClientContext clientContext, | ||
RetrySettings pollingSettings) { | ||
|
||
RetryAlgorithm<CheckConsistencyResponse> retryAlgorithm = new RetryAlgorithm<>( | ||
new PollResultAlgorithm(), | ||
new ExponentialPollAlgorithm(pollingSettings, clientContext.getClock()) | ||
); | ||
|
||
RetryingExecutor<CheckConsistencyResponse> retryingExecutor = new ScheduledRetryingExecutor<>( | ||
retryAlgorithm, | ||
clientContext.getExecutor() | ||
); | ||
|
||
return new AwaitReplicationCallable( | ||
generateCallable, | ||
checkCallable, | ||
retryingExecutor | ||
); | ||
} | ||
|
||
@VisibleForTesting | ||
AwaitReplicationCallable( | ||
UnaryCallable<GenerateConsistencyTokenRequest, GenerateConsistencyTokenResponse> generateCallable, | ||
UnaryCallable<CheckConsistencyRequest, CheckConsistencyResponse> checkCallable, | ||
RetryingExecutor<CheckConsistencyResponse> executor) { | ||
this.generateCallable = generateCallable; | ||
this.checkCallable = checkCallable; | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
public ApiFuture<Void> futureCall(final TableName tableName, final ApiCallContext context) { | ||
ApiFuture<GenerateConsistencyTokenResponse> tokenFuture = generateToken(tableName, context); | ||
|
||
return ApiFutures.transformAsync( | ||
tokenFuture, | ||
new ApiAsyncFunction<GenerateConsistencyTokenResponse, Void>() { | ||
@Override | ||
public ApiFuture<Void> apply(GenerateConsistencyTokenResponse input) { | ||
CheckConsistencyRequest request = CheckConsistencyRequest.newBuilder() | ||
.setName(tableName.toString()) | ||
.setConsistencyToken(input.getConsistencyToken()) | ||
.build(); | ||
|
||
return pollToken(request, context); | ||
} | ||
}, | ||
MoreExecutors.directExecutor() | ||
); | ||
} | ||
|
||
private ApiFuture<GenerateConsistencyTokenResponse> generateToken(TableName tableName, | ||
ApiCallContext context) { | ||
GenerateConsistencyTokenRequest generateRequest = GenerateConsistencyTokenRequest.newBuilder() | ||
.setName(tableName.toString()) | ||
.build(); | ||
return generateCallable.futureCall(generateRequest, context); | ||
} | ||
|
||
private ApiFuture<Void> pollToken(CheckConsistencyRequest request, ApiCallContext context) { | ||
AttemptCallable<CheckConsistencyRequest, CheckConsistencyResponse> attemptCallable = | ||
new AttemptCallable<>(checkCallable, request, context); | ||
RetryingFuture<CheckConsistencyResponse> retryingFuture = executor | ||
.createFuture(attemptCallable); | ||
attemptCallable.setExternalFuture(retryingFuture); | ||
attemptCallable.call(); | ||
|
||
return ApiFutures.transform( | ||
retryingFuture, | ||
new ApiFunction<CheckConsistencyResponse, Void>() { | ||
@Override | ||
public Void apply(CheckConsistencyResponse input) { | ||
return null; | ||
} | ||
}, | ||
MoreExecutors.directExecutor() | ||
); | ||
} | ||
|
||
/** | ||
* A callable representing an attempt to make an RPC call. | ||
*/ | ||
private static class AttemptCallable<RequestT, ResponseT> implements Callable<ResponseT> { | ||
private final UnaryCallable<RequestT, ResponseT> callable; | ||
private final RequestT request; | ||
|
||
private volatile RetryingFuture<ResponseT> externalFuture; | ||
private volatile ApiCallContext callContext; | ||
|
||
AttemptCallable( | ||
UnaryCallable<RequestT, ResponseT> callable, RequestT request, ApiCallContext callContext) { | ||
this.callable = callable; | ||
this.request = request; | ||
this.callContext = callContext; | ||
} | ||
|
||
void setExternalFuture(RetryingFuture<ResponseT> externalFuture) { | ||
this.externalFuture = externalFuture; | ||
} | ||
|
||
@Override | ||
public ResponseT call() { | ||
try { | ||
// NOTE: unlike gax's AttemptCallable, this ignores rpc timeouts | ||
externalFuture.setAttemptFuture(new NonCancellableFuture<ResponseT>()); | ||
if (externalFuture.isDone()) { | ||
return null; | ||
} | ||
ApiFuture<ResponseT> internalFuture = callable.futureCall(request, callContext); | ||
externalFuture.setAttemptFuture(internalFuture); | ||
} catch (Throwable e) { | ||
externalFuture.setAttemptFuture(ApiFutures.<ResponseT>immediateFailedFuture(e)); | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* A polling algorithm for waiting for a consistent {@link CheckConsistencyResponse}. Please note | ||
* that this class doesn't handle retryable errors and expects the underlying callable chain to | ||
* handle this. | ||
*/ | ||
private static class PollResultAlgorithm implements | ||
ResultRetryAlgorithm<CheckConsistencyResponse> { | ||
@Override | ||
public TimedAttemptSettings createNextAttempt(Throwable prevThrowable, | ||
CheckConsistencyResponse prevResponse, TimedAttemptSettings prevSettings) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean shouldRetry(Throwable prevThrowable, CheckConsistencyResponse prevResponse) | ||
throws CancellationException { | ||
return prevResponse != null && !prevResponse.getConsistent(); | ||
} | ||
} | ||
} |
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.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.