Seven (+-2) Sins of Concurrency Chen Shapira In which I will show - - PowerPoint PPT Presentation

seven 2 sins of concurrency
SMART_READER_LITE
LIVE PREVIEW

Seven (+-2) Sins of Concurrency Chen Shapira In which I will show - - PowerPoint PPT Presentation

Seven (+-2) Sins of Concurrency Chen Shapira In which I will show classical concurrency problems and some techniques of detecting and avoiding them I have a B.Sc. in CS and Statistics, OCP, 10 years of production IT experience and Im an


slide-1
SLIDE 1

Seven (+-2) Sins of Concurrency

Chen Shapira

slide-2
SLIDE 2

In which I will show classical concurrency problems and some techniques of detecting and avoiding them

slide-3
SLIDE 3

I have a B.Sc. in CS and Statistics, OCP, 10 years of production IT experience and I’m an Oracle

  • Ace. So I know what I’m talking

about.

But you don’t have to trust me – I have scripts that prove everything I say.

slide-4
SLIDE 4

Since 1967 computers can walk and chew gum at the same time

slide-5
SLIDE 5

Programs need to learn to share

slide-6
SLIDE 6

Example: Shared Bank Account

slide-7
SLIDE 7

create or replace procedure update_account(p_id number,p_amount number) as n number; begin SELECT amount into n FROM bank_account WHERE id=p_id; UPDATE bank_account SET amount = n+p_amount; end;

slide-8
SLIDE 8

SQL> exec deposit(1,500) SQL> commit; SQL> exec withdraw(1,-500) SQL> commit;

SQL> select amount from bank_account; AMOUNT

  • 500
slide-9
SLIDE 9

Race Condition

Sin #1

slide-10
SLIDE 10

Can your code share?

Are you 100% sure?

slide-11
SLIDE 11

Does this look familiar?

spool XXX_drop_db_links.sql select 'drop database link '||OBJECT_NAME||';' from obj where OBJECT_TYPE='DATABASE LINK'; spool off @XXX_drop_db_links.sql

slide-12
SLIDE 12

Ostrich Algorithm

Sin #2

slide-13
SLIDE 13

Few words about critical sections and mutual exclusion

slide-14
SLIDE 14

Laws of Good Concurrency

  • No two processes will be in their critical

section at same time

  • No assumptions about number or speed of

CPUs

  • No process outside the critical section may

block other processes

  • No process will wait forever to enter critical

section

slide-15
SLIDE 15

Mutual Exclusion in Oracle

Locks and latches and mutexes,

  • h my!
slide-16
SLIDE 16

User Defined Locks

dbms_lock.allocate_unique( lockname => 'Synchronize', lockhandle => m_handle ); n1 := dbms_lock.request( lockhandle => m_handle, lockmode => dbms_lock.x_mode, timeout => dbms_lock.maxwait, release_on_commit => true );

slide-17
SLIDE 17

dbms_lock.allocate_unique('Synchronize',m_handle); dbms_lock.request(m_handle,dbms_lock.x_mode, dbms_lock.maxwait,false); spool XXX_drop_db_links.sql select 'drop database link '||OBJECT_NAME||';' from obj where OBJECT_TYPE='DATABASE LINK'; spool off @XXX_drop_db_links.sql dbms_lock.release(m_handle);

slide-18
SLIDE 18

Another Race

select max(id) into max_id from my_table; insert into my_table values (max_id+1,some_data); commit;

slide-19
SLIDE 19

Protecting the critical section - I

select max(id) into max_id from my_table for update; insert into my_table values (max_id+1,some_data); commit; ERROR at line 1: ORA-01786: FOR UPDATE of this query expression is not allowed

slide-20
SLIDE 20

Protecting the critical section - II

select id into max_id from my_table where id=(select max(id) from my_table) for update; insert into my_table values (max_id+1,some_data); commit;

slide-21
SLIDE 21

Protecting the critical section - III

select max_id into p_max_id from extra_table for update; insert into my_table values (max_id+1,some_data); update extra_table set max_id=max_id+1; commit;

slide-22
SLIDE 22

Solving the race condition led to serialization

Sin #3

slide-23
SLIDE 23

The right way to do it:

create sequence my_table_seq start with 1 increment by 1 cache 20; insert into my_table (my_table_seq.nextval,some_data); commit;

slide-24
SLIDE 24

Quick Review

slide-25
SLIDE 25

"Insanity: Doing the same thing over and over again and expecting different results."

Albert Einstein.

slide-26
SLIDE 26

Classical Concurrency Problems

slide-27
SLIDE 27

Dining Philosophers

slide-28
SLIDE 28
  • - number of philosophers

select count(*) into N from sticks; think(); update sticks set owner=philosopher_id where s_id=p_id; -- take right fork update sticks set owner=philosopher_id where s_id=mod(p_id+1,N); -- take left fork eat(); -- nom nom nom commit; -- put down forks

slide-29
SLIDE 29

ORA-00060: Deadlock detected

Sin #4

slide-30
SLIDE 30

think(); update sticks set owner=in_p_id where s_id=in_p_id; select s_id into r_s from sticks where s_id=mod(in_p_id+1,N) for update nowait; update sticks set owner=in_p_id where s_id=mod(in_p_id+1,N); eat(); commit; exception when resource_busy then rollback;

slide-31
SLIDE 31

Starvation

Sin #5

0.2 0.4 0.6 0.8 1 1.2 1.4 1 2 3 4 5 Eat 2 Eat 3

slide-32
SLIDE 32

think(); update sticks set owner=in_p_id where s_id=least(in_p_id,mod(in_p_id+1,N)); update sticks set owner=in_p_id where s_id=greatest(in_p_id,mod(in_p_id+1,N)); eat(); commit;

slide-33
SLIDE 33

Partial Hierarchy Solution

1 5 4 3 2

slide-34
SLIDE 34

Or just index your foreign keys!

slide-35
SLIDE 35

Quick Review

slide-36
SLIDE 36

Barbershop Queue

slide-37
SLIDE 37

Generating customers

update customers set needs_cut=1,entered_shop=systimestamp where id in ( select id from (select id from customers where needs_cut=0

  • rder by dbms_random.random)

where rownum<=(dbms_random.value*(p_avg_customers_pe r_sec*2+1))); commit; dbms_lock.sleep(1);

slide-38
SLIDE 38

Each barber does:

cursor c is select * from customers where needs_cut=1 order by entered_shop for update skip locked; … fetch c into l_rec; exit when c%NOTFOUND; cut_hair(dbms_random.value*p_avg_cut_time*2); finish_work(l_rec.id);

slide-39
SLIDE 39

3 Barbers, Haircut in 0.3 seconds

Sin #6

5 10 15 20 25 2 4 6 8 10 12 14 16 Response Time Number of Customers AVG_WAIT MAX_WAIT

slide-40
SLIDE 40

About 10 concurrent customers

Sin #7

0.5 1 1.5 2 2.5 3 3.5 1 2 3 4 5 6 7 8 9 Response Time Number of Barbers AVG_WAIT MAX_WAIT

slide-41
SLIDE 41

Quick Review

slide-42
SLIDE 42

Oracle Concurrency Problems

Because consistency has a price

slide-43
SLIDE 43

Non-transactional changes

Sin #8

slide-44
SLIDE 44

00:01 Session 1 started update where X=? row 250,000 00:00 Session 2 updates column X 00:05 commit row 1 00:05 Session 1 sees newer data in column X 00:05.1 Session I starts again

slide-45
SLIDE 45

Forgetting the extra IO

Sin #9

slide-46
SLIDE 46