2323import java .util .regex .Matcher ;
2424import java .util .regex .Pattern ;
2525
26+ import org .apache .commons .lang3 .Validate ;
2627import org .apache .hadoop .classification .InterfaceAudience ;
2728import org .apache .hadoop .fs .Path ;
2829import org .apache .hadoop .hdfs .DFSUtil ;
@@ -287,6 +288,10 @@ enum ReplicaState {
287288 /** Temporary replica: created for replication and relocation only. */
288289 TEMPORARY (4 );
289290
291+ // Since ReplicaState (de)serialization depends on ordinal, either adding
292+ // new value should be avoided to this enum or newly appended value should
293+ // be handled by NameNodeLayoutVersion#Feature.
294+
290295 private static final ReplicaState [] cachedValues = ReplicaState .values ();
291296
292297 private final int value ;
@@ -299,13 +304,32 @@ public int getValue() {
299304 return value ;
300305 }
301306
307+ /**
308+ * Retrieve ReplicaState corresponding to given index.
309+ *
310+ * @param v Index to retrieve {@link ReplicaState}.
311+ * @return {@link ReplicaState} object.
312+ * @throws IndexOutOfBoundsException if the index is invalid.
313+ */
302314 public static ReplicaState getState (int v ) {
315+ Validate .validIndex (cachedValues , v , "Index Expected range: [0, "
316+ + (cachedValues .length - 1 ) + "]. Actual value: " + v );
303317 return cachedValues [v ];
304318 }
305319
306- /** Read from in */
320+ /**
321+ * Retrieve ReplicaState corresponding to index provided in binary stream.
322+ *
323+ * @param in Index value provided as bytes in given binary stream.
324+ * @return {@link ReplicaState} object.
325+ * @throws IOException if an I/O error occurs while reading bytes.
326+ * @throws IndexOutOfBoundsException if the index is invalid.
327+ */
307328 public static ReplicaState read (DataInput in ) throws IOException {
308- return cachedValues [in .readByte ()];
329+ byte idx = in .readByte ();
330+ Validate .validIndex (cachedValues , idx , "Index Expected range: [0, "
331+ + (cachedValues .length - 1 ) + "]. Actual value: " + idx );
332+ return cachedValues [idx ];
309333 }
310334
311335 /** Write to out */
0 commit comments