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

Sketch out recursive merging for template mappings. #57393

Closed

Conversation

jtibshirani
Copy link
Contributor

@jtibshirani jtibshirani commented May 30, 2020

Note: this is just a sketch, and not an actual PR for review.

This is a prototype for how we could support recursive merging for composable template mappings. I wrote this really quickly, we may want to factor things differently (also there are no tests!)

The overall approach:

  • When creating the index, each mapping definition is parsed into a map. Then they are merged into the index service one by one, using the standard way to add mappings MapperService#merge.
  • When performing the merge, we track the ‘reason’ so we know that this is an index template merge.
  • Most mapper merge logic stays the same, but certain mappers adjust their behavior. For example, ObjectMapper replaces leaf fields instead of merging them.

I really liked that this approach unifies all mapping merging under the same set of concepts. Before, we treated composable template mappings as raw maps, and had custom logic for deduplicating dynamic templates and resolving dots in field names.

assert mergeIntoMapper instanceof FieldMapper || mergeIntoMapper instanceof FieldAliasMapper;
// If we're merging template mappings when creating an index, then a field definition always
// replaces an existing one.
if (reason == MergeReason.INDEX_TEMPLATE) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the most interesting part, we decide to overwrite leaf fields instead of merging them.

@jtibshirani
Copy link
Contributor Author

We've decided to go with this approach. I'm closing out this draft and will open a new PR for review.

@jtibshirani jtibshirani closed this Jun 3, 2020
@jtibshirani jtibshirani deleted the merge-template-mapping branch June 3, 2020 19:11
dakrone added a commit to dakrone/elasticsearch that referenced this pull request Jun 4, 2020
This commit changes the merge strategy introduced in elastic#55607 and elastic#55982. Instead of overwriting these
fields, we now prevent them from being merged with an exception when a user attempts to
overwrite a field.

As part of this, a more robust validation has been added. The existing validation checked whether
templates (composable and component) were valid on their own, this new validation now checks that
the composite template (mappings/settings/aliases) is valid. This means that when a composable
template is added or updated, we confirm that it is valid with its component pieces. When a
component template is updated we ensure that all composable templates that make use of the component
template continue to be valid before allowing the component template to be updated.

This change also necessitated changes in the tests, however, I have left tests that exercise mapping
merging with nested object fields as `@AwaitsFix`, as we intend to change the behavior soon to allow
merging in a recursive-with-replacement fashion (see: elastic#57393). I have added tests that check the new
disallowing behavior in the meantime.
dakrone added a commit that referenced this pull request Jun 8, 2020
)

* Disallow merging existing mapping field definitions in templates

This commit changes the merge strategy introduced in #55607 and #55982. Instead of overwriting these
fields, we now prevent them from being merged with an exception when a user attempts to
overwrite a field.

As part of this, a more robust validation has been added. The existing validation checked whether
templates (composable and component) were valid on their own, this new validation now checks that
the composite template (mappings/settings/aliases) is valid. This means that when a composable
template is added or updated, we confirm that it is valid with its component pieces. When a
component template is updated we ensure that all composable templates that make use of the component
template continue to be valid before allowing the component template to be updated.

This change also necessitated changes in the tests, however, I have left tests that exercise mapping
merging with nested object fields as `@AwaitsFix`, as we intend to change the behavior soon to allow
merging in a recursive-with-replacement fashion (see: #57393). I have added tests that check the new
disallowing behavior in the meantime.

* Use functional instead of imperative prefix collection

* Use IndexService.withTempIndexService

* Rename tests

* Fix tests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
dakrone added a commit to dakrone/elasticsearch that referenced this pull request Jun 8, 2020
…stic#57701)

* Disallow merging existing mapping field definitions in templates

This commit changes the merge strategy introduced in elastic#55607 and elastic#55982. Instead of overwriting these
fields, we now prevent them from being merged with an exception when a user attempts to
overwrite a field.

As part of this, a more robust validation has been added. The existing validation checked whether
templates (composable and component) were valid on their own, this new validation now checks that
the composite template (mappings/settings/aliases) is valid. This means that when a composable
template is added or updated, we confirm that it is valid with its component pieces. When a
component template is updated we ensure that all composable templates that make use of the component
template continue to be valid before allowing the component template to be updated.

This change also necessitated changes in the tests, however, I have left tests that exercise mapping
merging with nested object fields as `@AwaitsFix`, as we intend to change the behavior soon to allow
merging in a recursive-with-replacement fashion (see: elastic#57393). I have added tests that check the new
disallowing behavior in the meantime.

* Use functional instead of imperative prefix collection

* Use IndexService.withTempIndexService

* Rename tests

* Fix tests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
dakrone added a commit to dakrone/elasticsearch that referenced this pull request Jun 8, 2020
…stic#57701)

* Disallow merging existing mapping field definitions in templates

This commit changes the merge strategy introduced in elastic#55607 and elastic#55982. Instead of overwriting these
fields, we now prevent them from being merged with an exception when a user attempts to
overwrite a field.

As part of this, a more robust validation has been added. The existing validation checked whether
templates (composable and component) were valid on their own, this new validation now checks that
the composite template (mappings/settings/aliases) is valid. This means that when a composable
template is added or updated, we confirm that it is valid with its component pieces. When a
component template is updated we ensure that all composable templates that make use of the component
template continue to be valid before allowing the component template to be updated.

This change also necessitated changes in the tests, however, I have left tests that exercise mapping
merging with nested object fields as `@AwaitsFix`, as we intend to change the behavior soon to allow
merging in a recursive-with-replacement fashion (see: elastic#57393). I have added tests that check the new
disallowing behavior in the meantime.

* Use functional instead of imperative prefix collection

* Use IndexService.withTempIndexService

* Rename tests

* Fix tests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant