-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Only store single reference to mapping for indices that share the same mappings in the cluster state #19929
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
Draft
cwperks
wants to merge
4
commits into
opensearch-project:main
Choose a base branch
from
cwperks:mapping-pool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Only store single reference to mapping for indices that share the same mappings in the cluster state #19929
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
server/src/test/java/org/opensearch/cluster/metadata/MetadataMappingTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.metadata; | ||
|
|
||
| import org.opensearch.Version; | ||
| import org.opensearch.common.compress.CompressedXContent; | ||
| import org.opensearch.common.io.stream.BytesStreamOutput; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.core.common.io.stream.NamedWriteableAwareStreamInput; | ||
| import org.opensearch.core.common.io.stream.NamedWriteableRegistry; | ||
| import org.opensearch.core.common.io.stream.StreamInput; | ||
| import org.opensearch.test.OpenSearchTestCase; | ||
|
|
||
| public class MetadataMappingTests extends OpenSearchTestCase { | ||
|
|
||
| private static final String MAPPING_JSON = """ | ||
| { | ||
| "properties": { | ||
| "title": { "type": "keyword" }, | ||
| "year": { "type": "integer" } | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| private static IndexMetadata newIndexWithMapping(String name, String mappingJson) throws Exception { | ||
| MappingMetadata mm = new MappingMetadata(new CompressedXContent(mappingJson)); | ||
| return IndexMetadata.builder(name) | ||
| .settings( | ||
| Settings.builder() | ||
| .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT.id) // or your current | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) | ||
| .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) | ||
| ) | ||
| .putMapping(mm) | ||
| .build(); | ||
| } | ||
|
|
||
| private static CompressedXContent cx(String json) { | ||
| try { | ||
| return new CompressedXContent(json); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public void testDedupsIdenticalMappingsByIdentity() throws Exception { | ||
| Metadata.Builder mb = Metadata.builder(); | ||
| IndexMetadata idx1 = newIndexWithMapping("idx-000001", MAPPING_JSON); | ||
| IndexMetadata idx2 = newIndexWithMapping("idx-000002", MAPPING_JSON + ""); // intentional cloning | ||
| mb.put(idx1, false); | ||
| mb.put(idx2, false); | ||
|
|
||
| Metadata md = mb.build(); | ||
|
|
||
| CompressedXContent aAfter = md.index("idx-000001").mapping().source(); | ||
| CompressedXContent bAfter = md.index("idx-000002").mapping().source(); | ||
|
|
||
| // Identity must be the same now | ||
| assertSame("De-duplicating should make both point to same instance", aAfter, bAfter); | ||
| } | ||
|
|
||
| public void testPartialSharingOnlyDedupsMatchingOnes() throws Exception { | ||
| String different = """ | ||
| {"properties":{"title":{"type":"keyword"},"score":{"type":"float"}}} | ||
| """; | ||
|
|
||
| Metadata md = Metadata.builder() | ||
| .put(newIndexWithMapping("idx-A", MAPPING_JSON), false) | ||
| .put(newIndexWithMapping("idx-B", MAPPING_JSON + ""), false) // intentional cloning | ||
| .put(newIndexWithMapping("idx-C", different), false) | ||
| .build(); | ||
|
|
||
| var aSrc = md.index("idx-A").mapping().source(); | ||
| var bSrc = md.index("idx-B").mapping().source(); | ||
| var cSrc = md.index("idx-C").mapping().source(); | ||
|
|
||
| assertSame("A and B should share the same canonical instance", aSrc, bSrc); | ||
| assertNotSame("C should not share with A/B", aSrc, cSrc); | ||
| } | ||
|
|
||
| public void testWriteReadRoundTripDedupsMappings() throws Exception { | ||
| // Build once | ||
| var builder = Metadata.builder() | ||
| .put(newIndexWithMapping("i1", MAPPING_JSON), false) | ||
| .put(newIndexWithMapping("i2", MAPPING_JSON + ""), false); // intentional cloning | ||
|
|
||
| Metadata md1 = builder.build(); | ||
| var s1 = md1.index("i1").mapping().source(); | ||
| var s2 = md1.index("i2").mapping().source(); | ||
| assertSame(s1, s2); | ||
|
|
||
| BytesStreamOutput out = new BytesStreamOutput(); | ||
| md1.writeTo(out); | ||
|
|
||
| // Minimal registry: only IndexGraveyard (the default Custom in Builder) | ||
| NamedWriteableRegistry registry = new NamedWriteableRegistry( | ||
| java.util.List.of(new NamedWriteableRegistry.Entry(Metadata.Custom.class, IndexGraveyard.TYPE, IndexGraveyard::new)) | ||
| ); | ||
|
|
||
| StreamInput in = new NamedWriteableAwareStreamInput(out.bytes().streamInput(), registry); | ||
| Metadata md2 = Metadata.readFrom(in); | ||
|
|
||
| var t1 = md2.index("i1").mapping().source(); | ||
| var t2 = md2.index("i2").mapping().source(); | ||
| // After readFrom(), builder.build() was called internally; de-duplicating should apply again. | ||
| assertSame("Round-trip should still end up deduplicated", t1, t2); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't see a case where we are deleting a index, because it can happen that the mappings object we are holding in memory is deleted as part of the delete index call, then all other indices with same mapping pointing to null effectively.
Is that not the case ?
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 logic added here is within the metadata builder so the indices object in this builder would be only the present indices. I need to check, but on index deletion it would create a new version of the metadata using this builder with all of the indices except the one that has been deleted. This function is then called in
build()to de-dup the mappings so that we don't hold multiple versions of the same mappings in-memory for the new in-memory Metadata.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.
Relevant code: https://github.com/opensearch-project/OpenSearch/blob/main/server/src/main/java/org/opensearch/cluster/metadata/MetadataDeleteIndexService.java#L159-L169