Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will...
Transcript of Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will...
![Page 1: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/1.jpg)
Seven (+-2) Sins of Concurrency
Chen Shapira
![Page 2: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/2.jpg)
In which I will show classical
concurrency problems and some
techniques of detecting and
avoiding them
![Page 3: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/3.jpg)
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.
![Page 4: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/4.jpg)
Since 1967
computers
can walk and
chew gum at
the same
time
![Page 5: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/5.jpg)
Programs need to learn to share
![Page 6: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/6.jpg)
Example: Shared Bank Account
![Page 7: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/7.jpg)
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
WHERE id=p_id;
end;
![Page 8: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/8.jpg)
SQL> exec deposit(1,500)
SQL> commit;
SQL> exec withdraw(1,-500)
SQL> commit;
SQL> select amount from bank_account;
AMOUNT
----------
-500
![Page 9: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/9.jpg)
Race Condition
Sin #1
![Page 10: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/10.jpg)
Can your code share?
Are you 100% sure?
![Page 11: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/11.jpg)
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
![Page 12: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/12.jpg)
Ostrich Algorithm
Sin #2
![Page 13: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/13.jpg)
Few words about critical sections
and mutual exclusion
![Page 14: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/14.jpg)
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
![Page 15: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/15.jpg)
Mutual Exclusion in Oracle
Locks and latches and mutexes,
oh my!
![Page 16: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/16.jpg)
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
);
![Page 17: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/17.jpg)
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);
![Page 18: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/18.jpg)
Another Race
select max(id) into max_id from my_table;
insert into my_table values (max_id+1,some_data);
commit;
![Page 19: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/19.jpg)
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
![Page 20: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/20.jpg)
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;
![Page 21: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/21.jpg)
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;
![Page 22: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/22.jpg)
Solving the race condition led to
serialization
Sin #3
![Page 23: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/23.jpg)
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;
![Page 24: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/24.jpg)
Quick Review
![Page 25: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/25.jpg)
"Insanity: Doing the same thing over
and over again and expecting
different results."
Albert Einstein.
![Page 26: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/26.jpg)
Classical Concurrency Problems
![Page 27: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/27.jpg)
Dining Philosophers
![Page 28: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/28.jpg)
-- 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
![Page 29: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/29.jpg)
ORA-00060: Deadlock detected
Sin #4
![Page 30: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/30.jpg)
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;
![Page 31: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/31.jpg)
Starvation
Sin #5
![Page 32: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/32.jpg)
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;
![Page 33: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/33.jpg)
Partial Hierarchy Solution
04
3 2
1
![Page 34: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/34.jpg)
Or just index your foreign keys!
![Page 35: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/35.jpg)
Quick Review
![Page 36: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/36.jpg)
Barbershop Queue
![Page 37: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/37.jpg)
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
order by dbms_random.random)
where
rownum<=(dbms_random.value*(p_avg_customers_pe
r_sec*2+1)));
commit;
dbms_lock.sleep(1);
![Page 38: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/38.jpg)
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);
![Page 39: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/39.jpg)
3 Barbers, Haircut in 0.3 seconds
Sin #6
![Page 40: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/40.jpg)
About 3 concurrent customers
Sin #7
![Page 41: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/41.jpg)
Quick Review
![Page 42: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/42.jpg)
Oracle Concurrency Problems
Because consistency has a price
![Page 43: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/43.jpg)
Non-transactional changes
Sin #8
![Page 44: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/44.jpg)
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
![Page 45: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/45.jpg)
Forgetting the extra IO
Sin #9
![Page 46: Seven (+-2) Sins of Concurrency · Seven (+-2) Sins of Concurrency Chen Shapira. In which I will show classical concurrency problems and some techniques of detecting and avoiding](https://reader035.fdocuments.in/reader035/viewer/2022071021/5fd4f8ace853e246ff0dfc72/html5/thumbnails/46.jpg)