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" ;
@@ -83,15 +93,22 @@ private static IndexWriteLoad create(
8393 return new IndexWriteLoad (
8494 shardsWriteLoad .stream ().mapToDouble (shardLoad -> shardLoad ).toArray (),
8595 shardsUptimeInMillis .stream ().mapToLong (shardUptime -> shardUptime ).toArray (),
86- shardsRecentWriteLoad != null ? shardsRecentWriteLoad .stream ().mapToDouble (shardLoad -> shardLoad ).toArray () : null
96+ shardsRecentWriteLoad != null ? shardsRecentWriteLoad .stream ().mapToDouble (shardLoad -> shardLoad ).toArray () : null ,
97+ shardsPeakWriteLoad != null ? shardsPeakWriteLoad .stream ().mapToDouble (shardLoad -> shardLoad ).toArray () : null
8798 );
8899 }
89100
90101 private final double [] shardWriteLoad ;
91102 private final long [] shardUptimeInMillis ;
92103 private final double [] shardRecentWriteLoad ;
104+ private final double [] shardPeakWriteLoad ;
93105
94- private IndexWriteLoad (double [] shardWriteLoad , long [] shardUptimeInMillis , @ Nullable double [] shardRecentWriteLoad ) {
106+ private IndexWriteLoad (
107+ double [] shardWriteLoad ,
108+ long [] shardUptimeInMillis ,
109+ @ Nullable double [] shardRecentWriteLoad ,
110+ @ Nullable double [] shardPeakWriteLoad
111+ ) {
95112 assert shardWriteLoad .length == shardUptimeInMillis .length
96113 : "IndexWriteLoad constructor was called with non-matched lengths for shardWriteLoad and shardUptimeInMillis" ;
97114 this .shardWriteLoad = shardWriteLoad ;
@@ -104,30 +121,43 @@ private IndexWriteLoad(double[] shardWriteLoad, long[] shardUptimeInMillis, @Nul
104121 this .shardRecentWriteLoad = new double [shardUptimeInMillis .length ];
105122 Arrays .fill (this .shardRecentWriteLoad , UNKNOWN_LOAD );
106123 }
124+ if (shardPeakWriteLoad != null ) {
125+ assert shardPeakWriteLoad .length == shardUptimeInMillis .length
126+ : "IndexWriteLoad constructor was called with non-matched lengths for shardRecentWriteLoad and shardUptimeInMillis" ;
127+ this .shardPeakWriteLoad = shardPeakWriteLoad ;
128+ } else {
129+ this .shardPeakWriteLoad = new double [shardUptimeInMillis .length ];
130+ Arrays .fill (this .shardPeakWriteLoad , UNKNOWN_LOAD );
131+ }
107132 }
108133
109134 public IndexWriteLoad (StreamInput in ) throws IOException {
110135 this (
111136 in .readDoubleArray (),
112137 in .readLongArray (),
113- in .getTransportVersion ().onOrAfter (TransportVersions .INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD ) ? in .readDoubleArray () : null
138+ in .getTransportVersion ().onOrAfter (INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD ) ? in .readDoubleArray () : null ,
139+ in .getTransportVersion ().onOrAfter (INDEX_STATS_AND_METADATA_INCLUDE_PEAK_WRITE_LOAD ) ? in .readDoubleArray () : null
114140 );
115141 }
116142
117143 @ Override
118144 public void writeTo (StreamOutput out ) throws IOException {
119145 out .writeDoubleArray (shardWriteLoad );
120146 out .writeLongArray (shardUptimeInMillis );
121- if (out .getTransportVersion ().onOrAfter (TransportVersions . INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD )) {
147+ if (out .getTransportVersion ().onOrAfter (INDEX_METADATA_INCLUDES_RECENT_WRITE_LOAD )) {
122148 out .writeDoubleArray (shardRecentWriteLoad );
123149 }
150+ if (out .getTransportVersion ().onOrAfter (INDEX_STATS_AND_METADATA_INCLUDE_PEAK_WRITE_LOAD )) {
151+ out .writeDoubleArray (shardPeakWriteLoad );
152+ }
124153 }
125154
126155 @ Override
127156 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
128157 builder .field (SHARDS_WRITE_LOAD_FIELD .getPreferredName (), shardWriteLoad );
129158 builder .field (SHARDS_UPTIME_IN_MILLIS .getPreferredName (), shardUptimeInMillis );
130159 builder .field (SHARDS_RECENT_WRITE_LOAD_FIELD .getPreferredName (), shardRecentWriteLoad );
160+ builder .field (SHARDS_PEAK_WRITE_LOAD_FIELD .getPreferredName (), shardPeakWriteLoad );
131161 return builder ;
132162 }
133163
@@ -149,6 +179,13 @@ public OptionalDouble getRecentWriteLoadForShard(int shardId) {
149179 return load != UNKNOWN_LOAD ? OptionalDouble .of (load ) : OptionalDouble .empty ();
150180 }
151181
182+ public OptionalDouble getPeakWriteLoadForShard (int shardId ) {
183+ assertShardInBounds (shardId );
184+
185+ double load = shardPeakWriteLoad [shardId ];
186+ return load != UNKNOWN_LOAD ? OptionalDouble .of (load ) : OptionalDouble .empty ();
187+ }
188+
152189 public OptionalLong getUptimeInMillisForShard (int shardId ) {
153190 assertShardInBounds (shardId );
154191
@@ -172,14 +209,16 @@ public boolean equals(Object o) {
172209 IndexWriteLoad that = (IndexWriteLoad ) o ;
173210 return Arrays .equals (shardWriteLoad , that .shardWriteLoad )
174211 && Arrays .equals (shardUptimeInMillis , that .shardUptimeInMillis )
175- && Arrays .equals (shardRecentWriteLoad , that .shardRecentWriteLoad );
212+ && Arrays .equals (shardRecentWriteLoad , that .shardRecentWriteLoad )
213+ && Arrays .equals (shardPeakWriteLoad , that .shardPeakWriteLoad );
176214 }
177215
178216 @ Override
179217 public int hashCode () {
180218 int result = Arrays .hashCode (shardWriteLoad );
181219 result = 31 * result + Arrays .hashCode (shardUptimeInMillis );
182220 result = 31 * result + Arrays .hashCode (shardRecentWriteLoad );
221+ result = 31 * result + Arrays .hashCode (shardPeakWriteLoad );
183222 return result ;
184223 }
185224
@@ -192,30 +231,34 @@ public static class Builder {
192231 private final double [] shardWriteLoad ;
193232 private final long [] uptimeInMillis ;
194233 private final double [] shardRecentWriteLoad ;
234+ private final double [] shardPeakWriteLoad ;
195235
196236 private Builder (int numShards ) {
197237 this .shardWriteLoad = new double [numShards ];
198238 this .uptimeInMillis = new long [numShards ];
199239 this .shardRecentWriteLoad = new double [numShards ];
240+ this .shardPeakWriteLoad = new double [numShards ];
200241 Arrays .fill (shardWriteLoad , UNKNOWN_LOAD );
201242 Arrays .fill (uptimeInMillis , UNKNOWN_UPTIME );
202243 Arrays .fill (shardRecentWriteLoad , UNKNOWN_LOAD );
244+ Arrays .fill (shardPeakWriteLoad , UNKNOWN_LOAD );
203245 }
204246
205- public Builder withShardWriteLoad (int shardId , double load , double recentLoad , long uptimeInMillis ) {
247+ public Builder withShardWriteLoad (int shardId , double load , double recentLoad , double peakLoad , long uptimeInMillis ) {
206248 if (shardId >= this .shardWriteLoad .length ) {
207249 throw new IllegalArgumentException ();
208250 }
209251
210252 this .shardWriteLoad [shardId ] = load ;
211253 this .uptimeInMillis [shardId ] = uptimeInMillis ;
212254 this .shardRecentWriteLoad [shardId ] = recentLoad ;
255+ this .shardPeakWriteLoad [shardId ] = peakLoad ;
213256
214257 return this ;
215258 }
216259
217260 public IndexWriteLoad build () {
218- return new IndexWriteLoad (shardWriteLoad , uptimeInMillis , shardRecentWriteLoad );
261+ return new IndexWriteLoad (shardWriteLoad , uptimeInMillis , shardRecentWriteLoad , shardPeakWriteLoad );
219262 }
220263 }
221264}
0 commit comments