|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.core.security.action; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 12 | +import org.elasticsearch.action.ActionType; |
| 13 | +import org.elasticsearch.action.support.ActionFilters; |
| 14 | +import org.elasticsearch.action.support.master.MasterNodeRequest; |
| 15 | +import org.elasticsearch.action.support.master.TransportMasterNodeAction; |
| 16 | +import org.elasticsearch.cluster.ClusterState; |
| 17 | +import org.elasticsearch.cluster.ClusterStateTaskListener; |
| 18 | +import org.elasticsearch.cluster.SimpleBatchedExecutor; |
| 19 | +import org.elasticsearch.cluster.block.ClusterBlockException; |
| 20 | +import org.elasticsearch.cluster.block.ClusterBlockLevel; |
| 21 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 22 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 23 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 24 | +import org.elasticsearch.cluster.service.ClusterService; |
| 25 | +import org.elasticsearch.cluster.service.MasterServiceTaskQueue; |
| 26 | +import org.elasticsearch.common.Priority; |
| 27 | +import org.elasticsearch.common.collect.ImmutableOpenMap; |
| 28 | +import org.elasticsearch.common.inject.Inject; |
| 29 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 30 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 31 | +import org.elasticsearch.core.TimeValue; |
| 32 | +import org.elasticsearch.core.Tuple; |
| 33 | +import org.elasticsearch.tasks.Task; |
| 34 | +import org.elasticsearch.threadpool.ThreadPool; |
| 35 | +import org.elasticsearch.transport.TransportService; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +/** |
| 41 | + * Updates the migration version in the custom metadata for an index in cluster state |
| 42 | + */ |
| 43 | +public class UpdateIndexMigrationVersionAction extends ActionType<UpdateIndexMigrationVersionResponse> { |
| 44 | + |
| 45 | + public static final UpdateIndexMigrationVersionAction INSTANCE = new UpdateIndexMigrationVersionAction(); |
| 46 | + public static final String NAME = "internal:index/metadata/migration_version/update"; |
| 47 | + public static final String MIGRATION_VERSION_CUSTOM_KEY = "migration_version"; |
| 48 | + public static final String MIGRATION_VERSION_CUSTOM_DATA_KEY = "version"; |
| 49 | + |
| 50 | + public UpdateIndexMigrationVersionAction() { |
| 51 | + super(NAME); |
| 52 | + } |
| 53 | + |
| 54 | + public static class Request extends MasterNodeRequest<Request> { |
| 55 | + private final int indexMigrationVersion; |
| 56 | + private final String indexName; |
| 57 | + |
| 58 | + public Request(TimeValue timeout, int indexMigrationVersion, String indexName) { |
| 59 | + super(timeout); |
| 60 | + this.indexMigrationVersion = indexMigrationVersion; |
| 61 | + this.indexName = indexName; |
| 62 | + } |
| 63 | + |
| 64 | + protected Request(StreamInput in) throws IOException { |
| 65 | + super(in); |
| 66 | + this.indexMigrationVersion = in.readInt(); |
| 67 | + this.indexName = in.readString(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void writeTo(StreamOutput out) throws IOException { |
| 72 | + super.writeTo(out); |
| 73 | + out.writeInt(indexMigrationVersion); |
| 74 | + out.writeString(indexName); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ActionRequestValidationException validate() { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + public int getIndexMigrationVersion() { |
| 83 | + return indexMigrationVersion; |
| 84 | + } |
| 85 | + |
| 86 | + public String getIndexName() { |
| 87 | + return indexName; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public static class TransportAction extends TransportMasterNodeAction<Request, UpdateIndexMigrationVersionResponse> { |
| 92 | + private final MasterServiceTaskQueue<UpdateIndexMigrationVersionTask> updateIndexMigrationVersionTaskQueue; |
| 93 | + |
| 94 | + @Inject |
| 95 | + public TransportAction( |
| 96 | + TransportService transportService, |
| 97 | + ClusterService clusterService, |
| 98 | + ThreadPool threadPool, |
| 99 | + ActionFilters actionFilters, |
| 100 | + IndexNameExpressionResolver indexNameExpressionResolver |
| 101 | + ) { |
| 102 | + super( |
| 103 | + UpdateIndexMigrationVersionAction.NAME, |
| 104 | + transportService, |
| 105 | + clusterService, |
| 106 | + threadPool, |
| 107 | + actionFilters, |
| 108 | + Request::new, |
| 109 | + indexNameExpressionResolver, |
| 110 | + UpdateIndexMigrationVersionResponse::new, |
| 111 | + threadPool.executor(ThreadPool.Names.MANAGEMENT) |
| 112 | + ); |
| 113 | + this.updateIndexMigrationVersionTaskQueue = clusterService.createTaskQueue( |
| 114 | + "update-index-migration-version-task-queue", |
| 115 | + Priority.LOW, |
| 116 | + UPDATE_INDEX_MIGRATION_VERSION_TASK_EXECUTOR |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + private static final SimpleBatchedExecutor<UpdateIndexMigrationVersionTask, Void> UPDATE_INDEX_MIGRATION_VERSION_TASK_EXECUTOR = |
| 121 | + new SimpleBatchedExecutor<>() { |
| 122 | + @Override |
| 123 | + public Tuple<ClusterState, Void> executeTask(UpdateIndexMigrationVersionTask task, ClusterState clusterState) { |
| 124 | + return Tuple.tuple(task.execute(clusterState), null); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void taskSucceeded(UpdateIndexMigrationVersionTask task, Void unused) { |
| 129 | + task.listener.onResponse(null); |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + static class UpdateIndexMigrationVersionTask implements ClusterStateTaskListener { |
| 134 | + private final ActionListener<Void> listener; |
| 135 | + private final int indexMigrationVersion; |
| 136 | + private final String indexName; |
| 137 | + |
| 138 | + UpdateIndexMigrationVersionTask(ActionListener<Void> listener, int indexMigrationVersion, String indexName) { |
| 139 | + this.listener = listener; |
| 140 | + this.indexMigrationVersion = indexMigrationVersion; |
| 141 | + this.indexName = indexName; |
| 142 | + } |
| 143 | + |
| 144 | + ClusterState execute(ClusterState currentState) { |
| 145 | + IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(currentState.metadata().getIndices().get(indexName)); |
| 146 | + indexMetadataBuilder.putCustom( |
| 147 | + MIGRATION_VERSION_CUSTOM_KEY, |
| 148 | + Map.of(MIGRATION_VERSION_CUSTOM_DATA_KEY, Integer.toString(indexMigrationVersion)) |
| 149 | + ); |
| 150 | + indexMetadataBuilder.version(indexMetadataBuilder.version() + 1); |
| 151 | + |
| 152 | + final ImmutableOpenMap.Builder<String, IndexMetadata> builder = ImmutableOpenMap.builder( |
| 153 | + currentState.metadata().getIndices() |
| 154 | + ); |
| 155 | + builder.put(indexName, indexMetadataBuilder.build()); |
| 156 | + |
| 157 | + return ClusterState.builder(currentState) |
| 158 | + .metadata(Metadata.builder(currentState.metadata()).indices(builder.build()).build()) |
| 159 | + .build(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void onFailure(Exception e) { |
| 164 | + listener.onFailure(e); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + protected void masterOperation( |
| 170 | + Task task, |
| 171 | + Request request, |
| 172 | + ClusterState state, |
| 173 | + ActionListener<UpdateIndexMigrationVersionResponse> listener |
| 174 | + ) throws Exception { |
| 175 | + updateIndexMigrationVersionTaskQueue.submitTask( |
| 176 | + "Updating cluster state with a new index migration version", |
| 177 | + new UpdateIndexMigrationVersionTask( |
| 178 | + ActionListener.wrap(response -> listener.onResponse(new UpdateIndexMigrationVersionResponse()), listener::onFailure), |
| 179 | + request.getIndexMigrationVersion(), |
| 180 | + request.getIndexName() |
| 181 | + ), |
| 182 | + null |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + protected ClusterBlockException checkBlock(Request request, ClusterState state) { |
| 188 | + return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_WRITE, new String[] { request.getIndexName() }); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments