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
feat: add buffering layer to BulkWriter #611
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,10 +25,13 @@ | |
import com.google.api.core.ApiFutures; | ||
import com.google.api.core.BetaApi; | ||
import com.google.api.core.SettableApiFuture; | ||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.api.gax.rpc.StatusCode.Code; | ||
import com.google.cloud.firestore.v1.FirestoreSettings; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
|
@@ -110,6 +113,14 @@ enum OperationType { | |
*/ | ||
private static final int RATE_LIMITER_MULTIPLIER_MILLIS = 5 * 60 * 1000; | ||
|
||
/** | ||
* The default maximum number of pending operations that can be enqueued onto a BulkWriter | ||
* instance. An operation is considered pending if BulkWriter has sent it via RPC and is awaiting | ||
* the result. BulkWriter buffers additional writes after this many pending operations in order to | ||
* avoiding going OOM. | ||
*/ | ||
private static final int DEFAULT_MAXIMUM_PENDING_OPERATIONS_COUNT = 500; | ||
|
||
/** | ||
* The default jitter to apply to the exponential backoff used in retries. For example, a factor | ||
* of 0.3 means a 30% jitter is applied. | ||
|
@@ -158,6 +169,26 @@ public boolean onError(BulkWriterException error) { | |
@GuardedBy("lock") | ||
private final RateLimiter rateLimiter; | ||
|
||
/** | ||
* The number of pending operations enqueued on this BulkWriter instance. An operation is | ||
* considered pending if BulkWriter has sent it via RPC and is awaiting the result. | ||
*/ | ||
@GuardedBy("lock") | ||
private int pendingOpsCount = 0; | ||
|
||
/** | ||
* An array containing buffered BulkWriter operations after the maximum number of pending | ||
* operations has been enqueued. | ||
*/ | ||
@GuardedBy("lock") | ||
private final List<Runnable> bufferedOperations = new ArrayList<>(); | ||
|
||
/** | ||
* The maximum number of pending operations that can be enqueued onto this BulkWriter instance. | ||
* Once the this number of writes have been enqueued, subsequent writes are buffered. | ||
*/ | ||
private int maxPendingOpCount = DEFAULT_MAXIMUM_PENDING_OPERATIONS_COUNT; | ||
|
||
/** | ||
* The batch that is currently used to schedule operations. Once this batch reaches maximum | ||
* capacity, a new batch is created. | ||
|
@@ -627,7 +658,7 @@ private ApiFuture<WriteResult> executeWrite( | |
final DocumentReference documentReference, | ||
final OperationType operationType, | ||
final ApiFunction<BulkCommitBatch, ApiFuture<WriteResult>> enqueueOperationOnBatchCallback) { | ||
BulkWriterOperation operation = | ||
final BulkWriterOperation operation = | ||
new BulkWriterOperation( | ||
documentReference, | ||
operationType, | ||
|
@@ -660,10 +691,72 @@ public ApiFuture<Boolean> apply(BulkWriterException e) { | |
synchronized (lock) { | ||
verifyNotClosedLocked(); | ||
writesEnqueued = true; | ||
sendOperationLocked(enqueueOperationOnBatchCallback, operation); | ||
|
||
// Advance the lastOperation pointer. This ensures that lastOperation only completes when | ||
// both the previous and the current write complete. | ||
lastOperation = | ||
ApiFutures.transformAsync( | ||
lastOperation, | ||
new ApiAsyncFunction<Void, Void>() { | ||
@Override | ||
public ApiFuture<Void> apply(Void aVoid) { | ||
return silenceFuture(operation.getFuture()); | ||
} | ||
}, | ||
MoreExecutors.directExecutor()); | ||
|
||
// Schedule the operation if the BulkWriter has fewer than the maximum number of allowed | ||
// pending operations, or add the operation to the buffer. | ||
if (pendingOpsCount < maxPendingOpCount) { | ||
pendingOpsCount++; | ||
sendOperationLocked(enqueueOperationOnBatchCallback, operation); | ||
} else { | ||
bufferedOperations.add( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
synchronized (lock) { | ||
pendingOpsCount++; | ||
sendOperationLocked(enqueueOperationOnBatchCallback, operation); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
return operation.getFuture(); | ||
ApiFuture<WriteResult> transformedFuture = | ||
ApiFutures.transformAsync( | ||
operation.getFuture(), | ||
new ApiAsyncFunction<WriteResult, WriteResult>() { | ||
public ApiFuture<WriteResult> apply(WriteResult result) throws Exception { | ||
pendingOpsCount--; | ||
processBufferedOperations(); | ||
return ApiFutures.immediateFuture(result); | ||
} | ||
}); | ||
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. Does this not need an executor for the non-deprecated version? 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. added executor, thanks for catching! |
||
|
||
return ApiFutures.catchingAsync( | ||
transformedFuture, | ||
ApiException.class, | ||
new ApiAsyncFunction<ApiException, WriteResult>() { | ||
public ApiFuture<WriteResult> apply(ApiException e) throws Exception { | ||
pendingOpsCount--; | ||
processBufferedOperations(); | ||
throw e; | ||
} | ||
}, | ||
MoreExecutors.directExecutor()); | ||
} | ||
|
||
/** | ||
* Manages the pending operation counter and schedules the next BulkWriter operation if we're | ||
* under the maximum limit. | ||
*/ | ||
private void processBufferedOperations() { | ||
if (pendingOpsCount < maxPendingOpCount && bufferedOperations.size() > 0) { | ||
Runnable nextOp = bufferedOperations.remove(0); | ||
nextOp.run(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -927,6 +1020,16 @@ RateLimiter getRateLimiter() { | |
return rateLimiter; | ||
} | ||
|
||
@VisibleForTesting | ||
int getBufferedOperationsCount() { | ||
return bufferedOperations.size(); | ||
} | ||
|
||
@VisibleForTesting | ||
void setMaxPendingOpCount(int newMax) { | ||
maxPendingOpCount = newMax; | ||
} | ||
|
||
/** | ||
* Schedules the provided operations on the current BulkCommitBatch. Sends the BulkCommitBatch if | ||
* it reaches maximum capacity. | ||
|
@@ -946,17 +1049,6 @@ private void sendOperationLocked( | |
bulkCommitBatch.enqueueOperation(op); | ||
enqueueOperationOnBatchCallback.apply(bulkCommitBatch); | ||
|
||
lastOperation = | ||
ApiFutures.transformAsync( | ||
lastOperation, | ||
new ApiAsyncFunction<Void, Void>() { | ||
@Override | ||
public ApiFuture<Void> apply(Void aVoid) { | ||
return silenceFuture(op.getFuture()); | ||
} | ||
}, | ||
MoreExecutors.directExecutor()); | ||
|
||
if (bulkCommitBatch.getMutationsSize() == maxBatchSize) { | ||
scheduleCurrentBatchLocked(/* flush= */ false); | ||
} | ||
|
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
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.
A slightly less generic name would do wonders here :)
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.
renamed to
processedOperationFuture
.