2222import org .elasticsearch .cluster .routing .allocation .decider .EnableAllocationDecider ;
2323import org .elasticsearch .cluster .routing .allocation .decider .ShardsLimitAllocationDecider ;
2424import org .elasticsearch .cluster .service .ClusterService ;
25+ import org .elasticsearch .common .ParseField ;
2526import org .elasticsearch .common .inject .Inject ;
2627import org .elasticsearch .common .io .stream .StreamInput ;
2728import org .elasticsearch .common .io .stream .StreamOutput ;
2829import org .elasticsearch .common .settings .Setting ;
2930import org .elasticsearch .common .settings .Settings ;
3031import 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 ;
3137import org .elasticsearch .index .IndexSettings ;
3238import org .elasticsearch .index .IndexingSlowLog ;
3339import 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