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 tasks module to define tasks system index #61540

Merged
merged 1 commit into from
Aug 26, 2020
Merged
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
25 changes: 0 additions & 25 deletions modules/tasks/build.gradle

This file was deleted.

This file was deleted.

This file was deleted.

44 changes: 38 additions & 6 deletions server/src/main/java/org/elasticsearch/indices/SystemIndices.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@
import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.index.Index;
import org.elasticsearch.tasks.TaskResultsService;

import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toUnmodifiableList;
import static org.elasticsearch.tasks.TaskResultsService.TASK_INDEX;

/**
* This class holds the {@link SystemIndexDescriptor} objects that represent system indices the
Expand All @@ -45,12 +48,17 @@
*/
public class SystemIndices {

private static final Map<String, Collection<SystemIndexDescriptor>> SERVER_SYSTEM_INDEX_DESCRIPTORS = Map.of(
TaskResultsService.class.getName(), List.of(new SystemIndexDescriptor(TASK_INDEX + "*", "Task Result Index"))
);

private final CharacterRunAutomaton runAutomaton;
private final Collection<SystemIndexDescriptor> systemIndexDescriptors;

public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexDescriptorMap) {
checkForOverlappingPatterns(systemIndexDescriptorMap);
this.systemIndexDescriptors = systemIndexDescriptorMap.values()
public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
final Map<String, Collection<SystemIndexDescriptor>> descriptorsMap = buildSystemIndexDescriptorMap(pluginAndModulesDescriptors);
checkForOverlappingPatterns(descriptorsMap);
this.systemIndexDescriptors = descriptorsMap.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableList());
Expand All @@ -63,7 +71,16 @@ public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexD
* @return true if the {@link Index}'s name matches a pattern from a {@link SystemIndexDescriptor}
*/
public boolean isSystemIndex(Index index) {
return runAutomaton.run(index.getName());
return isSystemIndex(index.getName());
}

/**
* Determines whether a given index is a system index by comparing its name to the collection of loaded {@link SystemIndexDescriptor}s
* @param indexName the index name to check against loaded {@link SystemIndexDescriptor}s
* @return true if the index name matches a pattern from a {@link SystemIndexDescriptor}
*/
public boolean isSystemIndex(String indexName) {
return runAutomaton.run(indexName);
}

/**
Expand Down Expand Up @@ -126,10 +143,10 @@ static void checkForOverlappingPatterns(Map<String, Collection<SystemIndexDescri
.filter(d -> overlaps(descriptorToCheck.v2(), d.v2()))
.collect(Collectors.toUnmodifiableList());
if (descriptorsMatchingThisPattern.isEmpty() == false) {
throw new IllegalStateException("a system index descriptor [" + descriptorToCheck.v2() + "] from plugin [" +
throw new IllegalStateException("a system index descriptor [" + descriptorToCheck.v2() + "] from [" +
descriptorToCheck.v1() + "] overlaps with other system index descriptors: [" +
descriptorsMatchingThisPattern.stream()
.map(descriptor -> descriptor.v2() + " from plugin [" + descriptor.v1() + "]")
.map(descriptor -> descriptor.v2() + " from [" + descriptor.v1() + "]")
.collect(Collectors.joining(", ")));
}
});
Expand All @@ -140,4 +157,19 @@ private static boolean overlaps(SystemIndexDescriptor a1, SystemIndexDescriptor
Automaton a2Automaton = Regex.simpleMatchToAutomaton(a2.getIndexPattern());
return Operations.isEmpty(Operations.intersection(a1Automaton, a2Automaton)) == false;
}

private static Map<String, Collection<SystemIndexDescriptor>> buildSystemIndexDescriptorMap(
Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesMap) {
final Map<String, Collection<SystemIndexDescriptor>> map =
new HashMap<>(pluginAndModulesMap.size() + SERVER_SYSTEM_INDEX_DESCRIPTORS.size());
map.putAll(pluginAndModulesMap);
// put the server items last since we expect less of them
SERVER_SYSTEM_INDEX_DESCRIPTORS.forEach((source, descriptors) -> {
if (map.putIfAbsent(source, descriptors) != null) {
throw new IllegalArgumentException("plugin or module attempted to define the same source [" + source +
"] as a built-in system index");
}
});
return Map.copyOf(map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

package org.elasticsearch.indices;

import org.elasticsearch.tasks.TaskResultsService;
import org.elasticsearch.test.ESTestCase;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.tasks.TaskResultsService.TASK_INDEX;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
Expand All @@ -50,8 +52,8 @@ public void testBasicOverlappingPatterns() {
IllegalStateException exception = expectThrows(IllegalStateException.class,
() -> SystemIndices.checkForOverlappingPatterns(descriptors));
assertThat(exception.getMessage(), containsString("a system index descriptor [" + broadPattern +
"] from plugin [" + broadPatternSource + "] overlaps with other system index descriptors:"));
String fromPluginString = " from plugin [" + otherSource + "]";
"] from [" + broadPatternSource + "] overlaps with other system index descriptors:"));
String fromPluginString = " from [" + otherSource + "]";
assertThat(exception.getMessage(), containsString(overlapping1.toString() + fromPluginString));
assertThat(exception.getMessage(), containsString(overlapping2.toString() + fromPluginString));
assertThat(exception.getMessage(), containsString(overlapping3.toString() + fromPluginString));
Expand All @@ -77,10 +79,25 @@ public void testComplexOverlappingPatterns() {
IllegalStateException exception = expectThrows(IllegalStateException.class,
() -> SystemIndices.checkForOverlappingPatterns(descriptors));
assertThat(exception.getMessage(), containsString("a system index descriptor [" + pattern1 +
"] from plugin [" + source1 + "] overlaps with other system index descriptors:"));
assertThat(exception.getMessage(), containsString(pattern2.toString() + " from plugin [" + source2 + "]"));
"] from [" + source1 + "] overlaps with other system index descriptors:"));
assertThat(exception.getMessage(), containsString(pattern2.toString() + " from [" + source2 + "]"));

IllegalStateException constructorException = expectThrows(IllegalStateException.class, () -> new SystemIndices(descriptors));
assertThat(constructorException.getMessage(), equalTo(exception.getMessage()));
}

public void testBuiltInSystemIndices() {
SystemIndices systemIndices = new SystemIndices(Map.of());
assertTrue(systemIndices.isSystemIndex(".tasks"));
assertTrue(systemIndices.isSystemIndex(".tasks1"));
assertTrue(systemIndices.isSystemIndex(".tasks-old"));
}

public void testPluginCannotOverrideBuiltInSystemIndex() {
Map<String, Collection<SystemIndexDescriptor>> pluginMap = Map.of(
TaskResultsService.class.getName(), List.of(new SystemIndexDescriptor(TASK_INDEX, "Task Result Index"))
);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new SystemIndices(pluginMap));
assertThat(e.getMessage(), containsString("plugin or module attempted to define the same source"));
}
}