Another improvement that we can do is to introduce the same hierarchical locking to the Topology structure maintained in the Cloud Controller. Its actually the CC who will be updating the local Topology and sending the relevant events. In that case, if we have have only one write lock for the whole Topology in the CC's TopologyBuilder, it will still be a bottleneck. WDYT?

On Thu, Oct 9, 2014 at 3:36 PM, Isuru Haththotuwa <isuruh@apache.org> wrote:
Hi,


Since the Topology is updated from the messaging component only, I removed the methods to access the write locks to an internal class. Those methods will not be exposed to outside by the messaging component. Now, the following methods to obtain read-only locks are exposed from TopologyManager:

    // Topology level read locks

    /**
     * Acquires read lock for the Complete Topology
     */
    public static void acquireReadLock() ;

    /**
     * Releases read lock for the Complete Topology
     */
    public static void releaseReadLock();

    // Application and Service read locks

    /**
     * Acquires read lock for the all Applications
     */
    public static void acquireReadLockForApplications() ;

    /**
     * Releases read lock for the all Applications
     */
    public static void releaseReadLockForApplications();
    /**
     * Acquires read lock for the all Services
     */
    public static void acquireReadLockForServices();

    /**
     * Releases read lock for the all Services
     */
    public static void releaseReadLockForServices() ;

    /**
     * Acquires read lock for a Service
     *
     * @param serviceName service name to acquire read lock
     */
    public static void acquireReadLockForService (String serviceName) ;

    /**
     * Releases read lock for a Service
     *
     * @param serviceName service name to release read lock
     */
    public static void releaseReadLockForService (String serviceName) ;

    /**
     * Acquires read lock for a Cluster. This will acquire the read lock in the following order
     *      1. for the Service
     *      2. for the Cluster
     *
     * @param serviceName service name to acquire read lock
     * @param clusterId cluster id to acquire read lock
     */
    public static void acquireReadLockForCluster (String serviceName, String clusterId);

    /**
     * Releases read lock for a Cluster. This will release the read lock in the following order
     *      1. for the Cluster
     *      2. for the Service
     *
     * @param serviceName service name to release read lock
     * @param clusterId cluster id to release read lock
     */
    public static void releaseReadLockForCluster (String serviceName, String clusterId);

    /**
     * Acquires read lock for the Application
     *
     * @param appId Application id
     */
    public static void acquireReadLockForApplication (String appId) ;

    /**
     * Releases read lock for the Application
     *
     * @param appId Application id
     */
    public static void releaseReadLockForApplication (String appId);


 


On Mon, Oct 6, 2014 at 2:51 PM, Imesh Gunaratne <imesh@apache.org> wrote:
This looks great! As we discussed may be we could provide an interface in the messaging component to acquire and release locks at different sub tree levels. The whole idea is to avoid any possibilities of reading the topology in an inconsistent state.

Thanks

On Mon, Oct 6, 2014 at 12:37 PM, Isuru Haththotuwa <isuruh@apache.org> wrote:
Thanks Imesh.

I have shown some examples for the new hierarchical locking approach. Please do let me know your feedback on this.

   Acquire a write lock:
  
   From root level, acquire read lock, and acquire a write lock only for the
   relevant sub tree.
  
   Acquire a read lock:
  
   From root level, acquire read locks till the relevant sub tree
  
   Examples -
  
   Example 1: Acquiring write lock for a Cluster to modify the Cluster object -
            acquiring:
            1. acquire read lock for all Services
            2. acquire read lock for the particular Service, to which the cluster belongs to
            3. acquire write lock for the Cluster
           
            releasing:
            1. release write lock for the Cluster
            2. release read lock for the particular Service
            3. release read lock for all Services
                                                      
   Example 2: Acquiring write lock to add a new Cluster object -
            acquiring:
            1. acquire read lock for all Services
            2. acquire write lock for the particular Service, to which the cluster belongs to
          
            releasing:
            1. release write lock for the particular Service
            2. release read lock for all Services
          
   Example 3: Acquiring read lock to read Cluster information
            acquiring:
            1. acquire read lock for all Services
            2. acquire read lock for the particular Service, to which the cluster belongs to
            3. acquire read lock for the relevant Cluster
          
            releasing:
            1. release read lock for the relevant Cluster
            2. release read lock for the particular Service
            3. release read lock for all Services
          
   Example 4: Acquiring the write lock to add a deploy a Cartridge (add a new Service)
            acquire:
            1. acquire write lock for all Services
          
            release:
            1. release write lock for all Services

In all of these examples, the lock acquiring happens from top of the tree to down. This is to avoid deadlocks.

On Mon, Oct 6, 2014 at 10:50 AM, Imesh Gunaratne <imesh@apache.org> wrote:
Great! Thanks for the clarification Isuru! 

Yes I agree, I think what we can do is, identify the sub trees that will not break the consistency of the data structure and manage locks at those sub tree level.

Thanks

On Sun, Oct 5, 2014 at 8:07 PM, Isuru Haththotuwa <isuruh@apache.org> wrote:
Hi Lahiru and Imesh,

Thanks a lot for the input.

What I do here is locking only the relevant sub tree of the complete Topology tree, as locking the whole tree is rather inefficient. For an example, when a MemberActivated event is received, we have the cluster id of the cluster that particular member belongs to. IMHO, we only need to acquire the write lock for that cluster , and do not need the lock for complete Topology tree. Therefore, any other thread which needs to do another operation on a separate sub tree (for an example, deploy a new service, etc.) can do that concurrently.

On Sun, Oct 5, 2014 at 12:20 AM, Lahiru Sandaruwan <lahirus@wso2.com> wrote:
Hi Isuru,

Looks like a good move to improve the efficiency,

On Fri, Oct 3, 2014 at 3:05 PM, Isuru Haththotuwa <isuruh@apache.org> wrote:
I did the initial changes, at the testing phase now. For an example, if we need to add a new Service, we do not need to lock everything now. We an only acquire the write lock on Services, and add the Service.

Service can be an element of a group or an app. So shouldn't we lock complete application if we add/modify a service? Otherwise a different thread might change parents/relationships otherwise.
AFAIK a Service gets created when we deploy a cartridge. An application/Group can refer a service. In the case of modifying an Application, we do need to lock the relevant clusters that belong to that Application. I implemented that.

So generally i think we can bring down the locking level to Application, but not the services. Also if we need to read any part, we have to get the read lock for the whole topology, such that the receiver get a particular snapshot of the topology as Imesh also mentioned.
If we need to lock the complete Topology, we can still do that, such as in a Complete Topology event. But IMHO, if we know the exact part (a particular Cluster, etc.) that we need to read/write, we do not need to lock the whole Topology.

Thanks.

If we need to modify a particular Service, can read lock the Services and acquire the write lock on the relevant Service and do the modification.
This support is there for Services, Cluster and Applications.

On Wed, Oct 1, 2014 at 9:27 PM, Manula Chathurika Thantriwatte <manulac@wso2.com> wrote:
Hi Isuru,

+1 for the hierarchical locking approach. Using hierarchical locking we can have more benefits [1].


On Wed, Oct 1, 2014 at 7:46 PM, Isuru Haththotuwa <isuruh@apache.org> wrote:
Hi Devs,

In the current Topology implementation, we acquire read/write locks on Topology from the root level itself. For an example, if we need to modify a single Cluster object, we still need to acquire a write lock from the Topology root level. But, this is a costly operation. Specially, with Service Grouping changes, we would need to traverse through an Application. Since an Application can be a recursive structure, it can be a time consuming operation. in such a scenario, if we are to lock the whole Topology, there will be many threads waiting on that lock.

As a solution, I think we can use hierarchical locking [1]. For an example, when you need to obtain the write lock for a particular Application, you do not need to lock the whole tree, but can lock only that Application itself. However, still we need to get the read locks for the parents.

A separate Lock tree will be maintained for the Topology.

Please share your feedback.



--
Regards,
Manula Chathurika Thantriwatte
Software Engineer
WSO2 Inc. : http://wso2.com
lean . enterprise . middleware




--
--
Lahiru Sandaruwan
Committer and PMC member, Apache Stratos,
Senior Software Engineer,
WSO2 Inc., http://wso2.com



--
Imesh Gunaratne

Technical Lead, WSO2
Committer & PMC Member, Apache Stratos

--
Thanks and Regards,

Isuru H.






--
Imesh Gunaratne

Technical Lead, WSO2
Committer & PMC Member, Apache Stratos

--