Skip to content

KAFKA-17185: Declare Loggers as static to prevent multiple logger instances#16680

Merged
chia7712 merged 7 commits intoapache:trunkfrom
mingyen066:KAFKA-17185-Make-logger-static-to-avoid-creating-multiple-logger-instances
Jul 30, 2024
Merged

KAFKA-17185: Declare Loggers as static to prevent multiple logger instances#16680
chia7712 merged 7 commits intoapache:trunkfrom
mingyen066:KAFKA-17185-Make-logger-static-to-avoid-creating-multiple-logger-instances

Conversation

@mingyen066
Copy link
Copy Markdown
Collaborator

@mingyen066 mingyen066 commented Jul 24, 2024

As discussed in #16657 (comment) , we should make logger as static to avoid creating multiple logger instances.
I use the regex private.*Logger.*LoggerFactory to search and check all the results if certain logs need to be static.

There are some exceptions that loggers don't need to be static:

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

@mingyen066 mingyen066 changed the title KAFKA-17185: Declare all Loggers static to avoid creating multiple logger instances KAFKA-17185: Declare all Loggers as static to prevent multiple logger instances Jul 24, 2024
static final double CONNECTION_SETUP_TIMEOUT_JITTER = 0.2;
private final Map<String, NodeConnectionState> nodeState;
private final Logger log;
private static final Logger log;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Noted that this static variable should NOT be initialized in constructor.

Copy link
Copy Markdown
Collaborator Author

@mingyen066 mingyen066 Jul 25, 2024

Choose a reason for hiding this comment

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

Thanks for finding the bug. This is the case that I shouldn't change the logger to static.
I have completed the code updates with different approach and also written the methodology and criteria used to PR message. PTAL.

@mingyen066 mingyen066 changed the title KAFKA-17185: Declare all Loggers as static to prevent multiple logger instances KAFKA-17185: Declare Loggers as static to prevent multiple logger instances Jul 25, 2024
…ogger-static-to-avoid-creating-multiple-logger-instances
@chia7712
Copy link
Copy Markdown
Member

@mingyen066 Could you please merge trunk to run CI again? there are thread leak. I don't think there are related to your PR, but it is always good to check it again

…ogger-static-to-avoid-creating-multiple-logger-instances
@mingyen066
Copy link
Copy Markdown
Collaborator Author

@chia7712 Sure, I merged trunk branch and the CI is running again.

Copy link
Copy Markdown
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

LGTM

@chia7712 chia7712 merged commit 7c0a96d into apache:trunk Jul 30, 2024
abhi-ksolves pushed a commit to ksolves/kafka that referenced this pull request Jul 31, 2024
…ances (apache#16680)

As discussed in apache#16657 (comment) , we should make logger as static to avoid creating multiple logger instances.
I use the regex private.*Logger.*LoggerFactory to search and check all the results if certain logs need to be static.

There are some exceptions that loggers don't need to be static:
1) The logger in the inner class. Since java8 doesn't support static field in the inner class.
        https://github.com/apache/kafka/blob/trunk/clients/src/test/java/org/apache/kafka/clients/consumer/internals/FetchRequestManagerTest.java#L3676

2) Custom loggers for each instance (non-static + non-final). In this case, multiple logger instances is actually really needed.
        https://github.com/apache/kafka/blob/trunk/storage/src/test/java/org/apache/kafka/server/log/remote/storage/LocalTieredStorage.java#L166

3) The logger is initialized in constructor by LogContext. Many non-static but with final modifier loggers are in this category, that's why I use .*LoggerFactory to only check the loggers that are assigned initial value when declaration.
    
4) protected final Logger log = Logger.getLogger(getClass())
    This is for subclass can do logging with subclass name instead of superclass name.
    But in this case, if the log access modifier is private, the purpose cannot be achieved since subclass cannot access the log defined in superclass. So if access modifier is private, we can replace getClass() with <className>.class

Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
@mingyen066 mingyen066 deleted the KAFKA-17185-Make-logger-static-to-avoid-creating-multiple-logger-instances branch January 30, 2025 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants