Skip to content

Commit 04b5681

Browse files
committed
[CCR] Move api parameters from url to request body. (#31949)
Relates to #30102
1 parent b260ef5 commit 04b5681

File tree

11 files changed

+157
-86
lines changed

11 files changed

+157
-86
lines changed

x-pack/plugin/ccr/qa/multi-cluster-with-security/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexSecurityIT.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,13 @@ private static void refresh(String index) throws IOException {
145145

146146
private static void followIndex(String leaderIndex, String followIndex) throws IOException {
147147
final Request request = new Request("POST", "/" + followIndex + "/_xpack/ccr/_follow");
148-
request.addParameter("leader_index", leaderIndex);
149-
request.addParameter("idle_shard_retry_delay", "10ms");
148+
request.setJsonEntity("{\"leader_index\": \"" + leaderIndex + "\", \"idle_shard_retry_delay\": \"10ms\"}");
150149
assertOK(client().performRequest(request));
151150
}
152151

153152
private static void createAndFollowIndex(String leaderIndex, String followIndex) throws IOException {
154153
final Request request = new Request("POST", "/" + followIndex + "/_xpack/ccr/_create_and_follow");
155-
request.addParameter("leader_index", leaderIndex);
156-
request.addParameter("idle_shard_retry_delay", "10ms");
154+
request.setJsonEntity("{\"leader_index\": \"" + leaderIndex + "\", \"idle_shard_retry_delay\": \"10ms\"}");
157155
assertOK(client().performRequest(request));
158156
}
159157

x-pack/plugin/ccr/qa/multi-cluster/src/test/java/org/elasticsearch/xpack/ccr/FollowIndexIT.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,13 @@ private static void refresh(String index) throws IOException {
9595

9696
private static void followIndex(String leaderIndex, String followIndex) throws IOException {
9797
final Request request = new Request("POST", "/" + followIndex + "/_xpack/ccr/_follow");
98-
request.addParameter("leader_index", leaderIndex);
99-
request.addParameter("idle_shard_retry_delay", "10ms");
98+
request.setJsonEntity("{\"leader_index\": \"" + leaderIndex + "\", \"idle_shard_retry_delay\": \"10ms\"}");
10099
assertOK(client().performRequest(request));
101100
}
102101

103102
private static void createAndFollowIndex(String leaderIndex, String followIndex) throws IOException {
104103
final Request request = new Request("POST", "/" + followIndex + "/_xpack/ccr/_create_and_follow");
105-
request.addParameter("leader_index", leaderIndex);
106-
request.addParameter("idle_shard_retry_delay", "10ms");
104+
request.setJsonEntity("{\"leader_index\": \"" + leaderIndex + "\", \"idle_shard_retry_delay\": \"10ms\"}");
107105
assertOK(client().performRequest(request));
108106
}
109107

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,15 @@ protected Boolean newResponse(boolean acknowledged) {
278278

279279
@Override
280280
public ClusterState execute(ClusterState currentState) throws Exception {
281-
String followIndex = request.getFollowRequest().getFollowIndex();
281+
String followIndex = request.getFollowRequest().getFollowerIndex();
282282
IndexMetaData currentIndex = currentState.metaData().index(followIndex);
283283
if (currentIndex != null) {
284284
throw new ResourceAlreadyExistsException(currentIndex.getIndex());
285285
}
286286

287287
MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
288288
IndexMetaData.Builder imdBuilder = IndexMetaData.builder(followIndex);
289-
289+
290290
// Copy all settings, but overwrite a few settings.
291291
Settings.Builder settingsBuilder = Settings.builder();
292292
settingsBuilder.put(leaderIndexMetaData.getSettings());
@@ -295,7 +295,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
295295
settingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, followIndex);
296296
settingsBuilder.put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true);
297297
imdBuilder.settings(settingsBuilder);
298-
298+
299299
// Copy mappings from leader IMD to follow IMD
300300
for (ObjectObjectCursor<String, MappingMetaData> cursor : leaderIndexMetaData.getMappings()) {
301301
imdBuilder.putMapping(cursor.value);
@@ -309,21 +309,21 @@ public ClusterState execute(ClusterState currentState) throws Exception {
309309
ClusterState updatedState = builder.build();
310310

311311
RoutingTable.Builder routingTableBuilder = RoutingTable.builder(updatedState.routingTable())
312-
.addAsNew(updatedState.metaData().index(request.getFollowRequest().getFollowIndex()));
312+
.addAsNew(updatedState.metaData().index(request.getFollowRequest().getFollowerIndex()));
313313
updatedState = allocationService.reroute(
314314
ClusterState.builder(updatedState).routingTable(routingTableBuilder.build()).build(),
315-
"follow index [" + request.getFollowRequest().getFollowIndex() + "] created");
316-
315+
"follow index [" + request.getFollowRequest().getFollowerIndex() + "] created");
316+
317317
logger.info("[{}] creating index, cause [ccr_create_and_follow], shards [{}]/[{}]",
318318
followIndex, followIMD.getNumberOfShards(), followIMD.getNumberOfReplicas());
319-
319+
320320
return updatedState;
321321
}
322322
});
323323
}
324324

325325
private void initiateFollowing(Request request, ActionListener<Response> listener) {
326-
activeShardsObserver.waitForActiveShards(new String[]{request.followRequest.getFollowIndex()},
326+
activeShardsObserver.waitForActiveShards(new String[]{request.followRequest.getFollowerIndex()},
327327
ActiveShardCount.DEFAULT, request.timeout(), result -> {
328328
if (result) {
329329
client.execute(FollowIndexAction.INSTANCE, request.getFollowRequest(), ActionListener.wrap(
@@ -338,7 +338,7 @@ private void initiateFollowing(Request request, ActionListener<Response> listene
338338

339339
@Override
340340
protected ClusterBlockException checkBlock(Request request, ClusterState state) {
341-
return state.blocks().indexBlockedException(ClusterBlockLevel.METADATA_WRITE, request.getFollowRequest().getFollowIndex());
341+
return state.blocks().indexBlockedException(ClusterBlockLevel.METADATA_WRITE, request.getFollowRequest().getFollowerIndex());
342342
}
343343

344344
}

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

Lines changed: 109 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@
2222
import org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider;
2323
import org.elasticsearch.cluster.routing.allocation.decider.ShardsLimitAllocationDecider;
2424
import org.elasticsearch.cluster.service.ClusterService;
25+
import org.elasticsearch.common.ParseField;
2526
import org.elasticsearch.common.inject.Inject;
2627
import org.elasticsearch.common.io.stream.StreamInput;
2728
import org.elasticsearch.common.io.stream.StreamOutput;
2829
import org.elasticsearch.common.settings.Setting;
2930
import org.elasticsearch.common.settings.Settings;
3031
import org.elasticsearch.common.unit.TimeValue;
32+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
33+
import org.elasticsearch.common.xcontent.ObjectParser;
34+
import org.elasticsearch.common.xcontent.ToXContentObject;
35+
import org.elasticsearch.common.xcontent.XContentBuilder;
36+
import org.elasticsearch.common.xcontent.XContentParser;
3137
import org.elasticsearch.index.IndexSettings;
3238
import org.elasticsearch.index.IndexingSlowLog;
3339
import org.elasticsearch.index.SearchSlowLog;
@@ -76,10 +82,51 @@ public Response newResponse() {
7682
return new Response();
7783
}
7884

79-
public static class Request extends ActionRequest {
85+
public static class Request extends ActionRequest implements ToXContentObject {
86+
87+
private static final ParseField LEADER_INDEX_FIELD = new ParseField("leader_index");
88+
private static final ParseField FOLLOWER_INDEX_FIELD = new ParseField("follower_index");
89+
private static final ConstructingObjectParser<Request, String> PARSER = new ConstructingObjectParser<>(NAME, true,
90+
(args, followerIndex) -> {
91+
if (args[1] != null) {
92+
followerIndex = (String) args[1];
93+
}
94+
return new Request((String) args[0], followerIndex, (Integer) args[2], (Integer) args[3], (Long) args[4],
95+
(Integer) args[5], (Integer) args[6], (TimeValue) args[7], (TimeValue) args[8]);
96+
});
97+
98+
static {
99+
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), LEADER_INDEX_FIELD);
100+
PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), FOLLOWER_INDEX_FIELD);
101+
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), ShardFollowTask.MAX_BATCH_OPERATION_COUNT);
102+
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), ShardFollowTask.MAX_CONCURRENT_READ_BATCHES);
103+
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), ShardFollowTask.MAX_BATCH_SIZE_IN_BYTES);
104+
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), ShardFollowTask.MAX_CONCURRENT_WRITE_BATCHES);
105+
PARSER.declareInt(ConstructingObjectParser.optionalConstructorArg(), ShardFollowTask.MAX_WRITE_BUFFER_SIZE);
106+
PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(),
107+
(p, c) -> TimeValue.parseTimeValue(p.text(), ShardFollowTask.RETRY_TIMEOUT.getPreferredName()),
108+
ShardFollowTask.RETRY_TIMEOUT, ObjectParser.ValueType.STRING);
109+
PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(),
110+
(p, c) -> TimeValue.parseTimeValue(p.text(), ShardFollowTask.IDLE_SHARD_RETRY_DELAY.getPreferredName()),
111+
ShardFollowTask.IDLE_SHARD_RETRY_DELAY, ObjectParser.ValueType.STRING);
112+
}
113+
114+
public static Request fromXContent(XContentParser parser, String followerIndex) throws IOException {
115+
Request request = PARSER.parse(parser, followerIndex);
116+
if (followerIndex != null) {
117+
if (request.followerIndex == null) {
118+
request.followerIndex = followerIndex;
119+
} else {
120+
if (request.followerIndex.equals(followerIndex) == false) {
121+
throw new IllegalArgumentException("provided follower_index is not equal");
122+
}
123+
}
124+
}
125+
return request;
126+
}
80127

81128
private String leaderIndex;
82-
private String followIndex;
129+
private String followerIndex;
83130
private int maxBatchOperationCount;
84131
private int maxConcurrentReadBatches;
85132
private long maxOperationSizeInBytes;
@@ -88,9 +135,37 @@ public static class Request extends ActionRequest {
88135
private TimeValue retryTimeout;
89136
private TimeValue idleShardRetryDelay;
90137

91-
public Request(String leaderIndex, String followIndex, int maxBatchOperationCount, int maxConcurrentReadBatches,
92-
long maxOperationSizeInBytes, int maxConcurrentWriteBatches, int maxWriteBufferSize,
138+
public Request(String leaderIndex, String followerIndex, Integer maxBatchOperationCount, Integer maxConcurrentReadBatches,
139+
Long maxOperationSizeInBytes, Integer maxConcurrentWriteBatches, Integer maxWriteBufferSize,
93140
TimeValue retryTimeout, TimeValue idleShardRetryDelay) {
141+
if (leaderIndex == null) {
142+
throw new IllegalArgumentException("leader_index is missing");
143+
}
144+
if (followerIndex == null) {
145+
throw new IllegalArgumentException("follower_index is missing");
146+
}
147+
if (maxBatchOperationCount == null) {
148+
maxBatchOperationCount = ShardFollowNodeTask.DEFAULT_MAX_BATCH_OPERATION_COUNT;
149+
}
150+
if (maxConcurrentReadBatches == null) {
151+
maxConcurrentReadBatches = ShardFollowNodeTask.DEFAULT_MAX_CONCURRENT_READ_BATCHES;
152+
}
153+
if (maxOperationSizeInBytes == null) {
154+
maxOperationSizeInBytes = ShardFollowNodeTask.DEFAULT_MAX_BATCH_SIZE_IN_BYTES;
155+
}
156+
if (maxConcurrentWriteBatches == null) {
157+
maxConcurrentWriteBatches = ShardFollowNodeTask.DEFAULT_MAX_CONCURRENT_WRITE_BATCHES;
158+
}
159+
if (maxWriteBufferSize == null) {
160+
maxWriteBufferSize = ShardFollowNodeTask.DEFAULT_MAX_WRITE_BUFFER_SIZE;
161+
}
162+
if (retryTimeout == null) {
163+
retryTimeout = ShardFollowNodeTask.DEFAULT_RETRY_TIMEOUT;
164+
}
165+
if (idleShardRetryDelay == null) {
166+
idleShardRetryDelay = ShardFollowNodeTask.DEFAULT_IDLE_SHARD_RETRY_DELAY;
167+
}
168+
94169
if (maxBatchOperationCount < 1) {
95170
throw new IllegalArgumentException("maxBatchOperationCount must be larger than 0");
96171
}
@@ -107,15 +182,15 @@ public Request(String leaderIndex, String followIndex, int maxBatchOperationCoun
107182
throw new IllegalArgumentException("maxWriteBufferSize must be larger than 0");
108183
}
109184

110-
this.leaderIndex = Objects.requireNonNull(leaderIndex);
111-
this.followIndex = Objects.requireNonNull(followIndex);
185+
this.leaderIndex = leaderIndex;
186+
this.followerIndex = followerIndex;
112187
this.maxBatchOperationCount = maxBatchOperationCount;
113188
this.maxConcurrentReadBatches = maxConcurrentReadBatches;
114189
this.maxOperationSizeInBytes = maxOperationSizeInBytes;
115190
this.maxConcurrentWriteBatches = maxConcurrentWriteBatches;
116191
this.maxWriteBufferSize = maxWriteBufferSize;
117-
this.retryTimeout = Objects.requireNonNull(retryTimeout);
118-
this.idleShardRetryDelay = Objects.requireNonNull(idleShardRetryDelay);
192+
this.retryTimeout = retryTimeout;
193+
this.idleShardRetryDelay = idleShardRetryDelay;
119194
}
120195

121196
Request() {
@@ -125,8 +200,8 @@ public String getLeaderIndex() {
125200
return leaderIndex;
126201
}
127202

128-
public String getFollowIndex() {
129-
return followIndex;
203+
public String getFollowerIndex() {
204+
return followerIndex;
130205
}
131206

132207
public int getMaxBatchOperationCount() {
@@ -142,7 +217,7 @@ public ActionRequestValidationException validate() {
142217
public void readFrom(StreamInput in) throws IOException {
143218
super.readFrom(in);
144219
leaderIndex = in.readString();
145-
followIndex = in.readString();
220+
followerIndex = in.readString();
146221
maxBatchOperationCount = in.readVInt();
147222
maxConcurrentReadBatches = in.readVInt();
148223
maxOperationSizeInBytes = in.readVLong();
@@ -156,7 +231,7 @@ public void readFrom(StreamInput in) throws IOException {
156231
public void writeTo(StreamOutput out) throws IOException {
157232
super.writeTo(out);
158233
out.writeString(leaderIndex);
159-
out.writeString(followIndex);
234+
out.writeString(followerIndex);
160235
out.writeVInt(maxBatchOperationCount);
161236
out.writeVInt(maxConcurrentReadBatches);
162237
out.writeVLong(maxOperationSizeInBytes);
@@ -166,6 +241,24 @@ public void writeTo(StreamOutput out) throws IOException {
166241
out.writeOptionalTimeValue(idleShardRetryDelay);
167242
}
168243

244+
@Override
245+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
246+
builder.startObject();
247+
{
248+
builder.field(LEADER_INDEX_FIELD.getPreferredName(), leaderIndex);
249+
builder.field(FOLLOWER_INDEX_FIELD.getPreferredName(), followerIndex);
250+
builder.field(ShardFollowTask.MAX_BATCH_OPERATION_COUNT.getPreferredName(), maxBatchOperationCount);
251+
builder.field(ShardFollowTask.MAX_BATCH_SIZE_IN_BYTES.getPreferredName(), maxOperationSizeInBytes);
252+
builder.field(ShardFollowTask.MAX_WRITE_BUFFER_SIZE.getPreferredName(), maxWriteBufferSize);
253+
builder.field(ShardFollowTask.MAX_CONCURRENT_READ_BATCHES.getPreferredName(), maxConcurrentReadBatches);
254+
builder.field(ShardFollowTask.MAX_CONCURRENT_WRITE_BATCHES.getPreferredName(), maxConcurrentWriteBatches);
255+
builder.field(ShardFollowTask.RETRY_TIMEOUT.getPreferredName(), retryTimeout.getStringRep());
256+
builder.field(ShardFollowTask.IDLE_SHARD_RETRY_DELAY.getPreferredName(), idleShardRetryDelay.getStringRep());
257+
}
258+
builder.endObject();
259+
return builder;
260+
}
261+
169262
@Override
170263
public boolean equals(Object o) {
171264
if (this == o) return true;
@@ -179,12 +272,12 @@ public boolean equals(Object o) {
179272
Objects.equals(retryTimeout, request.retryTimeout) &&
180273
Objects.equals(idleShardRetryDelay, request.idleShardRetryDelay) &&
181274
Objects.equals(leaderIndex, request.leaderIndex) &&
182-
Objects.equals(followIndex, request.followIndex);
275+
Objects.equals(followerIndex, request.followerIndex);
183276
}
184277

185278
@Override
186279
public int hashCode() {
187-
return Objects.hash(leaderIndex, followIndex, maxBatchOperationCount, maxConcurrentReadBatches, maxOperationSizeInBytes,
280+
return Objects.hash(leaderIndex, followerIndex, maxBatchOperationCount, maxConcurrentReadBatches, maxOperationSizeInBytes,
188281
maxConcurrentWriteBatches, maxWriteBufferSize, retryTimeout, idleShardRetryDelay);
189282
}
190283
}
@@ -229,7 +322,7 @@ public TransportAction(Settings settings, ThreadPool threadPool, TransportServic
229322
@Override
230323
protected void doExecute(Request request, ActionListener<Response> listener) {
231324
ClusterState localClusterState = clusterService.state();
232-
IndexMetaData followIndexMetadata = localClusterState.getMetaData().index(request.followIndex);
325+
IndexMetaData followIndexMetadata = localClusterState.getMetaData().index(request.followerIndex);
233326

234327
String[] indices = new String[]{request.leaderIndex};
235328
Map<String, List<String>> remoteClusterIndices = remoteClusterService.groupClusterIndices(indices, s -> false);
@@ -390,7 +483,7 @@ static void validate(Request request, IndexMetaData leaderIndex, IndexMetaData f
390483
throw new IllegalArgumentException("leader index [" + request.leaderIndex + "] does not exist");
391484
}
392485
if (followIndex == null) {
393-
throw new IllegalArgumentException("follow index [" + request.followIndex + "] does not exist");
486+
throw new IllegalArgumentException("follow index [" + request.followerIndex + "] does not exist");
394487
}
395488
if (leaderIndex.getSettings().getAsBoolean(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), false) == false) {
396489
throw new IllegalArgumentException("leader index [" + request.leaderIndex + "] does not have soft deletes enabled");

0 commit comments

Comments
 (0)