Skip to content

Commit 34c4fc3

Browse files
authored
Remove tasks module to define tasks system index (#61588)
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). Backport of #61540
1 parent f2dc664 commit 34c4fc3

File tree

5 files changed

+68
-113
lines changed

5 files changed

+68
-113
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: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@
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

41+
import static java.util.Collections.singletonList;
42+
import static java.util.Collections.singletonMap;
3943
import static java.util.Collections.unmodifiableList;
44+
import static java.util.Collections.unmodifiableMap;
45+
import static org.elasticsearch.tasks.TaskResultsService.TASK_INDEX;
4046

4147
/**
4248
* This class holds the {@link SystemIndexDescriptor} objects that represent system indices the
@@ -45,12 +51,17 @@
4551
*/
4652
public class SystemIndices {
4753

54+
private static final Map<String, Collection<SystemIndexDescriptor>> SERVER_SYSTEM_INDEX_DESCRIPTORS = singletonMap(
55+
TaskResultsService.class.getName(), singletonList(new SystemIndexDescriptor(TASK_INDEX + "*", "Task Result Index"))
56+
);
57+
4858
private final CharacterRunAutomaton runAutomaton;
4959
private final Collection<SystemIndexDescriptor> systemIndexDescriptors;
5060

51-
public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexDescriptorMap) {
52-
checkForOverlappingPatterns(systemIndexDescriptorMap);
53-
this.systemIndexDescriptors = unmodifiableList(systemIndexDescriptorMap.values()
61+
public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesDescriptors) {
62+
final Map<String, Collection<SystemIndexDescriptor>> descriptorsMap = buildSystemIndexDescriptorMap(pluginAndModulesDescriptors);
63+
checkForOverlappingPatterns(descriptorsMap);
64+
this.systemIndexDescriptors = unmodifiableList(descriptorsMap.values()
5465
.stream()
5566
.flatMap(Collection::stream)
5667
.collect(Collectors.toList()));
@@ -63,7 +74,16 @@ public SystemIndices(Map<String, Collection<SystemIndexDescriptor>> systemIndexD
6374
* @return true if the {@link Index}'s name matches a pattern from a {@link SystemIndexDescriptor}
6475
*/
6576
public boolean isSystemIndex(Index index) {
66-
return runAutomaton.run(index.getName());
77+
return isSystemIndex(index.getName());
78+
}
79+
80+
/**
81+
* Determines whether a given index is a system index by comparing its name to the collection of loaded {@link SystemIndexDescriptor}s
82+
* @param indexName the index name to check against loaded {@link SystemIndexDescriptor}s
83+
* @return true if the index name matches a pattern from a {@link SystemIndexDescriptor}
84+
*/
85+
public boolean isSystemIndex(String indexName) {
86+
return runAutomaton.run(indexName);
6787
}
6888

6989
/**
@@ -126,10 +146,10 @@ static void checkForOverlappingPatterns(Map<String, Collection<SystemIndexDescri
126146
.filter(d -> overlaps(descriptorToCheck.v2(), d.v2()))
127147
.collect(Collectors.toList());
128148
if (descriptorsMatchingThisPattern.isEmpty() == false) {
129-
throw new IllegalStateException("a system index descriptor [" + descriptorToCheck.v2() + "] from plugin [" +
149+
throw new IllegalStateException("a system index descriptor [" + descriptorToCheck.v2() + "] from [" +
130150
descriptorToCheck.v1() + "] overlaps with other system index descriptors: [" +
131151
descriptorsMatchingThisPattern.stream()
132-
.map(descriptor -> descriptor.v2() + " from plugin [" + descriptor.v1() + "]")
152+
.map(descriptor -> descriptor.v2() + " from [" + descriptor.v1() + "]")
133153
.collect(Collectors.joining(", ")));
134154
}
135155
});
@@ -140,4 +160,19 @@ private static boolean overlaps(SystemIndexDescriptor a1, SystemIndexDescriptor
140160
Automaton a2Automaton = Regex.simpleMatchToAutomaton(a2.getIndexPattern());
141161
return Operations.isEmpty(Operations.intersection(a1Automaton, a2Automaton)) == false;
142162
}
163+
164+
private static Map<String, Collection<SystemIndexDescriptor>> buildSystemIndexDescriptorMap(
165+
Map<String, Collection<SystemIndexDescriptor>> pluginAndModulesMap) {
166+
final Map<String, Collection<SystemIndexDescriptor>> map =
167+
new HashMap<>(pluginAndModulesMap.size() + SERVER_SYSTEM_INDEX_DESCRIPTORS.size());
168+
map.putAll(pluginAndModulesMap);
169+
// put the server items last since we expect less of them
170+
SERVER_SYSTEM_INDEX_DESCRIPTORS.forEach((source, descriptors) -> {
171+
if (map.putIfAbsent(source, descriptors) != null) {
172+
throw new IllegalArgumentException("plugin or module attempted to define the same source [" + source +
173+
"] as a built-in system index");
174+
}
175+
});
176+
return unmodifiableMap(map);
177+
}
143178
}

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919

2020
package org.elasticsearch.indices;
2121

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

2425
import java.util.Arrays;
2526
import java.util.Collection;
26-
import java.util.Collections;
2727
import java.util.HashMap;
2828
import java.util.Map;
2929

30+
import static java.util.Collections.emptyMap;
31+
import static java.util.Collections.singletonList;
32+
import static java.util.Collections.singletonMap;
33+
import static org.elasticsearch.tasks.TaskResultsService.TASK_INDEX;
3034
import static org.hamcrest.Matchers.containsString;
3135
import static org.hamcrest.Matchers.equalTo;
3236
import static org.hamcrest.Matchers.not;
@@ -45,14 +49,14 @@ public void testBasicOverlappingPatterns() {
4549
String broadPatternSource = "AAA" + randomAlphaOfLength(5);
4650
String otherSource = "ZZZ" + randomAlphaOfLength(6);
4751
Map<String, Collection<SystemIndexDescriptor>> descriptors = new HashMap<>();
48-
descriptors.put(broadPatternSource, Collections.singletonList(broadPattern));
52+
descriptors.put(broadPatternSource, singletonList(broadPattern));
4953
descriptors.put(otherSource, Arrays.asList(notOverlapping, overlapping1, overlapping2, overlapping3));
5054

5155
IllegalStateException exception = expectThrows(IllegalStateException.class,
5256
() -> SystemIndices.checkForOverlappingPatterns(descriptors));
5357
assertThat(exception.getMessage(), containsString("a system index descriptor [" + broadPattern +
54-
"] from plugin [" + broadPatternSource + "] overlaps with other system index descriptors:"));
55-
String fromPluginString = " from plugin [" + otherSource + "]";
58+
"] from [" + broadPatternSource + "] overlaps with other system index descriptors:"));
59+
String fromPluginString = " from [" + otherSource + "]";
5660
assertThat(exception.getMessage(), containsString(overlapping1.toString() + fromPluginString));
5761
assertThat(exception.getMessage(), containsString(overlapping2.toString() + fromPluginString));
5862
assertThat(exception.getMessage(), containsString(overlapping3.toString() + fromPluginString));
@@ -72,16 +76,31 @@ public void testComplexOverlappingPatterns() {
7276
String source1 = "AAA" + randomAlphaOfLength(5);
7377
String source2 = "ZZZ" + randomAlphaOfLength(6);
7478
Map<String, Collection<SystemIndexDescriptor>> descriptors = new HashMap<>();
75-
descriptors.put(source1, Collections.singletonList(pattern1));
76-
descriptors.put(source2, Collections.singletonList(pattern2));
79+
descriptors.put(source1, singletonList(pattern1));
80+
descriptors.put(source2, singletonList(pattern2));
7781

7882
IllegalStateException exception = expectThrows(IllegalStateException.class,
7983
() -> SystemIndices.checkForOverlappingPatterns(descriptors));
8084
assertThat(exception.getMessage(), containsString("a system index descriptor [" + pattern1 +
81-
"] from plugin [" + source1 + "] overlaps with other system index descriptors:"));
82-
assertThat(exception.getMessage(), containsString(pattern2.toString() + " from plugin [" + source2 + "]"));
85+
"] from [" + source1 + "] overlaps with other system index descriptors:"));
86+
assertThat(exception.getMessage(), containsString(pattern2.toString() + " from [" + source2 + "]"));
8387

8488
IllegalStateException constructorException = expectThrows(IllegalStateException.class, () -> new SystemIndices(descriptors));
8589
assertThat(constructorException.getMessage(), equalTo(exception.getMessage()));
8690
}
91+
92+
public void testBuiltInSystemIndices() {
93+
SystemIndices systemIndices = new SystemIndices(emptyMap());
94+
assertTrue(systemIndices.isSystemIndex(".tasks"));
95+
assertTrue(systemIndices.isSystemIndex(".tasks1"));
96+
assertTrue(systemIndices.isSystemIndex(".tasks-old"));
97+
}
98+
99+
public void testPluginCannotOverrideBuiltInSystemIndex() {
100+
Map<String, Collection<SystemIndexDescriptor>> pluginMap = singletonMap(
101+
TaskResultsService.class.getName(), singletonList(new SystemIndexDescriptor(TASK_INDEX, "Task Result Index"))
102+
);
103+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new SystemIndices(pluginMap));
104+
assertThat(e.getMessage(), containsString("plugin or module attempted to define the same source"));
105+
}
87106
}

0 commit comments

Comments
 (0)