Skip to content

Commit d259982

Browse files
committed
Make system index migration project-aware
1 parent 4dc1e6a commit d259982

File tree

24 files changed

+250
-364
lines changed

24 files changed

+250
-364
lines changed

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,18 +1008,13 @@ public static void cleanUpFeature(
10081008
}
10091009

10101010
// No-op pre-migration function to be used as the default in case none are provided.
1011-
private static void noopPreMigrationFunction(
1012-
ClusterService clusterService,
1013-
Client client,
1014-
ActionListener<Map<String, Object>> listener
1015-
) {
1011+
private static void noopPreMigrationFunction(ProjectMetadata project, Client client, ActionListener<Map<String, Object>> listener) {
10161012
listener.onResponse(Collections.emptyMap());
10171013
}
10181014

10191015
// No-op pre-migration function to be used as the default in case none are provided.
10201016
private static void noopPostMigrationFunction(
10211017
Map<String, Object> preUpgradeMetadata,
1022-
ClusterService clusterService,
10231018
Client client,
10241019
ActionListener<Boolean> listener
10251020
) {
@@ -1028,25 +1023,20 @@ private static void noopPostMigrationFunction(
10281023

10291024
/**
10301025
* Type for the handler that's invoked prior to migrating a Feature's system indices.
1031-
* See {@link SystemIndexPlugin#prepareForIndicesMigration(ClusterService, Client, ActionListener)}.
1026+
* See {@link SystemIndexPlugin#prepareForIndicesMigration(ProjectMetadata, Client, ActionListener)}.
10321027
*/
10331028
@FunctionalInterface
10341029
public interface MigrationPreparationHandler {
1035-
void prepareForIndicesMigration(ClusterService clusterService, Client client, ActionListener<Map<String, Object>> listener);
1030+
void prepareForIndicesMigration(ProjectMetadata project, Client client, ActionListener<Map<String, Object>> listener);
10361031
}
10371032

10381033
/**
10391034
* Type for the handler that's invoked when all of a feature's system indices have been migrated.
1040-
* See {@link SystemIndexPlugin#indicesMigrationComplete(Map, ClusterService, Client, ActionListener)}.
1035+
* See {@link SystemIndexPlugin#indicesMigrationComplete(Map, Client, ActionListener)}.
10411036
*/
10421037
@FunctionalInterface
10431038
public interface MigrationCompletionHandler {
1044-
void indicesMigrationComplete(
1045-
Map<String, Object> preUpgradeMetadata,
1046-
ClusterService clusterService,
1047-
Client client,
1048-
ActionListener<Boolean> listener
1049-
);
1039+
void indicesMigrationComplete(Map<String, Object> preUpgradeMetadata, Client client, ActionListener<Boolean> listener);
10501040
}
10511041

10521042
public interface CleanupFunction {

server/src/main/java/org/elasticsearch/plugins/SystemIndexPlugin.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.action.ActionListener;
1313
import org.elasticsearch.action.admin.cluster.snapshots.features.ResetFeatureStateResponse;
1414
import org.elasticsearch.client.internal.Client;
15+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1516
import org.elasticsearch.cluster.project.ProjectResolver;
1617
import org.elasticsearch.cluster.service.ClusterService;
1718
import org.elasticsearch.common.settings.Settings;
@@ -45,8 +46,8 @@
4546
* in order to provide a “factory reset” of the plugin state. This can be useful for testing. The default method will simply retrieve a list
4647
* of system and associated indices and delete them.
4748
*
48-
* <p>An implementation may also override {@link #prepareForIndicesMigration(ClusterService, Client, ActionListener)} and
49-
* {@link #indicesMigrationComplete(Map, ClusterService, Client, ActionListener)} in order to take special action before and after a
49+
* <p>An implementation may also override {@link #prepareForIndicesMigration(ProjectMetadata, Client, ActionListener)} and
50+
* {@link #indicesMigrationComplete(Map, Client, ActionListener)} in order to take special action before and after a
5051
* feature migration, which will temporarily block access to system indices. For example, a plugin might want to enter a safe mode and
5152
* reject certain requests while the migration is in progress. See org.elasticsearch.upgrades.SystemIndexMigrationExecutor for
5253
* more details.
@@ -126,19 +127,19 @@ default void cleanUpFeature(
126127
* very rare.
127128
*
128129
* This method can also store metadata to be passed to
129-
* {@link SystemIndexPlugin#indicesMigrationComplete(Map, ClusterService, Client, ActionListener)} when it is called; see the
130+
* {@link SystemIndexPlugin#indicesMigrationComplete(Map, Client, ActionListener)} when it is called; see the
130131
* {@code listener} parameter for details.
131132
*
132-
* @param clusterService The cluster service.
133+
* @param project The project metadata.
133134
* @param client A {@link org.elasticsearch.client.internal.ParentTaskAssigningClient} with the parent task set to the upgrade task.
134135
* Does not set the origin header, so implementors of this method will likely want to wrap it in an
135136
* {@link org.elasticsearch.client.internal.OriginSettingClient}.
136137
* @param listener A listener that should have {@link ActionListener#onResponse(Object)} called once all necessary preparations for the
137138
* upgrade of indices owned by this plugin have been completed. The {@link Map} passed to the listener will be stored
138-
* and passed to {@link #indicesMigrationComplete(Map, ClusterService, Client, ActionListener)}. Note the contents of
139+
* and passed to {@link #indicesMigrationComplete(Map, Client, ActionListener)}. Note the contents of
139140
* the map *must* be writeable using {@link org.elasticsearch.common.io.stream.StreamOutput#writeGenericValue(Object)}.
140141
*/
141-
default void prepareForIndicesMigration(ClusterService clusterService, Client client, ActionListener<Map<String, Object>> listener) {
142+
default void prepareForIndicesMigration(ProjectMetadata project, Client client, ActionListener<Map<String, Object>> listener) {
142143
listener.onResponse(Collections.emptyMap());
143144
}
144145

@@ -155,20 +156,14 @@ default void prepareForIndicesMigration(ClusterService clusterService, Client cl
155156
* with no data format changes allowed).
156157
*
157158
* @param preUpgradeMetadata The metadata that was given to the listener by
158-
* {@link #prepareForIndicesMigration(ClusterService, Client, ActionListener)}.
159-
* @param clusterService The cluster service.
159+
* {@link #prepareForIndicesMigration(ProjectMetadata, Client, ActionListener)}.
160160
* @param client A {@link org.elasticsearch.client.internal.ParentTaskAssigningClient} with the parent task set to the upgrade task.
161161
* Does not set the origin header, so implementors of this method will likely want to wrap it in an
162162
* {@link org.elasticsearch.client.internal.OriginSettingClient}.
163163
* @param listener A listener that should have {@code ActionListener.onResponse(true)} called once all actions following the upgrade
164164
* of this plugin's system indices have been completed.
165165
*/
166-
default void indicesMigrationComplete(
167-
Map<String, Object> preUpgradeMetadata,
168-
ClusterService clusterService,
169-
Client client,
170-
ActionListener<Boolean> listener
171-
) {
166+
default void indicesMigrationComplete(Map<String, Object> preUpgradeMetadata, Client client, ActionListener<Boolean> listener) {
172167
listener.onResponse(true);
173168
}
174169
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/MlMetadata.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.cluster.NamedDiff;
1515
import org.elasticsearch.cluster.SimpleDiffable;
1616
import org.elasticsearch.cluster.metadata.Metadata;
17+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1718
import org.elasticsearch.common.Strings;
1819
import org.elasticsearch.common.collect.Iterators;
1920
import org.elasticsearch.common.io.stream.StreamInput;
@@ -252,4 +253,12 @@ public static MlMetadata getMlMetadata(ClusterState state) {
252253
}
253254
return mlMetadata;
254255
}
256+
257+
public static MlMetadata getMlMetadata(ProjectMetadata project) {
258+
MlMetadata mlMetadata = project == null ? null : project.custom(TYPE);
259+
if (mlMetadata == null) {
260+
return EMPTY_METADATA;
261+
}
262+
return mlMetadata;
263+
}
255264
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/TransformMetadata.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
import org.elasticsearch.cluster.ProjectState;
1616
import org.elasticsearch.cluster.metadata.Metadata;
1717
import org.elasticsearch.cluster.metadata.ProjectId;
18+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1819
import org.elasticsearch.common.Strings;
1920
import org.elasticsearch.common.collect.Iterators;
2021
import org.elasticsearch.common.io.stream.StreamInput;
2122
import org.elasticsearch.common.io.stream.StreamOutput;
23+
import org.elasticsearch.core.FixForMultiProject;
2224
import org.elasticsearch.core.Nullable;
2325
import org.elasticsearch.xcontent.ObjectParser;
2426
import org.elasticsearch.xcontent.ParseField;
@@ -212,8 +214,9 @@ public TransformMetadata build() {
212214
}
213215

214216
/**
215-
* @deprecated use {@link #transformMetadata(ClusterState, ProjectId)}
217+
* @deprecated use {@link #transformMetadata(ProjectMetadata)}
216218
*/
219+
@FixForMultiProject
217220
@Deprecated(forRemoval = true)
218221
public static TransformMetadata getTransformMetadata(ClusterState state) {
219222
TransformMetadata TransformMetadata = (state == null) ? null : state.metadata().getSingleProjectCustom(TYPE);
@@ -223,25 +226,44 @@ public static TransformMetadata getTransformMetadata(ClusterState state) {
223226
return TransformMetadata;
224227
}
225228

229+
/**
230+
* @deprecated use {@link #transformMetadata(ProjectMetadata)}
231+
*/
232+
@FixForMultiProject
233+
@Deprecated(forRemoval = true)
226234
public static TransformMetadata transformMetadata(@Nullable ClusterState state, @Nullable ProjectId projectId) {
227235
if (state == null || projectId == null) {
228236
return EMPTY_METADATA;
229237
}
230238
return transformMetadata(state.projectState(projectId));
231239
}
232240

241+
/**
242+
* @deprecated use {@link #transformMetadata(ProjectMetadata)}
243+
*/
244+
@FixForMultiProject
245+
@Deprecated(forRemoval = true)
233246
public static TransformMetadata transformMetadata(@Nullable ProjectState projectState) {
234247
if (projectState == null) {
235248
return EMPTY_METADATA;
236249
}
237-
TransformMetadata transformMetadata = projectState.metadata().custom(TYPE);
250+
return transformMetadata(projectState.metadata());
251+
}
252+
253+
public static TransformMetadata transformMetadata(ProjectMetadata project) {
254+
TransformMetadata transformMetadata = project == null ? null : project.custom(TYPE);
238255
if (transformMetadata == null) {
239256
return EMPTY_METADATA;
240257
}
241258
return transformMetadata;
242259
}
243260

261+
@Deprecated(forRemoval = true)
244262
public static boolean upgradeMode(ClusterState state) {
245263
return getTransformMetadata(state).upgradeMode();
246264
}
265+
266+
public static boolean upgradeMode(ProjectMetadata project) {
267+
return transformMetadata(project).upgradeMode();
268+
}
247269
}

x-pack/plugin/migrate/src/internalClusterTest/java/org/elasticsearch/system_indices/action/AbstractFeatureMigrationIntegTest.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
import org.elasticsearch.action.support.ActionFilters;
2121
import org.elasticsearch.action.support.ActiveShardCount;
2222
import org.elasticsearch.client.internal.Client;
23-
import org.elasticsearch.cluster.ClusterState;
2423
import org.elasticsearch.cluster.metadata.IndexMetadata;
24+
import org.elasticsearch.cluster.metadata.ProjectId;
2525
import org.elasticsearch.cluster.metadata.ProjectMetadata;
26-
import org.elasticsearch.cluster.service.ClusterService;
2726
import org.elasticsearch.common.Strings;
2827
import org.elasticsearch.common.settings.Settings;
2928
import org.elasticsearch.index.IndexVersion;
@@ -57,7 +56,7 @@
5756
import java.util.concurrent.ExecutionException;
5857
import java.util.concurrent.TimeUnit;
5958
import java.util.concurrent.atomic.AtomicReference;
60-
import java.util.function.BiConsumer;
59+
import java.util.function.Consumer;
6160
import java.util.function.Function;
6261
import java.util.stream.Collectors;
6362

@@ -151,7 +150,11 @@ public abstract class AbstractFeatureMigrationIntegTest extends ESIntegTestCase
151150
protected String masterName;
152151

153152
protected static ProjectMetadata assertMetadataAfterMigration(String featureName) {
154-
ProjectMetadata finalMetadata = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT).get().getState().metadata().getProject();
153+
ProjectMetadata finalMetadata = clusterAdmin().prepareState(TEST_REQUEST_TIMEOUT)
154+
.get()
155+
.getState()
156+
.metadata()
157+
.getProject(ProjectId.DEFAULT);
155158
// Check that the results metadata is what we expect.
156159
FeatureMigrationResults currentResults = finalMetadata.custom(FeatureMigrationResults.TYPE);
157160
assertThat(currentResults, notNullValue());
@@ -169,8 +172,8 @@ public void setup() {
169172
masterAndDataNode = internalCluster().startNode();
170173

171174
TestPlugin testPlugin = getPlugin(TestPlugin.class);
172-
testPlugin.preMigrationHook.set((state) -> Collections.emptyMap());
173-
testPlugin.postMigrationHook.set((state, metadata) -> {});
175+
testPlugin.preMigrationHook.set((project) -> Collections.emptyMap());
176+
testPlugin.postMigrationHook.set((metadata) -> {});
174177
}
175178

176179
protected <T extends Plugin> T getPlugin(Class<T> type) {
@@ -340,8 +343,8 @@ protected static TestPlugin.BlockingActionFilter blockAction(String actionTypeNa
340343
}
341344

342345
public static class TestPlugin extends Plugin implements SystemIndexPlugin, ActionPlugin {
343-
public final AtomicReference<Function<ClusterState, Map<String, Object>>> preMigrationHook = new AtomicReference<>();
344-
public final AtomicReference<BiConsumer<ClusterState, Map<String, Object>>> postMigrationHook = new AtomicReference<>();
346+
public final AtomicReference<Function<ProjectMetadata, Map<String, Object>>> preMigrationHook = new AtomicReference<>();
347+
public final AtomicReference<Consumer<Map<String, Object>>> postMigrationHook = new AtomicReference<>();
345348
private final BlockingActionFilter blockingActionFilter;
346349

347350
public TestPlugin() {
@@ -375,18 +378,13 @@ public Collection<AssociatedIndexDescriptor> getAssociatedIndexDescriptors() {
375378
}
376379

377380
@Override
378-
public void prepareForIndicesMigration(ClusterService clusterService, Client client, ActionListener<Map<String, Object>> listener) {
379-
listener.onResponse(preMigrationHook.get().apply(clusterService.state()));
381+
public void prepareForIndicesMigration(ProjectMetadata project, Client client, ActionListener<Map<String, Object>> listener) {
382+
listener.onResponse(preMigrationHook.get().apply(project));
380383
}
381384

382385
@Override
383-
public void indicesMigrationComplete(
384-
Map<String, Object> preUpgradeMetadata,
385-
ClusterService clusterService,
386-
Client client,
387-
ActionListener<Boolean> listener
388-
) {
389-
postMigrationHook.get().accept(clusterService.state(), preUpgradeMetadata);
386+
public void indicesMigrationComplete(Map<String, Object> preUpgradeMetadata, Client client, ActionListener<Boolean> listener) {
387+
postMigrationHook.get().accept(preUpgradeMetadata);
390388
listener.onResponse(true);
391389
}
392390

x-pack/plugin/migrate/src/internalClusterTest/java/org/elasticsearch/system_indices/action/FeatureMigrationIT.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
2323
import org.elasticsearch.cluster.metadata.IndexMetadata;
2424
import org.elasticsearch.cluster.metadata.Metadata;
25+
import org.elasticsearch.cluster.metadata.ProjectId;
2526
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2627
import org.elasticsearch.cluster.metadata.Template;
2728
import org.elasticsearch.cluster.service.ClusterService;
@@ -153,7 +154,7 @@ public void testMigrateSystemIndex() throws Exception {
153154

154155
SetOnce<Boolean> preUpgradeHookCalled = new SetOnce<>();
155156
SetOnce<Boolean> postUpgradeHookCalled = new SetOnce<>();
156-
getPlugin(TestPlugin.class).preMigrationHook.set(clusterState -> {
157+
getPlugin(TestPlugin.class).preMigrationHook.set(project -> {
157158
// Check that the ordering of these calls is correct.
158159
assertThat(postUpgradeHookCalled.get(), nullValue());
159160
Map<String, Object> metadata = new HashMap<>();
@@ -170,7 +171,7 @@ public void testMigrateSystemIndex() throws Exception {
170171
return metadata;
171172
});
172173

173-
getPlugin(TestPlugin.class).postMigrationHook.set((clusterState, metadata) -> {
174+
getPlugin(TestPlugin.class).postMigrationHook.set((metadata) -> {
174175
assertThat(preUpgradeHookCalled.get(), is(true));
175176

176177
assertThat(metadata, hasEntry("stringKey", "stringValue"));
@@ -182,7 +183,8 @@ public void testMigrateSystemIndex() throws Exception {
182183
assertThat(innerMap, hasEntry("innerKey", "innerValue"));
183184

184185
// We shouldn't have any results in the cluster state as no features have fully finished yet.
185-
FeatureMigrationResults currentResults = clusterState.metadata().getProject().custom(FeatureMigrationResults.TYPE);
186+
final var project = clusterService().state().metadata().getProject(ProjectId.DEFAULT);
187+
FeatureMigrationResults currentResults = project.custom(FeatureMigrationResults.TYPE);
186188
assertThat(currentResults, nullValue());
187189
postUpgradeHookCalled.set(true);
188190
});

0 commit comments

Comments
 (0)