Nuno Santos created OAK-12299:
---------------------------------

             Summary: PersistentDiskCache: precompute elementCount and 
currentWeight to avoid expensive filesystem traversal on metrics reads
                 Key: OAK-12299
                 URL: https://issues.apache.org/jira/browse/OAK-12299
             Project: Jackrabbit Oak
          Issue Type: Improvement
          Components: segment-azure
            Reporter: Nuno Santos


{{PersistentDiskCache}} exposes {{elementCount}} and {{currentWeight}} through 
{{{}SegmentCacheStats{}}}, but both metrics are computed by traversing the file 
store on every read:
 * {{elementCount}} calls {{directory.listFiles().length}} — a syscall that 
allocates an array of all cache files.
 * {{currentWeight}} calls {{FileUtils.sizeOfDirectory(directory)}} — a full 
recursive directory traversal that reads every file's size from the filesystem.

Both operations are O(n) in the number of cached segments and are called each 
time metrics are collected, adding significant overhead to every metrics scrape.

*Solution:* Replace both suppliers with reads of in-memory {{AtomicLong}} 
fields. At construction time, scan the directory once to initialize 
{{cacheSize}} (already maintained in {{{}AbstractPersistentCache{}}}) and a new 
{{elementCount}} field. Increment/decrement {{elementCount}} alongside 
{{cacheSize}} in the existing write and eviction paths. Metric reads then 
become O(1) field reads.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to