소스 검색

Avoid negative DesiredBalanceStats#lastConvergedIndex (#101998)

The initial state of the desired-balance allocator has
`lastConvergedIndex` set to `-1`. This is not important to represent in
the stats, so with this commit we map it to zero.
David Turner 1 년 전
부모
커밋
c5560bcfdb

+ 5 - 0
docs/changelog/101998.yaml

@@ -0,0 +1,5 @@
+pr: 101998
+summary: Avoid negative `DesiredBalanceStats#lastConvergedIndex`
+area: Allocation
+type: bug
+issues: []

+ 1 - 1
server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceShardsAllocator.java

@@ -264,7 +264,7 @@ public class DesiredBalanceShardsAllocator implements ShardsAllocator {
 
     public DesiredBalanceStats getStats() {
         return new DesiredBalanceStats(
-            currentDesiredBalance.lastConvergedIndex(),
+            Math.max(currentDesiredBalance.lastConvergedIndex(), 0L),
             desiredBalanceComputation.isActive(),
             computationsSubmitted.count(),
             computationsExecuted.count(),

+ 7 - 0
server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceStats.java

@@ -33,6 +33,13 @@ public record DesiredBalanceStats(
 
     private static final TransportVersion COMPUTED_SHARD_MOVEMENTS_VERSION = TransportVersions.V_8_8_0;
 
+    public DesiredBalanceStats {
+        if (lastConvergedIndex < 0) {
+            assert false : lastConvergedIndex;
+            throw new IllegalStateException("lastConvergedIndex must be nonnegative, but got [" + lastConvergedIndex + ']');
+        }
+    }
+
     public static DesiredBalanceStats readFrom(StreamInput in) throws IOException {
         return new DesiredBalanceStats(
             in.readVLong(),

+ 12 - 0
server/src/test/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceShardsAllocatorTests.java

@@ -68,6 +68,7 @@ import static org.elasticsearch.cluster.routing.TestShardRouting.newShardRouting
 import static org.elasticsearch.cluster.routing.UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING;
 import static org.elasticsearch.common.settings.ClusterSettings.createBuiltInClusterSettings;
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
 import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.not;
 
@@ -158,6 +159,7 @@ public class DesiredBalanceShardsAllocatorTests extends ESAllocationTestCase {
             clusterService,
             reconcileAction
         );
+        assertValidStats(desiredBalanceShardsAllocator.getStats());
         var allocationService = createAllocationService(desiredBalanceShardsAllocator, createGatewayAllocator(allocateUnassigned));
         allocationServiceRef.set(allocationService);
 
@@ -200,11 +202,21 @@ public class DesiredBalanceShardsAllocatorTests extends ESAllocationTestCase {
                     }
                 }
             }
+            assertValidStats(desiredBalanceShardsAllocator.getStats());
         } finally {
             clusterService.close();
         }
     }
 
+    private void assertValidStats(DesiredBalanceStats stats) {
+        assertThat(stats.lastConvergedIndex(), greaterThanOrEqualTo(0L));
+        try {
+            assertEquals(stats, copyWriteable(stats, writableRegistry(), DesiredBalanceStats::readFrom));
+        } catch (Exception e) {
+            fail(e);
+        }
+    }
+
     public void testShouldNotRemoveAllocationDelayMarkersOnReconcile() {
 
         var localNode = newNode(LOCAL_NODE_ID);