Skip to content

Commit 0beb3c9

Browse files
authored
Clean up duplicate follow config parameter code (#37688)
Introduced FollowParameters class that put follow, resume follow, put auto follow pattern requests and follow info response classes reuse. The FollowParameters class had the fields, getters etc. for the common parameters that all these APIs have. Also binary and xcontent serialization / parsing is handled by this class. The follow, resume follow, put auto follow pattern request classes originally used optional non primitive fields, so FollowParameters has that too and the follow info api can handle that now too. Also the followerIndex field can in production only be specified via the url path. If it is also specified via the request body then it must have the same value as is specified in the url path. This option only existed to xcontent testing. However the AbstractSerializingTestCase base class now also supports createXContextTestInstance() to provide a different test instance when testing xcontent, so allowing followerIndex to be specified via the request body is no longer needed. By moving the followerIndex field from Body to ResumeFollowAction.Request class and not allowing the followerIndex field to be specified via the request body the Body class is redundant and can be removed. The ResumeFollowAction.Request class can then directly use the FollowParameters class. For consistency I also removed the ability to specified followerIndex in the put follow api and the name in put auto follow pattern api via the request body.
1 parent 2f6afd2 commit 0beb3c9

25 files changed

+705
-1016
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/PutFollowRequest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public final class PutFollowRequest extends FollowConfig implements Validatable,
3232

3333
static final ParseField REMOTE_CLUSTER_FIELD = new ParseField("remote_cluster");
3434
static final ParseField LEADER_INDEX_FIELD = new ParseField("leader_index");
35-
static final ParseField FOLLOWER_INDEX_FIELD = new ParseField("follower_index");
3635

3736
private final String remoteCluster;
3837
private final String leaderIndex;
@@ -55,7 +54,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
5554
builder.startObject();
5655
builder.field(REMOTE_CLUSTER_FIELD.getPreferredName(), remoteCluster);
5756
builder.field(LEADER_INDEX_FIELD.getPreferredName(), leaderIndex);
58-
builder.field(FOLLOWER_INDEX_FIELD.getPreferredName(), followerIndex);
5957
toXContentFragment(builder, params);
6058
builder.endObject();
6159
return builder;

client/rest-high-level/src/main/java/org/elasticsearch/client/ccr/ResumeFollowRequest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.io.IOException;
2727
import java.util.Objects;
2828

29-
import static org.elasticsearch.client.ccr.PutFollowRequest.FOLLOWER_INDEX_FIELD;
30-
3129
public final class ResumeFollowRequest extends FollowConfig implements Validatable, ToXContentObject {
3230

3331
private final String followerIndex;
@@ -39,7 +37,6 @@ public ResumeFollowRequest(String followerIndex) {
3937
@Override
4038
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
4139
builder.startObject();
42-
builder.field(FOLLOWER_INDEX_FIELD.getPreferredName(), followerIndex);
4340
toXContentFragment(builder, params);
4441
builder.endObject();
4542
return builder;

client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/PutFollowRequestTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@
3131
public class PutFollowRequestTests extends AbstractXContentTestCase<PutFollowRequest> {
3232

3333
private static final ConstructingObjectParser<PutFollowRequest, Void> PARSER = new ConstructingObjectParser<>("test_parser",
34-
true, (args) -> new PutFollowRequest((String) args[0], (String) args[1], (String) args[2]));
34+
true, (args) -> new PutFollowRequest((String) args[0], (String) args[1], "followerIndex"));
3535

3636
static {
3737
PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.REMOTE_CLUSTER_FIELD);
3838
PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.LEADER_INDEX_FIELD);
39-
PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.FOLLOWER_INDEX_FIELD);
4039
PARSER.declareInt(PutFollowRequest::setMaxReadRequestOperationCount, PutFollowRequest.MAX_READ_REQUEST_OPERATION_COUNT);
4140
PARSER.declareField(
4241
PutFollowRequest::setMaxReadRequestSize,
@@ -82,7 +81,7 @@ protected boolean supportsUnknownFields() {
8281
@Override
8382
protected PutFollowRequest createTestInstance() {
8483
PutFollowRequest putFollowRequest =
85-
new PutFollowRequest(randomAlphaOfLength(4), randomAlphaOfLength(4), randomAlphaOfLength(4));
84+
new PutFollowRequest(randomAlphaOfLength(4), randomAlphaOfLength(4), "followerIndex");
8685
if (randomBoolean()) {
8786
putFollowRequest.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE));
8887
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ccr/ResumeFollowRequestTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.elasticsearch.common.unit.ByteSizeValue;
2323
import org.elasticsearch.common.unit.TimeValue;
24-
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2524
import org.elasticsearch.common.xcontent.ObjectParser;
2625
import org.elasticsearch.common.xcontent.XContentParser;
2726
import org.elasticsearch.test.AbstractXContentTestCase;
@@ -30,11 +29,10 @@
3029

3130
public class ResumeFollowRequestTests extends AbstractXContentTestCase<ResumeFollowRequest> {
3231

33-
private static final ConstructingObjectParser<ResumeFollowRequest, Void> PARSER = new ConstructingObjectParser<>("test_parser",
34-
true, (args) -> new ResumeFollowRequest((String) args[0]));
32+
private static final ObjectParser<ResumeFollowRequest, Void> PARSER = new ObjectParser<>("test_parser",
33+
true, () -> new ResumeFollowRequest("followerIndex"));
3534

3635
static {
37-
PARSER.declareString(ConstructingObjectParser.constructorArg(), PutFollowRequest.FOLLOWER_INDEX_FIELD);
3836
PARSER.declareInt(ResumeFollowRequest::setMaxReadRequestOperationCount, FollowConfig.MAX_READ_REQUEST_OPERATION_COUNT);
3937
PARSER.declareField(
4038
ResumeFollowRequest::setMaxReadRequestSize,
@@ -79,7 +77,7 @@ protected boolean supportsUnknownFields() {
7977

8078
@Override
8179
protected ResumeFollowRequest createTestInstance() {
82-
ResumeFollowRequest resumeFollowRequest = new ResumeFollowRequest(randomAlphaOfLength(4));
80+
ResumeFollowRequest resumeFollowRequest = new ResumeFollowRequest("followerIndex");
8381
if (randomBoolean()) {
8482
resumeFollowRequest.setMaxOutstandingReadRequests(randomIntBetween(0, Integer.MAX_VALUE));
8583
}

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.elasticsearch.xpack.core.ccr.AutoFollowMetadata.AutoFollowPattern;
4242
import org.elasticsearch.xpack.core.ccr.AutoFollowStats;
4343
import org.elasticsearch.xpack.core.ccr.action.PutFollowAction;
44-
import org.elasticsearch.xpack.core.ccr.action.ResumeFollowAction;
4544

4645
import java.util.ArrayList;
4746
import java.util.Collections;
@@ -514,23 +513,20 @@ private void followLeaderIndex(String autoFollowPattenName,
514513
final String leaderIndexName = indexToFollow.getName();
515514
final String followIndexName = getFollowerIndexName(pattern, leaderIndexName);
516515

517-
ResumeFollowAction.Request followRequest = new ResumeFollowAction.Request();
518-
followRequest.setFollowerIndex(followIndexName);
519-
followRequest.setMaxReadRequestOperationCount(pattern.getMaxReadRequestOperationCount());
520-
followRequest.setMaxReadRequestSize(pattern.getMaxReadRequestSize());
521-
followRequest.setMaxOutstandingReadRequests(pattern.getMaxOutstandingReadRequests());
522-
followRequest.setMaxWriteRequestOperationCount(pattern.getMaxWriteRequestOperationCount());
523-
followRequest.setMaxWriteRequestSize(pattern.getMaxWriteRequestSize());
524-
followRequest.setMaxOutstandingWriteRequests(pattern.getMaxOutstandingWriteRequests());
525-
followRequest.setMaxWriteBufferCount(pattern.getMaxWriteBufferCount());
526-
followRequest.setMaxWriteBufferSize(pattern.getMaxWriteBufferSize());
527-
followRequest.setMaxRetryDelay(pattern.getMaxRetryDelay());
528-
followRequest.setReadPollTimeout(pattern.getPollTimeout());
529-
530516
PutFollowAction.Request request = new PutFollowAction.Request();
531517
request.setRemoteCluster(remoteCluster);
532518
request.setLeaderIndex(indexToFollow.getName());
533-
request.setFollowRequest(followRequest);
519+
request.setFollowerIndex(followIndexName);
520+
request.getParameters().setMaxReadRequestOperationCount(pattern.getMaxReadRequestOperationCount());
521+
request.getParameters().setMaxReadRequestSize(pattern.getMaxReadRequestSize());
522+
request.getParameters().setMaxOutstandingReadRequests(pattern.getMaxOutstandingReadRequests());
523+
request.getParameters().setMaxWriteRequestOperationCount(pattern.getMaxWriteRequestOperationCount());
524+
request.getParameters().setMaxWriteRequestSize(pattern.getMaxWriteRequestSize());
525+
request.getParameters().setMaxOutstandingWriteRequests(pattern.getMaxOutstandingWriteRequests());
526+
request.getParameters().setMaxWriteBufferCount(pattern.getMaxWriteBufferCount());
527+
request.getParameters().setMaxWriteBufferSize(pattern.getMaxWriteBufferSize());
528+
request.getParameters().setMaxRetryDelay(pattern.getMaxRetryDelay());
529+
request.getParameters().setReadPollTimeout(pattern.getPollTimeout());
534530

535531
// Execute if the create and follow api call succeeds:
536532
Runnable successHandler = () -> {

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.elasticsearch.transport.TransportService;
2323
import org.elasticsearch.xpack.ccr.Ccr;
2424
import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction;
25-
import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction.Response.FollowParameters;
25+
import org.elasticsearch.xpack.core.ccr.action.FollowParameters;
2626
import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction.Response.FollowerInfo;
2727
import org.elasticsearch.xpack.core.ccr.action.FollowInfoAction.Response.Status;
2828

@@ -97,18 +97,17 @@ static List<FollowerInfo> getFollowInfos(List<String> concreteFollowerIndices, C
9797
String leaderIndex = ccrCustomData.get(Ccr.CCR_CUSTOM_METADATA_LEADER_INDEX_NAME_KEY);
9898
if (result.isPresent()) {
9999
ShardFollowTask params = result.get();
100-
FollowParameters followParameters = new FollowParameters(
101-
params.getMaxReadRequestOperationCount(),
102-
params.getMaxReadRequestSize(),
103-
params.getMaxOutstandingReadRequests(),
104-
params.getMaxWriteRequestOperationCount(),
105-
params.getMaxWriteRequestSize(),
106-
params.getMaxOutstandingWriteRequests(),
107-
params.getMaxWriteBufferCount(),
108-
params.getMaxWriteBufferSize(),
109-
params.getMaxRetryDelay(),
110-
params.getReadPollTimeout()
111-
);
100+
FollowParameters followParameters = new FollowParameters();
101+
followParameters.setMaxOutstandingReadRequests(params.getMaxOutstandingReadRequests());
102+
followParameters.setMaxOutstandingWriteRequests(params.getMaxOutstandingWriteRequests());
103+
followParameters.setMaxReadRequestOperationCount(params.getMaxReadRequestOperationCount());
104+
followParameters.setMaxWriteRequestOperationCount(params.getMaxWriteRequestOperationCount());
105+
followParameters.setMaxReadRequestSize(params.getMaxReadRequestSize());
106+
followParameters.setMaxWriteRequestSize(params.getMaxWriteRequestSize());
107+
followParameters.setMaxWriteBufferCount(params.getMaxWriteBufferCount());
108+
followParameters.setMaxWriteBufferSize(params.getMaxWriteBufferSize());
109+
followParameters.setMaxRetryDelay(params.getMaxRetryDelay());
110+
followParameters.setReadPollTimeout(params.getReadPollTimeout());
112111
followerInfos.add(new FollowerInfo(followerIndex, remoteCluster, leaderIndex, Status.ACTIVE, followParameters));
113112
} else {
114113
followerInfos.add(new FollowerInfo(followerIndex, remoteCluster, leaderIndex, Status.PAUSED, null));

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ static ClusterState innerPut(PutAutoFollowPatternAction.Request request,
147147
markExistingIndicesAsAutoFollowedForNewPatterns(request.getLeaderIndexPatterns(), remoteClusterState.metaData(),
148148
previousPattern, followedIndexUUIDs);
149149
} else {
150-
markExistingIndicesAsAutoFollowed(request.getLeaderIndexPatterns(), remoteClusterState.metaData(),
151-
followedIndexUUIDs);
150+
markExistingIndicesAsAutoFollowed(request.getLeaderIndexPatterns(), remoteClusterState.metaData(), followedIndexUUIDs);
152151
}
153152

154153
if (filteredHeaders != null) {
@@ -159,16 +158,16 @@ static ClusterState innerPut(PutAutoFollowPatternAction.Request request,
159158
request.getRemoteCluster(),
160159
request.getLeaderIndexPatterns(),
161160
request.getFollowIndexNamePattern(),
162-
request.getMaxReadRequestOperationCount(),
163-
request.getMaxReadRequestSize(),
164-
request.getMaxConcurrentReadBatches(),
165-
request.getMaxWriteRequestOperationCount(),
166-
request.getMaxWriteRequestSize(),
167-
request.getMaxConcurrentWriteBatches(),
168-
request.getMaxWriteBufferCount(),
169-
request.getMaxWriteBufferSize(),
170-
request.getMaxRetryDelay(),
171-
request.getReadPollTimeout());
161+
request.getParameters().getMaxReadRequestOperationCount(),
162+
request.getParameters().getMaxReadRequestSize(),
163+
request.getParameters().getMaxOutstandingReadRequests(),
164+
request.getParameters().getMaxWriteRequestOperationCount(),
165+
request.getParameters().getMaxWriteRequestSize(),
166+
request.getParameters().getMaxOutstandingWriteRequests(),
167+
request.getParameters().getMaxWriteBufferCount(),
168+
request.getParameters().getMaxWriteBufferSize(),
169+
request.getParameters().getMaxRetryDelay(),
170+
request.getParameters().getReadPollTimeout());
172171
patterns.put(request.getName(), autoFollowPattern);
173172
ClusterState.Builder newState = ClusterState.builder(localState);
174173
newState.metaData(MetaData.builder(localState.getMetaData())

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.xpack.ccr.CcrLicenseChecker;
3939
import org.elasticsearch.xpack.ccr.CcrSettings;
4040
import org.elasticsearch.xpack.ccr.repository.CcrRepository;
41+
import org.elasticsearch.xpack.core.ccr.action.FollowParameters;
4142
import org.elasticsearch.xpack.core.ccr.action.PutFollowAction;
4243
import org.elasticsearch.xpack.core.ccr.action.ResumeFollowAction;
4344

@@ -126,18 +127,18 @@ private void createFollowerIndex(
126127
// soft deletes are enabled by default on indices created on 7.0.0 or later
127128
if (leaderIndexMetaData.getSettings().getAsBoolean(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(),
128129
IndexMetaData.SETTING_INDEX_VERSION_CREATED.get(leaderIndexMetaData.getSettings()).onOrAfter(Version.V_7_0_0)) == false) {
129-
listener.onFailure(
130-
new IllegalArgumentException("leader index [" + request.getLeaderIndex() + "] does not have soft deletes enabled"));
130+
listener.onFailure(new IllegalArgumentException("leader index [" + request.getLeaderIndex() +
131+
"] does not have soft deletes enabled"));
131132
return;
132133
}
133134

134135
final Settings.Builder settingsBuilder = Settings.builder()
135-
.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getFollowRequest().getFollowerIndex())
136+
.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getFollowerIndex())
136137
.put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true);
137138
final String leaderClusterRepoName = CcrRepository.NAME_PREFIX + request.getRemoteCluster();
138139
final RestoreSnapshotRequest restoreRequest = new RestoreSnapshotRequest(leaderClusterRepoName, CcrRepository.LATEST)
139140
.indices(request.getLeaderIndex()).indicesOptions(request.indicesOptions()).renamePattern("^(.*)$")
140-
.renameReplacement(request.getFollowRequest().getFollowerIndex()).masterNodeTimeout(request.masterNodeTimeout())
141+
.renameReplacement(request.getFollowerIndex()).masterNodeTimeout(request.masterNodeTimeout())
141142
.indexSettings(settingsBuilder);
142143

143144
final Client clientWithHeaders = CcrLicenseChecker.wrapClient(this.client, threadPool.getThreadContext().getHeaders());
@@ -217,10 +218,14 @@ private void initiateFollowing(
217218
final PutFollowAction.Request request,
218219
final ActionListener<PutFollowAction.Response> listener) {
219220
assert request.waitForActiveShards() != ActiveShardCount.DEFAULT : "PutFollowAction does not support DEFAULT.";
220-
activeShardsObserver.waitForActiveShards(new String[]{request.getFollowRequest().getFollowerIndex()},
221+
activeShardsObserver.waitForActiveShards(new String[]{request.getFollowerIndex()},
221222
request.waitForActiveShards(), request.timeout(), result -> {
222223
if (result) {
223-
client.execute(ResumeFollowAction.INSTANCE, request.getFollowRequest(), ActionListener.wrap(
224+
FollowParameters parameters = request.getParameters();
225+
ResumeFollowAction.Request resumeFollowRequest = new ResumeFollowAction.Request();
226+
resumeFollowRequest.setFollowerIndex(request.getFollowerIndex());
227+
resumeFollowRequest.setParameters(new FollowParameters(parameters));
228+
client.execute(ResumeFollowAction.INSTANCE, resumeFollowRequest, ActionListener.wrap(
224229
r -> listener.onResponse(new PutFollowAction.Response(true, true, r.isAcknowledged())),
225230
listener::onFailure
226231
));
@@ -232,6 +237,6 @@ private void initiateFollowing(
232237

233238
@Override
234239
protected ClusterBlockException checkBlock(final PutFollowAction.Request request, final ClusterState state) {
235-
return state.blocks().indexBlockedException(ClusterBlockLevel.METADATA_WRITE, request.getFollowRequest().getFollowerIndex());
240+
return state.blocks().indexBlockedException(ClusterBlockLevel.METADATA_WRITE, request.getFollowerIndex());
236241
}
237242
}

0 commit comments

Comments
 (0)