locking and concurrency control

54
<Insert Picture Here> Locking and Concurrency Control Morgan Tocker, MySQL Community Manager

Upload: morgan-tocker

Post on 07-May-2015

978 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Locking and Concurrency Control

<Insert Picture Here>

Locking and Concurrency Control Morgan Tocker, MySQL Community Manager

Page 2: Locking and Concurrency Control

Safe Harbor Statement

The  following  is  intended  to  outline  our  general  product  direction.  It  is  intended  for  information  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a  commitment  to  deliver  any  material,  code,  or  functionality,  and  should  not  be  relied  upon  in  making  purchasing  decisions.  The  development,  release,  and  timing  of  any  features  or  functionality  described  for  Oracle’s  products  remains  at  the  sole  discretion  of  Oracle.

Page 3: Locking and Concurrency Control

MySQL 5.5MySQL Cluster 7.3

MySQL Enterprise Monitor 2.3 & 3.0MySQL Enterprise Backup Security Scalability HA Audit

MySQL 5.6MySQL Workbench 6.0

M y S Q L U t i l i t i e s

M y S Q L A p p l i e r f o r

H a d o o p

MySQL Workbench 5.2 & 6.0M y S Q L E n t e r p r i s e O r a c l e C e r t i f i c a t i o n s

4 Years of MySQL Innovation

M y S Q L C l u s t e r M a n a g e r

Windows installer & Tools

MySQL Cluster 7.2MySQL Cluster 7.1

MySQL Migration Wizard

MySQL 5.7

Page 4: Locking and Concurrency Control

Agenda

1.Table Locks, Row Locks and MVCC 2.Live Demo 3.Deadlocks and Lock Waits Discussion 4.ACID Discussion 5.Summary

Page 5: Locking and Concurrency Control

Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.

Table  Locks,  Row  Locks  and  MVCC

Page 6: Locking and Concurrency Control

Customer Accounting System

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

invoice_item

Page 7: Locking and Concurrency Control

Start Backup

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

invoice_item

Page 8: Locking and Concurrency Control

Continues…

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

invoice_item

Page 9: Locking and Concurrency Control

Concurrent Access System - New Customer Added

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc4 Savvy Squirrel Global

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

Page 10: Locking and Concurrency Control

Backup Continues

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc4 Savvy Squirrel Global

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

Page 11: Locking and Concurrency Control

Backup Continues…

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc4 Savvy Squirrel Global

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

Page 12: Locking and Concurrency Control

Backup Complete

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc4 Savvy Squirrel Global

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

Page 13: Locking and Concurrency Control

What was really backed up

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

Page 14: Locking and Concurrency Control

Next Operation

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc4 Acorn Intolerant Squirrels Inc

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

X X

Page 15: Locking and Concurrency Control

• Operation needs to represent a single point in time • When does not matter.

• This race condition can exist in all database operations - not just backups.

Solution

Page 16: Locking and Concurrency Control

MyISAM Implementation

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc4 Savvy Squirrel Global

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

Page 17: Locking and Concurrency Control

MyISAM Implementation

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc4 Savvy Squirrel Global

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

Page 18: Locking and Concurrency Control

MyISAM Implementation

customerid name

1 Acorn Enterprises2 First National Squirrel Bank3 Squirrels 5000 Inc4 Savvy Squirrel Global

invoiceid customerid ...

1 1 # Acorn Enterprises2 1 # Acorn Enterprises3 3 # Squirrels 5000 Inc4 4 # Savvy Squirrels

customer

invoice

itemid invoiceid Description Amount

1 1 1 hrs Professional Services

$10

2 2 Acorn Parts $12.50

3 2 5 hrs Professional Services

$50

4 3 Delivery Fee $17.50

5 3 Replacement Acorn

$20

6 4 Premium Acorn $999

invoice_item

Page 19: Locking and Concurrency Control

• Readers may co-exist. • Writers are forbidden. • Locks a table by default. • Programs like mysqldump responsibly lock all tables

they need access to before starting.

MyISAM

Page 20: Locking and Concurrency Control

InnoDB Implementation

Trx ID Rollback Ptr customerid name

123 ## 1 Acorn Enterprises123 ## 2 First National Squirrel Bank123 ## 3 Squirrels 5000 Inc401 ## 4 Savvy Squirrel

Trx ID Rollback Ptr invoiceid customerid ...

123 ## 1 1 # Acorn Enterprises123 ## 2 1 # Acorn Enterprises123 ## 3 3 # Squirrels 5000 Inc401 ## 4 4 # Savvy

customer

invoice

Trx ID Rollback Ptr itemid invoiceid Description Amount

123 ## 1 1 1 hrs Profess

$10

123 ## 2 2 Acorn Parts

$12.50

123 ## 3 2 5 hrs Profess

$50

123 ## 4 3 Delivery Fee

$17.50

123 ## 5 3 Replacement

$20

401 ## 6 4 Premium

$999

invoice_item

Page 21: Locking and Concurrency Control

• Hidden meta data attached to each row: • Row version • Rollback pointer to older versions if needed.

• Every transaction gets allocated a monotonic Transaction ID.

InnoDB

Page 22: Locking and Concurrency Control

InnoDB Implementation

Trx ID Rollback Ptr customerid name

123 ## 1 Acorn Enterprises123 ## 2 First National Squirrel Bank123 ## 3 Squirrels 5000 Inc401 ## 4 Savvy Squirrel

Trx ID Rollback Ptr invoiceid customerid ...

123 ## 1 1 # Acorn Enterprises123 ## 2 1 # Acorn Enterprises123 ## 3 3 # Squirrels 5000 Inc401 ## 4 4 # Savvy

customer

invoice

Trx ID Rollback Ptr itemid invoiceid Description Amount

123 ## 1 1 1 hrs Profess

$10

123 ## 2 2 Acorn Parts

$12.50

123 ## 3 2 5 hrs Profess

$50

123 ## 4 3 Delivery Fee

$17.50

123 ## 5 3 Replacement

$20

401 ## 6 4 Premium

$999

invoice_item

Backup process is TRX ID #394. Remembers it is not supposed to see anything newer than itself. New changes are permitted to continue.

Page 23: Locking and Concurrency Control

• Stands for “Multi-version concurrency control” - http://en.wikipedia.org/wiki/Multiversion_concurrency_control

• Readers don’t block writers! • And actually writers don’t block readers, because

they can read older versions.

MVCC Algorithm

Page 24: Locking and Concurrency Control

• Multiple versions of the same row may exist. • What you will see depends on the context of your

transaction. Even the number of rows in a table depends on your context.

MVCC (cont.)

Page 25: Locking and Concurrency Control

Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.

Live  Demo

Page 26: Locking and Concurrency Control

Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.

Deadlocks  &  Lock  Waits

Page 27: Locking and Concurrency Control

• A situation when two sessions are trying to acquire each-other’s respective locks.

• Neither can proceed until the other backs down. • It’s no different to when two colleagues get in an

irrational argument and neither will back down :)

What is a deadlock?

Page 28: Locking and Concurrency Control

• One step short of a deadlock. • You keep trying to do something, but it’s not in the

budget this quarter? • Not quite sure of the analogy here, but you are

free to retry. • But.. a fair amount of the time it still won’t work

What is a lock wait?

Page 29: Locking and Concurrency Control

• InnoDB as a storage engine will detect deadlocks. • Algorithm is Graph detection http://en.wikipedia.org/

wiki/Cycle_detection - happens virtually instantly. • Kills the transaction that modified the least rows. • All changes are rolled back - application developer

must now handle/retry.

Deadlock Detection

Page 30: Locking and Concurrency Control

• Deadlock is a concurrency issue. • Same solutions as when you have colleagues

arguing!

Preventing Deadlocks

Page 31: Locking and Concurrency Control

• Don’t make them work-together. • Reduce concurrency / run known conflicting workers

in serial execution.

Solution #1

X

Page 32: Locking and Concurrency Control

• Don’t make them work on big projects together. • aka lower the stakes! • Locks are held for the duration of a transaction:

• Put people who don’t work together only on small projects as needed (smaller transactions).

• Reduce locking required for transactions (more efficient queries, better indexing).

Solution #2

Page 33: Locking and Concurrency Control

• Also, we should accept that some deadlocks are a fact of life. Continuing co-worker analogy: • Essential for business growth that people work

together. • Have to be prepared to handle disputes. !

• MyISAM as a storage engine does not actually need deadlock detection (table locking). • Where does this leave concurrency?

Preventing Deadlocks (cont.)

Page 34: Locking and Concurrency Control

• You actually want databases to be optimistic. • That is set as few locks as possible, knowing that

sometimes oopses will happen. • Alternative is more locks and less concurrency.

Preventing Deadlocks (cont.)

Page 35: Locking and Concurrency Control

• Database only owns responsibility of alerting application

• It may not reapply transaction, since state has changed.

• Application owns responsibility of handling errors. • Yet maybe 5% of applications do this :(

Handing Deadlocks

Page 36: Locking and Concurrency Control

• Will proceed immediately if it can acquire the lock. • Since most transactions are less than 1 second, at

50 seconds seems unlikely retrying will work. • May need to voluntarily rollback and try again or give

a user error.

Lock Wait Timeout

Page 37: Locking and Concurrency Control

• The problems probably won’t happen often. • May be easier to kick back to the user: • “try again - no changes have been applied”. • Exception may be asynchronous workers?

• Usually you’ve made the promise of work without opportunity to back-out.

“Requires a replay of the transaction”

Page 38: Locking and Concurrency Control

Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.

ACID

Page 39: Locking and Concurrency Control

• Atomicity - All or none. • Consistency - Generic term to say all constraints etc

must be honoured (primary key, unique key). • Isolation - Changes in one transaction to not

interfere with another transaction. • Durability - When you said that the atomic

transaction was committed, you will honour it. Even if the server were to crash.

The “ACID” Contract

Page 40: Locking and Concurrency Control

• Most people know this one: • Transfer $20 from Account A --> Account B.

• Multiple failure states... • The money may be subtracted from A, but has

not reached B yet. • Wrap your code between START TRANSACTION

and COMMIT and you get this solved for free.

Atomicity

Page 41: Locking and Concurrency Control

• Not too much more to add on this one. • InnoDB has foreign key constraints • MySQL does not feature CHECK constraints where

another database may allow more domain specific data validation here as well.

Consistency

Page 42: Locking and Concurrency Control

• Actually a very expensive feature for a database to implement (may require lots of locking, stale data kept around etc.)

• Many databases make this somewhat tunable. • There are four levels of isolation describing what

potential unsafe conditions are possible.

Isolation

Page 43: Locking and Concurrency Control

• READ-UNCOMMITTED - No isolation • READ-COMMITTED - Can see other session’s

committed rows. • REPEATABLE-READ - Won’t see other session’s

committed rows once your session starts (default). • SERIALIZABLE - Reads actually lock. Can’t modify

any data another session has changed.

Isolation Levels

Page 44: Locking and Concurrency Control

• This one’s actually tunable as well. • InnoDB will not lie by default.

• It means on COMMIT everything should survive a power loss not be stuck in a buffer in memory.

Durable

Page 45: Locking and Concurrency Control

Copyright  ©  2012  Oracle  and/or  its  affiliates.  All  rights  reserved.

Summary

Page 46: Locking and Concurrency Control

• Wrap every logical unit of work in START TRANSACTION and COMMIT.

• Expect failure because failure is a database allowing concurrency.

• LOCK WAIT TIMEOUT = Retry “may” be possible. • DEADLOCK = Everything must be retried.

Application Responsibilities

Page 47: Locking and Concurrency Control
Page 48: Locking and Concurrency Control

<Insert Picture Here>

Page 49: Locking and Concurrency Control

• Common names given to database “internal locking”. • Today we are not going to differentiate between

them. • Not unique to databases, required to provide “thread

safety” in multi-threaded programs.

Mutexes, Latches & Semaphores

Page 50: Locking and Concurrency Control

• InnoDB buffer pool is full • Free space needs to be created in order to be able to

load your desired page into memory. • In between freeing and loading, someone else fills

desired slot.

Example #1

Page 51: Locking and Concurrency Control

• When a page becomes too full, it may need to split data into other pages.

• It is possible that there could be a thread deadlock if two pages left and right of each-other reach this condition at once.

• Can’t decide who should proceed first.

Example #2

Page 52: Locking and Concurrency Control

• When mutexes have high contention other tasks are not runable.

• Which means that CPUs can appear idle, but nothing is happening.

Internal Locking (cont.)

Page 53: Locking and Concurrency Control

• Responsibility to fix is on MySQL Engineering Team. • High Priority for Us.

• Some DBAs will have peripheral knowledge - know situations when it’s time to upgrade.

Fixes in newer versions

Page 54: Locking and Concurrency Control

• Kernel mutex split (5.6) • Lock open mutex split (5.6) • Buffer pool mutex contention reduced w/8 buffer

pool instances (5.6) • Multiple rollback segments (5.5) • Log-sys mutex split (5.5) • Flush-list mutex split (5.5)

Time to Upgrade?

More mentioned at: http://www.tocker.ca/2013/11/27/what-is-a-mutex-anyway.html