polaris: `EntityCache` leaks memory & prone to inconsistencies & race conditions
Describe the bug
org.apache.polaris.core.persistence.cache.EntityCache uses two containers:
private final Cache<Long, EntityCacheEntry> byId;
private final AbstractMap<EntityCacheByNameKey, EntityCacheEntry> byName;
The first one is a Caffeine cache, the second one is a Java Map.
byName can become inconsistent due to race conditions, multiple threads acting on the same entity IDs and/or entity names.
The implementation assumes, that there are no multi-threaded accesses against the same entity IDs and/or entity names, which is not guaranteed.
To Reproduce
No response
Actual Behavior
No response
Expected Behavior
No response
Additional context
No response
System information
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 18 (14 by maintainers)
@ben-manes I noticed that you are working on the IndexedCache class. Could you review the one I embedded in #1339 please?
Here is some feedback on using
IndexedCacheas suggested by @ben-manes. I have hacked an implementation of EntityCache that uses it.IndexedCachethat exists at https://github.com/ben-manes/caffeine/blob/master/examples/indexable/src/main/java/com/github/benmanes/caffeine/examples/indexable/IndexedCache.java cannot be used as-is because it seems polaris-core depends on Java 11. Btw it is a mismatch with the README file from the root folder that says Polaris is built with Java 21.PolarisMetaStoreManagerneeds a reference toPolarisCallContext. This makes it tricky to use the cache as the key that will be indexed in primary key (Long) and secondary key (EntityCacheByNameKey) is not enough to use the mapping function and fetch values from the database when there is a cache miss.PolarisCallContext: https://github.com/pingtimeout/polaris/commit/a3e6ad571fdb967ac8bea50338d22b7a4648f472#diff-e3a2577e61a31e81fb23a9c4d7e9cb9c75e1ffed7d6292ed4744949652865a96R38-R80.primaryKey(...)and.addSecondaryKey(...)methods become hacky as well. They have to passnullas the context: https://github.com/pingtimeout/polaris/commit/a3e6ad571fdb967ac8bea50338d22b7a4648f472#diff-e3a2577e61a31e81fb23a9c4d7e9cb9c75e1ffed7d6292ed4744949652865a96R98-R99Let me try to unblock this discussion.
To determine whether these issues are real, I worked on JCStress tests. JCStress is a testing framework developed by Oracle that helps verify correct behavior of concurrent Java code.
I have implemented multiple concurrency tests against the
EntityCacheand found strange behaviour. Would it be possible to get a review? The full analysis is available here. Note that the test cases are far from covering all code paths in EntityCache. So there may be other undiscovered issues. The code is available here.Observed behaviour:
nulleven in the case where an entity exist in the databaseThere is also a strange result where all actors observed the entity in version 3 but none observed entity in version 2, which should have been the case. However I have no explanation for that one.
@eric-maynard @flyrain could you review the tests and confirm they are correct please?
Elaborating more on this, because it seems that the concurrency implications in EntityCache.java of the implementation are maybe not obvious to everybody:
byNameis aj,u.c.ConcurrentHashMap. It is not guaranteed to be in sync with thebyIdCaffeine cache, because updates tobyNamehappen outside of the (“atomic”) operations of Caffeine. This means, that ‘byName’ can contain different values than in ‘byId’.byNameupdates are not in-sync withbyId, it can contain stale entries and retain those forever. This means, that the map can grow infinitely.byNamequeries can yield wrong results, which may cause issues at the calling codeNB: Ben proposed a workaround above ~ 2 months ago