Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2389

fix the CacheEntry map in ThrowableProxy#toExtendedStackTrace to be put and gotten with same key

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.6.2, 2.7, 2.8, 2.8.1, 2.8.2, 2.9.0, 2.9.1, 2.10.0, 2.11.0
    • 2.11.1
    • Core
    • None

    Description

      • fix the CacheEntry map in ThrowableProxy#toExtendedStackTrace to be put and gotten with same key
      • stackTraceElement.toString() returns a string representation of this stack trace element, just as MyClass.mash(MyClass.java)
      • stackTraceElement.getClassName() returns the fully qualified name of the Class, just as org.apache.logging.log4j.MyClass
        final String className = stackTraceElement.getClassName();
        ……
        final CacheEntry cacheEntry = map.get(className);
        if (cacheEntry != null) {
              final CacheEntry entry = cacheEntry;
              extClassInfo = entry.element;
              if (entry.loader != null) {
                     lastLoader = entry.loader;
              }
        } else {
              final CacheEntry entry = this.toCacheEntry(stackTraceElement,
              this.loadClass(lastLoader, className), false);
              extClassInfo = entry.element;
              map.put(stackTraceElement.toString(), entry);
              if (entry.loader != null) {
                     lastLoader = entry.loader;
              }
        }
        
      • The main impact of the problem was that it would increase of frequency of loading classes ,which led to the execution of the program be slow down, because of the synchronization mechanism in the method loadClass
      • In addition to fixing the problem, I think cache map could be made global, instead of a new one for each exception instance. 
        private ThrowableProxy(final Throwable throwable, final Set<Throwable> visited) {
            ...
            final Map<String, CacheEntry> map = new HashMap<>();
            this.extendedStackTrace = this.toExtendedStackTrace(stack, map, null,      
            throwable.getStackTrace());
            ...
         }
        
      • We made the benchmark test to compares the performance of ThrowableProxy optimizations for different exception
      • baseline: test `ThrowableProxy` in log4j2, not optimized, with bug, for comparison consideration.
      • bugfixed: fixed bug in `ThrowableProxy` accessing the cache.
      • optimized: make the cache global, instead of a new one for each exception instance.

      Then we see

      • more than `10` times improvement when the bug is fixed for exceptions with stack element class duplicated many times.
      • and about `2` times further improvement when make the cache global (compared with log4j2, more than `20` times improvement).

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            liuwenchn LIU WEN
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment