Skip to content

Commit 486785e

Browse files
committed
Add options to include closed and hidden segments in segments cat API
1 parent ebd868b commit 486785e

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@
9191
"micros",
9292
"nanos"
9393
]
94+
},
95+
"expand_wildcards": {
96+
"type": "enum",
97+
"options": [
98+
"open",
99+
"closed",
100+
"hidden",
101+
"none",
102+
"all"
103+
],
104+
"default": "all",
105+
"description": "Whether to expand wildcard expression to concrete indices that are open, closed or both."
106+
},
107+
"allow_closed": {
108+
"type": "boolean",
109+
"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",
110+
"default": false
94111
}
95112
}
96113
}

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.*false(\s)+true.*\nclosed-index.*true(\s)+false.*\nhidden-index.*false(\s)+true.*/

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+
boolean allowClosed = request.paramAsBoolean("allow_closed", false);
70+
IndicesOptions defaultOptions = allowClosed
71+
? IndicesOptions.strictExpandHidden()
72+
: IndicesOptions.strictExpandOpenAndForbidClosed();
73+
IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, defaultOptions);
74+
75+
clusterStateRequest.clear().nodes(true).routingTable(true).indices(indices).indicesOptions(indicesOptions);
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)