-
Notifications
You must be signed in to change notification settings - Fork 117
Add support for MDC context passing into the throttled iterator #3740
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
base: main
Are you sure you want to change the base?
Changes from all commits
23c0421
5045719
f9fc823
a8ae709
533334c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| import javax.annotation.Nullable; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.concurrent.Executor; | ||
|
|
@@ -389,14 +390,6 @@ void init() { | |
| } | ||
| } | ||
|
|
||
| public static <T> Builder<T> builder(TransactionalRunner runner, | ||
| Executor executor, | ||
| ScheduledExecutorService scheduledExecutor, | ||
| CursorFactory<T> cursorCreator, | ||
| ItemHandler<T> singleItemHandler) { | ||
| return new Builder<>(runner, executor, scheduledExecutor, cursorCreator, singleItemHandler); | ||
| } | ||
|
|
||
| public static <T> Builder<T> builder(FDBDatabase database, | ||
| CursorFactory<T> cursorCreator, | ||
| ItemHandler<T> singleItemHandler) { | ||
|
|
@@ -409,9 +402,13 @@ public static <T> Builder<T> builder(FDBDatabase database, | |
| * @param <T> the item type being iterated on. | ||
| */ | ||
| public static class Builder<T> { | ||
| private final TransactionalRunner transactionalRunner; | ||
| private final Executor executor; | ||
| private final ScheduledExecutorService scheduledExecutor; | ||
| // Fields constructed during build() | ||
| private TransactionalRunner transactionalRunner; | ||
| private Executor executor; | ||
| private ScheduledExecutorService scheduledExecutor; | ||
| // Fields initialized by setters/constructor | ||
| private FDBDatabase database; | ||
| private FDBRecordContextConfig.Builder contextConfigBuilder; | ||
| private final CursorFactory<T> cursorCreator; | ||
| private final ItemHandler<T> singleItemHandler; | ||
| private Consumer<QuotaManager> transactionSuccessNotification; | ||
|
|
@@ -422,17 +419,10 @@ public static class Builder<T> { | |
| private int maxRecordDeletesPerSec; | ||
| private int numOfRetries; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * @param runner the FDB runner to use when creating transactions | ||
| * @param cursorCreator the factory to use when creating the inner cursor | ||
| * @param singleItemHandler the handler of a single item while iterating | ||
| */ | ||
| private Builder(TransactionalRunner runner, Executor executor, ScheduledExecutorService scheduledExecutor, CursorFactory<T> cursorCreator, ItemHandler<T> singleItemHandler) { | ||
| private Builder(FDBDatabase database, FDBRecordContextConfig.Builder contextConfigBuilder, CursorFactory<T> cursorCreator, ItemHandler<T> singleItemHandler) { | ||
| // Mandatory fields are set in the constructor. Everything else is optional. | ||
| this.transactionalRunner = runner; | ||
| this.executor = executor; | ||
| this.scheduledExecutor = scheduledExecutor; | ||
| this.database = database; | ||
| this.contextConfigBuilder = contextConfigBuilder; | ||
| this.cursorCreator = cursorCreator; | ||
| this.singleItemHandler = singleItemHandler; | ||
| // set defaults | ||
|
|
@@ -443,14 +433,6 @@ private Builder(TransactionalRunner runner, Executor executor, ScheduledExecutor | |
| this.numOfRetries = NUMBER_OF_RETRIES; | ||
| } | ||
|
|
||
| private Builder(FDBDatabase database, FDBRecordContextConfig.Builder contextConfigBuilder, CursorFactory<T> cursorCreator, ItemHandler<T> singleItemHandler) { | ||
| this(new TransactionalRunner(database, contextConfigBuilder), | ||
| database.newContextExecutor(contextConfigBuilder.getMdcContext()), | ||
| database.getScheduledExecutor(), | ||
| cursorCreator, | ||
| singleItemHandler); | ||
| } | ||
|
|
||
| /** | ||
| * Set the amount of time for each transaction before committing and starting another. | ||
| * Defaults to 4000. | ||
|
|
@@ -538,11 +520,27 @@ public Builder<T> withNumOfRetries(int numOfRetries) { | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Set the MDC context for the runner/executor. | ||
| * This MDC context will be carried out into the runner and executor and will allow them to pass that down to | ||
| * LOGGER calls used by the item handlers. | ||
| * Defaults to empty context. | ||
| * @param mdcContext the MDC context to use | ||
| * @return this builder | ||
| */ | ||
| public Builder<T> withMdcContext(Map<String, String> mdcContext) { | ||
| this.contextConfigBuilder.setMdcContext(mdcContext); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Create the iterator. | ||
| * @return the newly minted iterator | ||
| */ | ||
| public ThrottledRetryingIterator<T> build() { | ||
| this.transactionalRunner = new TransactionalRunner(database, contextConfigBuilder); | ||
|
Collaborator
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. If the constructor that takes a
Contributor
Author
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. Yeah, deleted (should have been deleted earlier). |
||
| this.executor = database.newContextExecutor(contextConfigBuilder.getMdcContext()); | ||
| this.scheduledExecutor = database.getScheduledExecutor(); | ||
| return new ThrottledRetryingIterator<>(this); | ||
| } | ||
| } | ||
|
|
||
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.
I suggest below that you remove the other constructor, but if not, perhaps worth settings these ones in the field declaration, and remove the duplication.
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.
Yes, the other constructor should have been deleted