7
7
8
8
package org .elasticsearch .xpack .core .security .action ;
9
9
10
+ import org .apache .logging .log4j .LogManager ;
11
+ import org .apache .logging .log4j .Logger ;
10
12
import org .elasticsearch .action .ActionListener ;
11
13
import org .elasticsearch .action .ActionRequestValidationException ;
12
14
import org .elasticsearch .action .ActionType ;
19
21
import org .elasticsearch .cluster .block .ClusterBlockException ;
20
22
import org .elasticsearch .cluster .block .ClusterBlockLevel ;
21
23
import org .elasticsearch .cluster .metadata .IndexMetadata ;
24
+ import org .elasticsearch .cluster .metadata .Metadata ;
25
+ import org .elasticsearch .cluster .metadata .ProjectId ;
22
26
import org .elasticsearch .cluster .metadata .ProjectMetadata ;
27
+ import org .elasticsearch .cluster .project .ProjectResolver ;
23
28
import org .elasticsearch .cluster .service .ClusterService ;
24
29
import org .elasticsearch .cluster .service .MasterServiceTaskQueue ;
25
30
import org .elasticsearch .common .Priority ;
41
46
*/
42
47
public class UpdateIndexMigrationVersionAction extends ActionType <UpdateIndexMigrationVersionResponse > {
43
48
49
+ private static final Logger logger = LogManager .getLogger (UpdateIndexMigrationVersionAction .class );
50
+
44
51
public static final UpdateIndexMigrationVersionAction INSTANCE = new UpdateIndexMigrationVersionAction ();
45
52
public static final String NAME = "internal:index/metadata/migration_version/update" ;
46
53
public static final String MIGRATION_VERSION_CUSTOM_KEY = "migration_version" ;
@@ -89,13 +96,15 @@ public String getIndexName() {
89
96
90
97
public static class TransportAction extends TransportMasterNodeAction <Request , UpdateIndexMigrationVersionResponse > {
91
98
private final MasterServiceTaskQueue <UpdateIndexMigrationVersionTask > updateIndexMigrationVersionTaskQueue ;
99
+ private final ProjectResolver projectResolver ;
92
100
93
101
@ Inject
94
102
public TransportAction (
95
103
TransportService transportService ,
96
104
ClusterService clusterService ,
97
105
ThreadPool threadPool ,
98
- ActionFilters actionFilters
106
+ ActionFilters actionFilters ,
107
+ ProjectResolver projectResolver
99
108
) {
100
109
super (
101
110
UpdateIndexMigrationVersionAction .NAME ,
@@ -112,6 +121,7 @@ public TransportAction(
112
121
Priority .LOW ,
113
122
UPDATE_INDEX_MIGRATION_VERSION_TASK_EXECUTOR
114
123
);
124
+ this .projectResolver = projectResolver ;
115
125
}
116
126
117
127
private static final SimpleBatchedExecutor <UpdateIndexMigrationVersionTask , Void > UPDATE_INDEX_MIGRATION_VERSION_TASK_EXECUTOR =
@@ -131,15 +141,34 @@ static class UpdateIndexMigrationVersionTask implements ClusterStateTaskListener
131
141
private final ActionListener <Void > listener ;
132
142
private final int indexMigrationVersion ;
133
143
private final String indexName ;
134
-
135
- UpdateIndexMigrationVersionTask (ActionListener <Void > listener , int indexMigrationVersion , String indexName ) {
144
+ private final ProjectId projectId ;
145
+
146
+ UpdateIndexMigrationVersionTask (
147
+ ActionListener <Void > listener ,
148
+ int indexMigrationVersion ,
149
+ String indexName ,
150
+ ProjectId projectId
151
+ ) {
136
152
this .listener = listener ;
137
153
this .indexMigrationVersion = indexMigrationVersion ;
138
154
this .indexName = indexName ;
155
+ this .projectId = projectId ;
139
156
}
140
157
141
158
ClusterState execute (ClusterState currentState ) {
142
- final var project = currentState .metadata ().getProject ();
159
+ final Metadata metadata = currentState .metadata ();
160
+ if (metadata .hasProject (projectId ) == false ) {
161
+ // project has been deleted? nothing to do
162
+ logger .warn (
163
+ "Cannot update security index [{}] in project [{}] to migration-version [{}]"
164
+ + " because it does not exist in cluster state" ,
165
+ indexName ,
166
+ projectId ,
167
+ indexMigrationVersion
168
+ );
169
+ return currentState ;
170
+ }
171
+ final var project = metadata .getProject (projectId );
143
172
IndexMetadata .Builder indexMetadataBuilder = IndexMetadata .builder (project .indices ().get (indexName ));
144
173
indexMetadataBuilder .putCustom (
145
174
MIGRATION_VERSION_CUSTOM_KEY ,
@@ -168,20 +197,30 @@ protected void masterOperation(
168
197
ClusterState state ,
169
198
ActionListener <UpdateIndexMigrationVersionResponse > listener
170
199
) throws Exception {
200
+ final ProjectId projectId = projectResolver .getProjectId ();
171
201
updateIndexMigrationVersionTaskQueue .submitTask (
172
202
"Updating cluster state with a new index migration version" ,
173
- new UpdateIndexMigrationVersionTask (
174
- ActionListener .wrap (response -> listener .onResponse (new UpdateIndexMigrationVersionResponse ()), listener ::onFailure ),
175
- request .getIndexMigrationVersion (),
176
- request .getIndexName ()
177
- ),
203
+ new UpdateIndexMigrationVersionTask (ActionListener .wrap (response -> {
204
+ logger .info (
205
+ "Updated project=[{}] index=[{}] to migration-version=[{}]" ,
206
+ projectId ,
207
+ request .getIndexName (),
208
+ request .getIndexMigrationVersion ()
209
+ );
210
+ listener .onResponse (new UpdateIndexMigrationVersionResponse ());
211
+ }, listener ::onFailure ), request .getIndexMigrationVersion (), request .getIndexName (), projectId ),
178
212
null
179
213
);
180
214
}
181
215
182
216
@ Override
183
217
protected ClusterBlockException checkBlock (Request request , ClusterState state ) {
184
- return state .blocks ().indicesBlockedException (ClusterBlockLevel .METADATA_WRITE , new String [] { request .getIndexName () });
218
+ return state .blocks ()
219
+ .indicesBlockedException (
220
+ projectResolver .getProjectId (),
221
+ ClusterBlockLevel .METADATA_WRITE ,
222
+ new String [] { request .getIndexName () }
223
+ );
185
224
}
186
225
}
187
226
}
0 commit comments