Skip to content

Commit 25a870b

Browse files
authored
Remove tasks module to define tasks system index (#61540)
This commit removes the tasks module that only existed to define the tasks result index, `.tasks`, as a system index. The definition for the tasks results system index descriptor is moved to the `SystemIndices` class with a check that no other plugin or module attempts to define an entry with the same source. Additionally, this change also makes the pattern for the tasks result index a wildcard pattern since we will need this when the index is upgraded (reindex to new name and then alias that to .tasks).
1 parent e0ec9ac commit 25a870b

File tree

5 files changed

+59
-109
lines changed

5 files changed

+59
-109
lines changed

modules/tasks/build.gradle

Lines changed: 0 additions & 25 deletions
This file was deleted.

modules/tasks/src/main/java/org/elasticsearch/tasksplugin/TasksPlugin.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

modules/tasks/src/test/java/org/elasticsearch/tasksplugin/TasksPluginTests.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

server/src/main/java/org/elasticsearch/indices/SystemIndices.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@
2828
import org.elasticsearch.common.collect.Tuple;
2929
import org.elasticsearch.common.regex.Regex;
3030
import org.elasticsearch.index.Index;
31+
import org.elasticsearch.tasks.TaskResultsService;
3132

3233
import java.util.Collection;
3334
import java.util.Comparator;
35+
import java.util.HashMap;
3436
import java.util.List;
3537
import java.util.Map;
3638
import java.util.Optional;
3739
import java.util.stream.Collectors;
3840

3941
import static java.util.stream.Collectors.toUnmodifiableList;
42+
import static org.elasticsearch.tasks.TaskResultsService.TASK_INDEX;
4043

4144
/**
4245
* This class holds the {@link SystemIndexDescriptor} objects that represent system indices the
@@ -45,12 +48,17 @@
4548
*/
4649
public class SystemIndices {
4750

51+
private static final Map<String, Collection<SystemIndexDescriptor>> SERVER_SYSTEM_INDEX_DESCRIPTORS = Map.of(
52+
TaskResultsService.class.getName(), List.of(new SystemIndexDescriptor(TASK_INDEX + "*", "Task Result Index"))
53+
);
54+
4855
private final CharacterRunAutomaton runAutomaton;
4956
private final Collection<SystemIndexDescriptor> systemIndexDescriptors;
5057

51-
public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexDescriptorMap) {
52-
checkForOverlappingPatterns(systemIndexDescriptorMap);
53-
this.systemIndexDescriptors = systemIndexDescriptorMap.values()
58+
public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
59+
final Map<String, Collection<SystemIndexDescriptor>> descriptorsMap = buildSystemIndexDescriptorMap(pluginAndModulesDescriptors);
60+
checkForOverlappingPatterns(descriptorsMap);
61+
this.systemIndexDescriptors = descriptorsMap.values()
5462
.stream()
5563
.flatMap(Collection::stream)
5664
.collect(Collectors.toUnmodifiableList());
@@ -63,7 +71,16 @@ public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexD
6371
* @return true if the {@link Index}'s name matches a pattern from a {@link SystemIndexDescriptor}
6472
*/
6573
public boolean isSystemIndex(Index index) {
66-
return runAutomaton.run(index.getName());
74+
return isSystemIndex(index.getName());
75+
}
76+
77+
/**
78+
* Determines whether a given index is a system index by comparing its name to the collection of loaded {@link SystemIndexDescriptor}s
79+
* @param indexName the index name to check against loaded {@link SystemIndexDescriptor}s
80+
* @return true if the index name matches a pattern from a {@link SystemIndexDescriptor}
81+
*/
82+
public boolean isSystemIndex(String indexName) {
83+
return runAutomaton.run(indexName);
6784
}
6885

6986
/**
@@ -126,10 +143,10 @@ static void checkForOverlappingPatterns(Map<String, Collection<SystemIndexDescri
126143
.filter(d -> overlaps(descriptorToCheck.v2(), d.v2()))
127144
.collect(Collectors.toUnmodifiableList());
128145
if (descriptorsMatchingThisPattern.isEmpty() == false) {
129-
throw new IllegalStateException("a system index descriptor [" + descriptorToCheck.v2() + "] from plugin [" +
146+
throw new IllegalStateException("a system index descriptor [" + descriptorToCheck.v2() + "] from [" +
130147
descriptorToCheck.v1() + "] overlaps with other system index descriptors: [" +
131148
descriptorsMatchingThisPattern.stream()
132-
.map(descriptor -> descriptor.v2() + " from plugin [" + descriptor.v1() + "]")
149+
.map(descriptor -> descriptor.v2() + " from [" + descriptor.v1() + "]")
133150
.collect(Collectors.joining(", ")));
134151
}
135152
});
@@ -140,4 +157,19 @@ private static boolean overlaps(SystemIndexDescriptor a1, SystemIndexDescriptor
140157
Automaton a2Automaton = Regex.simpleMatchToAutomaton(a2.getIndexPattern());
141158
return Operations.isEmpty(Operations.intersection(a1Automaton, a2Automaton)) == false;
142159
}
160+
161+
private static Map<String, Collection<SystemIndexDescriptor>> buildSystemIndexDescriptorMap(
162+
Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesMap) {
163+
final Map<String, Collection<SystemIndexDescriptor>> map =
164+
new HashMap<>(pluginAndModulesMap.size() + SERVER_SYSTEM_INDEX_DESCRIPTORS.size());
165+
map.putAll(pluginAndModulesMap);
166+
// put the server items last since we expect less of them
167+
SERVER_SYSTEM_INDEX_DESCRIPTORS.forEach((source, descriptors) -> {
168+
if (map.putIfAbsent(source, descriptors) != null) {
169+
throw new IllegalArgumentException("plugin or module attempted to define the same source [" + source +
170+
"] as a built-in system index");
171+
}
172+
});
173+
return Map.copyOf(map);
174+
}
143175
}

server/src/test/java/org/elasticsearch/indices/SystemIndicesTests.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919

2020
package org.elasticsearch.indices;
2121

22+
import org.elasticsearch.tasks.TaskResultsService;
2223
import org.elasticsearch.test.ESTestCase;
2324

2425
import java.util.Collection;
2526
import java.util.HashMap;
2627
import java.util.List;
2728
import java.util.Map;
2829

30+
import static org.elasticsearch.tasks.TaskResultsService.TASK_INDEX;
2931
import static org.hamcrest.Matchers.containsString;
3032
import static org.hamcrest.Matchers.equalTo;
3133
import static org.hamcrest.Matchers.not;
@@ -50,8 +52,8 @@ public void testBasicOverlappingPatterns() {
5052
IllegalStateException exception = expectThrows(IllegalStateException.class,
5153
() -> SystemIndices.checkForOverlappingPatterns(descriptors));
5254
assertThat(exception.getMessage(), containsString("a system index descriptor [" + broadPattern +
53-
"] from plugin [" + broadPatternSource + "] overlaps with other system index descriptors:"));
54-
String fromPluginString = " from plugin [" + otherSource + "]";
55+
"] from [" + broadPatternSource + "] overlaps with other system index descriptors:"));
56+
String fromPluginString = " from [" + otherSource + "]";
5557
assertThat(exception.getMessage(), containsString(overlapping1.toString() + fromPluginString));
5658
assertThat(exception.getMessage(), containsString(overlapping2.toString() + fromPluginString));
5759
assertThat(exception.getMessage(), containsString(overlapping3.toString() + fromPluginString));
@@ -77,10 +79,25 @@ public void testComplexOverlappingPatterns() {
7779
IllegalStateException exception = expectThrows(IllegalStateException.class,
7880
() -> SystemIndices.checkForOverlappingPatterns(descriptors));
7981
assertThat(exception.getMessage(), containsString("a system index descriptor [" + pattern1 +
80-
"] from plugin [" + source1 + "] overlaps with other system index descriptors:"));
81-
assertThat(exception.getMessage(), containsString(pattern2.toString() + " from plugin [" + source2 + "]"));
82+
"] from [" + source1 + "] overlaps with other system index descriptors:"));
83+
assertThat(exception.getMessage(), containsString(pattern2.toString() + " from [" + source2 + "]"));
8284

8385
IllegalStateException constructorException = expectThrows(IllegalStateException.class, () -> new SystemIndices(descriptors));
8486
assertThat(constructorException.getMessage(), equalTo(exception.getMessage()));
8587
}
88+
89+
public void testBuiltInSystemIndices() {
90+
SystemIndices systemIndices = new SystemIndices(Map.of());
91+
assertTrue(systemIndices.isSystemIndex(".tasks"));
92+
assertTrue(systemIndices.isSystemIndex(".tasks1"));
93+
assertTrue(systemIndices.isSystemIndex(".tasks-old"));
94+
}
95+
96+
public void testPluginCannotOverrideBuiltInSystemIndex() {
97+
Map<String, Collection<SystemIndexDescriptor>> pluginMap = Map.of(
98+
TaskResultsService.class.getName(), List.of(new SystemIndexDescriptor(TASK_INDEX, "Task Result Index"))
99+
);
100+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new SystemIndices(pluginMap));
101+
assertThat(e.getMessage(), containsString("plugin or module attempted to define the same source"));
102+
}
86103
}

0 commit comments

Comments
 (0)