Details
-
Improvement
-
Resolution: Unresolved
-
Major
-
None
-
10.7
-
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
- is related to
-
XCOMMONS-3938 Cache bean descriptors with ClassValue instead of SoftCache
-
- Closed
-
- relates to
-
XWIKI-22979 Access to pages is slow under high load on the security cache
-
- Closed
-
-
XWIKI-25890 Pages are slow under high load because of contention on the async renderer cache
-
- Open
-