-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Avoid unnecessary explanations in WriteLoadConstraintDecider
#137755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid unnecessary explanations in WriteLoadConstraintDecider
#137755
Conversation
Today the `WriteLoadConstraintDecider` constructs various strings explaining its decisions, but it only uses these messages if debug logging is enabled or it's responding to an allocation explain request. This commit skips the unnecessary work on the hot path.
|
Pinging @elastic/es-distributed-coordination (Team:Distributed Coordination) |
|
Maybe you could document Lines 372 to 386 in a06527f
Perhaps a bit obvious, but I did it, so here we are :) |
|
Though |
DiannaHohensee
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for fixing it!
Left various suggestions with the aim of avoiding a repeat situation.
| if (logger.isDebugEnabled()) { | ||
| logInterventionMessage.maybeExecute(() -> logger.debug(explain)); | ||
| } | ||
| return allocation.decision(Decision.NOT_PREFERRED, NAME, explain); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allocation.decision will technically check the debug again, redundantly.
Maybe something more dramatic like allocation.decisionWithOptionalExplain would be more clear in the decision between RoutingAllocation#decision() and Decision#single() 😌
Anyway, I don't feel strongly about any of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not redundant, we could be here because debug logging is enabled but still we don't want to construct a new Decision when we can just return Decision.NOT_PREFERRED.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right 👍
| } | ||
|
|
||
| String explanation = Strings.format( | ||
| return allocation.decision( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this also have a if-else around allocation.debugDecision()?
Either for performance of not bothering with some of the string creation, or to model a consistent choice in the code for future readers.
I wonder if having RoutingAllocation#decision confuses things too much in the first place. What amount of string creation is OK vs not OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need here, none of the parameters allocate anything new, they're just trivial accessor methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't passing "Shard [%s] in index [%s] can be assigned to node [%s]. The node's utilization would become [%s]" as a parameter create a String?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, string literals are constants, they're only allocated once. The bytecode that calls RoutingAllocation#decision just uses a simple ldc to push the string constant onto the stack, no allocation needed:
$ javap -p -c server/build/classes/java/main/org/elasticsearch/cluster/routing/allocation/decider/WriteLoadConstraintDecider.class | grep -A25 'utilization would become'
526: ldc #214 // String Shard [%s] in index [%s] can be assigned to node [%s]. The node\'s utilization would become [%s]
528: iconst_4
529: anewarray #77 // class java/lang/Object
532: dup
533: iconst_0
534: aload_1
535: invokevirtual #102 // Method org/elasticsearch/cluster/routing/ShardRouting.shardId:()Lorg/elasticsearch/index/shard/ShardId;
538: aastore
539: dup
540: iconst_1
541: aload_1
542: invokevirtual #216 // Method org/elasticsearch/cluster/routing/ShardRouting.index:()Lorg/elasticsearch/index/Index;
545: aastore
546: dup
547: iconst_2
548: aload_2
549: invokevirtual #122 // Method org/elasticsearch/cluster/routing/RoutingNode.nodeId:()Ljava/lang/String;
552: aastore
553: dup
554: iconst_3
555: fload 11
557: invokestatic #172 // Method java/lang/Float.valueOf:(F)Ljava/lang/Float;
560: aastore
561: invokevirtual #79 // Method org/elasticsearch/cluster/routing/allocation/RoutingAllocation.decision:(Lorg/elasticsearch/cluster/routing/allocation/decider/Decision;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Lorg/elasticsearch/cluster/routing/allocation/decider/Decision;
564: areturn
|
Added some docs in 50d1fb8. |
…ic#137755) Today the `WriteLoadConstraintDecider` constructs various strings explaining its decisions, but it only uses these messages if debug logging is enabled or it's responding to an allocation explain request. This commit skips the unnecessary work on the hot path.
Today the
WriteLoadConstraintDeciderconstructs various stringsexplaining its decisions, but it only uses these messages if debug
logging is enabled or it's responding to an allocation explain request.
This commit skips the unnecessary work on the hot path.