com.bigdata.jini.start
Class ServiceConfigurationZNodeMonitorTask

java.lang.Object
  extended by com.bigdata.jini.start.ServiceConfigurationZNodeMonitorTask
All Implemented Interfaces:
Callable<Void>

public class ServiceConfigurationZNodeMonitorTask
extends Object
implements Callable<Void>

This is a standing task whose Future is monitored by the ServicesManagerServer. For each ServiceConfiguration znode, there is one such task per ServicesManagerServer. The task holding the ZLock is the one that handles state changes for ServiceConfiguration znode, including its children, which are the logical service instance znodes.

If the ServicesManagerServer dies, then its znodes will be deleted from the lock queues and someone else (if anyone is left alive) will get the lock and manage the logical service.

If the task itself dies, then the ServicesManagerServer needs to log an error and create a new instance of the task.

Version:
$Id$
Author:
Bryan Thompson

Field Summary
protected  String className
           
protected  JiniFederation<?> fed
           
protected  IServiceListener listener
           
 
Constructor Summary
ServiceConfigurationZNodeMonitorTask(JiniFederation<?> fed, IServiceListener listener, String className)
           
 
Method Summary
protected  void acquireLockAndRun()
          Waits until it acquires the lock and then #runWithLock(ZLock).
protected  void balanceAll(org.apache.zookeeper.ZooKeeper zookeeper, ManagedServiceConfiguration config, String[] watchedSet)
           
protected  void balanceLogicalServices(org.apache.zookeeper.ZooKeeper zookeeper, ManagedServiceConfiguration config)
          Consider the logical service instances for the ServiceConfiguration and if necessary makes an adjustment to the #of logical services that are running.
protected  void balancePhysicalServices(org.apache.zookeeper.ZooKeeper zookeeper, ManagedServiceConfiguration config, String[] watchedSet)
          Consider all the physical service containers and initiates a competition to create or destroy a physical service instance for each logical service that is out of balance.
 Void call()
          Contends for a ZLock on #zpath and then maintains a balance between: (a) the ServiceConfiguration state for a service type; (b) the #of logical service instances for that service type; and (c) the #of physical service instances for any logical service instance for that service type.
protected  void handleEvent(org.apache.zookeeper.ZooKeeper zookeeper, com.bigdata.jini.start.ServiceConfigurationZNodeMonitorTask.ServiceConfigurationHierarchyWatcher watcher, org.apache.zookeeper.WatchedEvent e)
          Handle a WatchedEvent that was read from a queue.
protected  void runWithLock(org.apache.zookeeper.ZooKeeper zookeeper, ZLock zlock)
          Runs with the zlock held.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fed

protected final JiniFederation<?> fed

listener

protected final IServiceListener listener

className

protected final String className
Constructor Detail

ServiceConfigurationZNodeMonitorTask

public ServiceConfigurationZNodeMonitorTask(JiniFederation<?> fed,
                                            IServiceListener listener,
                                            String className)
Parameters:
fed -
className - The class name (aka service type).
Method Detail

call

public Void call()
          throws Exception
Contends for a ZLock on #zpath and then maintains a balance between: (a) the ServiceConfiguration state for a service type; (b) the #of logical service instances for that service type; and (c) the #of physical service instances for any logical service instance for that service type.

The set of znodes to be watched is dynamic. Its only fixed fixed member is the ServiceConfiguration znode. We need to change the watch set as new logical services are created or destroyed and as new physical services are created or destroyed.

This only watch znodes and takes action while the ZLock is held. If it looses the ZLock, it will seek to re-acquire the lock. No actions are taken in the zookeeper event thread. They are all pushed into a queue and then processed in the thread that executes call(). The queue also prevents us from attempting to make more than one adjustment to the managed services at a time.

Note: This task is designed to run until cancelled. It will trap anything but an InterruptedException and restart from acquireLockAndRun().

Specified by:
call in interface Callable<Void>
Throws:
Exception

acquireLockAndRun

protected void acquireLockAndRun()
                          throws Exception
Waits until it acquires the lock and then #runWithLock(ZLock).

Note: This is invoked by call(). If there is any problem, for example a KeeperException.SessionExpiredException, then call() will re-run this method. Therefore, this method MUST request a ZooKeeper each time in order to guarantee that it has a valid zk session when it runs. That zk connection is then passed down so it is available to the invoked methods.

Throws:
Exception

runWithLock

protected void runWithLock(org.apache.zookeeper.ZooKeeper zookeeper,
                           ZLock zlock)
                    throws Exception
Runs with the zlock held.

Parameters:
zookeeper -
zlock -
Throws:
Exception

balanceAll

protected void balanceAll(org.apache.zookeeper.ZooKeeper zookeeper,
                          ManagedServiceConfiguration config,
                          String[] watchedSet)
                   throws Exception
Parameters:
config -
watchedSet -
Throws:
Exception

balanceLogicalServices

protected void balanceLogicalServices(org.apache.zookeeper.ZooKeeper zookeeper,
                                      ManagedServiceConfiguration config)
                               throws Exception
Consider the logical service instances for the ServiceConfiguration and if necessary makes an adjustment to the #of logical services that are running.

Parameters:
config - A recent snapshot of the ServiceConfiguration state.
Throws:
Exception

balancePhysicalServices

protected void balancePhysicalServices(org.apache.zookeeper.ZooKeeper zookeeper,
                                       ManagedServiceConfiguration config,
                                       String[] watchedSet)
                                throws org.apache.zookeeper.KeeperException,
                                       InterruptedException
Consider all the physical service containers and initiates a competition to create or destroy a physical service instance for each logical service that is out of balance. The competition itself is handled by the MonitorCreatePhysicalServiceLocksTask, which watches a well known znode for the appearance of new lock nodes and then enters into a competition to create a new physical service instance. Therefore while this initiates the competition for processing to handle the imbalance, the imbalance is NOT correct while we are in this method.

Parameters:
config - A recent snapshot of the ServiceConfiguration state.
watchedSet - The set of watched znodes (this includes the physical service containers, which is what we need here).
Throws:
InterruptedException
org.apache.zookeeper.KeeperException

handleEvent

protected void handleEvent(org.apache.zookeeper.ZooKeeper zookeeper,
                           com.bigdata.jini.start.ServiceConfigurationZNodeMonitorTask.ServiceConfigurationHierarchyWatcher watcher,
                           org.apache.zookeeper.WatchedEvent e)
                    throws Exception
Handle a WatchedEvent that was read from a queue. Note: Since the events were read from a queue, this method runs in the application thread NOT the zookeeper event thread.

Throws:
Exception


Copyright © 2006-2012 SYSTAP, LLC. All Rights Reserved.