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

Fix template not equals when setting does not start with "index." #83707

Closed
wants to merge 4 commits into from

Conversation

weizijun
Copy link
Contributor

@weizijun weizijun commented Feb 9, 2022

When create template like this:

PUT _index_template/test
{
  "template": {
    "settings": {
      "number_of_shards": 1
    }
  }
}

The setting does not start with "index.". But ClusterState will add the "index." prefix to the settings.
So the template don't equal the inner template.

In MetadataIndexTemplateServiceTests.testAddComponentTemplate, modify the setting like:

        Template template = new Template(Settings.builder().put("number_of_shards", 1).build(), new CompressedXContent("""
            {"properties":{"@timestamp":{"type":"date"}}}
            """), ComponentTemplateTests.randomAliases());

It will cause the case failed.

I improve the Settings.equals, add the normalizePrefix to the settings before compare.

I fixed two equals case in #77008 and #80864.
This PR is a new case.

@elasticsearchmachine elasticsearchmachine added v8.2.0 external-contributor Pull request authored by a developer outside the Elasticsearch team labels Feb 9, 2022
@weizijun
Copy link
Contributor Author

weizijun commented Feb 9, 2022

hi, @martijnvg, @dakrone can you help to review this PR?

@dakrone dakrone added the :Data Management/Indices APIs APIs to create and manage indices and templates label Feb 10, 2022
@elasticmachine elasticmachine added the Team:Data Management Meta label for data/management team label Feb 10, 2022
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-data-management (Team:Data Management)

@dakrone
Copy link
Member

dakrone commented Feb 10, 2022

I'm not sure about this—on one hand the settings are functionally the same, however, when it comes to the actual structure of JSON and key names, they are not the same. Internally where is this template comparison causing problems? Can you explain where this pops up a bit more?

@dakrone dakrone self-assigned this Feb 10, 2022
@dakrone dakrone self-requested a review February 10, 2022 20:35
@weizijun
Copy link
Contributor Author

I'm not sure about this—on one hand the settings are functionally the same, however, when it comes to the actual structure of JSON and key names, they are not the same. Internally where is this template comparison causing problems? Can you explain where this pops up a bit more?

It's the template equals case in our internal plugin, I found that the template I create is not equal to the template that I get from ClusterState.

The test case: MetadataIndexTemplateServiceTests.testAddComponentTemplate is the case, I add a setting, and the test will failed.

If it is not safe to modify the equals in the setting, I can add a settingsEquals in Template, like mappingsEquals.

@dakrone
Copy link
Member

dakrone commented Feb 14, 2022

It's the template equals case in our internal plugin, I found that the template I create is not equal to the template that I get from ClusterState.

I don't think we should set this precedent, our internal tools should always be allowed to transform things as needed when they are added through the API. We have no guarantees for internal equals usage from plugins.

If it is not safe to modify the equals in the setting, I can add a settingsEquals in Template, like mappingsEquals.

I think doing something like this in your plugin would be a better solution. Changing this behavior in the settings themselves is dangerous and I think will cause problems later down the road. We don't need an additional equals method in Template because they should actually be different when the Settings differ, this is part of why the MetadataIndexTemplateService does its own normalization (changing potentially settings and mappings) prior to doing a comparison to test whether a template has been changed.

@weizijun
Copy link
Contributor Author

I think doing something like this in your plugin would be a better solution. Changing this behavior in the settings themselves is dangerous and I think will cause problems later down the road. We don't need an additional equals method in Template because they should actually be different when the Settings differ, this is part of why the MetadataIndexTemplateService does its own normalization (changing potentially settings and mappings) prior to doing a comparison to test whether a template has been changed.

I change the logic from Settings.equals to Template.settingsEquals.
As Settings don't use only in index, it also used in cluster settings, node settings. It's not suit the change the internal equals method.
I think Template is only used for index, adding a settingsEquals method is reasonable. As ElasticSearch server add the index. prefix internal. This makes the templates different between input and output.

@weizijun
Copy link
Contributor Author

@elasticmachine update branch

@dakrone
Copy link
Member

dakrone commented Feb 16, 2022

I change the logic from Settings.equals to Template.settingsEquals.

I still don't think we should do this in the template, as this is only a problem for plugins and does not affect the API.

Out of curiosity, why can you not normalize/fix the index settings themselves before comparing templates in the first place?

@weizijun
Copy link
Contributor Author

Out of curiosity, why can you not normalize/fix the index settings themselves before comparing templates in the first place?

Yeah, we can do the same logic before execute put template action. But the create template logic change the setting of user input, and cause not equals problem. If it's not fixed now, we can see if the feature is needed later.

@elasticsearchmachine elasticsearchmachine changed the base branch from master to main July 22, 2022 23:09
@mark-vieira mark-vieira added v8.5.0 and removed v8.4.0 labels Jul 27, 2022
@weizijun weizijun closed this Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Data Management/Indices APIs APIs to create and manage indices and templates external-contributor Pull request authored by a developer outside the Elasticsearch team Team:Data Management Meta label for data/management team team-discuss v8.5.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants