Skip to content

Commit e062fed

Browse files
authored
Merge branch 'main' into fork
2 parents 5a14086 + 2bfc700 commit e062fed

File tree

13 files changed

+116
-25
lines changed

13 files changed

+116
-25
lines changed

docs/changelog/122458.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 122458
2+
summary: '`DesiredBalanceReconciler` always returns `AllocationStats`'
3+
area: Allocation
4+
type: bug
5+
issues: []

libs/entitlement/asm-provider/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ apply plugin: 'elasticsearch.build'
1212
dependencies {
1313
compileOnly project(':libs:entitlement')
1414
compileOnly project(':libs:core')
15+
compileOnly project(':libs:logging')
1516
implementation 'org.ow2.asm:asm:9.7.1'
1617
testImplementation project(":test:framework")
1718
testImplementation project(":libs:entitlement:bridge")

libs/entitlement/asm-provider/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
requires org.elasticsearch.entitlement;
1616

1717
requires static org.elasticsearch.base; // for SuppressForbidden
18+
requires org.elasticsearch.logging;
1819

1920
provides InstrumentationService with InstrumentationServiceImpl;
2021
}

libs/entitlement/asm-provider/src/main/java/org/elasticsearch/entitlement/instrumentation/impl/InstrumenterImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.elasticsearch.entitlement.instrumentation.CheckMethod;
1313
import org.elasticsearch.entitlement.instrumentation.Instrumenter;
1414
import org.elasticsearch.entitlement.instrumentation.MethodKey;
15+
import org.elasticsearch.logging.LogManager;
16+
import org.elasticsearch.logging.Logger;
1517
import org.objectweb.asm.AnnotationVisitor;
1618
import org.objectweb.asm.ClassReader;
1719
import org.objectweb.asm.ClassVisitor;
@@ -36,6 +38,7 @@
3638
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
3739

3840
public class InstrumenterImpl implements Instrumenter {
41+
private static final Logger logger = LogManager.getLogger(InstrumenterImpl.class);
3942

4043
private final String getCheckerClassMethodDescriptor;
4144
private final String handleClass;
@@ -155,10 +158,10 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str
155158
var key = new MethodKey(className, name, Stream.of(Type.getArgumentTypes(descriptor)).map(Type::getInternalName).toList());
156159
var instrumentationMethod = checkMethods.get(key);
157160
if (instrumentationMethod != null) {
158-
// System.out.println("Will instrument method " + key);
161+
logger.debug("Will instrument {}", key);
159162
return new EntitlementMethodVisitor(Opcodes.ASM9, mv, isStatic, isCtor, descriptor, instrumentationMethod);
160163
} else {
161-
// System.out.println("Will not instrument method " + key);
164+
logger.trace("Will not instrument {}", key);
162165
}
163166
}
164167
return mv;

server/src/main/java/org/elasticsearch/cluster/routing/RoutingNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public String nodeId() {
112112
return this.nodeId;
113113
}
114114

115+
/**
116+
* Number of shards assigned to this node. Includes relocating shards. Use {@link #numberOfOwningShards()} to exclude relocating shards.
117+
*/
115118
public int size() {
116119
return shards.size();
117120
}

server/src/main/java/org/elasticsearch/cluster/routing/allocation/allocator/DesiredBalanceReconciler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -515,11 +515,6 @@ private void moveShards() {
515515
}
516516

517517
private DesiredBalanceMetrics.AllocationStats balance() {
518-
// Check if rebalancing is disabled.
519-
if (allocation.deciders().canRebalance(allocation).type() != Decision.Type.YES) {
520-
return DesiredBalanceMetrics.EMPTY_ALLOCATION_STATS;
521-
}
522-
523518
int unassignedShards = routingNodes.unassigned().size() + routingNodes.unassigned().ignored().size();
524519
int totalAllocations = 0;
525520
int undesiredAllocationsExcludingShuttingDownNodes = 0;
@@ -549,9 +544,15 @@ private DesiredBalanceMetrics.AllocationStats balance() {
549544
}
550545

551546
if (allocation.metadata().nodeShutdowns().contains(shardRouting.currentNodeId()) == false) {
547+
// shard is not on a shutting down node, nor is it on a desired node per the previous check.
552548
undesiredAllocationsExcludingShuttingDownNodes++;
553549
}
554550

551+
if (allocation.deciders().canRebalance(allocation).type() != Decision.Type.YES) {
552+
// Rebalancing is disabled, we're just here to collect the AllocationStats to return.
553+
continue;
554+
}
555+
555556
if (allocation.deciders().canRebalance(shardRouting, allocation).type() != Decision.Type.YES) {
556557
// rebalancing disabled for this shard
557558
continue;

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDecider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public Decision shouldAutoExpandToNode(IndexMetadata indexMetadata, DiscoveryNod
7575
}
7676

7777
/**
78-
* Returns a {@link Decision} whether the cluster can execute
79-
* re-balanced operations at all.
80-
* {@link Decision#ALWAYS}.
78+
* Returns a {@link Decision} on whether the cluster is allowed to rebalance shards to improve relative node shard weights and
79+
* performance.
80+
* @return {@link Decision#ALWAYS} is returned by default if not overridden.
8181
*/
8282
public Decision canRebalance(RoutingAllocation allocation) {
8383
return Decision.ALWAYS;

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/AllocationDeciders.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, Routing
8282
);
8383
}
8484

85+
/**
86+
* Returns whether rebalancing (move shards to improve relative node weights and performance) is allowed right now.
87+
* Rebalancing can be disabled via cluster settings, or throttled by cluster settings (e.g. max concurrent shard moves).
88+
*/
8589
public Decision canRebalance(RoutingAllocation allocation) {
8690
return withDeciders(
8791
allocation,

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ClusterRebalanceAllocationDecider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca
150150
+ "]"
151151
);
152152

153+
/**
154+
* Rebalancing may be enabled, disabled, or only allowed after all primaries have started, depending on the cluster setting
155+
* {@link #CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING}.
156+
*/
153157
@SuppressWarnings("fallthrough")
154158
@Override
155159
public Decision canRebalance(RoutingAllocation allocation) {

server/src/main/java/org/elasticsearch/cluster/routing/allocation/decider/ConcurrentRebalanceAllocationDecider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public Decision canRebalance(ShardRouting shardRouting, RoutingAllocation alloca
6161
return canRebalance(allocation);
6262
}
6363

64+
/**
65+
* We allow a limited number of concurrent shard relocations, per the cluster setting
66+
* {@link #CLUSTER_ROUTING_ALLOCATION_CLUSTER_CONCURRENT_REBALANCE_SETTING}.
67+
* Returns a {@link Decision#THROTTLE} decision if the limit is exceeded, otherwise returns {@link Decision#YES}.
68+
*/
6469
@Override
6570
public Decision canRebalance(RoutingAllocation allocation) {
6671
int relocatingShards = allocation.routingNodes().getRelocatingShardCount();

0 commit comments

Comments
 (0)