Skip to content

Commit a9d778d

Browse files
authored
Opt-in closed/hidden segments in /_cat/segments (#136168) (#136361)
* Add options to include closed and hidden segments in segments cat API * Update docs/changelog/136168.yaml * Update rest-api-spec/src/main/resources/rest-api-spec/api/cat.segments.json Co-authored-by: Copilot <[email protected]> * Update docs/changelog/136168.yaml Co-authored-by: Szymon Bialkowski <[email protected]> * Update rest-api-spec/src/main/resources/rest-api-spec/api/cat.segments.json Co-authored-by: Szymon Bialkowski <[email protected]> * PR Changes * Delete docs/changelog/136168.yaml * Remove routing table request from cluster state request * Add additional fields to the spec * Refactor new YAML test to be serverless compatible --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Szymon Bialkowski <[email protected]> (cherry picked from commit a3c2dd6) # Conflicts: # rest-api-spec/src/main/resources/rest-api-spec/api/cat.segments.json # server/src/main/java/org/elasticsearch/rest/action/cat/RestSegmentsAction.java
1 parent de0097b commit a9d778d

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

rest-api-spec/src/main/resources/rest-api-spec/api/cat.segments.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@
7878
"type":"boolean",
7979
"description":"Verbose mode. Display column headers",
8080
"default":false
81+
},
82+
"ignore_unavailable": {
83+
"type": "boolean",
84+
"description": "Whether specified concrete indices should be ignored when unavailable (missing or closed). Only allowed when providing an index expression."
85+
},
86+
"ignore_throttled": {
87+
"type": "boolean",
88+
"description": "Whether specified concrete, expanded or aliased indices should be ignored when throttled. Only allowed when providing an index expression."
89+
},
90+
"allow_no_indices": {
91+
"type": "boolean",
92+
"description": "Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified). Only allowed when providing an index expression."
93+
},
94+
"expand_wildcards": {
95+
"type": "enum",
96+
"options": ["open", "closed", "hidden", "none", "all"],
97+
"default": "open",
98+
"description": "Whether to expand wildcard expression to concrete indices that are open, closed or both."
99+
},
100+
"allow_closed": {
101+
"type": "boolean",
102+
"description": "If true, allow closed indices to be returned in the response otherwise if false, keep the legacy behaviour of throwing an exception if index pattern matches closed indices",
103+
"default": false
81104
}
82105
}
83106
}

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/cat.segments/10_basic.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,77 @@ tsdb:
211211
$body: |
212212
/^(tsdb \s+ 0 \s+ p \s+ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} \s+ _\d (\s\d){3} \s+
213213
(\d+|\d+[.]\d+)(kb|b) \s+ \d+ (\s+ (false|true)){2} \s+ \d+\.\d+(\.\d+)? \s+ (false|true) \s? \n?)$/
214+
215+
---
216+
Wildcard Expansion Settings:
217+
- requires:
218+
capabilities:
219+
- method: GET
220+
path: /_cat/segments
221+
capabilities: [ allow_closed ]
222+
test_runner_features: [ capabilities ]
223+
reason: Capability required to run test
224+
225+
- do:
226+
indices.create:
227+
index: basic-index
228+
body:
229+
settings:
230+
number_of_shards: 1
231+
number_of_replicas: 0
232+
233+
- do:
234+
index:
235+
index: basic-index
236+
id: "1"
237+
body:
238+
field: "basic doc 1"
239+
240+
- do:
241+
indices.create:
242+
index: hidden-index
243+
body:
244+
settings:
245+
number_of_shards: 1
246+
number_of_replicas: 0
247+
index.hidden: true
248+
249+
- do:
250+
index:
251+
index: hidden-index
252+
id: "1"
253+
body:
254+
field: "hidden doc 1"
255+
256+
- do:
257+
indices.create:
258+
index: closed-index
259+
body:
260+
settings:
261+
number_of_shards: 1
262+
number_of_replicas: 0
263+
264+
- do:
265+
index:
266+
index: closed-index
267+
id: "1"
268+
body:
269+
field: "closed doc 1"
270+
271+
- do:
272+
indices.refresh:
273+
index: [ basic-index, closed-index, hidden-index ]
274+
275+
- do:
276+
indices.close:
277+
index: closed-index
278+
279+
- do:
280+
cat.segments:
281+
v: true
282+
s: index
283+
expand_wildcards: all
284+
allow_closed: true
285+
- match:
286+
$body: |
287+
/basic-index(\s)+0(\s)+p.*\nclosed-index(\s)+0(\s)+p.*\nhidden-index(\s)+0(\s)+p.*/

server/src/main/java/org/elasticsearch/rest/action/cat/RestSegmentsAction.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse;
1717
import org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest;
1818
import org.elasticsearch.action.admin.indices.segments.ShardSegments;
19+
import org.elasticsearch.action.support.IndicesOptions;
1920
import org.elasticsearch.client.internal.node.NodeClient;
2021
import org.elasticsearch.cluster.node.DiscoveryNodes;
2122
import org.elasticsearch.common.Strings;
@@ -32,13 +33,16 @@
3233

3334
import java.util.List;
3435
import java.util.Map;
36+
import java.util.Set;
3537

3638
import static org.elasticsearch.rest.RestRequest.Method.GET;
3739
import static org.elasticsearch.rest.RestUtils.getMasterNodeTimeout;
3840

3941
@ServerlessScope(Scope.INTERNAL)
4042
public class RestSegmentsAction extends AbstractCatAction {
4143

44+
private static final Set<String> CAPABILITIES = Set.of("allow_closed");
45+
4246
@Override
4347
public List<Route> routes() {
4448
return List.of(new Route(GET, "/_cat/segments"), new Route(GET, "/_cat/segments/{index}"));
@@ -60,16 +64,23 @@ protected RestChannelConsumer doCatRequest(final RestRequest request, final Node
6064

6165
final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(getMasterNodeTimeout(request));
6266
clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
63-
clusterStateRequest.clear().nodes(true).routingTable(true).indices(indices);
67+
68+
final boolean allowClosed = request.paramAsBoolean("allow_closed", false);
69+
final IndicesOptions defaultOptions = allowClosed
70+
? IndicesOptions.strictExpandHidden()
71+
: IndicesOptions.strictExpandOpenAndForbidClosed();
72+
final IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, defaultOptions);
73+
74+
clusterStateRequest.clear().nodes(true);
6475

6576
final RestCancellableNodeClient cancelClient = new RestCancellableNodeClient(client, request.getHttpChannel());
6677

67-
return channel -> cancelClient.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {
78+
return channel -> cancelClient.admin().cluster().state(clusterStateRequest, new RestActionListener<>(channel) {
6879
@Override
6980
public void processResponse(final ClusterStateResponse clusterStateResponse) {
7081
final IndicesSegmentsRequest indicesSegmentsRequest = new IndicesSegmentsRequest();
71-
indicesSegmentsRequest.indices(indices);
72-
cancelClient.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener<IndicesSegmentResponse>(channel) {
82+
indicesSegmentsRequest.indices(indices).indicesOptions(indicesOptions);
83+
cancelClient.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener<>(channel) {
7384
@Override
7485
public RestResponse buildResponse(final IndicesSegmentResponse indicesSegmentResponse) throws Exception {
7586
if (request.getHttpChannel().isOpen() == false) {
@@ -156,4 +167,9 @@ private Table buildTable(final RestRequest request, ClusterStateResponse state,
156167

157168
return table;
158169
}
170+
171+
@Override
172+
public Set<String> supportedCapabilities() {
173+
return CAPABILITIES;
174+
}
159175
}

0 commit comments

Comments
 (0)