Skip to content
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

Merged
merged 4 commits into from
Dec 23, 2022

Conversation

sachinpkale
Copy link
Member

@sachinpkale sachinpkale commented Dec 22, 2022

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.
  • To avoid this, we introduced Builder class for EngineConfig.

Issues Resolved

  • NA

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff
  • Commit changes are listed out in CHANGELOG.md file (See: Changelog)

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.

Sachin Kale added 2 commits December 22, 2022 21:58
Signed-off-by: Sachin Kale <kalsac@amazon.com>
Signed-off-by: Sachin Kale <kalsac@amazon.com>
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

  • RESULT: UNSTABLE ❕
  • TEST FAILURES:
      1 org.opensearch.index.IndexServiceTests.testAsyncTranslogTrimTaskOnClosedIndex

Copy link
Member

@andrross andrross left a 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() {
Copy link
Member

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(
Copy link
Member

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.

Copy link
Member Author

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.

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;
Copy link
Member

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.

Copy link
Member

@dreamer-89 dreamer-89 left a 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;
Copy link
Member

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.

Comment on lines 36 to 57
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)
Copy link
Member

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.

Copy link
Member Author

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.

Comment on lines 79 to 102
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();
Copy link
Member

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)
Copy link
Member

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>
Copy link
Member

@andrross andrross left a 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())
Copy link
Member

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();

Copy link
Member Author

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.

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

  • RESULT: UNSTABLE ❕
  • TEST FAILURES:
      1 org.opensearch.client.ReindexIT.testReindexTask

return this;
}

public Builder setTranslogFactory(TranslogFactory translogFactory) {

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>
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

  • RESULT: UNSTABLE ❕
  • TEST FAILURES:
      1 org.opensearch.cluster.allocation.AwarenessAllocationIT.testThreeZoneOneReplicaWithForceZoneValueAndLoadAwareness

@codecov-commenter
Copy link

Codecov Report

Merging #5618 (7f77ca6) into main (306b199) will increase coverage by 0.03%.
The diff coverage is 89.68%.

@@             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     
Impacted Files Coverage Δ
...va/org/opensearch/index/engine/EngineTestCase.java 86.07% <80.00%> (-0.35%) ⬇️
...java/org/opensearch/index/engine/EngineConfig.java 98.41% <100.00%> (+1.01%) ⬆️
...g/opensearch/index/engine/EngineConfigFactory.java 92.75% <100.00%> (+4.11%) ⬆️
...n/indices/forcemerge/ForceMergeRequestBuilder.java 0.00% <0.00%> (-75.00%) ⬇️
.../indices/forcemerge/TransportForceMergeAction.java 25.00% <0.00%> (-75.00%) ⬇️
...h/action/ingest/SimulateDocumentVerboseResult.java 60.71% <0.00%> (-39.29%) ⬇️
...arch/search/aggregations/pipeline/LinearModel.java 23.07% <0.00%> (-34.62%) ⬇️
...on/admin/indices/forcemerge/ForceMergeRequest.java 50.00% <0.00%> (-31.58%) ⬇️
...rc/main/java/org/opensearch/ingest/IngestInfo.java 51.72% <0.00%> (-27.59%) ⬇️
...a/org/opensearch/index/mapper/MapperException.java 75.00% <0.00%> (-25.00%) ⬇️
... and 481 more

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 {
Copy link
Collaborator

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

Copy link
Member Author

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.

Copy link
Collaborator

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)

@Bukhtawar Bukhtawar merged commit 4fe8097 into opensearch-project:main Dec 23, 2022
@andrross andrross added the backport 2.x Backport to 2.x branch label Dec 27, 2022
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.x failed:

The process '/usr/bin/git' failed with exit code 128

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 base branch is 2.x and the compare/head branch is backport/backport-5618-to-2.x.

ashking94 pushed a commit to ashking94/OpenSearch that referenced this pull request Jan 18, 2023
* Add builder for EngineConfig

Signed-off-by: Sachin Kale <kalsac@amazon.com>
Signed-off-by: Ashish Singh <ssashish@amazon.com>
ashking94 pushed a commit to ashking94/OpenSearch that referenced this pull request Jan 18, 2023
* Add builder for EngineConfig

Signed-off-by: Sachin Kale <kalsac@amazon.com>
Signed-off-by: Ashish Singh <ssashish@amazon.com>
ashking94 pushed a commit to ashking94/OpenSearch that referenced this pull request Jan 18, 2023
* Add builder for EngineConfig

Signed-off-by: Sachin Kale <kalsac@amazon.com>
Signed-off-by: Ashish Singh <ssashish@amazon.com>
gbbafna pushed a commit that referenced this pull request Jan 18, 2023
* Add builder for EngineConfig

Signed-off-by: Sachin Kale <kalsac@amazon.com>
kotwanikunal pushed a commit that referenced this pull request Jan 25, 2023
* Add builder for EngineConfig

Signed-off-by: Sachin Kale <kalsac@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x Backport to 2.x branch skip-changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants