Skip to content
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

Remove type casts in logging in server component #28807

Merged
merged 6 commits into from
Mar 23, 2018

Conversation

dnhatn
Copy link
Member

@dnhatn dnhatn commented Feb 23, 2018

This commit removes type-casts in logging in the server component (other components will be done later). This also adds a parameterized message test which would be able to catch breaking-changes related to lambdas in Log4J.

We now can remove all type casts in logging as Eclipse can infer them
correctly. This commit also removes incorrect usages of Supplier - we
should use `java.util.Supplier` instead of `org.apache.logging.log4j.util`.
@dnhatn dnhatn added >non-issue :Core/Infra/Logging Log management and logging utilities v7.0.0 v6.3.0 labels Feb 23, 2018
@dnhatn dnhatn requested a review from jasontedor February 23, 2018 19:34
Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean incorrect, there is not any method on Logger that accepts a java.util.Supplier, that actually can not be used here? Using a java.util.function.Supplier would actually be wrong because it would invoke

Logger#void info(Object message, Throwable t);

Please note that this change cause these method invocations to change from invoking:

Logger#void info(Supplier<?> msgSupplier, Throwable t);

to invoking

Logger#void info(MessageSupplier msgSupplier, Throwable t);

(for example). This is probably fine. Both of these will be removed in Log4j 3 where they will use JDK interfaces instead.

Historically, the reason that we use Supplier<?> is because MessageSupplier was initially deprecated in 2.6 (we started with Log4j 2.6.2). It has since been un-deprecated in 2.8.1 and is now deprecated again (for removal in Log4j 3). The Javadocs for Logger say this:

 * Note that although {@link MessageSupplier} is provided, using {@link Supplier Supplier<Message>} works just the
 * same. MessageSupplier was deprecated in 2.6 and un-deprecated in 2.8.1. Anonymous class usage of these APIs
 * should prefer using Supplier instead.

Functionally, the two are equivalent because we supply Messages everywhere.

My preference is that we leave this as-is. Why? Because in Log4j 3 when these are removed we will break at compile time. That is a good thing, it means we can check that when we switch to using java.util.function.Supplier we are invoking the desired method (rather than this silently switching on us from Logger#void info(MessageSupplier msgSupplier, Throwable t); to something we do not check. The explicitness is advantageous here.

@dnhatn
Copy link
Member Author

dnhatn commented Feb 23, 2018

@jasontedor Thank you for your clear explanation.

What do you mean incorrect, there is not any method on Logger that accepts a java.util.Supplier, that actually can not be used here? Using a java.util.function.Supplier would actually be wrong.

I meant we should not use org.apache.logging.log4j.util.Supplier in places which are not related to logging. In some classes, we already imported org.apache.logging.log4j.util.Supplier and happily use the log4j's Supplier for other purposes. For example:

private Supplier<ClusterState.Builder> stateBuilderSupplier;

Actually @ywelsch suggested me to clean up these (#28534 (comment))

@jasontedor
Copy link
Member

Well, I think what we have here is confusion that arises when two distinct changes are attempted in the same pull request. Changing the usage in ClusterApplierService to java.util.function.Supplier is a change that we should make, it will feel cleaner. We could consider a static checker that ensures that if a org.apache.logging.log4j.util.Supplier is a method parameter that that method is a method on the Logger interface. However, I am not sure if this is worth the trouble, and I certainly do not think it should crowd out other more important efforts. The usage here is not incorrect nor broken nor wrong, the code still does what we want it to do, obviously, it just feels dirty because instead of using the built-in Java interface for the same we ended up accidentally using one from a logging dependency. Oh well. Let's fix it in a small change and decide what to do with the rest of this PR separately.

@dnhatn
Copy link
Member Author

dnhatn commented Feb 24, 2018

@jasontedor I opened #28812 to replace log4j.Supplier by jdk’s in non-logging usage.

My preference is that we leave this as-is.

I am fine to close this. I don't have any objection here.

dnhatn added 3 commits March 15, 2018 13:30
# Conflicts:
#	server/src/main/java/org/elasticsearch/action/support/replication/TransportWriteAction.java
#	server/src/main/java/org/elasticsearch/client/transport/TransportClientNodesService.java
#	server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataMappingService.java
#	server/src/main/java/org/elasticsearch/cluster/service/ClusterApplierService.java
#	server/src/main/java/org/elasticsearch/common/util/IndexFolderUpgrader.java
#	server/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java
#	server/src/main/java/org/elasticsearch/tasks/TaskResultsService.java
#	server/src/main/java/org/elasticsearch/transport/TcpTransport.java
@dnhatn dnhatn changed the title Remove type casts in logging in server_main component Remove type casts in logging in server component Mar 15, 2018
@dnhatn
Copy link
Member Author

dnhatn commented Mar 15, 2018

As discussed with Yannick, this PR can be proceeded (after #28969).
@jasontedor, Would you please give it another go. Thank you!

Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@dnhatn
Copy link
Member Author

dnhatn commented Mar 23, 2018

@jasontedor Thanks for reviewing!

@dnhatn dnhatn merged commit 794de63 into elastic:master Mar 23, 2018
@dnhatn dnhatn deleted the remove-type-cast branch March 23, 2018 11:35
dnhatn added a commit that referenced this pull request Mar 23, 2018
This commit removes type-casts in logging in the server component (other 
components will be done later). This also adds a parameterized message
test which would catch breaking-changes related to lambdas in Log4J.
martijnvg added a commit that referenced this pull request Mar 26, 2018
* es/master: (27 commits)
  [Docs] Add rank_eval size parameter k (#29218)
  [DOCS] Remove ignore_z_value parameter link
  Docs: Update docs/index_.asciidoc (#29172)
  Docs: Link C++ client lib elasticlient (#28949)
  [DOCS] Unregister repository instead of deleting it (#29206)
  Docs: HighLevelRestClient#multiSearch (#29144)
  Add Z value support to geo_shape
  Remove type casts in logging in server component (#28807)
  Change BroadcastResponse from ToXContentFragment to ToXContentObject (#28878)
  REST : Split `RestUpgradeAction` into two actions (#29124)
  Add error file docs to important settings
  Add note to low-level client docs for DNS caching (#29213)
  Harden periodically check to avoid endless flush loop (#29125)
  Remove deprecated options for query_string (#29203)
  REST high-level client: add force merge API (#28896)
  Remove license information from README.textile (#29198)
  Decouple more classes from XContentBuilder and make builder strict (#29197)
  [Docs] Fix missing closing block in cluster/misc.asciidoc
  RankEvalRequest should implement IndicesRequest (#29188)
  Use EnumMap in ClusterBlocks (#29112)
  ...
martijnvg added a commit that referenced this pull request Mar 26, 2018
* es/6.x: (29 commits)
  [Docs] Add rank_eval size parameter k (#29218)
  Docs: Update docs/index_.asciidoc (#29172)
  Docs: Link C++ client lib elasticlient (#28949)
  Docs: HighLevelRestClient#multiSearch (#29144)
  [DOCS] Remove ignore_z_value parameter link
  Add Z value support to geo_shape
  Change BroadcastResponse from ToXContentFragment to ToXContentObject (#28878)
  REST : Split `RestUpgradeAction` into two actions (#29124)
  [DOCS] Unregister repository instead of deleting it (#29206)
  Remove type casts in logging in server component (#28807)
  Add error file docs to important settings
  Add note to low-level client docs for DNS caching (#29213)
  testShrinkAfterUpgrade should only set mapping.single_type if bwc version > 5.5.0
  Harden periodically check to avoid endless flush loop (#29125)
  REST high-level client: add force merge API (#28896)
  Remove license information from README.textile (#29198)
  Decouple more classes from XContentBuilder and make builder strict (#29197)
  Propagate mapping.single_type setting on shrinked index (#29202)
  [Docs] Fix missing closing block in cluster/misc.asciidoc
  RankEvalRequest should implement IndicesRequest (#29188)
  ...
jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Mar 27, 2018
* master: (40 commits)
  Do not optimize append-only if seen normal op with higher seqno (elastic#28787)
  [test] packaging: gradle tasks for groovy tests (elastic#29046)
  Prune only gc deletes below local checkpoint (elastic#28790)
  remove testUnassignedShardAndEmptyNodesInRoutingTable
  elastic#28745: remove extra option in the composite rest tests
  Fold EngineDiskUtils into Store, for better lock semantics (elastic#29156)
  Add file permissions checks to precommit task
  Remove execute mode bit from source files
  Optimize the composite aggregation for match_all and range queries (elastic#28745)
  [Docs] Add rank_eval size parameter k (elastic#29218)
  [DOCS] Remove ignore_z_value parameter link
  Docs: Update docs/index_.asciidoc (elastic#29172)
  Docs: Link C++ client lib elasticlient (elastic#28949)
  [DOCS] Unregister repository instead of deleting it (elastic#29206)
  Docs: HighLevelRestClient#multiSearch (elastic#29144)
  Add Z value support to geo_shape
  Remove type casts in logging in server component (elastic#28807)
  Change BroadcastResponse from ToXContentFragment to ToXContentObject (elastic#28878)
  REST : Split `RestUpgradeAction` into two actions (elastic#29124)
  Add error file docs to important settings
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants