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

Remove direct ClusterState access in LocalClusterState #2696

Merged

Conversation

fddattal
Copy link
Contributor

@fddattal fddattal commented May 24, 2024

Description

This commit refactors the LocalClusterState singleton to instead access cluster state through the org.opensearch.client.Client abstract interface.

Issues Resolved

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented.
    • New functionality has javadoc added
    • New functionality has user manual doc added
  • Commits are signed per the DCO using --signoff

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.

Copy link

codecov bot commented May 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.33%. Comparing base (3a28d2a) to head (bc189aa).
Report is 2 commits behind head on main.

Current head bc189aa differs from pull request most recent head e67da5f

Please upload reports for the commit e67da5f to get more accurate results.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2696      +/-   ##
============================================
- Coverage     95.33%   95.33%   -0.01%     
- Complexity     5089     5097       +8     
============================================
  Files           494      496       +2     
  Lines         14287    14297      +10     
  Branches        957      957              
============================================
+ Hits          13621    13630       +9     
- Misses          645      646       +1     
  Partials         21       21              
Flag Coverage Δ
sql-engine 95.33% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

Looks like you've got some spotless violations here:

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':spotlessJavaCheck'.
> The following files had format violations:
      legacy/src/main/java/org/opensearch/sql/legacy/esdomain/LocalClusterState.java

@@ -202,6 +201,7 @@ public Collection<Object> createComponents(
dataSourceService.createDataSource(defaultOpenSearchDataSourceMetadata());
LocalClusterState.state().setClusterService(clusterService);
LocalClusterState.state().setPluginSettings((OpenSearchSettings) pluginSettings);
LocalClusterState.state().setClient(client);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"LocalClusterState" is a misnomer now that it is just a pass-through to making a call via the Client, though renaming might make this change more intrusive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.settings.Setting;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.sql.common.setting.Settings;
import org.opensearch.sql.legacy.esdomain.mapping.IndexMappings;
import org.opensearch.sql.opensearch.setting.OpenSearchSettings;

import java.util.Arrays;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should generally avoid re-ordering imports unless the new order follows a project-wide convention.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! I'll submit a new revision with this change reverted

* Sets the plugin's settings.
* @param settings The non-null plugin settings instance
*/
public void setPluginSettings(@NonNull OpenSearchSettings settings) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly outside the scope of this PR, but it's not immediately obvious why this class is registering a settings update consumer and maintaining it's local cache of settings in latestSettings. It appears to be duplicating what is already happening in OpenSearchSettings. I wonder if getSettingValue() can be changed to just be a pass-through to OpenSearchSettings and all usage of the ClusterService can be removed from this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the feeling that you're right, but I also haven't spent enough time around it to be sure

public void setResolver(IndexNameExpressionResolver resolver) {
this.resolver = resolver;
}

private LocalClusterState() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No-op method, do we need this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm keeping it here to keep the constructor visibility rules the same. We could also use lombok to add an auto-generated private constructor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I've decided to use lombok to generate the constructor instead

} else {
mappings = findMappings(state, concreteIndices, fieldFilter);
}
Map<String, MappingMetadata> mappingMetadata = client.admin()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying to understand, we are removing SQL cached mappings and always fetch it from client.
Why do we always want to fetch it from client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mappings are already cached locally in the cluster state. By fetching via the client we are removing the data synchronization issues between cluster service and this class and avoiding the need for push based notifications to keep data in sync

Map<String, MappingMetadata> mappingMetadata = client.admin()
.indices()
.prepareGetMappings(indices)
.setLocal(true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume this always gets mappings from local node, what if the data is stale?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the data is stale, it will behave as it does today as even today's access is resolved locally.

That being said, it will fail requests with a 4xx as the fields are missing

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can accept this as it is the same as V2. Any thoughts on the long-term implications if schema freshness is critical?

@fddattal fddattal force-pushed the local_cluster_state_refactor_main branch from bc189aa to e67da5f Compare May 30, 2024 19:38
Copy link
Member

@rupal-bq rupal-bq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks for the changes.

Copy link
Collaborator

@penghuo penghuo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx!

Map<String, MappingMetadata> mappingMetadata = client.admin()
.indices()
.prepareGetMappings(indices)
.setLocal(true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can accept this as it is the same as V2. Any thoughts on the long-term implications if schema freshness is critical?

@penghuo penghuo merged commit 3f1e3bd into opensearch-project:main Jun 6, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants