1๏ธโฃ Lock-based Protocols
Lock-based Protocols
locking protocol : set of rules followed by all transactions while requesting and releasing locks
→ ๋์์ฑ ์ ์ด๋ฅผ ์ํด ๊ฐ๋ฐ, ํธ๋์ญ์ ์ค์ผ์ค์ ์ ์ดํ์ฌ ์ํ๋ ํธ๋์ญ์ ์ค์ผ์ค๋ง์ ์์ฑํ๋๋ก ํจ
[ Lock modes ]
- exclusive (X) mode : Read or Write ⇒ lock-X()
- shared (S) mode : Read only ⇒ lock-S()
[ Lock ํธํ์ฑ ]
| S | X | |
| S | true | false |
| X | false | false |
→ lock-S()๋ผ๋ฆฌ๋ง ํธํ ๊ฐ๋ฅ ์ด์ธ์ ๊ฒ๋ค์ conflict ๋ฐ์ํจ
โlock-X()๊ฐ ํธํ๋์ง ์๋ ์ด์ ?

→ lock-X๊ฐ ํธํ๋๋ค๋ฉด write ๊ณผ์ ์์ ๋ค๋ฅธ ๋๋ฏธ ๊ฐ ๋ค์ด๊ฐ ์๋ ์๊ธฐ ๋๋ฌธ์?
[ Lock-based Protocols์ ํน์ง ]
- transactions can proceed only after request is granted
- A transaction may be granted a lock on an item if the requested lock is compatible with locks already held on the item by other transactions : compatibleํ ๊ด๊ณ์์๋ง lock ํ๋ณด ๊ฐ๋ฅ
- any numbers of transactions can hold shared locks on an item
- If any transaction holds an exclusive on the item, no other transaction may hold any lock on the item ⇒ lock-X๋ ํธํ ๋ถ๊ฐ
- If a lock cannot be granted, the requesting transaction is made to wait till all incompatible locks held by other transactions have been released
- The lock is then granted
- ๋ชจ๋ ์ฝ๊ธฐ/์ฐ๊ธฐ ์ฐ์ฐ์ ๋ํด ๋ก ๋ณด์ ํด์ผ ํจ.
- ํด๋น ๋ฐ์ดํฐ์ ๋ํ ๋ก์ ๋ฐ์ง ๋ชปํ์ฌ ๊ธฐ๋ค๋ฆฌ๋ ํธ๋์ญ์ ์ ๋ค๋ฅธ ํธ๋์ญ์ ์ ๋ถ์ฌ๋ ์ถฉ๋ ๋ก์ด ํด์ ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๊ณ , ํ์ ํธ๋์ญ์ ์ด ํด์ ๋๋ฉด ๋ฐ์ดํฐ์ ๋ํ ๋ก์ ๋ฐ๊ณ ํด๋น ์ฐ์ฐ์ ์ํํจ.
[ example ]
lock-S(A)
read(A)
unlock(A)
lock-S(B)
read(B)
unlock(B)
display(A+B)
⇒ ์ด๋ฌํ ํธ๋์ญ์ ์ serializability ๋ณด์ฅ X (read(A), read(B), display(A+B) ์ฌ์ด์ ๊ฐ์ด ๋ฐ๋ ์ ์๊ธฐ ๋๋ฌธ)
Two-phase Locking Protocol (=2PL)
: 2๋จ๊ณ locking ๊ท์ฝ
[ 2PL ๊ณผ์ ]
1๋จ๊ณ : growing phase (→ lock ๊ฑฐ๋ ๊ฒ๋ง)
- Transaction may obtain locks
- Transaction may not release locks
2๋จ๊ณ : shrinking phase (→ lock ํธ๋ ๊ฒ๋ง)
- Transaction may release locks
- Transaction may not obtain locks
[ 2PL ํน์ง ]
- serializability ๋ณด์ฅ ( Ensures to produce conflict serializable schedule)
- It can be proved that the transactions can be serialized in the order of their lock points
- 2PL does not ensure freedom from deadlocks
- Cascading rollback is possible under 2PL
- ๋ฐฉ์งํ๊ธฐ ์ํด? “strict 2PL” or “Rigorous 2PL” ์ฌ์ฉ
- strict 2PL
- : a transaction must hold all its exclusive locks till its commit/aborts (⇒ lock-X()๋ฅผ unlockํ๋ ์์ ? commit/abort ํ์)
- rigorous 2PL : all locks are held till commit / abort (๋ชจ๋ lock์ ํธ๋ ์์ ? commit/abort ํ์)
- ๋ฐฉ์งํ๊ธฐ ์ํด? “strict 2PL” or “Rigorous 2PL” ์ฌ์ฉ
Lock Conversions
: 2PL์ ํ์ฅ (2PL with lock conversions)

First phase : upgrade(S→X)
- Can acquire a lock-S on item
- Can acquire a lock-X on item
- Can convert a lock-S to a lock-X (upgrade)
Second phase: downgrade (X→S)
- Can release a lock-S
- Can release a lock-X
- Can convert a lock-X to a lock-S (downgrade)
⇒ serializability ๋ณด์ฅ
Why 2PL ensures conflict serializability
“์ 2PL์ด conflict serializability๋ฅผ ๋ณด์ฅํ๋๊ฐ”์ ๋ํ ์ฆ๋ช
โ 2PL์ด serializability๋ฅผ ๋ณด์ฅํ์ง ์๋๋ค๊ณ ๊ฐ์ → precedence graph ์ cycle ์กด์ฌ
โก Cycle์ด T0 → T1 → … → T1 → T0 ์ด๋ผ๊ณ ๊ฐ์ (ษi๊ฐ Ti๋ผ๋ ํธ๋์ญ์ ์ด last lock์ ์ป๋ ์์ ์ด๋ผ๊ณ ๋ณด์)
Ti → Tj๋ผ๋ ํธ๋์ญ์ ์ ๋ํด ษi < ษj๊ฐ ์ฑ๋ฆฝํจ.

โข ๋ฐ๋ผ์ T0 → T1 → … T1 → T0์ ๋ํด์ ษ0 < ษ1 < … < ษ1 < ษ2 ์ฑ์ง์ ๊ฐ์ง
โฃ ษ0 < ษ0์ ๋ชจ์์ ์ด๊ธฐ์, ์๊ธฐ cycle์ ์กด์ฌ
ํ ์ ์์.
∴ 2PL์ serializability ๋ณด์ฅํจ
Automatic Acquisition of Locks
A transaction Ti issues the standard read/write instruction without explicit locking calls
-> lock์ ๋ํ ํ๋/์ฒ ํ๋ ์ฌ์ฉ์์ ์๊ตฌ์ ์ํด ์ด์๋๋ ๊ฒ์ด ์๋๋ผ ์์คํ ๋ด๋ถ์์ ์๋์ผ๋ก ์ด์๋จ
ex. read()์ ๋ํ lock ํ๋ ๊ณผ์

ex. write()์ ๋ํ lock ํ๋ ๊ณผ์

- write lock์ ํธํ์ฑ์ด ์์ผ๋ฏ๋ก ๋ฐ๋์ ๋ฐฐํ์ ์ผ๋ก lock ํ๋ํด์ผ ํ๋ค.
- ํธ๋์ญ์ ์ด ์ฐ๊ธฐ ๋ก์ ํ๋ํ๊ณ ์ ํ ๋ ํํธ๋์ญ์ ์ด ๋์ผ ๋ฐ์ดํฐ์ ๋ํ์ฌ ๋ก์ ๋ณด์ ํ๊ณ ์๊ฑฐ๋ ๋๋ ํํธ๋์ญ์ ์ด ๋ก์ ๊ธฐ๋ค๋ฆฌ๊ณ ์์ผ๋ฉด, ๋ก ํ๋์ ์๊ตฌํ๋ ํธ๋์ญ์ ์ ๊ธฐ๋ค๋ ค์ผ ํ๋ค. (waiting queue์์ ๊ธฐ๋ค๋ฆผ)
Implementation of Locking
- lock manager๋ ํธ๋์ญ์ ์ด ์ ๊ธ ๋ฐ ์ ๊ธ ํด์ ์์ฒญ์ ๋ณด๋ด๋ ๋ณ๋์ ํ๋ก์ธ์ค๋ก ๊ตฌํ๋ ์ ์์
- The lock manager๋ lock grant message๋ฅผ ๋ณด๋ด๋ฉฐ lock ์์ฒญ์ ๋ํด ์๋ต
- ์์ฒญํ ํธ๋์ญ์ ์ ์๋ต๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ
- lock manager์ granted lock๊ณผ pending request๋ฅผ ๊ธฐ๋กํ๊ธฐ ์ํด lock table์ด๋ผ๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๊ณ ์์ (→ in-memory hash table)
-
- A lock manager can be implemented as a separate process to which transactions send lock and unlock requests
- The lock manager replies to a lock request by sending a lock grant messages (or a message asking the transaction to roll back in case of a deadlock)
- The requesting transaction waits until its request is answered
- The lock manager maintains a data structure called a lock table to record granted locks and pending requests
- The lock table is usually implemented as an in-memory hash table indexed on the name of the data item being locked
-
Lock Table

- - ๊ฒ์์ : lock ํ ๋น, ํฐ์ : ์์ฒญ ๋๊ธฐ ์ค
ex. ์ฒซ๋ฒ์งธ ์นธ์ hash[0]์ → hash๊ฐ์ด 0์ธ ๋ชจ๋ ํธ๋์ญ์ ์ lock ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์์.
I7์ด๋ผ๋ data์ ๋ํด T23 Transaction์ด lock ์ ๋ณด๋ฅผ ๊ฐ๊ณ ์์
I23 data๋ T1, T8์ lock-S๊ฐ ํ์ฉ๋์ด ์๊ณ , ์๋ต์ ๊ธฐ๋ค๋ฆฌ๋ T2๊ฐ ์์. (T2๊ฐ ์๊ตฌํ๋ ๊ฒ์ lock-X์ฌ์ผ ํจ)
Deadlock

- T3, T4 ๋ ๋ค ์คํ๋์ง ์์
- : executing lock-S(B) causes T4 to wait for T3 to release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its lock on A
⇒ ํด๊ฒฐ์ฑ
: T3 or T4 ์ค ํ๋ ์ ํํด์ Roll Back ์ํด (victim_ํฌ์์)
lock์ ์ฌ์ฉํ๋ concurrency control์์๋ deadlock ๋ฐ์ ๊ฐ๋ฅ
2๊ฐ ์ด์์ ํธ๋์ญ์ ์ด ๋ฌดํ์ ๊ธฐ๋ค๋ฆฌ๋ ์ํ → deadlock
Starvation
Starvation is also possible if concurrency control manager is badly designed (๊ธฐ์ ์ํ)
(ex. ์์ ์ ์ ํ victim๋ง ๊ณ์ ์ ์ ๋์ด roll backํ๊ฒ ๋๋ฉด → starvartion ๋ฐ์)
⇒ deadlock์ ๋ฐฉ์งํ ์ ์์ง๋ง starvation์ ๋ฐฉ์งํด์ผ ํจ!
Graph-based Protocol
2PL์ ๋์? graph-based Protocol
- ํ๋ฐฉํฅ์ผ๋ก ์ ๊ทผํ๋ฉด cycle์ด ์๊ธฐ์ง ์๋๋ค๋ ๊ฒ ์ด์ฉ
- ex. tree-based protocol
[ Graph-based Protocol์ ํน์ง ]
- conflict serializability & freedom from deadlock ๋ณด์ฅ
- 2PL๊ณผ ๋น๊ตํด์
- ์งง์ ๋๊ธฐ ์๊ฐ (๋ฐ๋ก unlockํ ์ ์๊ธฐ ๋๋ฌธ)
- ๋์์ฑ ์ฆ๊ฐ
- deadlock free (→ no rollbacks are required)
Tree-based Protocol
- Only exclusive locks are allowed
- The first lock by Ti may be on any data item
- Subsequently, a data Q can be locked by Ti only if the parent of Q is currently locked by Ti
- Data items may be unlocked at any time
- A data item that has been locked and unlocked by Ti cannot subsequently be relocked by Ti
- ๋ก ํด์ ๋ ์ ์ฝ ์์ด ๊ฐ๋ฅํ์ฌ ๋ก ํด์ ํ๊ณ ๋ค์ ๋ก์ ์๊ตฌํ ์ ์์
- ๋ค๋ง, ํ๋ฒ ๋ก์ ํ๋ํ๊ณ ํด์ ํ ๋ฐ์ดํฐ ํญ๋ชฉ์ ๋ํด์๋ ๋ค์ ๋ก์ ์ก์ ์ ์์
- ํธ๋ฆฌ ๊ธฐ๋ฐ ๊ท์ฝ์ ํธ๋์ญ์ ์ด ๋ก์ ํด์ ํ์๋ค๊ฐ๋ ํ์ ์๋ก์ด ๋ก์ ํ๋ํ ์ ์์

'๐ฉ๐ปโ๐ป CS > Database' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [DB/1-4. Recoverability(ํ๋ณต ๊ฐ๋ฅ)] (0) | 2024.03.27 |
|---|---|
| [DB/1-3.ย How to Test Serializability] (0) | 2024.03.27 |
| [DB/1-2.Serializability(์ง๋ ฌ์ฑ)] (1) | 2024.03.24 |
| [DB/1-1. Transaction(ํธ๋์ญ์ )] (0) | 2024.03.18 |