Skip to content

Commit ad53ee5

Browse files
authored
Make TransportUnpromotableShardRefreshAction multi-project-compatible (elastic#142354)
1 parent 0074142 commit ad53ee5

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/refresh/TransportUnpromotableShardRefreshAction.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import org.elasticsearch.cluster.action.shard.ShardStateAction;
2020
import org.elasticsearch.cluster.block.ClusterBlockException;
2121
import org.elasticsearch.cluster.metadata.MetadataCreateIndexService;
22+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2223
import org.elasticsearch.cluster.service.ClusterService;
2324
import org.elasticsearch.common.io.stream.StreamInput;
25+
import org.elasticsearch.core.Nullable;
2426
import org.elasticsearch.core.TimeValue;
27+
import org.elasticsearch.index.Index;
2528
import org.elasticsearch.index.engine.Engine;
2629
import org.elasticsearch.indices.IndicesService;
2730
import org.elasticsearch.injection.guice.Inject;
@@ -111,8 +114,11 @@ private void beforeDispatchingRequestToUnpromotableShards(UnpromotableShardRefre
111114
}
112115

113116
var clusterStateObserver = new ClusterStateObserver(clusterService, request.getTimeout(), logger, threadPool.getThreadContext());
117+
var state = clusterStateObserver.setAndGetObservedState();
118+
var index = request.shardId().getIndex();
119+
ProjectMetadata projectMetadata = state.metadata().lookupProject(index).orElse(null);
114120

115-
if (isIndexBlockedForRefresh(request.shardId().getIndexName(), clusterStateObserver.setAndGetObservedState()) == false) {
121+
if (isIndexBlockedForRefresh(projectMetadata, index, state) == false) {
116122
listener.onResponse(null);
117123
return;
118124
}
@@ -133,15 +139,18 @@ public void onTimeout(TimeValue timeout) {
133139
listener.onFailure(
134140
new ElasticsearchTimeoutException(
135141
"shard refresh timed out waiting for index block to be removed",
136-
new ClusterBlockException(Map.of(request.shardId().getIndexName(), Set.of(INDEX_REFRESH_BLOCK)))
142+
new ClusterBlockException(Map.of(index.getName(), Set.of(INDEX_REFRESH_BLOCK)))
137143
)
138144
);
139145
}
140-
}, clusterState -> isIndexBlockedForRefresh(request.shardId().getIndexName(), clusterState) == false);
146+
}, clusterState -> isIndexBlockedForRefresh(projectMetadata, index, clusterState) == false);
141147
}
142148

143-
private static boolean isIndexBlockedForRefresh(String index, ClusterState state) {
144-
return state.blocks().hasIndexBlock(index, INDEX_REFRESH_BLOCK);
149+
private static boolean isIndexBlockedForRefresh(@Nullable ProjectMetadata projectMetadata, Index index, ClusterState state) {
150+
if (projectMetadata == null) {
151+
return false;
152+
}
153+
return state.blocks().hasIndexBlock(projectMetadata.id(), index.getName(), INDEX_REFRESH_BLOCK);
145154
}
146155

147156
@Override

server/src/test/java/org/elasticsearch/action/admin/indices/refresh/TransportUnpromotableShardRefreshActionTests.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import org.elasticsearch.cluster.block.ClusterBlockException;
2020
import org.elasticsearch.cluster.block.ClusterBlocks;
2121
import org.elasticsearch.cluster.metadata.IndexMetadata;
22-
import org.elasticsearch.cluster.metadata.Metadata;
22+
import org.elasticsearch.cluster.metadata.ProjectId;
23+
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2324
import org.elasticsearch.cluster.node.DiscoveryNode;
2425
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
2526
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
@@ -32,6 +33,7 @@
3233
import org.elasticsearch.core.TimeValue;
3334
import org.elasticsearch.index.Index;
3435
import org.elasticsearch.index.IndexService;
36+
import org.elasticsearch.index.IndexVersion;
3537
import org.elasticsearch.index.shard.ShardId;
3638
import org.elasticsearch.indices.IndicesService;
3739
import org.elasticsearch.tasks.Task;
@@ -161,11 +163,7 @@ protected void unpromotableShardOperation(
161163

162164
var withRefreshBlock = randomBoolean();
163165
if (withRefreshBlock) {
164-
setState(
165-
clusterService,
166-
ClusterState.builder(clusterService.state())
167-
.blocks(ClusterBlocks.builder().addIndexBlock(shardId.getIndexName(), IndexMetadata.INDEX_REFRESH_BLOCK))
168-
);
166+
setState(clusterService, clusterStateWithRefreshBlock(clusterService.state(), shardId, ProjectId.DEFAULT));
169167
}
170168

171169
final var future = new PlainActionFuture<ActionResponse.Empty>();
@@ -196,7 +194,8 @@ protected void unpromotableShardOperation(
196194
ClusterState.builder(clusterService.state())
197195
.blocks(
198196
ClusterBlocks.builder()
199-
.removeIndexBlock(Metadata.DEFAULT_PROJECT_ID, shardId.getIndexName(), IndexMetadata.INDEX_REFRESH_BLOCK)
197+
.blocks(clusterService.state().blocks())
198+
.removeIndexBlock(ProjectId.DEFAULT, shardId.getIndexName(), IndexMetadata.INDEX_REFRESH_BLOCK)
200199
)
201200
);
202201
}
@@ -231,11 +230,7 @@ protected void unpromotableShardOperation(
231230
}
232231
};
233232

234-
setState(
235-
clusterService,
236-
ClusterState.builder(clusterService.state())
237-
.blocks(ClusterBlocks.builder().addIndexBlock(shardId.getIndexName(), IndexMetadata.INDEX_REFRESH_BLOCK))
238-
);
233+
setState(clusterService, clusterStateWithRefreshBlock(clusterService.state(), shardId, ProjectId.DEFAULT));
239234

240235
final var countDownLatch = new CountDownLatch(1);
241236
final var request = new UnpromotableShardRefreshRequest(
@@ -262,6 +257,18 @@ protected void unpromotableShardOperation(
262257
safeAwait(countDownLatch);
263258
}
264259

260+
private ClusterState clusterStateWithRefreshBlock(ClusterState base, ShardId shardId, ProjectId projectId) {
261+
IndexMetadata indexMetadata = IndexMetadata.builder(shardId.getIndexName())
262+
.settings(indexSettings(IndexVersion.current(), shardId.getIndex().getUUID(), 1, 0).build())
263+
.build();
264+
return ClusterState.builder(base)
265+
.putProjectMetadata(ProjectMetadata.builder(projectId).put(indexMetadata, false))
266+
.blocks(
267+
ClusterBlocks.builder(base.blocks()).addIndexBlock(projectId, shardId.getIndexName(), IndexMetadata.INDEX_REFRESH_BLOCK)
268+
)
269+
.build();
270+
}
271+
265272
private IndexShardRoutingTable createShardRoutingTableWithPrimaryAndSearchShards(ShardId shardId, boolean withSearchShards) {
266273
final var shardRouting = TestShardRouting.newShardRouting(
267274
shardId,

0 commit comments

Comments
 (0)