Skip to content

Commit a401856

Browse files
authored
Opt-in closed/hidden segments in /_cat/segments (elastic#136168) (elastic#136359)
* 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: # server/src/main/java/org/elasticsearch/rest/action/cat/RestSegmentsAction.java
1 parent 4c20b45 commit a401856

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;
@@ -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)