๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป CS/Database

[DB/2-1. Lock-based Protocols]

by kekeyo 2024. 3. 30.
728x90

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 ํ›„์—)

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
  • ๋ก ํ•ด์ œ๋Š” ์ œ์•ฝ ์—†์ด ๊ฐ€๋Šฅํ•˜์—ฌ ๋ก ํ•ด์ œํ•˜๊ณ  ๋‹ค์‹œ ๋ก์„ ์š”๊ตฌํ•  ์ˆ˜ ์žˆ์Œ
    • ๋‹ค๋งŒ, ํ•œ๋ฒˆ ๋ก์„ ํš๋“ํ•˜๊ณ  ํ•ด์ œํ•œ ๋ฐ์ดํ„ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ค์‹œ ๋ก์„ ์žก์„ ์ˆ˜ ์—†์Œ
  • ํŠธ๋ฆฌ ๊ธฐ๋ฐ˜ ๊ทœ์•ฝ์€ ํŠธ๋žœ์žญ์…˜์ด ๋ก์„ ํ•ด์ œํ•˜์˜€๋‹ค๊ฐ€๋„ ํ›„์— ์ƒˆ๋กœ์šด ๋ก์„ ํš๋“ํ•  ์ˆ˜ ์žˆ์Œ

728x90