88
99package org .elasticsearch .cluster .metadata ;
1010
11+ import org .apache .logging .log4j .message .ParameterizedMessage ;
12+ import org .elasticsearch .Version ;
1113import org .elasticsearch .cluster .AbstractDiffable ;
1214import org .elasticsearch .cluster .Diffable ;
1315import org .elasticsearch .common .io .stream .StreamInput ;
@@ -33,13 +35,16 @@ public class SingleNodeShutdownMetadata extends AbstractDiffable<SingleNodeShutd
3335 ToXContentObject ,
3436 Diffable <SingleNodeShutdownMetadata > {
3537
38+ public static final Version REPLACE_SHUTDOWN_TYPE_ADDED_VERSION = Version .V_8_0_0 ;
39+
3640 public static final ParseField NODE_ID_FIELD = new ParseField ("node_id" );
3741 public static final ParseField TYPE_FIELD = new ParseField ("type" );
3842 public static final ParseField REASON_FIELD = new ParseField ("reason" );
3943 public static final String STARTED_AT_READABLE_FIELD = "shutdown_started" ;
4044 public static final ParseField STARTED_AT_MILLIS_FIELD = new ParseField (STARTED_AT_READABLE_FIELD + "millis" );
4145 public static final ParseField ALLOCATION_DELAY_FIELD = new ParseField ("allocation_delay" );
4246 public static final ParseField NODE_SEEN_FIELD = new ParseField ("node_seen" );
47+ public static final ParseField TARGET_NODE_NAME_FIELD = new ParseField ("target_node_name" );
4348
4449 public static final ConstructingObjectParser <SingleNodeShutdownMetadata , Void > PARSER = new ConstructingObjectParser <>(
4550 "node_shutdown_info" ,
@@ -49,7 +54,8 @@ public class SingleNodeShutdownMetadata extends AbstractDiffable<SingleNodeShutd
4954 (String ) a [2 ],
5055 (long ) a [3 ],
5156 (boolean ) a [4 ],
52- (TimeValue ) a [5 ]
57+ (TimeValue ) a [5 ],
58+ (String ) a [6 ]
5359 )
5460 );
5561
@@ -64,6 +70,7 @@ public class SingleNodeShutdownMetadata extends AbstractDiffable<SingleNodeShutd
6470 (p , c ) -> TimeValue .parseTimeValue (p .textOrNull (), ALLOCATION_DELAY_FIELD .getPreferredName ()), ALLOCATION_DELAY_FIELD ,
6571 ObjectParser .ValueType .STRING_OR_NULL
6672 );
73+ PARSER .declareString (ConstructingObjectParser .optionalConstructorArg (), TARGET_NODE_NAME_FIELD );
6774 }
6875
6976 public static SingleNodeShutdownMetadata parse (XContentParser parser ) {
@@ -78,6 +85,7 @@ public static SingleNodeShutdownMetadata parse(XContentParser parser) {
7885 private final long startedAtMillis ;
7986 private final boolean nodeSeen ;
8087 @ Nullable private final TimeValue allocationDelay ;
88+ @ Nullable private final String targetNodeName ;
8189
8290 /**
8391 * @param nodeId The node ID that this shutdown metadata refers to.
@@ -91,7 +99,8 @@ private SingleNodeShutdownMetadata(
9199 String reason ,
92100 long startedAtMillis ,
93101 boolean nodeSeen ,
94- @ Nullable TimeValue allocationDelay
102+ @ Nullable TimeValue allocationDelay ,
103+ @ Nullable String targetNodeName
95104 ) {
96105 this .nodeId = Objects .requireNonNull (nodeId , "node ID must not be null" );
97106 this .type = Objects .requireNonNull (type , "shutdown type must not be null" );
@@ -102,6 +111,13 @@ private SingleNodeShutdownMetadata(
102111 throw new IllegalArgumentException ("shard allocation delay is only valid for RESTART-type shutdowns" );
103112 }
104113 this .allocationDelay = allocationDelay ;
114+ if (targetNodeName != null && type != Type .REPLACE ) {
115+ throw new IllegalArgumentException (new ParameterizedMessage ("target node name is only valid for REPLACE type shutdowns, " +
116+ "but was given type [{}] and target node name [{}]" , type , targetNodeName ).getFormattedMessage ());
117+ } else if (targetNodeName == null && type == Type .REPLACE ) {
118+ throw new IllegalArgumentException ("target node name is required for REPLACE type shutdowns" );
119+ }
120+ this .targetNodeName = targetNodeName ;
105121 }
106122
107123 public SingleNodeShutdownMetadata (StreamInput in ) throws IOException {
@@ -111,6 +127,11 @@ public SingleNodeShutdownMetadata(StreamInput in) throws IOException {
111127 this .startedAtMillis = in .readVLong ();
112128 this .nodeSeen = in .readBoolean ();
113129 this .allocationDelay = in .readOptionalTimeValue ();
130+ if (in .getVersion ().onOrAfter (REPLACE_SHUTDOWN_TYPE_ADDED_VERSION )) {
131+ this .targetNodeName = in .readOptionalString ();
132+ } else {
133+ this .targetNodeName = null ;
134+ }
114135 }
115136
116137 /**
@@ -148,6 +169,13 @@ public boolean getNodeSeen() {
148169 return nodeSeen ;
149170 }
150171
172+ /**
173+ * @return The name of the node to be used as a replacement for this node, or null.
174+ */
175+ public String getTargetNodeName () {
176+ return targetNodeName ;
177+ }
178+
151179 /**
152180 * @return The amount of time shard reallocation should be delayed for shards on this node, so that they will not be automatically
153181 * reassigned while the node is restarting. Will be {@code null} for non-restart shutdowns.
@@ -165,11 +193,18 @@ public TimeValue getAllocationDelay() {
165193 @ Override
166194 public void writeTo (StreamOutput out ) throws IOException {
167195 out .writeString (nodeId );
168- out .writeEnum (type );
196+ if (out .getVersion ().before (REPLACE_SHUTDOWN_TYPE_ADDED_VERSION ) && this .type == SingleNodeShutdownMetadata .Type .REPLACE ) {
197+ out .writeEnum (SingleNodeShutdownMetadata .Type .REMOVE );
198+ } else {
199+ out .writeEnum (type );
200+ }
169201 out .writeString (reason );
170202 out .writeVLong (startedAtMillis );
171203 out .writeBoolean (nodeSeen );
172204 out .writeOptionalTimeValue (allocationDelay );
205+ if (out .getVersion ().onOrAfter (REPLACE_SHUTDOWN_TYPE_ADDED_VERSION )) {
206+ out .writeOptionalString (targetNodeName );
207+ }
173208 }
174209
175210 @ Override
@@ -184,6 +219,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
184219 if (allocationDelay != null ) {
185220 builder .field (ALLOCATION_DELAY_FIELD .getPreferredName (), allocationDelay .getStringRep ());
186221 }
222+ if (targetNodeName != null ) {
223+ builder .field (TARGET_NODE_NAME_FIELD .getPreferredName (), targetNodeName );
224+ }
187225 }
188226 builder .endObject ();
189227
@@ -200,7 +238,8 @@ && getNodeId().equals(that.getNodeId())
200238 && getType () == that .getType ()
201239 && getReason ().equals (that .getReason ())
202240 && getNodeSeen () == that .getNodeSeen ()
203- && Objects .equals (allocationDelay , that .allocationDelay );
241+ && Objects .equals (allocationDelay , that .allocationDelay )
242+ && Objects .equals (targetNodeName , that .targetNodeName );
204243 }
205244
206245 @ Override
@@ -211,7 +250,8 @@ public int hashCode() {
211250 getReason (),
212251 getStartedAtMillis (),
213252 getNodeSeen (),
214- allocationDelay
253+ allocationDelay ,
254+ targetNodeName
215255 );
216256 }
217257
@@ -228,7 +268,8 @@ public static Builder builder(SingleNodeShutdownMetadata original) {
228268 .setType (original .getType ())
229269 .setReason (original .getReason ())
230270 .setStartedAtMillis (original .getStartedAtMillis ())
231- .setNodeSeen (original .getNodeSeen ());
271+ .setNodeSeen (original .getNodeSeen ())
272+ .setTargetNodeName (original .getTargetNodeName ());
232273 }
233274
234275 public static class Builder {
@@ -238,6 +279,7 @@ public static class Builder {
238279 private long startedAtMillis = -1 ;
239280 private boolean nodeSeen = false ;
240281 private TimeValue allocationDelay ;
282+ private String targetNodeName ;
241283
242284 private Builder () {}
243285
@@ -295,6 +337,15 @@ public Builder setAllocationDelay(TimeValue allocationDelay) {
295337 return this ;
296338 }
297339
340+ /**
341+ * @param targetNodeName The name of the node which should be used to replcae this one. Only valid if the shutdown type is REPLACE.
342+ * @return This builder.
343+ */
344+ public Builder setTargetNodeName (String targetNodeName ) {
345+ this .targetNodeName = targetNodeName ;
346+ return this ;
347+ }
348+
298349 public SingleNodeShutdownMetadata build () {
299350 if (startedAtMillis == -1 ) {
300351 throw new IllegalArgumentException ("start timestamp must be set" );
@@ -306,7 +357,8 @@ public SingleNodeShutdownMetadata build() {
306357 reason ,
307358 startedAtMillis ,
308359 nodeSeen ,
309- allocationDelay
360+ allocationDelay ,
361+ targetNodeName
310362 );
311363 }
312364 }
0 commit comments