Skip to content

Commit 2f726a2

Browse files
committed
Support listing current views
1 parent f8797bc commit 2f726a2

File tree

4 files changed

+81
-52
lines changed

4 files changed

+81
-52
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/CsvTestsDataLoader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ public static void loadViewsIntoEs(RestClient client) throws IOException {
446446
for (var view : VIEW_CONFIGS) {
447447
loadView(client, view.viewName, view.viewFileName, logger);
448448
}
449+
// Just for debugging output TODO: remove
450+
clusterHasViewSupport(client, logger);
449451
} else {
450452
logger.info("Skipping loading views as the cluster does not support views");
451453
}
@@ -458,6 +460,8 @@ public static void deleteViews(RestClient client) throws IOException {
458460
for (var view : VIEW_CONFIGS) {
459461
deleteView(client, view.viewName, logger);
460462
}
463+
// Just for debugging output TODO: remove
464+
clusterHasViewSupport(client, logger);
461465
} else {
462466
logger.info("Skipping deleting views as the cluster does not support views");
463467
}
@@ -644,6 +648,8 @@ private static boolean clusterHasViewSupport(RestClient client, Logger logger) t
644648
try {
645649
Response response = client.performRequest(request);
646650
logger.info("View listing response: {}", response.getStatusLine());
651+
logger.info("View response body info: {}", response.getEntity());
652+
logger.info("View response body: {}", new String(response.getEntity().getContent().readAllBytes()));
647653
} catch (ResponseException e) {
648654
logger.info("View listing error: {}", e.getMessage());
649655
int code = e.getResponse().getStatusLine().getStatusCode();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/view/ListViewsAction.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,20 @@
66
*/
77
package org.elasticsearch.xpack.esql.view;
88

9+
import org.elasticsearch.action.ActionRequest;
910
import org.elasticsearch.action.ActionRequestValidationException;
11+
import org.elasticsearch.action.ActionResponse;
1012
import org.elasticsearch.action.ActionType;
11-
import org.elasticsearch.action.support.master.AcknowledgedResponse;
12-
import org.elasticsearch.action.support.master.MasterNodeRequest;
13+
import org.elasticsearch.action.support.TransportAction;
1314
import org.elasticsearch.common.io.stream.StreamInput;
1415
import org.elasticsearch.common.io.stream.StreamOutput;
15-
import org.elasticsearch.core.TimeValue;
16+
import org.elasticsearch.xcontent.ToXContentObject;
17+
import org.elasticsearch.xcontent.XContentBuilder;
1618

1719
import java.io.IOException;
20+
import java.util.Map;
1821

19-
public class ListViewsAction extends ActionType<AcknowledgedResponse> {
22+
public class ListViewsAction extends ActionType<GetViewAction.Response> {
2023

2124
public static final ListViewsAction INSTANCE = new ListViewsAction();
2225
public static final String NAME = "cluster:admin/xpack/esql/views";
@@ -25,9 +28,9 @@ private ListViewsAction() {
2528
super(NAME);
2629
}
2730

28-
public static class Request extends MasterNodeRequest<ListViewsAction.Request> {
29-
public Request(TimeValue masterNodeTimeout) {
30-
super(masterNodeTimeout);
31+
public static class Request extends ActionRequest {
32+
public Request() {
33+
super();
3134
}
3235

3336
public Request(StreamInput in) throws IOException {
@@ -55,4 +58,49 @@ public int hashCode() {
5558
return ListViewsAction.Request.class.hashCode();
5659
}
5760
}
61+
62+
public static class Response extends ActionResponse implements ToXContentObject {
63+
64+
private final Map<String, View> views;
65+
66+
public Response(final Map<String, View> views) {
67+
this.views = views;
68+
}
69+
70+
public Map<String, View> getViews() {
71+
return views;
72+
}
73+
74+
@Override
75+
public void writeTo(StreamOutput out) throws IOException {
76+
TransportAction.localOnly();
77+
}
78+
79+
@Override
80+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
81+
builder.value(views);
82+
return builder;
83+
}
84+
85+
@Override
86+
public boolean equals(Object o) {
87+
if (this == o) {
88+
return true;
89+
}
90+
if (o == null || getClass() != o.getClass()) {
91+
return false;
92+
}
93+
return views.equals(((Response) o).views);
94+
}
95+
96+
@Override
97+
public int hashCode() {
98+
return views.hashCode();
99+
}
100+
101+
@Override
102+
public String toString() {
103+
return "GetViewAction.Response{view=" + views.toString() + '}';
104+
}
105+
}
58106
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/view/RestListViewsAction.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.elasticsearch.client.internal.node.NodeClient;
1111
import org.elasticsearch.rest.BaseRestHandler;
1212
import org.elasticsearch.rest.RestRequest;
13-
import org.elasticsearch.rest.RestUtils;
1413
import org.elasticsearch.rest.Scope;
1514
import org.elasticsearch.rest.ServerlessScope;
1615
import org.elasticsearch.rest.action.RestToXContentListener;
@@ -33,8 +32,8 @@ public String getName() {
3332
}
3433

3534
@Override
36-
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
37-
ListViewsAction.Request req = new ListViewsAction.Request(RestUtils.getMasterNodeTimeout(request));
38-
return channel -> client.execute(ListViewsAction.INSTANCE, req, new RestToXContentListener<>(channel));
35+
protected RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
36+
ListViewsAction.Request req = new ListViewsAction.Request();
37+
return channel -> client.execute(TransportListViewsAction.TYPE, req, new RestToXContentListener<>(channel));
3938
}
4039
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/view/TransportListViewsAction.java

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,37 @@
77
package org.elasticsearch.xpack.esql.view;
88

99
import org.elasticsearch.action.ActionListener;
10+
import org.elasticsearch.action.ActionType;
11+
import org.elasticsearch.action.admin.cluster.remote.RemoteInfoResponse;
1012
import org.elasticsearch.action.support.ActionFilters;
11-
import org.elasticsearch.action.support.master.AcknowledgedResponse;
12-
import org.elasticsearch.action.support.master.AcknowledgedTransportMasterNodeAction;
13-
import org.elasticsearch.cluster.ClusterState;
14-
import org.elasticsearch.cluster.block.ClusterBlockException;
15-
import org.elasticsearch.cluster.block.ClusterBlockLevel;
16-
import org.elasticsearch.cluster.service.ClusterService;
13+
import org.elasticsearch.action.support.HandledTransportAction;
1714
import org.elasticsearch.common.util.concurrent.EsExecutors;
1815
import org.elasticsearch.injection.guice.Inject;
1916
import org.elasticsearch.tasks.Task;
20-
import org.elasticsearch.threadpool.ThreadPool;
2117
import org.elasticsearch.transport.TransportService;
2218

23-
import java.util.Set;
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
2421

25-
public class TransportListViewsAction extends AcknowledgedTransportMasterNodeAction<ListViewsAction.Request> {
22+
public class TransportListViewsAction extends HandledTransportAction<ListViewsAction.Request, ListViewsAction.Response> {
23+
public static final ActionType<RemoteInfoResponse> TYPE = new ActionType<>(ListViewsAction.NAME);
2624
private final ViewService viewService;
2725

2826
@Inject
29-
public TransportListViewsAction(
30-
TransportService transportService,
31-
ClusterService clusterService,
32-
ThreadPool threadPool,
33-
ActionFilters actionFilters,
34-
ViewService viewService
35-
) {
36-
super(
37-
ListViewsAction.NAME,
38-
transportService,
39-
clusterService,
40-
threadPool,
41-
actionFilters,
42-
ListViewsAction.Request::new,
43-
EsExecutors.DIRECT_EXECUTOR_SERVICE
44-
);
27+
public TransportListViewsAction(TransportService transportService, ActionFilters actionFilters, ViewService viewService) {
28+
super(ListViewsAction.NAME, transportService, actionFilters, ListViewsAction.Request::new, EsExecutors.DIRECT_EXECUTOR_SERVICE);
4529
this.viewService = viewService;
4630
}
4731

4832
@Override
49-
protected void masterOperation(
50-
Task task,
51-
ListViewsAction.Request request,
52-
ClusterState state,
53-
ActionListener<AcknowledgedResponse> listener
54-
) {
55-
Set<String> view = viewService.list();
56-
if (view == null) {
57-
listener.onResponse(AcknowledgedResponse.FALSE);
58-
} else {
59-
listener.onResponse(AcknowledgedResponse.TRUE);
33+
protected void doExecute(Task task, ListViewsAction.Request request, ActionListener<ListViewsAction.Response> listener) {
34+
Map<String, View> views = new LinkedHashMap<>();
35+
for (String name : viewService.list()) {
36+
View view = viewService.get(name);
37+
if (view != null) {
38+
views.put(name, viewService.get(name));
39+
}
6040
}
61-
}
62-
63-
@Override
64-
protected ClusterBlockException checkBlock(ListViewsAction.Request request, ClusterState state) {
65-
return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ);
41+
listener.onResponse(new ListViewsAction.Response(views));
6642
}
6743
}

0 commit comments

Comments
 (0)