SDN Controller Programming Guide
73
Application needs to define a key that can be used to protect parallel access to a shared resource.
Applications on different controller nodes should agree upon the key and acquire necessary lock
on it before accessing the shared resource.
Following are the types of locks supported by distributed locking service:
Read Lock is a shared lock. More than one read lock can be allowed on a key
simultaneously.
Write Lock is an exclusive lock. No previous lock [read or write] can be in held state before
a write lock can be granted. Read lock cannot be granted until all the previous write locks
are released.
Multi Lock is a pool of read and/or write locks. A multi lock is granted only if all the
member locks are acquired.
While creating a lock, application needs to provide application Id [app name and instance id]
and a lock hierarchy [key] which uniquely identifies the lock.
In case of an application instance failure, its peer application instance running on a different
controller instance can take over the locks created by the failed application. Newer application
needs to query for the locks created by the failed instance using application name and instance
Id. To take over locks of an application instance, the new application instance needs to query lock
service for the locks owned by the older application. Based on the need, ownership of the locks
can be changed using the lock service methods.
HAService extends LockService interface and hence methods of this interface shall be accessed via
HAService in HP SDN environment.
LockService interface—provides the methods for instantiating different type of locks and query
them during fail over scenarios. An existing lock can be deleted through this interface methods.
Lock Interface—gives application access to a Lock. Enables applications to perform specific
operations like acquire, tryAcquire, unlock on a Lock.
Please refer to the JavaDocs for LockService and Lock interfaces for the detailed explanation of the
methods provided and corresponding functionality.
Lock Service Example:
import com.hp.sdn.ha.HAService;
import com.hp.dist.lock.LockService;
import com.hp.dist.lock.Lock;
...
@Component
public class LockUser {
private static String appName = "LockUserApp";
private static ApplicationId id = new ApplicationId(appName, "Inst1");
@Reference(cardinality=ReferenceCardinality.MANDATORY_UNARY,
policy=ReferencePolicy.DYNAMIC)
private volatile HAService haService;
private void readLockOperations() {
Lock readLock = null;
LockName name = new LockName(appName, "Key1");