Distributed Database Management Notes - 2
Distributed Database Management Notes - 2
The outline of this chapter is as follows. Section 9.1 addresses the deadlock problem and
the deadlock prevention methods for distributed system are discussed in Section 9.2. In
Section 9.3, distributed deadlock avoidance has been represented. The techniques for
distributed deadlock detection and recovery are focused in section 9.4.
Deadlock situation can be characterized by wait-for graphs, directed graphs that indicate
which transactions are waiting for which other transactions. In a wait-for graph, nodes of
the graph represent transactions and edges of the graph represent the waiting for
relationships among transactions. An edge is drawn in the wait-for graph from transaction
Ti to transaction Tj if the transaction Ti is waiting for the lock on a data item that is
currently hold by the transaction Tj. Using wait-for graph, it is very easy to detect
whether a deadlock situation has occurred in a database environment or not. There is a
deadlock in the system if and only if the corresponding wait-for graph contains a cycle.
The resolution of deadlock situation is much easier in centralized DBMS than that of
distributed DBMS. In a centralized DBMS, only one local wait-for graph is drawn to
detect the deadlock situation. The detection of deadlock in a distributed DBMS is more
complicated, because the circular waiting situation which determines a deadlock situation
may involve several different sites. Thus, in a distributed DBMS it is not sufficient to
draw a local wait-for graph for each local DBMS only, but it is also necessary to draw a
global wait-for graph for the entire system to detect deadlock situation. In a distributed
database, a local wait-for graph is a portion of the global wait-for graph which consist
only those nodes and edges that are completely contained at a single site. Three general
techniques are available for deadlock resolution in a distributed database system. These
are distributed deadlock prevention, distributed deadlock avoidance and distributed
deadlock detection and recovery from deadlock which are described in the following.
167
Example 9.1
Let us assume that in a database environment, there are two transactions T1 and T2
respectively. Further assume that the transaction T1currently holding an exclusive lock on
data item P and the transaction T2 holding an exclusive lock on data item Q. Now, if the
transaction T1 requires a write operation on data item Q, the transaction T 1 has to wait
until the transaction T2 releases the lock on the data item Q. However, in the meantime if
the transaction T2 requires a read or a write operation on the data item P, the transactions
T2 also has to wait for the transaction T1. In this situation, both the transactions T1 and T2
has to wait for one another indefinitely to release their respective locks and no transaction
can proceed for execution, thereby deadlock situation arises.
168
One solution to the above problem is that using unique timestamp value of each
transaction as the priority of the transaction. One way to obtain a global timestamp value
for every transaction in a distributed system is that a unique local timestamp value is to
be assigned to each transaction by its local transaction manager and the site identifier will
be appended to the low-order bits with this value. Hence, timestamp values are unique
throughout the distributed system and do not require that clocks at different sites should
be synchronized precisely. Based on timestamp values, there are two different techniques
for deadlock prevention, known as Wait-die and Wound-Wait.
The deadlock situation cannot arise and the younger transaction is restarted in both the
above methods. The main difference between the two techniques is that whether they
preempt the active transaction or not. In wait-die method a transaction can only be
restarted when it requires accessing a new data item for the first time. A transaction that
has acquired locks on all the required data items will never be aborted. In wound-wait
method, it is possible that a transaction that has already hold a lock on a data item is
aborted, because another older transaction may request the same data item. Wait-die
method gives preference to younger transactions and aborts older transactions, since older
transactions wait for younger ones and it tends to wait longer as it gets older. On the other
hand, wound-wait method prefers older transactions since an older transaction never
waits for a younger transaction. In wait-die method, it is possible that a younger
transaction is aborted and restarted for a number of times if the older transaction holds
the corresponding lock for a long time, while in wound-wait method the older transaction
is aborted and restarted for a single time.
169
The main disadvantage of deadlock prevention scheme is that it may result unnecessary
waiting and rollback. Furthermore, some deadlock prevention scheme may require more
sites in a distributed system are to be involved in the execution of a transaction.
Deadlock avoidance methods are more suitable than deadlock prevention schemes for
database environments, but it requires runtime support for deadlock management, which
adds runtime overheads in transaction execution. Further, in addition to requiring
predeclaration of locks, a principal disadvantage of this technique is that it forces locks to
be obtained sequentially, which tends to increase response time.
Deadlock detection and recovery is the most popular and suitable technique for
deadlock management in database environment. In deadlock detection and recovery
method, first it is detected whether any deadlock has occurred or not in the system. After
detection of a deadlock situation in the system, the victim transaction is chosen and
aborted in order to resolve the deadlock situation. Deadlock situations are detected by
explicitly constructing the wait-for graph and searching it for cycles. A cycle in the wait-
for graph indicates that deadlock has occurred and one transaction in the cycle is chosen
as victim, which is aborted and restarted. To minimize the cost of restarting, the victim
selection is usually based on the amount of data items used by each transaction in the
cycle.
170
indicates that a deadlock has occurred locally. The local wait-for graphs in figure 9.1
illustrated that no deadlock has occurred locally in three different sites since there are no
cycle in local wait-for graphs but it does not guarantee that no deadlock has occurred
globally. In order to detect deadlock situation in the distributed system, it is necessary to
construct a global wait-for graph from these different local wait-for graphs and searching
it for cycles.
T1 T2 T5 T1
T2 T3
T5 Site 3
Site 2
Site 1
The corresponding global wait-for graph in figure 9.2 illustrated that a deadlock has
occurred in the distributed system although no deadlock has occurred locally.
T1 T2 T3
T5
171
There are three different techniques for detecting deadlock situation in the distributed
system. These are Centralized deadlock detection, Hierarchical deadlock detection
and Distributed deadlock detection which are described in the following.
In order to minimize the communication cost, the deadlock detection coordinator can
only send the changes that have to be made in the local wait-for graph to the lock
manager. These changes represent the addition or removal of edges in the local wait-for
graph. The actual length of a period for global deadlock detection is a system design
decision and it is a trade-off between the communication cost of the deadlock detection
process and the cost of determining deadlocks late. The communication cost increases if
the length of the period is larger while some deadlocks in the system are undetected if the
length of the deadlock detection period is smaller.
The centralized deadlock detection approach is very simple, but it has several drawbacks.
This method is less reliable, since the failure of the central site causes the deadlock
detection impossible. The communication cost is very high in this case, since other sites
in the distributed system send their local wait-for graphs to the central site. Another
disadvantage of centralized deadlock detection technique is that false deadlocks can
detected for which the deadlock recovery procedure may be initiated, although no
deadlock has occurred. In this method, unnecessary rollbacks and restarts of transactions
may also results due to phantom deadlocks. (The details about false deadlock and
phantom deadlock have been discussed in Section 9.4.4).
172
leaf nodes of the tree, while the non-leaf nodes are responsible for detecting any deadlock
situation involving all its child nodes. The hierarchical deadlock detection method is
illustrated with an example in the figure 9.3.
In the following figure local deadlock detection is performed at leaf nodes site1, site2,
site3, site4 and site5. The deadlock detector at site6, namely DD12, is responsible for
detecting any deadlock situation involving its child nodes site1 and site2. Similarly, site3,
site4 and site5 send their local wait-for graphs to site7 and the deadlock detector at site7
searches for any deadlock involving its adjacent child nodes. A global deadlock detector
exists at the root of the tree that would detect the occurrence of global deadlock situation
for the entire distributed system. Hence, the global deadlock detector resides at site 8 that
would detect the deadlock between site6 and site7.
Site8
GDD
173
9.4.3 Distributed Deadlock Detection
In distributed deadlock detection method, a deadlock detector exists at each site of the
distributed system. In this method, each site has the same responsibility and there is no
such distinction like local and global deadlock detector. A variety of approaches had
been proposed for distributed deadlock detection algorithm, but the most well-known and
simplified version which is presented here was developed by R. Obermarck at 1982.
In this approach, a local wait-for graph is constructed for each site by the local deadlock
detector. An additional external node is added to the local wait-for graph since each site
in the distributed system receives the potential deadlock cycles from other sites. In
distributed deadlock detection algorithm, the external node Tex is added to local wait-for
graph in order to indicate whether any transaction from remote sites is waiting for the
data item that is being held by the transaction at local site or any transaction from local
site is waiting for the data item that is currently being used by the transaction at remote
sites. For instance, an edge from the node Ti to Tex exists in the local wait-for graph, if the
transaction Ti is waiting for a data item that is already being held by any transaction at
remote site. Similarly, an edge from the external node Tex to Ti exists in the graph, if a
transaction from remote site is waiting to acquire a data item that is currently being held
by the transaction Ti at local site. Hence, the local detector checks for two things in order
to determine deadlock situation. If a local wait-for graph contains a cycle that does not
involve the external node Tex, then it indicates that one deadlock has occurred locally and
it can be handled locally. On the other hand, a global deadlock potentially exists if the
local wait-for graph contains a cycle involving the external node Tex. However, the
existence of such a cycle does not necessarily imply that there is global deadlock, since
the external node Tex represents different agents.
The local wait-for graphs are merged in order to determine the global deadlock situation.
To prevent sites from transmitting their local wait-for graphs to each other, a simple
strategy is followed here. In this strategy, one timestamp value is allocated to each
transaction and imposes the rule that one site Si transmits its local wait-for graph to the
site Sk, if a transaction, say, Tk at site Sk is waiting for the data item that is currently being
held by the transaction Ti at site Si and ts(Ti) < ts(Tk). If ts(Ti) < ts(Tk), the site Si
transmits its local wait-for graph to the site Sk and the site Sk add this information to its
local wait-for graph and check for cycles not involving the external node Tex in the
extended graph. If there is no cycle in the extended graph, the process continues until a
cycle appears and it may happen that the entire global wait-for graph is constructed and
no cycle has been detected. In this case, it is decided that there is no deadlock in the
entire distributed system. On the other hand, if the global wait-for graph contains a cycle
without involving the external node Tex, it is concluded that deadlock has occurred in the
system. The distributed deadlock detection method is illustrated in figure 9.4.
174
Ti Site 2 Tj Site 3 Tk Site 1
The major benefit of distributed deadlock detection algorithm is that it is potentially more
robust than centralized or hierarchical methods. The deadlock detection is more
complicated in this method, because no single site contains all the information that is
necessary to detect global deadlock situation in the system and therefore substantial inter-
site communication is required which increases the communication overhead. Another
disadvantage of distributed deadlock detection approach is that it is more vulnerable to
the occurrence of false deadlocks than centralized or hierarchical methods.
175
Another problem is that a transaction Ti which blocks another transaction may be
restarted for reasons that are not related to deadlock detection. In this case, until the
restart message of the transaction Ti is transmitted to the deadlock detector, the deadlock
detector can find a cycle in the wait-for graph that includes the transaction T i. Hence, one
deadlock situation is detected by the deadlock detector and this is called phantom
deadlock. When the deadlock detector detects a phantom deadlock, it may unnecessarily
restart a transaction other than Ti. In order to avoid unnecessary restarts for deadlocks,
special safety measures are required.
176
9.6 Review Questions
Exercises
(ii) Which of the following method is best suitable for deadlock handling in
distributed system?
a. Deadlock prevention
b. Deadlock avoidance
c. Deadlock detection and recovery
d. All of these.
(iii) Wait-die is a
a. Non-preemptive deadlock avoidance algorithm
b. Preemptive deadlock prevention algorithm
c. Non-preemptive deadlock prevention algorithm
d. Preemptive deadlock avoidance algorithm.
(iv) Wound-wait is a
a. Non-preemptive deadlock avoidance algorithm
b. Preemptive deadlock prevention algorithm
c. Non-preemptive deadlock prevention algorithm
d. Preemptive deadlock avoidance algorithm.
177
(v) In wait-die technique
a. Older transaction is allowed to wait for younger transaction
b. Younger transaction is allowed to wait for older transaction
c. Both a. and b.
d. None of the above.
178
(xii) A deadlock detector exists in each site of the distributed system
a. In centralized deadlock detection
b. In hierarchical deadlock detection
c. In distributed deadlock detection
d. Both in hierarchical and distributed deadlock detection.
(xv) Which of the following deadlock detection method is more vulnerable to the
occurrence of false deadlocks?
a. Centralized deadlock detection
b. Hierarchical deadlock detection
c. Distributed deadlock detection
d. All of the above.
Site1 Site2
Tj Tj Tk
Ti Tk
Ti
3. Consider the same transactions of exercise 2 are executed with a deadlock prevention
method. Prove that the deadlock is prevented using both the preemptive and the non-
preemptive method. Assume arbitrarily transaction timestamp values and determine
which transactions are restarted in both cases.
179
4. Consider the local wait-for graphs at two different sites in the following figure.
T1 T2
T2 T3
T5 T5 T1
Site 1 Site 2
5. Consider the same transactions of exercise 4 are executed with a deadlock prevention
method. Prove that the deadlock is prevented using both the preemptive and the non-
preemptive method. Assume arbitrarily transaction timestamp values and determine
which transactions are restarted in both cases.
180