-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add builder for EngineConfig #5618
Add builder for EngineConfig #5618
Conversation
Signed-off-by: Sachin Kale <kalsac@amazon.com>
Signed-off-by: Sachin Kale <kalsac@amazon.com>
Gradle Check (Jenkins) Run Completed with:
|
Gradle Check (Jenkins) Run Completed with:
|
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.
Thanks for doing this! Just a few nitpicks.
return this; | ||
} | ||
|
||
public EngineConfig createEngineConfig() { |
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.
Nitpick, but create
would be fine here since "engine config" is in the class name
} | ||
|
||
public EngineConfig createEngineConfig() { | ||
return new EngineConfig( |
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.
Another nitpick...assuming this constructor can be made private, then you can pass the this
instance of EngineConfig.Builder and let the constructor read the fields. It's a bit more concise and makes adding a new field require fewer changes.
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 like this. Will change in next commit.
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.
+1 for the review comment from @andrross. Without a private constructor here, the builder for EngineConfig
can be bypassed.
private Supplier<RetentionLeases> retentionLeasesSupplier; | ||
private LongSupplier primaryTermSupplier; | ||
private TombstoneDocSupplier tombstoneDocSupplier; | ||
private TranslogDeletionPolicyFactory translogDeletionPolicyFactory = null; |
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.
All the others fields are implicitly assigned to null. Can probably remove this for consistency.
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.
Thanks @sachinpkale for this change. This improves the readability and removes lot of unnecessary code.
private LongSupplier primaryTermSupplier; | ||
private TombstoneDocSupplier tombstoneDocSupplier; | ||
private TranslogDeletionPolicyFactory translogDeletionPolicyFactory = null; | ||
private boolean isReadOnlyReplica = false; |
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.
isReadOnlyReplica
will have false
as default, so it can be removed as well.
EngineConfig config = new EngineConfig.Builder().setShardId(null) | ||
.setThreadPool(null) | ||
.setIndexSettings(defaultIndexSettings) | ||
.setWarmer(null) | ||
.setStore(null) | ||
.setMergePolicy(null) | ||
.setAnalyzer(null) | ||
.setSimilarity(null) | ||
.setCodecService(null) | ||
.setEventListener(null) | ||
.setQueryCache(null) | ||
.setQueryCachingPolicy(null) | ||
.setTranslogConfig(null) | ||
.setFlushMergesAfter(null) | ||
.setExternalRefreshListener(null) | ||
.setInternalRefreshListener(null) | ||
.setIndexSort(null) | ||
.setCircuitBreakerService(null) | ||
.setGlobalCheckpointSupplier(null) | ||
.setRetentionLeasesSupplier(() -> RetentionLeases.EMPTY) | ||
.setPrimaryTermSupplier(null) | ||
.setTombstoneDocSupplier(null) |
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.
We dont' need to use setXXX for settings null
values as it is default. This is one of the advantages of using builder
pattern as it replaces large constructor with default values.
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.
Thanks @dreamer-89 . I used IntelliJ's Replace constructor with builder
and did not pay much attention to these optimizations. Will take care of these in next commit.
return new EngineConfig.Builder().setShardId(null) | ||
.setThreadPool(null) | ||
.setIndexSettings(indexSettings) | ||
.setWarmer(null) | ||
.setStore(null) | ||
.setMergePolicy(null) | ||
.setAnalyzer(null) | ||
.setSimilarity(null) | ||
.setCodecService(null) | ||
.setEventListener(null) | ||
.setQueryCache(null) | ||
.setQueryCachingPolicy(null) | ||
.setTranslogConfig(null) | ||
.setFlushMergesAfter(null) | ||
.setExternalRefreshListener(null) | ||
.setInternalRefreshListener(null) | ||
.setIndexSort(null) | ||
.setCircuitBreakerService(null) | ||
.setGlobalCheckpointSupplier(null) | ||
.setRetentionLeasesSupplier(() -> RetentionLeases.EMPTY) | ||
.setPrimaryTermSupplier(null) | ||
.setTombstoneDocSupplier(null) | ||
.setIsReadOnlyReplica(true) | ||
.createEngineConfig(); |
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.
Same as above.
EngineConfig config = new EngineConfig.Builder().setShardId(shardId) | ||
.setThreadPool(threadPool) | ||
.setIndexSettings(indexSettings) | ||
.setWarmer(null) |
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.
This and other fields set to null can be omitted as mentioned above.
Signed-off-by: Sachin Kale <kalsac@amazon.com>
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.
Suggested another refactoring, but I'll leave it up to you whether to do it here or in a follow up.
config.getPrimaryTermSupplier(), | ||
config.getTombstoneDocSupplier() | ||
); | ||
EngineConfig configWithCustomTranslogDeletionPolicyFactory = new EngineConfig.Builder().setShardId(config.getShardId()) |
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.
There are multiple cases of taking an existing config and making a small change to it in these tests. What do you think about adding a toBuilder()
method that would make this more concise and more clear. This case would look like:
config.toBuilder().setTranslogDeletionPolicyFactory(translogDeletionPolicyFactory).create();
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.
Agree, but changing all these tests to use toBuilder
would take some time. I would like to do it as a follow-up.
I am also thinking of using Lombok in OpenSearch which will get rid of such boilerplate code (in other classes as well). I will create a tracking issue for the same.
Gradle Check (Jenkins) Run Completed with:
|
return this; | ||
} | ||
|
||
public Builder setTranslogFactory(TranslogFactory translogFactory) { |
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.
Nitpick:
For the setters in a builder we usually drop the set
prefix in favour of a fluent design
i.e. public Builder translogFactory(TranslogFactory translogFactory)
over public Builder setTranslogFactory(TranslogFactory translogFactory)
Signed-off-by: Sachin Kale <kalsac@amazon.com>
Gradle Check (Jenkins) Run Completed with:
|
Codecov Report
@@ Coverage Diff @@
## main #5618 +/- ##
============================================
+ Coverage 70.84% 70.88% +0.03%
+ Complexity 58296 58294 -2
============================================
Files 4741 4741
Lines 278541 278642 +101
Branches 40268 40268
============================================
+ Hits 197322 197503 +181
+ Misses 65068 65025 -43
+ Partials 16151 16114 -37
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
* | ||
* @opensearch.internal | ||
*/ | ||
public static class Builder { |
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 this Builder make certain ctor parameters optional? How do we know we don't miss out on all mandatory ones
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.
The build
method still calls the constructor (which is private now) so all the validations on parameters remain same.
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.
Agree on the validation but the notion of what are mandatory parameters and what are optional is diluted. Previously everything was mandatory, but is there something we could do to ease that and make this of the type EngineConfig#builder(mandatory_params).optionalParam(param).build(this)
The backport to
To backport manually, run these commands in your terminal: # Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/backport-2.x 2.x
# Navigate to the new working tree
pushd ../.worktrees/backport-2.x
# Create a new branch
git switch --create backport/backport-5618-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 4fe809784c356d3767d8baddf35d0612e747a0d0
# Push it to GitHub
git push --set-upstream origin backport/backport-5618-to-2.x
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/backport-2.x Then, create a pull request where the |
* Add builder for EngineConfig Signed-off-by: Sachin Kale <kalsac@amazon.com> Signed-off-by: Ashish Singh <ssashish@amazon.com>
* Add builder for EngineConfig Signed-off-by: Sachin Kale <kalsac@amazon.com> Signed-off-by: Ashish Singh <ssashish@amazon.com>
* Add builder for EngineConfig Signed-off-by: Sachin Kale <kalsac@amazon.com> Signed-off-by: Ashish Singh <ssashish@amazon.com>
Signed-off-by: Sachin Kale kalsac@amazon.com
Description
EngineConfig
class has 25+ fields and 3 constructors. Adding any new optional field results in addition of one or more constructors to keep the backward compatibility.Issues Resolved
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.