The top 4 business objects in Ambari:
All use ReadWriteLock implementations to prevent dirty reads and concurrent writes. However, ClusterImpl exposes a "global" ReadWriteLock which the other business objects share. This causes tremendous problems with deadlocks, especially on slow databases.
Consider the case where you have 3 threads:
- thread-1 acquires ClusterReadLock
- thread-2 acquires ServiceComponenWriteLock
- thread-3 tries to get ClusterWriteLock and is blocked by thread-1
- thread-2 tries to get ClusterReadLock and is blocked by thread-3
- thread-1 tries to get ServiceComponentReadLock and is blocked by thread-2
Essentially, the exposure of the "cluster global lock" causes problems since multiple threads can acquire other internal locks and be blocked waiting on the global lock.
In general, I don't believe that the read locks help at all. Ambari usually encounters these locks while try to display web page information. Once displayed, the locks are removed and the information is already stale if there were write threads waiting.
These locks should be investigated and, for the most part, except in some cases involving concurrent writes, removed.
Part of the problem revolves around our assumption about how the ReadWriteLock works. The issue in the above scenario is that the clusterWriteLock request is pending. This actually blocks all subsequent readers even though the lock is not fair.
FYI, this code shows that a reader, in unfair mode, will wait when there is a waiting writer: