seven 2 sins of concurrency
play

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


  1. Seven (+-2) Sins of Concurrency Chen Shapira

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

  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.

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

  5. Programs need to learn to share

  6. Example: Shared Bank Account

  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;

  8. SQL> exec deposit(1,500) SQL> exec withdraw(1,-500) SQL> commit; SQL> commit; SQL> select amount from bank_account; AMOUNT ---------- -500

  9. Sin #1 Race Condition

  10. Can your code share? Are you 100% sure?

  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

  12. Sin #2 Ostrich Algorithm

  13. Few words about critical sections and mutual exclusion

  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

  15. Mutual Exclusion in Oracle Locks and latches and mutexes, oh my!

  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 );

  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);

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

  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

  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;

  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;

  22. Sin #3 Solving the race condition led to serialization

  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;

  24. Quick Review

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

  26. Classical Concurrency Problems

  27. Dining Philosophers

  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

  29. ORA-00060: Deadlock detected Sin #4

  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;

  31. Starvation 1 1.4 1.2 1 0.8 0.6 5 2 0.4 0.2 Eat 2 0 Eat 3 4 3 Sin #5

  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;

  33. Partial Hierarchy Solution 1 5 2 3 4

  34. Or just index your foreign keys!

  35. Quick Review

  36. Barbershop Queue

  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 order by dbms_random.random) where rownum<=(dbms_random.value*(p_avg_customers_pe r_sec*2+1))); commit; dbms_lock.sleep(1);

  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);

  39. 3 Barbers, Haircut in 0.3 seconds 25 20 Response Time 15 10 5 0 0 2 4 6 8 10 12 14 16 Number of Customers AVG_WAIT MAX_WAIT Sin #6

  40. About 10 concurrent customers 3.5 3 2.5 Response Time 2 1.5 1 0.5 0 0 1 2 3 4 5 6 7 8 9 Number of Barbers AVG_WAIT MAX_WAIT Sin #7

  41. Quick Review

  42. Oracle Concurrency Problems Because consistency has a price

  43. Non-transactional changes Sin #8

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

  45. Forgetting the extra IO Sin #9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend