Method for accessing shared data

Abstract: Apparatus for scheduling at least two concurrent transactions accessing a shared data is provided. When a lock request is granted, the apparatus provides for constructing a history file for the shared data to show each data accessing transaction, and also provides for constructing a serialization graph with each node denoting an active transaction, and each directed edge denoting a dependency between two transactions. The serialization graph (20) is searched for a cycle formed by transactions (24,25,26), and if any is found, the transactions are aborted and restarted.

Description: TECHNICAL FIELD OF THE INVENTION This invention relates generally to the field of database concurrency control, and more particularly to an apparatus and method for accessing shared data. BACKGROUND OF THE INVENTION Computer programs are widely used by businesses to access databases for performing business transactions, such as making a savings account deposit or a withdrawal, reserve or purchase an airline ticket, buy or sell a security, etc. Each of these business transactions rely on the integrity of the data in the databases, i.e. the balance in the savings account must reflect the correct amount after the deposit or withdrawal. However, the databases described in the business transactions above are generally accessed and modified by multiple concurrently run computer programs. These concurrently run programs interleave their access to databases, which may result in one program's database access interfering with another program's access to the same database. Such interferences may result in erroneous data in the databases. Protocols exist presently to avoid such database errors caused by interfering programs. One such protocol is a static two-phase locking scheme. In a transaction including a plurality of steps, for example, some steps access at least one data item in a database. The static two-phase locking scheme would provide for "locking" all the data items to be accessed, before any step in the transaction is performed, to prevent any other transactions from accessing and altering the data items. The data items are "unlocked" immediately following the last step of the transaction. A similar protocol, dynamic two-phase locking scheme, locks each data item to be accessed by the transaction immediately prior to each accessing step, and then releases the locks on all the data items immediately following the last step of the transaction. Both the static and dynamic two-phase locking scheme exhibit a first phase, during which locks are acquired on required data items, and a second phase, during which all the locks are released. By locking the data items, a form of scheduling is achieved, since only the programs that have locked all of their required data items are executed to completion. Other programs which have not acquired locks on all of their data items, because at least one is locked by another program, must wait. In other words, the locking scheme in effect puts concurrently run programs in serial execution form. A problem arising from the two-phase locking scheme is the reduction in program execution throughput. For example, if access of data item A is required by transactions X and Y, and transaction X has a lock on data item A. Since the lock on data item A will not be released until the completion of the very last step of transaction X, transaction Y must wait until the end of transaction X even though the transaction X step associated with data item A may have long been completed. Therefore, from the foregoing it may be seen that the concurrency of the programs is not fully exploited to achieve high throughput when two-phase locking is employed. There often exists one data item that every transaction must access, such as a history database that records all transactions. By using the two-phase locking scheme, a bottleneck or hot spot is created at that data item, since every transaction must wait for the transaction that currently has the lock on that data item to complete. Therefore, hot spot detection and special handling are needed in a two-phase locking scheme, so that the blocked transactions do not need to wait the entire duration of the transaction which currently has a lock on the hot data item. Another protocol used to serialize database transactions is the tree and path protocol. In the tree and path protocols, the data items must be accessed following an inherent and specific sequence. The predictable access behavior is exploited by the tree and path protocols to serialize data item access. A data item in a tree or path structure may be locked and accessed, and the data item following it in sequence is then locked before releasing the lock on the previous data item. Although the tree and path protocols are not two-phased, and therefore do not have the reduced throughput problem described above, these protocols are only applicable to those databases which are accessible only in a predetermined order. Therefore, the tree and path protocols are ineffective for data items which do not have an inherent access order. Accordingly, a need has arisen for a locking scheme that more fully exploits the concurrency of multiple transactions to increase throughput, and which does not rely on the inherent order of the data. Proceedings of the 7 th ACM SIGACT-SIGMOD-SIGART Symposium on Database Systems, pages 135-141, on which the preamble of claim 1 is based, discloses a serialisation algorithm in which: each transaction is represented by a node on a graph; and the constraint that one transaction must read a data item after another transaction has written to it is represented by a directed edge joining their two nodes. As each step arrives, any necessary new edges are added to the graph. If a step cannot be added without creating a cycle, the node for the transaction to which that step belongs, and all of the edges connected to that node, are deleted. Lecture Notes in Computer Science No. 344, Graph Theoretic Concepts in Computer Science: proceedings of the International Workshop WG '88, pages 107-121, describes testing for serialisability by constructing such a graph and testing whether it is acyclic. The present invention as set forth in the appended claims provides a method for scheduling at least two concurrent transactions accessing a shared data, comprising the steps of: forming a history file associated with said shared data listing each accessing transaction sequentially; constructing from said history file a serialisation graph showing dependencies between said transactions; searching for a cycle in said serialisation graph; and detecting a cycle formed by said transactions in response to said searching step; characterised by aborting all of said transactions in said cycle and any transaction dependent therefrom. It is possible in accordance with the present invention to increase the concurrency and throughput of transactions which access shared data. It is also possible in accordance with the present invention to provide for serialisable scheduling of the execution of transactions without relying on the inherent access order of the data, and the present invention is therefore applicable to any shared data. It is further possible in accordance with the present invention to permit each transaction to specify the type of locking scheme desired, whether it be optimistic or pessimistic. BRIEF DESCRIPTION OF THE DRAWINGS For a better understanding of the present invention, reference may be made to the accompanying drawings, in which: FIG. 1 is an acyclic transaction serialization graph; and FIG. 2 is a transaction serialization graph showing a cycle. DETAILED DESCRIPTION OF THE INVENTION Referring to TABLE 1, three concurrently-run transactions A, B and C are shown with respective transaction steps. A transaction may be defined as an execution of a program that accesses a shared database. Additionally, a database may be defined as a collection of data items, where each data item has a value. Typically, transactions may alter the value of the data items in a database by writing another value in its place. At the end of the program execution, a commit or abort step is executed. The commit step indicates that the program has terminated normally and all of its effects should be made permanent. On the other hand, the abort step indicates that the transaction has terminated abnormally, due to a number of reasons, and all of its effects should be obliterated. Transaction A, shown in the leftmost column, has three sequential steps of locking a data item x, writing a value to data item x and then freeing the lock on data item x. Subsequently, transaction B, shown in the center column, also locks data item x and locks a second data item y prior to reading the value of data item x and writing to data item y. Transaction B then releases the locks on data items x and y. Transaction C, shown in the rightmost column, accesses data items y, x and z. Transaction C begins by locking data items y, x and z and then reads the value of data items y and x and writes to data item z. The locks on data items y, x, and z are then released. A B C . . . lock (x) . . . . . . write (x) . . . free (x) . . . lock (x) lock (y) . . . . . . read (x) write (y) . . . . . . free (x) free (y) ... lock (y) . . . lock (x) . . . lock (z) . . . read (y) . . . read (x) . . . write (z) . . . free (y) . . . free (x) free (z) Commit . . . Commit Commit x Read Write A B C y Read Write B C z Read Write C TABLE 2 tabularizes each of the transactions that act on data item x. It shows, in sequential order, transaction A writing to data item x, followed by transactions B and C reading data item x. TABLE 2 may represent a history file associated with data item x that maintains a list of active transactions as each locks data item x and frees it, but has not yet committed or aborted. As each transaction is committed or aborted, its corresponding entry in the history file is deleted. Similarly, TABLE 3 shows the current active transactions associated with data item y. As indicated in TABLE 1, transaction B wrote data item y and transaction C subsequently read data item y. TABLE 4 representing a history file for data item z shows that transaction C is currently active and has written to data item z. From the history files associated with each data item x, y and z, a transaction serialization graph may be constructed to show the dependencies, if any, between transactions A, B and C. Referring to FIG. 1, a transaction serialization graph 10 represents the interdependencies of the steps executed by transactions A, B and C. Transactions A, B and C are represented by nodes 11-13 in graph 10. Shown between node 11 and node 12 is a directed edge 14 from node 12 to node 11, which is indicative of a dependency of transaction B on transaction A. Transaction B is dependent on transaction A because it reads the value of data item x, which is written by transaction A. Therefore, the correctness of the step in which transaction B reads the value of data item x is dependent on whether transaction A commits or aborts. If transaction A commits, the step in transaction B that reads the value of data item x written by transaction A is correct. If transaction A aborts, the step in transaction B that reads the value of data item x will incorrectly read the value thereof and therefore transaction B should be aborted. In a similar fashion, transaction C is dependent on transaction B, as shown by a directed edge 15 from node 13 to node 12. Referring to TABLE 3, the history file associated with data item y shows that current active transaction B writes a value to data item y, and subsequently active transaction C reads the value written by transaction B. Transaction C is also dependent on transaction A, as shown in TABLE 2, where transaction C reads the value of data item x written by transaction A. This dependency is shown by a directed edge 16 from node 13 to node 11 in transaction serialization graph 10. Although transaction C additionally writes a value to a data item z, as shown in TABLE 4, no other transaction depends on this step of transaction C, nor does this step depend on any other transaction, therefore it is not represented in transaction serialization graph 10. As transaction serialization graph 10 is updated to show the inter-dependencies of the active transactions, it is searched for cycles. A cycle is defined in the art of graph theory as a simple path where the first and last nodes are identical. Algorithms for searching cycles in a directed graph are known in the art of computer science, for example, one is used to search for cycles in a wait-for graph in deadlock detection algorithms. Such cycle detection algorithms may be employed herein. For further details please consult reference books such as Concurrency Control and Recovery in Database Systems by P.A. Berstein et al. and The Theory of Database Concurrency Control by Christos Papadimitriou. It is determinable by an algorithm which searches for cycles that transaction serialization graph 10 does not include a cycle, and is therefore an acyclic graph. A transaction serialization graph which does not contain a cycle then has serializable transactions. In other words, if a transaction is not part of a cycle in a transaction serialization graph and is not dependent on another active transaction, then it may commit, so that its effects may be made permanent. Since transaction A does not depend on any other transaction, it may commit. The history file of data item x, shown in TABLE 2, is updated by deleting transaction A, since it is no longer active. Transaction serializable graph 10 is updated accordingly by deleting node 11, edge 14 and edge 16. Transaction B may now execute a commit step for its termination, since it also is not a part of a cycle and is not dependent on another active transaction. TABLES 2 and 3 are in turn updated by deleting transaction B, and node 12 and edge 15 are deleted from graph 10. Transaction C may also commit, since it satisfies the commit conditions enumerated above. Therefore, transaction C entry in the history files of data items x, y and z (TABLES 2, 3 and 4) are deleted. Graph 10 is also modified to show the commitment of transaction C. A B C . . . . . . lock (x) . . . write (x) . . . . . . free (x) lock (x) . . . . . . lock (y) read (x) . . . . . . write (y) free (x) . . . . . . free (y) lock (y) . . . lock (z) . . . read (y) . . . write (z) . . . free (y) free (z) lock (z) . . . read (z) . . . free (z) . . . . . . . . . . . . Abort . . . Abort Abort x Read Write A B y Read Write B C z Read Write C A Referring to TABLE 5, a different scenario arises. Transaction A, its steps shown in the leftmost column, locks, writes and frees data item x. This is reflected in an entry in a history file of data item x, represented by TABLE 6. Subsequently, transaction B locks data items x and y, reads data item x, writes data item y, and then frees data item x and y. Similarly, the accesses of data items x and y by transaction B are reflected in TABLES 6 and 7. Referring to FIG. 2, a second transaction serialization graph 20, showing nodes 21-23 representing transactions A, B and C, is updated with a directed edge 24 to indicate the dependency of transaction B on transaction A. Transaction C also acquires locks on data items y and z to read data item y and write to data item z, and subsequently frees data items y and z. The requirement to read data item y creates a dependency of transaction C on transaction B, since transaction C reads data item y, the value of which is written by transaction B, as shown in TABLE 7. Accordingly, a directed edge 25 is added to transaction serialization graph 20 from node 23 to node 22, representing transactions C and B, respectively. Returning to TABLE 5, transaction A then locks data item z, reads the value thereof, and releases the lock on it. TABLE 8, representing a history file of data item z, shows that transaction C has written to data item z prior to transaction A's read step. Therefore, transaction A is dependent on transaction C. In transaction serialization graph 20, a third directed edge 26 is added from node 21 to node 23, showing this dependency. As graph 20 is searched for presence of cycles, it is determined that a path exists from transaction A to transaction C, to transaction B and back to transaction A. Therefore, transaction serialization graph 20 is a cyclic graph, where a cycle of dependencies exists. Since transactions A, B and C in the cycle of graph 20 depend on each other's results, the transactions are not serializable. In order to achieve serializable execution of all transactions in a transaction serialization graph, each transaction in the cycle and its dependent transactions must be aborted and rerun. Therefore, transactions A, B and C must be aborted and restarted, as shown in TABLE 5. In order to prevent the repeated formation of a cycle in a transaction serialization graph by the rerun transactions, the restarted transactions should employ a pessimistic locking strategy, such as the two-phase locking scheme described above. The employment of two-phase locking guarantees serializable transaction executions. In summary, it may be viewed that by using a transaction serialization graph, each transaction releases locks on data items as soon as each data item is accessed and no longer needed. Thus inherently, no transaction is required to wait a long period of time for a lock on a data item. Those transactions which are not serializable are detected and resolved by rerunning those transactions using more conservative locking protocols. The present locking scheme enables transactions to be executed in a pipeline, serializable fashion to improve throughput. In addition, no special handling procedures are required for hot spot problems in the present locking scheme. It is also important to note that the present invention permits the designation of each transaction as optimistic or pessimistic, where pessimistic transactions employ more conservative locking protocols. The use of path or tree protocols in structured data items is also permitted. Cooperative transactions are another class of transactions in which a group of cooperative transactions maps a consistent database state to a new consistent state. In other words, cooperative transactions may depend on each other's intermediate steps and are therefore not serializable. However, a group of cooperative transactions may be treated as one transaction, and the group of cooperative transactions are allowed to form admissible cycles in a transaction serialization graph and are not aborted. Preferred Embodiment Features Various important features of the preferred embodiment are summarized below. A locking scheme for shared database access control of at least two concurrent transactions, comprising the steps of: requesting a lock on a data item in the shared database, locking the data item in response to the data item being available, recording in a history file associated with the data item the locking transaction, constructing a serialization graph from the history file, accessing the data item, releasing the lock on the data item immediately after the data item accessing step, searching the serialization graph for the presence of the cycle, and committing the transaction in response to the absence of a cycle. This method further includes the step of aborting any transaction forming the cycle in response to the presence of a cycle. A method for scheduling at least two concurrent transactions accessing a shared data, including the steps of constructing a history file for the shared data showing each accessing transaction, constructing a serialization graph from the history file showing dependencies between the transactions, detecting a cycle in the serialization graph formed by the transactions, aborting the transactions in response to detecting a cycle and restarting the transactions in the detected cycle. Furthermore, the history file constructing step includes the step of recording the type of data access. The recording step includes recording a read or write access as executed by the transactions. The shared data is composed of a plurality of data items, and the history file constructing step includes forming a history file for each of the plurality of data items. The serialization graph constructing step includes formation of a node for each active transaction and a directed edge from a first node representing a first transaction to a second node representing a second transaction to show dependency of the first transaction on the second transaction. The step of searching for a cycle includes searching a simple path originating from a node and ending at the same node. The method may additionally include steps for employing a more conservative locking scheme for the restarted transactions, e.g. a two-phase locking scheme. The step of aborting aborts all transactions in the detected cycle and any transaction dependent on the transactions in the cycle. The method may additionally include steps of locking an unlocked data item and immediately unlocking the locked data item after accessing the locked data item. Such steps of locking an unlocked data item may further comprise steps of employing a more conservative locking scheme such as two-phase locking scheme. At least one of the concurrent transactions is a group of cooperative transactions.

Claims: A method for scheduling at least two concurrent transactions accessing a shared data, comprising the steps of: forming a history file associated with said shared data listing each accessing transaction sequentially; constructing from said history file a serialization graph showing dependencies between said transactions; searching for a cycle in said serialization graph; and detecting a cycle formed by said transactions in response to said searching step; characterised by aborting all of said transactions in said cycle and any transaction dependent therefrom. The method, as set forth in Claim 1, wherein said step of forming a history file includes the step of recording the type of data access. The method, as set forth in Claim 2, wherein said step of recording the type of data access includes the step of recording a read or a write as executed by said transactions. The method, as set forth in Claim 1, wherein said shared data is composed of a plurality of data items, said step of forming said history file includes the step of forming a history file for each of said plurality of data items. The method, as set forth in Claim 1, wherein said step of constructing a serialization graph includes the steps of: forming a node for each of said transactions; forming a directed edge from a first node to a second node to show dependency of said transaction represented by said first node on said transaction represented by said second node; and repeating said above two steps for each transaction. The method, as set forth in Claim 5, wherein said step of forming said nodes includes the step of forming a node only for active transactions. The method, as set forth in Claim 1, wherein the step of searching for a cycle includes the step of searching a simple path originating from a node and ending at the same node. The method, as set forth in Claim 1, further comprising the step of restarting said aborted transactions. The method, as set forth in Claim 8, wherein said restarting step includes the step of employing a more conservative locking scheme. The method, as set forth in Claim 9, wherein said step of employing a more conservative locking scheme includes the step of employing a two-phase locking scheme. The method, as set forth in Claim 1, further comprising the step of locking an unlocked data item; and immediately unlocking said locked data item after accessing said locked data item. The method, as set forth in Claim 11, further comprising the step of employing a more conservative locking scheme. The method, as set forth in Claim 12, where said employing step employs a two-phase locking scheme.

Inventors: WANG CHUNG C, WANG, CHUNG C.
Applicants: TEXAS INSTRUMENTS INC, TEXAS INSTRUMENTS INCORPORATED
Application year: 1991
Main classification: G06F17
IPC codes: G06F17