Professional Documents
Culture Documents
SA Forum Extended Training Materials: Lock Service
SA Forum Extended Training Materials: Lock Service
SA Forum Extended Training Materials: Lock Service
Lock Service
SAI-AIS-LCK-B.02.01
Contents
This lesson addresses the following topics:
Defining, acquiring, and releasing cluster-wide locks by
processes on any node in the cluster Lock Service Model Lock resources Locks (PR and EX mode locks) Lock Service Types Lock Service Library Lifecycle Functions Lock Resource Functions Lock Functions Detecting and Handling, or Preventing, Deadlocks Orphan Locks and Lock Stripping Alarms and Notifications
Copyright 2006 Service Availability Forum, Inc
Lock Service
The Lock Service is a distributed lock service that allows different application processes on the same or different nodes in the cluster to compete for access to a shared resource in the cluster Examples of a shared resource (lock resource), include files,
databases, processes, components, hardware equipment
Access to a shared resource must be protected so that, e.g., one process does not try to read the resource while another process is writing it, or two processes do not try to write to the resource at the same time To protect access to a shared resource, a process must claim the lock on the resource and the Lock Service must grant the lock to the process before the process can access the resource
Copyright 2006 Service Availability Forum, Inc
Node V
Process Q Component D
Node W
Lock Resource R
Node X
Deadlock detection
Deadlock Two processes hold two different locks, each of
which is required by the other process, so that neither process can make progress The Lock Service warns a process requesting a lock that the lock request will deadlock
Lock orphaning
Lock orphan A lock that was held by a process that has failed
and, thus, will not release the lock The Lock Service does not strip the locks implicitly; rather, the locks are purged explicitly by calling a Lock Service function
Lock Service
Example of a resource being claimed by two processes
Process P on node U
saLckResourceLock() lock lockId1 granted saLckDispatch() Process P now holds the lock
Process Q on node V
saLckResourceLockAsync()
saLckLockWaiterCallback()
saLckDispatch()
saLckResourceUnlock() saLckLockGrant()
SA_LCK_LOCK_ORPHAN
SaLckLockModeT enum
typedef enum{ SA_LCK_PR_LOCK_MODE, SA_LCK_EX_LOCK_MODE } SaLckLockModeT
Allows the requesting process to read from a resource while other processes simultaneously read from the same resource. No process can write to the resource while one or more PR locks are hold on the resource Allows the requesting process to read from, or write to, a resource while it prevents any other process from accessing that resource
Copyright 2006 Service Availability Forum, Inc
Lock Service Library Lifecycle Functions Initializing and finalizing the Lock Service
SaAisErrorT saLckInitialize(
Initializes the Lock Service for the invoking process and registers the callbacks saLckLockGrantCallback() and saLckLockWaiterCallback() lckHandle is used for subsequent calls
SaAisErrorT saLckFinalize(
/* IN */ SaLckHandleT lckHandle);
Closes the association, represented by the lckHandle parameter, between the invoking process and the Lock Service
Releases resources and cancels pending callbacks
10
/* IN */
SaLckHandleT lckHandle,
Obtains the operating system handle, selectionObject, associated with the handle lckHandle This selectionObject is used to detect pending callbacks Invokes, in the context of the calling thread, pending callbacks for the handle lckHandle, as specified by the dispatchFlags parameter
/* IN */
/* IN */
SaLckHandleT lckHandle,
SaDispatchFlagsT dispatchFlags);
11
lckHandle The handle, obtained from the saLckInitialize() function, that designates this particular initialization of the Lock Service
lckOptions Bitmap of the flags:
12
13
SaAisErrorT saLckResourceOpenAsync( /* IN */ SaLckHandleT lckHandle, /* IN */ SaInvocationT invocation, /* IN */ const SaNameT *lockResourceName, /* IN */ SaLckResourceOpenFlagsT resourceFlags);
14
invocation Allows the caller to match this invocation of saLckResourceOpenCallback() with the corresponding invocation of saLckResourceOpenAsync()
lockResourceHandle The handle to the lock resource, which is assigned by the Lock Service and returned to the caller, if the operation completes successfully error Indicates whether the saLckResourceOpenAsync() function was successful
15
a lock resource
Deletes the association between the lockResourceHandle and the corresponding lock resource Any held or pending lock requests are canceled. Once all references to the lock resource have been closed, the Lock Service considers that the lock resource no longer exists However, if the implementation supports orphan locks, and there are orphan locks against the lock resource, the lock resource continues to exist until all of the orphan locks have been purged
16
Lock Functions
Requesting
SaAisErrorT saLckResourceLock( /* IN */ SaLckResourceHandleT lockResourceHandle, /* OUT */ SaLckLockIdT *lockId, /* IN */ SaLckLockModeT lockMode, /* IN */ SaLckLockFlagsT lockFlags, /* IN */ SaLckWaiterSignalT waiterSignal, /* IN */ SaTimeoutT timeout, /* OUT */ SaLckLockStatusT *lockStatus);
Lock Functions
Requesting
SaAisErrorT saLckResourceLockAsync(
/* IN */ SaLckResourceHandleT lockResourceHandle, /* IN */ SaInvocationT invocation, /* OUT */ SaLckLockIdT *lockId,
/* IN */
/* IN */ /* IN */
SaLckLockModeT lockMode,
SaLckLockFlagsT lockFlags, SaLckWaiterSignalT waiterSignal);
Lock Functions
Granting
a lock on a resource
Callback invoked when the operation requested by the invocation of saLckResourceLockAsync() completes invocation Used to match this invocation of saLckLockGrantCallback() with the corresponding invocation of saLckResourceLockAsync() lockStatus The status of the lock error Error value supplied by the Lock Service. The lock is granted, and the lock status is defined, only if the error value is SA_AIS_OK Implementations that support deadlock detection will detect deadlocks and indicate in the lockStatus field that granting the lock would cause a deadlock
Copyright 2006 Service Availability Forum, Inc
19
Lock Functions
Lock
waiter callback
Callback invoked when a process holds the lock, identified by lockId, that is blocking another lock request waiterSignal Specified by a lock requestor for delivery to the lock holder blocking the request lockId The lock identifier that was returned by the Lock Service to the process through saLckResourceLock() or saLckResourceLockAsync() modeHeld The lock mode (i.e., PR or EX) in which the process holds the lock
/* IN */
SaLckLockModeT modeRequested);
modeRequested The lock mode (i.e., PR or EX) requested, for which the lock request is blocked
20
Lock Functions
Releasing
a lock
Releases a lock on the lock resource, identified by lockId, synchronously lockId The identifier of the lock to be released. If the lockId identifies a pending lock request, the pending lock request will be canceled timeout The time by which the saLckResourceUnlock() function must complete
SaAisErrorT saLckResourceUnlockAsync( Releases a lock on the lock resource, identified by lockId, asynchronously /* IN */ SaInvocationT invocation, invocation Allows the caller to match this /* IN */ SaLckLockIdT lockId); invocation of saLckResourceUnlock() with the corresponding callback lockId The identifier of the lock to be released The result of the operation is returned in the saLckResourceUnlockCallback() function, supplied in saLckInitialize()
Copyright 2006 Service Availability Forum, Inc
21
Lock Functions
Issuing
Lock Service
Example of a resource being claimed by two processes
Process P on node U
saLckResourceLock() lock lockId1 granted saLckDispatch() Process P now holds the lock
Process Q on node V
saLckResourceLockAsync()
saLckLockWaiterCallback()
saLckDispatch()
saLckResourceUnlock() saLckLockGrant()
23
Deadlock
To prevent deadlock, processes that use the Lock Service can choose to define a priority order 0,1,...,n for the locks. The processes claim locks in priority order If a lock with priority j is held and another lock with priority k
is claimed, then k must be greater than j
Optionally, the Lock Service detects deadlocks, and returns to a lock requestor SA_LCK_LOCK_DEADLOCK as the lockStatus to indicate that claiming a lock will cause deadlock. The Lock Service does not resolve deadlocks It is the responsibility of processes that uses the Lock Service to ensure that invocations of lock requests do not result in deadlock
Copyright 2006 Service Availability Forum, Inc
24
Deadlock
Example of deadlock
Process P
lock lockId1 granted
Lock Service
Process Q
saLckResourceLock(..,lockId1,..)
saLckResourceLock(..,lockId2,..)
saLckResourceLock(..,lockId1,..) SA_LCK_LOCK_DEADLOCK
Deadlock !!!
Copyright 2006 Service Availability Forum, Inc
25
Lock Stripping
A lock can be stripped from its holder under the following conditions:
The process owning the lock fails The node that hosts the process owning the lock fails The process owning the lock calls the saLckResourceClose()
function of the Lock Service for the particular locked resource, or it calls the saLckFinalize() function of the Lock Service without first releasing all of its locks
26
Lock Stripping
Lock stripping without orphan locks
Process P Lock Service
Lock lockId1 is stripped from process P
saLckResourceLock(..,lockId1,..) Lock lockId1 granted
Process Q
Process crashes
If the lock is a PR lock and other processes are already holding the lock, no change is reported to the other lock holders
27
Lock Stripping
Lock stripping with orphan locks
SA_LCK_LOCK_ORPHAN
Process P
Lock Service
Process Q
Process crashes
If the implementation supports orphan locks and the lock requestor specified the SA_LCK_LOCK_ORPHAN flag, the lock is not stripped; rather, it remains in the grant queue where it can block other lock requests The Lock Service provides an API to purge orphan locks held on a lock resource
Copyright 2006 Service Availability Forum, Inc
28
29
SA_NTF_ALARM_ COMMUNICATION
LCK service, same as notifying object Applicable value from SaNtfProbableCauseT enum
Perceived Severity
Mandatory
30
Probable Cause
Mandatory
Perceived Severity
Mandatory
Summary
In this lesson you have learned about:
Defining, acquiring, and releasing cluster-wide locks Lock Service Model
Lock resources Locks (PR and EX mode locks) Lock Service Types
32