Browse Source

Make relocatingNodeIsDesired nullable when not relocating (#94279)

Ievgen Degtiarenko 2 years ago
parent
commit
3221d1d199

+ 1 - 1
docs/reference/cluster/get-desired-balance.asciidoc

@@ -132,7 +132,7 @@ The API returns the following result:
             "node": "UPYt8VwWTt-IADAEbqpLxA",
             "node_is_desired": true,
             "relocating_node": null,
-            "relocating_node_is_desired": false,
+            "relocating_node_is_desired": null,
             "shard_id": 0,
             "index": "test",
             "forecast_write_load": 8.0,

+ 1 - 1
server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetDesiredBalanceActionIT.java

@@ -130,7 +130,7 @@ public class TransportGetDesiredBalanceActionIT extends ESIntegTestCase {
             assertFalse(shardView.nodeIsDesired());
         }
         assertEquals(shard.relocatingNodeId(), shardView.relocatingNode());
-        assertFalse(shardView.relocatingNodeIsDesired());
+        assertNull(shardView.relocatingNodeIsDesired());
     }
 
     private static Set<String> getShardNodeIds(IndexShardRoutingTable shardRoutingTable) {

+ 44 - 18
server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/DesiredBalanceResponse.java

@@ -191,7 +191,7 @@ public class DesiredBalanceResponse extends ActionResponse implements ChunkedToX
         String node,
         boolean nodeIsDesired,
         @Nullable String relocatingNode,
-        boolean relocatingNodeIsDesired,
+        @Nullable Boolean relocatingNodeIsDesired,
         int shardId,
         String index,
         @Nullable Double forecastWriteLoad,
@@ -201,25 +201,47 @@ public class DesiredBalanceResponse extends ActionResponse implements ChunkedToX
 
         private static final TransportVersion ADD_FORECASTS_VERSION = TransportVersion.V_8_7_0;
         private static final TransportVersion ADD_TIER_PREFERENCE = TransportVersion.V_8_8_0;
+        private static final TransportVersion NULLABLE_RELOCATING_NODE_IS_DESIRED = TransportVersion.V_8_8_0;
+
+        public ShardView {
+            assert (relocatingNode == null) == (relocatingNodeIsDesired == null)
+                : "relocatingNodeIsDesired should only be set when relocatingNode is set";
+        }
 
         public static ShardView from(StreamInput in) throws IOException {
-            var view = new ShardView(
-                ShardRoutingState.fromValue(in.readByte()),
-                in.readBoolean(),
-                in.readOptionalString(),
-                in.readBoolean(),
-                in.readOptionalString(),
-                in.readBoolean(),
-                in.readVInt(),
-                in.readString(),
-                in.getTransportVersion().onOrAfter(ADD_FORECASTS_VERSION) ? in.readOptionalDouble() : null,
-                in.getTransportVersion().onOrAfter(ADD_FORECASTS_VERSION) ? in.readOptionalLong() : null,
-                in.getTransportVersion().onOrAfter(ADD_TIER_PREFERENCE) ? in.readStringList() : List.of()
-            );
+            ShardRoutingState state = ShardRoutingState.fromValue(in.readByte());
+            boolean primary = in.readBoolean();
+            String node = in.readOptionalString();
+            boolean nodeIsDesired = in.readBoolean();
+            String relocatingNode = in.readOptionalString();
+            Boolean relocatingNodeIsDesired;
+            if (in.getTransportVersion().onOrAfter(NULLABLE_RELOCATING_NODE_IS_DESIRED)) {
+                relocatingNodeIsDesired = in.readOptionalBoolean();
+            } else {
+                boolean wireRelocatingNodeIsDesired = in.readBoolean();
+                relocatingNodeIsDesired = relocatingNode == null ? null : wireRelocatingNodeIsDesired;
+            }
+            int shardId = in.readVInt();
+            String index = in.readString();
+            Double forecastWriteLoad = in.getTransportVersion().onOrAfter(ADD_FORECASTS_VERSION) ? in.readOptionalDouble() : null;
+            Long forecastShardSizeInBytes = in.getTransportVersion().onOrAfter(ADD_FORECASTS_VERSION) ? in.readOptionalLong() : null;
             if (in.getTransportVersion().onOrAfter(ADD_FORECASTS_VERSION) == false) {
                 in.readOptionalWriteable(AllocationId::new);
             }
-            return view;
+            List<String> tierPreference = in.getTransportVersion().onOrAfter(ADD_TIER_PREFERENCE) ? in.readStringList() : List.of();
+            return new ShardView(
+                state,
+                primary,
+                node,
+                nodeIsDesired,
+                relocatingNode,
+                relocatingNodeIsDesired,
+                shardId,
+                index,
+                forecastWriteLoad,
+                forecastShardSizeInBytes,
+                tierPreference
+            );
         }
 
         @Override
@@ -229,7 +251,11 @@ public class DesiredBalanceResponse extends ActionResponse implements ChunkedToX
             out.writeOptionalString(node);
             out.writeBoolean(nodeIsDesired);
             out.writeOptionalString(relocatingNode);
-            out.writeBoolean(relocatingNodeIsDesired);
+            if (out.getTransportVersion().onOrAfter(NULLABLE_RELOCATING_NODE_IS_DESIRED)) {
+                out.writeOptionalBoolean(relocatingNodeIsDesired);
+            } else {
+                out.writeBoolean(relocatingNodeIsDesired != null && relocatingNodeIsDesired);
+            }
             out.writeVInt(shardId);
             out.writeString(index);
             if (out.getTransportVersion().onOrAfter(ADD_FORECASTS_VERSION)) {
@@ -246,14 +272,14 @@ public class DesiredBalanceResponse extends ActionResponse implements ChunkedToX
         @Override
         public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
             return builder.startObject()
+                .field("index", index)
+                .field("shard_id", shardId)
                 .field("state", state.toString())
                 .field("primary", primary)
                 .field("node", node)
                 .field("node_is_desired", nodeIsDesired)
                 .field("relocating_node", relocatingNode)
                 .field("relocating_node_is_desired", relocatingNodeIsDesired)
-                .field("shard_id", shardId)
-                .field("index", index)
                 .field("forecast_write_load", forecastWriteLoad)
                 .field("forecast_shard_size_in_bytes", forecastShardSizeInBytes)
                 .field("tier_preference", tierPreference)

+ 6 - 6
server/src/main/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetDesiredBalanceAction.java

@@ -123,13 +123,9 @@ public class TransportGetDesiredBalanceAction extends TransportMasterNodeReadAct
                             shard.state(),
                             shard.primary(),
                             shard.currentNodeId(),
-                            shard.currentNodeId() != null
-                                && shardAssignment != null
-                                && shardAssignment.nodeIds().contains(shard.currentNodeId()),
+                            isDesired(shard.currentNodeId(), shardAssignment),
                             shard.relocatingNodeId(),
-                            shard.relocatingNodeId() != null
-                                && shardAssignment != null
-                                && shardAssignment.nodeIds().contains(shard.relocatingNodeId()),
+                            shard.relocatingNodeId() != null ? isDesired(shard.relocatingNodeId(), shardAssignment) : null,
                             shard.shardId().id(),
                             shard.getIndexName(),
                             forecastedWriteLoad.isPresent() ? forecastedWriteLoad.getAsDouble() : null,
@@ -158,6 +154,10 @@ public class TransportGetDesiredBalanceAction extends TransportMasterNodeReadAct
         return routingTable;
     }
 
+    private static boolean isDesired(@Nullable String nodeId, @Nullable ShardAssignment assignment) {
+        return nodeId != null && assignment != null && assignment.nodeIds().contains(nodeId);
+    }
+
     @Override
     protected ClusterBlockException checkBlock(DesiredBalanceRequest request, ClusterState state) {
         return state.blocks().globalBlockedException(ClusterBlockLevel.METADATA_READ);

+ 1 - 1
server/src/test/java/org/elasticsearch/action/admin/cluster/allocation/TransportGetDesiredBalanceActionTests.java

@@ -261,7 +261,7 @@ public class TransportGetDesiredBalanceActionTests extends ESAllocationTestCase
                         shardView.nodeIsDesired()
                     );
                     assertEquals(
-                        shard.relocatingNodeId() != null && desiredNodeIds.contains(shard.relocatingNodeId()),
+                        shard.relocatingNodeId() != null ? desiredNodeIds.contains(shard.relocatingNodeId()) : null,
                         shardView.relocatingNodeIsDesired()
                     );
                     assertEquals(indexMetadata.getTierPreference(), shardView.tierPreference());