Skip to content

Expunge stale entries in InternalLoggerRegistry #3681

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

Merged
merged 9 commits into from
May 31, 2025

Conversation

jhl221123
Copy link
Contributor

This pull request addresses issue #3430 by implementing a mechanism to expunge stale entries in InternalLoggerRegistry.

It builds upon PR #3474 and incorporates feedback from @vy and @ppkarwasz.

Key changes include:

  • Enabled stale entry detection via ReferenceQueue: When a Logger is GC'd, its WeakReference is enqueued. Subsequent calls to methods like getLogger() or computeIfAbsent() trigger expungeStaleEntries() to process it.
  • Implemented expungeStaleEntries logic: If the ReferenceQueue is not empty, a write lock is acquired, the queue is drained, and map entries are scanned once to remove WeakReferences with null referents and clean up any empty maps. No action or locking occurs if the queue is initially empty.
  • Added unit tests: Verified the expunging mechanism under various conditions, including GC scenarios and concurrent access.

@vy, ReferenceQueue has no peek()/isEmpty(), so I couldn’t do a proper double-check and used an initial poll() instead. Feedback is welcome if there's a better way or if I missed something. Thanks!

Fixes #3430

Suvrat1629 and others added 6 commits May 20, 2025 21:24
Use ReferenceQueue to track and remove GC'd Logger WeakReferences from the registry map to prevent potential memory leaks. Includes improved unit tests.
Copy link

github-actions bot commented May 21, 2025

Job Requested goals Build Tool Version Build Outcome Build Scan®
build-macos-latest clean install 3.9.8 Build Scan PUBLISHED
build-ubuntu-latest clean install 3.9.8 Build Scan PUBLISHED
build-windows-latest clean install 3.9.8 Build Scan PUBLISHED
Generated by gradle/develocity-actions

@ppkarwasz ppkarwasz added this to the 2.25.0 milestone May 22, 2025
Copy link
Contributor

@ppkarwasz ppkarwasz left a comment

Choose a reason for hiding this comment

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

@jhl221123, thank you for picking up this pull request!

The main code looks good to me overall. I do have a few suggestions regarding the unit tests:

* Adjusted test class file name and its package location.
* Used MockLogger in logger suppliers to avoid nested computeIfAbsent.
* Employed a local SimpleMessageFactory instance in tests.
Copy link
Contributor

@ppkarwasz ppkarwasz left a comment

Choose a reason for hiding this comment

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

The changes look solid—nice work! There's just one final task before we can wrap this up: could you please add a changelog entry under src/changelog/.2.x.x, as outlined in the changelog documentation?

Also, just a quick note on loggerRefByNameByMessageFactory: since it's a WeakHashMap, it doesn’t follow all the typical Map invariants. From what I can tell, you're using it safely, and I didn’t spot any issues that would lead to unexpected behavior. That said, given the sensitivity of this part of the codebase, I’d like a second pair of eyes on it.

@vy, could you take a moment to review this as well?

@ppkarwasz ppkarwasz requested a review from vy May 23, 2025 19:27
@ppkarwasz ppkarwasz moved this from To triage to In review in Log4j bug tracker May 23, 2025
@ppkarwasz ppkarwasz requested a review from Copilot May 23, 2025 19:28
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements a mechanism to expunge stale logger entries in InternalLoggerRegistry by tracking WeakReferences using a ReferenceQueue. Key changes include:

  • Adding a ReferenceQueue field and an expungeStaleEntries() method.
  • Updating logger retrieval and creation methods to trigger stale entry cleanup.
  • Introducing unit tests to verify the expunging behavior under various GC and concurrent scenarios.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
log4j-core/src/main/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistry.java Added ReferenceQueue and expungeStaleEntries() method, plus calls to it in getLogger/hasLogger/computeIfAbsent
log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/internal/InternalLoggerRegistryTest.java Added unit tests to cover stale entry removal and concurrent access scenarios

@vy
Copy link
Member

vy commented May 23, 2025 via email

@ppkarwasz ppkarwasz self-assigned this May 25, 2025
* Rename variables in InternalLoggerRegistry
* Add final keyword in InternalLoggerRegistry and tests
* Remove/clarify comments in InternalLoggerRegistry
* Remove MockLogger class, using anonymous Logger instances in tests
* Remove System.runFinalization() calls from tests
* Improve assertion logic in testExpungeStaleWeakReferenceEntries
* Remove testConcurrentAccess (lacked specific scenario)
@vy vy merged commit aa4ee5f into apache:2.x May 31, 2025
11 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in Log4j bug tracker May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Expunge stale entries in InternalLoggerRegistry
4 participants