Skip to content

Commit a3c2dd6

Browse files
lukewhitingCopilotszybia
authored
Opt-in closed/hidden segments in /_cat/segments (#136168)
* 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]>
1 parent ab1cccf commit a3c2dd6

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
@@ -93,6 +93,29 @@
9393
"micros",
9494
"nanos"
9595
]
96+
},
97+
"ignore_unavailable": {
98+
"type": "boolean",
99+
"description": "Whether specified concrete indices should be ignored when unavailable (missing or closed). Only allowed when providing an index expression."
100+
},
101+
"ignore_throttled": {
102+
"type": "boolean",
103+
"description": "Whether specified concrete, expanded or aliased indices should be ignored when throttled. Only allowed when providing an index expression."
104+
},
105+
"allow_no_indices": {
106+
"type": "boolean",
107+
"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."
108+
},
109+
"expand_wildcards": {
110+
"type": "enum",
111+
"options": ["open", "closed", "hidden", "none", "all"],
112+
"default": "open",
113+
"description": "Whether to expand wildcard expression to concrete indices that are open, closed or both."
114+
},
115+
"allow_closed": {
116+
"type": "boolean",
117+
"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",
118+
"default": false
96119
}
97120
}
98121
}

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

3435
import java.util.List;
3536
import java.util.Map;
37+
import java.util.Set;
3638

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

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

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

6266
final ClusterStateRequest clusterStateRequest = new ClusterStateRequest(getMasterNodeTimeout(request));
6367
RestUtils.consumeDeprecatedLocalParameter(request);
64-
clusterStateRequest.clear().nodes(true).routingTable(true).indices(indices);
68+
69+
final boolean allowClosed = request.paramAsBoolean("allow_closed", false);
70+
final IndicesOptions defaultOptions = allowClosed
71+
? IndicesOptions.strictExpandHidden()
72+
: IndicesOptions.strictExpandOpenAndForbidClosed();
73+
final IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, defaultOptions);
74+
75+
clusterStateRequest.clear().nodes(true);
6576

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

68-
return channel -> cancelClient.admin().cluster().state(clusterStateRequest, new RestActionListener<ClusterStateResponse>(channel) {
79+
return channel -> cancelClient.admin().cluster().state(clusterStateRequest, new RestActionListener<>(channel) {
6980
@Override
7081
public void processResponse(final ClusterStateResponse clusterStateResponse) {
7182
final IndicesSegmentsRequest indicesSegmentsRequest = new IndicesSegmentsRequest();
72-
indicesSegmentsRequest.indices(indices);
73-
cancelClient.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener<IndicesSegmentResponse>(channel) {
83+
indicesSegmentsRequest.indices(indices).indicesOptions(indicesOptions);
84+
cancelClient.admin().indices().segments(indicesSegmentsRequest, new RestResponseListener<>(channel) {
7485
@Override
7586
public RestResponse buildResponse(final IndicesSegmentResponse indicesSegmentResponse) throws Exception {
7687
if (request.getHttpChannel().isOpen() == false) {
@@ -157,4 +168,9 @@ private Table buildTable(final RestRequest request, ClusterStateResponse state,
157168

158169
return table;
159170
}
171+
172+
@Override
173+
public Set<String> supportedCapabilities() {
174+
return CAPABILITIES;
175+
}
160176
}

0 commit comments

Comments
 (0)