Uploaded image for project: 'XWiki Commons'
  1. XWiki Commons
  2. XCOMMONS-3937

Concurrent requests block each other when resolving entity references

    XMLWordPrintable

Details

    • Improvement
    • Resolution: Unresolved
    • Major
    • None
    • 10.7
    • Collection
    • Unknown

    Description

      Under high load, threads wait for each other in SoftCache. It's used by EntityReferenceFactory to share entity reference instances, so nearly every request goes through it many times: loading documents, right checks, configuration lookups.

      Observed

      During a load spike on xwiki.org (XWiki 18.4.x, CPU saturated), a Glowroot profile of the home page showed about 13% of the request time parked in SoftCache.get, called from EntityReferenceFactory.getReference (from XWikiDocument, DefaultSecurityReferenceFactory, the space configuration sources, …).

      Cause

      SoftCache is a commons-collections ReferenceMap behind a ReentrantReadWriteLock. Every cache miss takes the write lock, and once a writer is queued, every new reader parks behind it. The recursive EntityReference#hashCode() and equals() also run inside the lock.

      Two related bugs:

      • ReferenceMap#get() purges cleared references, which modifies the map, while only the read lock is held.
      • get(key, defaultValue) isn't atomic, so concurrent callers can get different instances for the same key.

      Proposal

      Reimplement SoftCache on a ConcurrentHashMap with soft references for both keys and values. Keys stay compared with equals, cleared references are purged on writes, and get(key, defaultValue) becomes atomic. Reads then take no lock. The public API stays the same and no dependency is added.

      Caffeine doesn't fit:

      • weakKeys() compares keys by identity.
      • With softValues() the key is held strongly, so interning caches, where the key is the value, would never release their entries.
      • It isn't a direct dependency of the xwiki-commons modules.

      A prototype micro-benchmark (16 threads, 99% reads) went from about 4M to about 100M operations per second.

      Attachments

        Issue Links

          Activity

            People

              MichaelHamann Michael Hamann
              MichaelHamann Michael Hamann
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated: