Skip to content

Commit cac6612

Browse files
committed
Send clear session as routable remote request (elastic#36805)
This commit adds a RemoteClusterAwareRequest interface that allows a request to specify which remote node it should be routed to. The remote cluster aware client will attempt to route the request directly to this node. Otherwise it will send it as a proxy action to eventually end up on the requested node. It implements the ccr clean_session action with this client.
1 parent 1e23d15 commit cac6612

File tree

6 files changed

+104
-132
lines changed

6 files changed

+104
-132
lines changed

server/src/main/java/org/elasticsearch/transport/RemoteClusterAwareClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.elasticsearch.action.ActionResponse;
2727
import org.elasticsearch.client.Client;
2828
import org.elasticsearch.client.support.AbstractClient;
29+
import org.elasticsearch.cluster.node.DiscoveryNode;
2930
import org.elasticsearch.common.settings.Settings;
3031
import org.elasticsearch.threadpool.ThreadPool;
3132

@@ -47,14 +48,19 @@ final class RemoteClusterAwareClient extends AbstractClient {
4748
ActionRequestBuilder<Request, Response, RequestBuilder>>
4849
void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
4950
remoteClusterService.ensureConnected(clusterAlias, ActionListener.wrap(res -> {
50-
Transport.Connection connection = remoteClusterService.getConnection(clusterAlias);
51+
Transport.Connection connection;
52+
if (request instanceof RemoteClusterAwareRequest) {
53+
DiscoveryNode preferredTargetNode = ((RemoteClusterAwareRequest) request).getPreferredTargetNode();
54+
connection = remoteClusterService.getConnection(preferredTargetNode, clusterAlias);
55+
} else {
56+
connection = remoteClusterService.getConnection(clusterAlias);
57+
}
5158
service.sendRequest(connection, action.name(), request, TransportRequestOptions.EMPTY,
5259
new ActionListenerResponseHandler<>(listener, action.getResponseReader()));
5360
},
5461
listener::onFailure));
5562
}
5663

57-
5864
@Override
5965
public void close() {
6066
// do nothing
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.transport;
21+
22+
import org.elasticsearch.cluster.node.DiscoveryNode;
23+
24+
public interface RemoteClusterAwareRequest {
25+
26+
/**
27+
* Returns the preferred discovery node for this request. The remote cluster client will attempt to send
28+
* this request directly to this node. Otherwise, it will send the request as a proxy action that will
29+
* be routed by the remote cluster to this node.
30+
*
31+
* @return preferred discovery node
32+
*/
33+
DiscoveryNode getPreferredTargetNode();
34+
35+
}

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionAction.java

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,21 @@
77
package org.elasticsearch.xpack.ccr.action.repositories;
88

99
import org.elasticsearch.action.Action;
10-
import org.elasticsearch.action.FailedNodeException;
10+
import org.elasticsearch.action.ActionListener;
11+
import org.elasticsearch.action.ActionResponse;
1112
import org.elasticsearch.action.support.ActionFilters;
12-
import org.elasticsearch.action.support.nodes.BaseNodeResponse;
13-
import org.elasticsearch.action.support.nodes.BaseNodesResponse;
14-
import org.elasticsearch.action.support.nodes.TransportNodesAction;
13+
import org.elasticsearch.action.support.HandledTransportAction;
1514
import org.elasticsearch.client.ElasticsearchClient;
16-
import org.elasticsearch.cluster.ClusterName;
1715
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
18-
import org.elasticsearch.cluster.node.DiscoveryNode;
19-
import org.elasticsearch.cluster.service.ClusterService;
2016
import org.elasticsearch.common.inject.Inject;
2117
import org.elasticsearch.common.io.stream.StreamInput;
22-
import org.elasticsearch.common.io.stream.StreamOutput;
2318
import org.elasticsearch.common.settings.Settings;
2419
import org.elasticsearch.threadpool.ThreadPool;
20+
import org.elasticsearch.transport.TransportActionProxy;
2521
import org.elasticsearch.transport.TransportService;
2622
import org.elasticsearch.xpack.ccr.repository.CcrRestoreSourceService;
2723

2824
import java.io.IOException;
29-
import java.util.List;
3025

3126
public class ClearCcrRestoreSessionAction extends Action<ClearCcrRestoreSessionRequest,
3227
ClearCcrRestoreSessionAction.ClearCcrRestoreSessionResponse, ClearCcrRestoreSessionRequestBuilder> {
@@ -48,84 +43,40 @@ public ClearCcrRestoreSessionRequestBuilder newRequestBuilder(ElasticsearchClien
4843
return new ClearCcrRestoreSessionRequestBuilder(client);
4944
}
5045

51-
public static class TransportDeleteCcrRestoreSessionAction extends TransportNodesAction<ClearCcrRestoreSessionRequest,
52-
ClearCcrRestoreSessionResponse, ClearCcrRestoreSessionRequest.Request, Response> {
46+
public static class TransportDeleteCcrRestoreSessionAction
47+
extends HandledTransportAction<ClearCcrRestoreSessionRequest, ClearCcrRestoreSessionResponse> {
5348

5449
private final CcrRestoreSourceService ccrRestoreService;
50+
private final ThreadPool threadPool;
5551

5652
@Inject
57-
public TransportDeleteCcrRestoreSessionAction(Settings settings, ThreadPool threadPool, ClusterService clusterService,
58-
ActionFilters actionFilters, IndexNameExpressionResolver resolver,
53+
public TransportDeleteCcrRestoreSessionAction(Settings settings, ThreadPool threadPool, ActionFilters actionFilters,
54+
IndexNameExpressionResolver resolver,
5955
TransportService transportService, CcrRestoreSourceService ccrRestoreService) {
60-
super(settings, NAME, threadPool, clusterService, transportService, actionFilters, resolver,
61-
ClearCcrRestoreSessionRequest::new, ClearCcrRestoreSessionRequest.Request::new, ThreadPool.Names.GENERIC, Response.class);
56+
super(settings, NAME, threadPool, transportService, actionFilters, resolver, ClearCcrRestoreSessionRequest::new);
57+
TransportActionProxy.registerProxyAction(transportService, NAME, ClearCcrRestoreSessionResponse::new);
6258
this.ccrRestoreService = ccrRestoreService;
59+
this.threadPool = transportService.getThreadPool();
6360
}
6461

6562
@Override
66-
protected ClearCcrRestoreSessionResponse newResponse(ClearCcrRestoreSessionRequest request, List<Response> responses,
67-
List<FailedNodeException> failures) {
68-
return new ClearCcrRestoreSessionResponse(clusterService.getClusterName(), responses, failures);
69-
}
70-
71-
@Override
72-
protected ClearCcrRestoreSessionRequest.Request newNodeRequest(String nodeId, ClearCcrRestoreSessionRequest request) {
73-
return request.getRequest();
74-
}
75-
76-
@Override
77-
protected Response newNodeResponse() {
78-
return new Response();
79-
}
80-
81-
@Override
82-
protected Response nodeOperation(ClearCcrRestoreSessionRequest.Request request) {
83-
ccrRestoreService.closeSession(request.getSessionUUID());
84-
return new Response(clusterService.localNode());
63+
protected void doExecute(ClearCcrRestoreSessionRequest request, ActionListener<ClearCcrRestoreSessionResponse> listener) {
64+
// TODO: Currently blocking actions might occur in the session closed callbacks. This dispatch
65+
// may be unnecessary when we remove these callbacks.
66+
threadPool.generic().execute(() -> {
67+
ccrRestoreService.closeSession(request.getSessionUUID());
68+
listener.onResponse(new ClearCcrRestoreSessionResponse());
69+
});
8570
}
8671
}
8772

88-
public static class Response extends BaseNodeResponse {
89-
90-
private Response() {
91-
}
92-
93-
private Response(StreamInput in) throws IOException {
94-
readFrom(in);
95-
}
96-
97-
private Response(DiscoveryNode node) {
98-
super(node);
99-
}
100-
101-
@Override
102-
public void writeTo(StreamOutput out) throws IOException {
103-
super.writeTo(out);
104-
}
105-
106-
@Override
107-
public void readFrom(StreamInput in) throws IOException {
108-
super.readFrom(in);
109-
}
110-
}
111-
112-
public static class ClearCcrRestoreSessionResponse extends BaseNodesResponse<Response> {
73+
public static class ClearCcrRestoreSessionResponse extends ActionResponse {
11374

11475
ClearCcrRestoreSessionResponse() {
11576
}
11677

117-
ClearCcrRestoreSessionResponse(ClusterName clusterName, List<Response> chunkResponses, List<FailedNodeException> failures) {
118-
super(clusterName, chunkResponses, failures);
119-
}
120-
121-
@Override
122-
protected List<Response> readNodesFrom(StreamInput in) throws IOException {
123-
return in.readList(Response::new);
124-
}
125-
126-
@Override
127-
protected void writeNodesTo(StreamOutput out, List<Response> nodes) throws IOException {
128-
out.writeList(nodes);
78+
ClearCcrRestoreSessionResponse(StreamInput in) throws IOException {
79+
super(in);
12980
}
13081
}
13182
}

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/ClearCcrRestoreSessionRequest.java

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,51 @@
66

77
package org.elasticsearch.xpack.ccr.action.repositories;
88

9-
import org.elasticsearch.action.support.nodes.BaseNodeRequest;
10-
import org.elasticsearch.action.support.nodes.BaseNodesRequest;
9+
import org.elasticsearch.action.ActionRequest;
10+
import org.elasticsearch.action.ActionRequestValidationException;
11+
import org.elasticsearch.transport.RemoteClusterAwareRequest;
12+
import org.elasticsearch.cluster.node.DiscoveryNode;
1113
import org.elasticsearch.common.io.stream.StreamInput;
1214
import org.elasticsearch.common.io.stream.StreamOutput;
1315

1416
import java.io.IOException;
1517

16-
public class ClearCcrRestoreSessionRequest extends BaseNodesRequest<ClearCcrRestoreSessionRequest> {
18+
public class ClearCcrRestoreSessionRequest extends ActionRequest implements RemoteClusterAwareRequest {
1719

18-
private Request request;
20+
private DiscoveryNode node;
21+
private String sessionUUID;
1922

2023
ClearCcrRestoreSessionRequest() {
2124
}
2225

23-
public ClearCcrRestoreSessionRequest(String nodeId, Request request) {
24-
super(nodeId);
25-
this.request = request;
26+
public ClearCcrRestoreSessionRequest(String sessionUUID, DiscoveryNode node) {
27+
this.sessionUUID = sessionUUID;
28+
this.node = node;
2629
}
2730

2831
@Override
29-
public void readFrom(StreamInput streamInput) throws IOException {
30-
super.readFrom(streamInput);
31-
request = new Request();
32-
request.readFrom(streamInput);
32+
public ActionRequestValidationException validate() {
33+
return null;
3334
}
3435

3536
@Override
36-
public void writeTo(StreamOutput streamOutput) throws IOException {
37-
super.writeTo(streamOutput);
38-
request.writeTo(streamOutput);
37+
public void readFrom(StreamInput in) throws IOException {
38+
super.readFrom(in);
39+
sessionUUID = in.readString();
3940
}
4041

41-
public Request getRequest() {
42-
return request;
42+
@Override
43+
public void writeTo(StreamOutput out) throws IOException {
44+
super.writeTo(out);
45+
out.writeString(sessionUUID);
4346
}
4447

45-
public static class Request extends BaseNodeRequest {
46-
47-
private String sessionUUID;
48-
49-
Request() {
50-
}
51-
52-
public Request(String nodeId, String sessionUUID) {
53-
super(nodeId);
54-
this.sessionUUID = sessionUUID;
55-
}
56-
57-
@Override
58-
public void readFrom(StreamInput in) throws IOException {
59-
super.readFrom(in);
60-
sessionUUID = in.readString();
61-
}
62-
63-
@Override
64-
public void writeTo(StreamOutput out) throws IOException {
65-
super.writeTo(out);
66-
out.writeString(sessionUUID);
67-
}
48+
String getSessionUUID() {
49+
return sessionUUID;
50+
}
6851

69-
public String getSessionUUID() {
70-
return sessionUUID;
71-
}
52+
@Override
53+
public DiscoveryNode getPreferredTargetNode() {
54+
return node;
7255
}
7356
}

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/repositories/PutCcrRestoreSessionAction.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.client.ElasticsearchClient;
1414
import org.elasticsearch.cluster.ClusterState;
1515
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
16+
import org.elasticsearch.cluster.node.DiscoveryNode;
1617
import org.elasticsearch.cluster.routing.ShardsIterator;
1718
import org.elasticsearch.cluster.service.ClusterService;
1819
import org.elasticsearch.common.inject.Inject;
@@ -79,7 +80,7 @@ protected PutCcrRestoreSessionResponse shardOperation(PutCcrRestoreSessionReques
7980
throw new ShardNotFoundException(shardId);
8081
}
8182
ccrRestoreService.openSession(request.getSessionUUID(), indexShard);
82-
return new PutCcrRestoreSessionResponse(indexShard.routingEntry().currentNodeId());
83+
return new PutCcrRestoreSessionResponse(clusterService.localNode());
8384
}
8485

8586
@Override
@@ -102,34 +103,34 @@ protected ShardsIterator shards(ClusterState state, InternalRequest request) {
102103

103104
public static class PutCcrRestoreSessionResponse extends ActionResponse {
104105

105-
private String nodeId;
106+
private DiscoveryNode node;
106107

107108
PutCcrRestoreSessionResponse() {
108109
}
109110

110-
PutCcrRestoreSessionResponse(String nodeId) {
111-
this.nodeId = nodeId;
111+
PutCcrRestoreSessionResponse(DiscoveryNode node) {
112+
this.node = node;
112113
}
113114

114115
PutCcrRestoreSessionResponse(StreamInput in) throws IOException {
115116
super(in);
116-
nodeId = in.readString();
117+
node = new DiscoveryNode(in);
117118
}
118119

119120
@Override
120121
public void readFrom(StreamInput in) throws IOException {
121122
super.readFrom(in);
122-
nodeId = in.readString();
123+
node = new DiscoveryNode(in);
123124
}
124125

125126
@Override
126127
public void writeTo(StreamOutput out) throws IOException {
127128
super.writeTo(out);
128-
out.writeString(nodeId);
129+
node.writeTo(out);
129130
}
130131

131-
public String getNodeId() {
132-
return nodeId;
132+
public DiscoveryNode getNode() {
133+
return node;
133134
}
134135
}
135136
}

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/repository/CcrRepository.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ public void restoreShard(IndexShard indexShard, SnapshotId snapshotId, Version v
253253
String sessionUUID = UUIDs.randomBase64UUID();
254254
PutCcrRestoreSessionAction.PutCcrRestoreSessionResponse response = remoteClient.execute(PutCcrRestoreSessionAction.INSTANCE,
255255
new PutCcrRestoreSessionRequest(sessionUUID, leaderShardId, recoveryMetadata)).actionGet();
256-
String nodeId = response.getNodeId();
256+
DiscoveryNode node = response.getNode();
257257
// TODO: Implement file restore
258-
closeSession(remoteClient, nodeId, sessionUUID);
258+
closeSession(remoteClient, node, sessionUUID);
259259
maybeUpdateMappings(client, remoteClient, leaderIndex, indexShard.indexSettings());
260260
}
261261

@@ -280,13 +280,9 @@ private void maybeUpdateMappings(Client localClient, Client remoteClient, Index
280280
}
281281
}
282282

283-
private void closeSession(Client remoteClient, String nodeId, String sessionUUID) {
284-
ClearCcrRestoreSessionRequest clearRequest = new ClearCcrRestoreSessionRequest(nodeId,
285-
new ClearCcrRestoreSessionRequest.Request(nodeId, sessionUUID));
283+
private void closeSession(Client remoteClient, DiscoveryNode node, String sessionUUID) {
284+
ClearCcrRestoreSessionRequest clearRequest = new ClearCcrRestoreSessionRequest(sessionUUID, node);
286285
ClearCcrRestoreSessionAction.ClearCcrRestoreSessionResponse response =
287286
remoteClient.execute(ClearCcrRestoreSessionAction.INSTANCE, clearRequest).actionGet();
288-
if (response.hasFailures()) {
289-
throw response.failures().get(0);
290-
}
291287
}
292288
}

0 commit comments

Comments
 (0)