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 validation on context template removal #15608

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.opensearch.common.Priority;
import org.opensearch.common.UUIDs;
import org.opensearch.common.ValidationException;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.compress.CompressedXContent;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.logging.HeaderWarning;
Expand Down Expand Up @@ -474,6 +475,29 @@
+ templatesStillUsing
);
}

validateNoIndexUsesContext(metadata, matchingComponentTemplates);
}

Check warning on line 480 in server/src/main/java/org/opensearch/cluster/metadata/MetadataIndexTemplateService.java

View check run for this annotation

Codecov / codecov/patch

server/src/main/java/org/opensearch/cluster/metadata/MetadataIndexTemplateService.java#L479-L480

Added lines #L479 - L480 were not covered by tests

/**
* Validate that a system template is not being used directly by an index.
*/
static void validateNoIndexUsesContext(Metadata metadata, Set<String> matchingComponentTemplates) {
List<Tuple<String, String>> indicesUsingSpecifiedContexts = metadata.indices()
.values()
.stream()
.filter(imd -> imd.context() != null)
.filter(imd -> matchingComponentTemplates.contains(findContextTemplateName(metadata, imd.context())))
.map(imd -> Tuple.tuple(imd.getIndex().getName(), findContextTemplateName(metadata, imd.context())))
.collect(Collectors.toList());

if (!indicesUsingSpecifiedContexts.isEmpty()) {
throw new IllegalArgumentException(
"Cannot process request to remove component templates as they are being used by indices."
+ "First few indices preventing the deletion are: "
+ indicesUsingSpecifiedContexts.subList(0, Math.min(10, indicesUsingSpecifiedContexts.size()))
);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -2017,6 +2019,43 @@ public void testRemoveComponentTemplateInUse() throws Exception {
);
}

public void testValidateNoIndexUsesContextWithIndexUsingContext() throws Exception {
String contextName = "testcontext";
String indexName = "index";
Metadata metadata = mock(Metadata.class);
when(metadata.systemTemplatesLookup()).thenReturn(Map.of(contextName, new TreeMap<>() {
{
put(1L, contextName);
}
}));

IndexMetadata indexMetadata = mock(IndexMetadata.class);
when(indexMetadata.context()).thenReturn(new Context(contextName));
when(indexMetadata.getIndex()).thenReturn(new Index(indexName, UUID.randomUUID().toString()));
when(metadata.indices()).thenReturn(Map.of(indexName, indexMetadata));

expectThrows(
IllegalArgumentException.class,
() -> MetadataIndexTemplateService.validateNoIndexUsesContext(metadata, Set.of(contextName))
);
}

public void testValidateNoIndexUsesContextWithNoIndexUsingContext() throws Exception {
String contextName = "testcontext";
String indexName = "index";
Metadata metadata = mock(Metadata.class);
when(metadata.systemTemplatesLookup()).thenReturn(Map.of(contextName, new TreeMap<>() {
{
put(1L, contextName);
}
}));

IndexMetadata indexMetadata = mock(IndexMetadata.class);
when(metadata.indices()).thenReturn(Map.of(indexName, indexMetadata));

MetadataIndexTemplateService.validateNoIndexUsesContext(metadata, Set.of(contextName));
}

/**
* Tests that we check that settings/mappings/etc are valid even after template composition,
* when adding/updating a composable index template
Expand Down
Loading