99
1010package org .elasticsearch .cluster .metadata ;
1111
12- import org .elasticsearch .TransportVersions ;
1312import org .elasticsearch .common .io .stream .StreamInput ;
1413import org .elasticsearch .common .io .stream .StreamOutput ;
1514import org .elasticsearch .common .io .stream .Writeable ;
2625import java .util .OptionalDouble ;
2726import java .util .OptionalLong ;
2827
28+ import static org .elasticsearch .TransportVersions .INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD ;
29+ import static org .elasticsearch .TransportVersions .INDEX_STATS_AND_METADATA_INCLUDE_PEAK_WRITE_LOAD ;
30+
2931public class IndexWriteLoad implements Writeable , ToXContentFragment {
3032 public static final ParseField SHARDS_WRITE_LOAD_FIELD = new ParseField ("loads" );
3133 public static final ParseField SHARDS_UPTIME_IN_MILLIS = new ParseField ("uptimes" );
3234 public static final ParseField SHARDS_RECENT_WRITE_LOAD_FIELD = new ParseField ("recent_loads" );
35+ public static final ParseField SHARDS_PEAK_WRITE_LOAD_FIELD = new ParseField ("peak_loads" );
3336 private static final Double UNKNOWN_LOAD = -1.0 ;
3437 private static final long UNKNOWN_UPTIME = -1 ;
3538
3639 @ SuppressWarnings ("unchecked" )
3740 private static final ConstructingObjectParser <IndexWriteLoad , Void > PARSER = new ConstructingObjectParser <>(
3841 "index_write_load_parser" ,
3942 false ,
40- (args , unused ) -> IndexWriteLoad .create ((List <Double >) args [0 ], (List <Long >) args [1 ], (List <Double >) args [2 ])
43+ (args , unused ) -> IndexWriteLoad .create (
44+ (List <Double >) args [0 ],
45+ (List <Long >) args [1 ],
46+ (List <Double >) args [2 ],
47+ (List <Double >) args [3 ]
48+ )
4149 );
4250
4351 static {
4452 PARSER .declareDoubleArray (ConstructingObjectParser .constructorArg (), SHARDS_WRITE_LOAD_FIELD );
4553 PARSER .declareLongArray (ConstructingObjectParser .constructorArg (), SHARDS_UPTIME_IN_MILLIS );
46- // The recent write load field is optional so that we can parse XContent built by older versions which did not include it :
54+ // The recent and peak write load fields are optional so that we can parse XContent built by older versions which did not have them :
4755 PARSER .declareDoubleArray (ConstructingObjectParser .optionalConstructorArg (), SHARDS_RECENT_WRITE_LOAD_FIELD );
56+ PARSER .declareDoubleArray (ConstructingObjectParser .optionalConstructorArg (), SHARDS_PEAK_WRITE_LOAD_FIELD );
4857 }
4958
5059 private static IndexWriteLoad create (
5160 List <Double > shardsWriteLoad ,
5261 List <Long > shardsUptimeInMillis ,
53- @ Nullable List <Double > shardsRecentWriteLoad
62+ @ Nullable List <Double > shardsRecentWriteLoad ,
63+ @ Nullable List <Double > shardsPeakWriteLoad
5464 ) {
5565 if (shardsWriteLoad .size () != shardsUptimeInMillis .size ()) {
5666 assert false : "IndexWriteLoad.create() was called with non-matched lengths for shardWriteLoad and shardUptimeInMillis" ;
@@ -72,8 +82,19 @@ private static IndexWriteLoad create(
7282 if (shardsRecentWriteLoad != null && shardsRecentWriteLoad .size () != shardsUptimeInMillis .size ()) {
7383 assert false : "IndexWriteLoad.create() was called with non-matched lengths for shardsRecentWriteLoad and shardUptimeInMillis" ;
7484 throw new IllegalArgumentException (
75- "The same number of shard write loads and shard uptimes should be provided, but "
76- + shardsWriteLoad
85+ "The same number of shard recent write loads and shard uptimes should be provided, but "
86+ + shardsRecentWriteLoad
87+ + " "
88+ + shardsUptimeInMillis
89+ + " were provided"
90+ );
91+ }
92+
93+ if (shardsPeakWriteLoad != null && shardsPeakWriteLoad .size () != shardsUptimeInMillis .size ()) {
94+ assert false : "IndexWriteLoad.create() was called with non-matched lengths for shardsPeakWriteLoad and shardUptimeInMillis" ;
95+ throw new IllegalArgumentException (
96+ "The same number of shard peak write loads and shard uptimes should be provided, but "
97+ + shardsPeakWriteLoad
7798 + " "
7899 + shardsUptimeInMillis
79100 + " were provided"
@@ -83,15 +104,22 @@ private static IndexWriteLoad create(
83104 return new IndexWriteLoad (
84105 shardsWriteLoad .stream ().mapToDouble (shardLoad -> shardLoad ).toArray (),
85106 shardsUptimeInMillis .stream ().mapToLong (shardUptime -> shardUptime ).toArray (),
86- shardsRecentWriteLoad != null ? shardsRecentWriteLoad .stream ().mapToDouble (shardLoad -> shardLoad ).toArray () : null
107+ shardsRecentWriteLoad != null ? shardsRecentWriteLoad .stream ().mapToDouble (shardLoad -> shardLoad ).toArray () : null ,
108+ shardsPeakWriteLoad != null ? shardsPeakWriteLoad .stream ().mapToDouble (shardLoad -> shardLoad ).toArray () : null
87109 );
88110 }
89111
90112 private final double [] shardWriteLoad ;
91113 private final long [] shardUptimeInMillis ;
92114 private final double [] shardRecentWriteLoad ;
115+ private final double [] shardPeakWriteLoad ;
93116
94- private IndexWriteLoad (double [] shardWriteLoad , long [] shardUptimeInMillis , @ Nullable double [] shardRecentWriteLoad ) {
117+ private IndexWriteLoad (
118+ double [] shardWriteLoad ,
119+ long [] shardUptimeInMillis ,
120+ @ Nullable double [] shardRecentWriteLoad ,
121+ @ Nullable double [] shardPeakWriteLoad
122+ ) {
95123 assert shardWriteLoad .length == shardUptimeInMillis .length
96124 : "IndexWriteLoad constructor was called with non-matched lengths for shardWriteLoad and shardUptimeInMillis" ;
97125 this .shardWriteLoad = shardWriteLoad ;
@@ -104,30 +132,43 @@ private IndexWriteLoad(double[] shardWriteLoad, long[] shardUptimeInMillis, @Nul
104132 this .shardRecentWriteLoad = new double [shardUptimeInMillis .length ];
105133 Arrays .fill (this .shardRecentWriteLoad , UNKNOWN_LOAD );
106134 }
135+ if (shardPeakWriteLoad != null ) {
136+ assert shardPeakWriteLoad .length == shardUptimeInMillis .length
137+ : "IndexWriteLoad constructor was called with non-matched lengths for shardPeakWriteLoad and shardUptimeInMillis" ;
138+ this .shardPeakWriteLoad = shardPeakWriteLoad ;
139+ } else {
140+ this .shardPeakWriteLoad = new double [shardUptimeInMillis .length ];
141+ Arrays .fill (this .shardPeakWriteLoad , UNKNOWN_LOAD );
142+ }
107143 }
108144
109145 public IndexWriteLoad (StreamInput in ) throws IOException {
110146 this (
111147 in .readDoubleArray (),
112148 in .readLongArray (),
113- in .getTransportVersion ().onOrAfter (TransportVersions .INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD ) ? in .readDoubleArray () : null
149+ in .getTransportVersion ().onOrAfter (INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD ) ? in .readDoubleArray () : null ,
150+ in .getTransportVersion ().onOrAfter (INDEX_STATS_AND_METADATA_INCLUDE_PEAK_WRITE_LOAD ) ? in .readDoubleArray () : null
114151 );
115152 }
116153
117154 @ Override
118155 public void writeTo (StreamOutput out ) throws IOException {
119156 out .writeDoubleArray (shardWriteLoad );
120157 out .writeLongArray (shardUptimeInMillis );
121- if (out .getTransportVersion ().onOrAfter (TransportVersions . INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD )) {
158+ if (out .getTransportVersion ().onOrAfter (INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD )) {
122159 out .writeDoubleArray (shardRecentWriteLoad );
123160 }
161+ if (out .getTransportVersion ().onOrAfter (INDEX_STATS_AND_METADATA_INCLUDE_PEAK_WRITE_LOAD )) {
162+ out .writeDoubleArray (shardPeakWriteLoad );
163+ }
124164 }
125165
126166 @ Override
127167 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
128168 builder .field (SHARDS_WRITE_LOAD_FIELD .getPreferredName (), shardWriteLoad );
129169 builder .field (SHARDS_UPTIME_IN_MILLIS .getPreferredName (), shardUptimeInMillis );
130170 builder .field (SHARDS_RECENT_WRITE_LOAD_FIELD .getPreferredName (), shardRecentWriteLoad );
171+ builder .field (SHARDS_PEAK_WRITE_LOAD_FIELD .getPreferredName (), shardPeakWriteLoad );
131172 return builder ;
132173 }
133174
@@ -149,6 +190,13 @@ public OptionalDouble getRecentWriteLoadForShard(int shardId) {
149190 return load != UNKNOWN_LOAD ? OptionalDouble .of (load ) : OptionalDouble .empty ();
150191 }
151192
193+ public OptionalDouble getPeakWriteLoadForShard (int shardId ) {
194+ assertShardInBounds (shardId );
195+
196+ double load = shardPeakWriteLoad [shardId ];
197+ return load != UNKNOWN_LOAD ? OptionalDouble .of (load ) : OptionalDouble .empty ();
198+ }
199+
152200 public OptionalLong getUptimeInMillisForShard (int shardId ) {
153201 assertShardInBounds (shardId );
154202
@@ -172,14 +220,16 @@ public boolean equals(Object o) {
172220 IndexWriteLoad that = (IndexWriteLoad ) o ;
173221 return Arrays .equals (shardWriteLoad , that .shardWriteLoad )
174222 && Arrays .equals (shardUptimeInMillis , that .shardUptimeInMillis )
175- && Arrays .equals (shardRecentWriteLoad , that .shardRecentWriteLoad );
223+ && Arrays .equals (shardRecentWriteLoad , that .shardRecentWriteLoad )
224+ && Arrays .equals (shardPeakWriteLoad , that .shardPeakWriteLoad );
176225 }
177226
178227 @ Override
179228 public int hashCode () {
180229 int result = Arrays .hashCode (shardWriteLoad );
181230 result = 31 * result + Arrays .hashCode (shardUptimeInMillis );
182231 result = 31 * result + Arrays .hashCode (shardRecentWriteLoad );
232+ result = 31 * result + Arrays .hashCode (shardPeakWriteLoad );
183233 return result ;
184234 }
185235
@@ -192,30 +242,34 @@ public static class Builder {
192242 private final double [] shardWriteLoad ;
193243 private final long [] uptimeInMillis ;
194244 private final double [] shardRecentWriteLoad ;
245+ private final double [] shardPeakWriteLoad ;
195246
196247 private Builder (int numShards ) {
197248 this .shardWriteLoad = new double [numShards ];
198249 this .uptimeInMillis = new long [numShards ];
199250 this .shardRecentWriteLoad = new double [numShards ];
251+ this .shardPeakWriteLoad = new double [numShards ];
200252 Arrays .fill (shardWriteLoad , UNKNOWN_LOAD );
201253 Arrays .fill (uptimeInMillis , UNKNOWN_UPTIME );
202254 Arrays .fill (shardRecentWriteLoad , UNKNOWN_LOAD );
255+ Arrays .fill (shardPeakWriteLoad , UNKNOWN_LOAD );
203256 }
204257
205- public Builder withShardWriteLoad (int shardId , double load , double recentLoad , long uptimeInMillis ) {
258+ public Builder withShardWriteLoad (int shardId , double load , double recentLoad , double peakLoad , long uptimeInMillis ) {
206259 if (shardId >= this .shardWriteLoad .length ) {
207260 throw new IllegalArgumentException ();
208261 }
209262
210263 this .shardWriteLoad [shardId ] = load ;
211264 this .uptimeInMillis [shardId ] = uptimeInMillis ;
212265 this .shardRecentWriteLoad [shardId ] = recentLoad ;
266+ this .shardPeakWriteLoad [shardId ] = peakLoad ;
213267
214268 return this ;
215269 }
216270
217271 public IndexWriteLoad build () {
218- return new IndexWriteLoad (shardWriteLoad , uptimeInMillis , shardRecentWriteLoad );
272+ return new IndexWriteLoad (shardWriteLoad , uptimeInMillis , shardRecentWriteLoad , shardPeakWriteLoad );
219273 }
220274 }
221275}
0 commit comments