Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.

Slides:



Advertisements
Similar presentations
Optimistic Methods for Concurrency Control By : H.T. Kung & John T. Robinson Presenters: Munawer Saeed.
Advertisements

Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
TRANSACTION PROCESSING SYSTEM ROHIT KHOKHER. TRANSACTION RECOVERY TRANSACTION RECOVERY TRANSACTION STATES SERIALIZABILITY CONFLICT SERIALIZABILITY VIEW.
Deadlock and Starvation
CS492B Analysis of Concurrent Programs Lock Basics Jaehyuk Huh Computer Science, KAIST.
CS6223: Distributed Systems
CSC271 Database Systems Lecture # 32.
Lock-Based Concurrency Control
Concurrency 101 Shared state. Part 1: General Concepts 2.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
1 Chapter 3. Synchronization. STEMPusan National University STEM-PNU 2 Synchronization in Distributed Systems Synchronization in a single machine Same.
1 CSIS 7102 Spring 2004 Lecture 8: Recovery (overview) Dr. King-Ip Lin.
CMPT Dr. Alexandra Fedorova Lecture X: Transactions.
Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
CS 582 / CMPE 481 Distributed Systems
Synchronization. Physical Clocks Solar Physical Clocks Cesium Clocks International Atomic Time Universal Coordinate Time (UTC) Clock Synchronization Algorithms.
Non-blocking Atomic Commitment Aaron Kaminsky Presenting Chapter 6 of Distributed Systems, 2nd edition, 1993, ed. Mullender.
Transaction Management and Concurrency Control
CS510 Concurrent Systems Class 2 A Lock-Free Multiprocessor OS Kernel.
Transaction Management
CS510 Concurrent Systems Class 13 Software Transactional Memory Should Not be Obstruction-Free.
Language Support for Lightweight transactions Tim Harris & Keir Fraser Presented by Narayanan Sundaram 04/28/2008.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Software Transaction Memory for Dynamic-Sized Data Structures presented by: Mark Schall.
Transactions and concurrency control
Transactions and Reliability. File system components Disk management Naming Reliability  What are the reliability issues in file systems? Security.
Distributed Deadlocks and Transaction Recovery.
Software Transactional Memory for Dynamic-Sized Data Structures Maurice Herlihy, Victor Luchangco, Mark Moir, William Scherer Presented by: Gokul Soundararajan.
CS510 Concurrent Systems Jonathan Walpole. A Lock-Free Multiprocessor OS Kernel.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
CS5204 – Operating Systems Transactional Memory Part 2: Software-Based Approaches.
Lecture 12 Recoverability and failure. 2 Optimistic Techniques Based on assumption that conflict is rare and more efficient to let transactions proceed.
Concurrency Server accesses data on behalf of client – series of operations is a transaction – transactions are atomic Several clients may invoke transactions.
Games Development 2 Concurrent Programming CO3301 Week 9.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
The Relational Model1 Transaction Processing Units of Work.
Transactions and Concurrency Control. Concurrent Accesses to an Object Multiple threads Atomic operations Thread communication Fairness.
Concurrency Chapter 6.2 V3.1 Napier University Dr Gordon Russell.
Database Systems Recovery & Concurrency Lecture # 20 1 st April, 2011.
SPL/2010 Guarded Methods and Waiting 1. SPL/2010 Reminder! ● Concurrency problem: asynchronous modifications to object states lead to failure of thread.
CSC 240 (Blum)1 Database Transactions. CSC 240 (Blum)2 Transaction  A transaction is an interaction between a user (or application) and a database. A.
Transaction Management Transparencies. ©Pearson Education 2009 Chapter 14 - Objectives Function and importance of transactions. Properties of transactions.
AtomCaml: First-class Atomicity via Rollback Michael F. Ringenburg and Dan Grossman University of Washington International Conference on Functional Programming.
NOEA/IT - FEN: Databases/Transactions1 Transactions ACID Concurrency Control.
MULTIVIE W Slide 1 (of 21) Software Transactional Memory Should Not Be Obstruction Free Paper: Robert Ennals Presenter: Emerson Murphy-Hill.
Optimistic Design CDP 1. Guarded Methods Do something based on the fact that one or more objects have particular states Make a set of purchases assuming.
Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Part 2: Software-Based Approaches
Transaction Management and Concurrency Control
Concurrency Control.
Operating System Reliability
Operating System Reliability
Conditions and Ifs BIS1523 – Lecture 8.
Designing Parallel Algorithms (Synchronization)
Operating System Reliability
Operating System Reliability
Concurrency Unit 4.2 Dr Gordon Russell, Napier University
Software Transactional Memory Should Not be Obstruction-Free
Operating System Reliability
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
Concurrency Unit 4.2 Dr Gordon Russell, Napier University
CSE 542: Operating Systems
Operating System Reliability
Operating System Reliability
Transactions, Properties of Transactions
Presentation transcript:

Optimistic Design 1

Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming all items are available and not too expensive  Make a withdrawal based on the fact that the client has sufficient balance How do we do that? 2

Pessimistic Design Lock the relevant object(s) to avoid concurrent changes Check that state is still OK If the state is no longer as required, release the lock  What do we do next in this case? Apply the necessary actions Finally release the lock 3

Analysis Consider a scenario in which we lock every access to an object. Assume:  The program has 1,000 objects like ours  One of the other threads is accessing one of those objects 10% of the time  The changes made to the object invalidate our action 10% of the time  On average the synchronization is necessary only 0.001% of the time!  But we are paying for it 100% of the time for each object! Maybe we can skip the synchronization?  Whatever can go wrong, will go wrong 4

Optimistic Design Attempt actions with / without partial synchronization Rollback (undo) actions that should not have been performed after we discover the problem Main issues:  How to discover the problem  How to rollback  What to do after the rollback (on failure) 5

Reduces the time holding locks Reduces the number of lock operations Advantages of Optimistic Design Commit / Verify 6

When to Apply Chance of failure is low enough Synchronization overhead is high enough Can tell when some operations has been applied incorrectly / illegally Always can go back to the last correct state 7

Example In a word processor we have two threads: the user thread and the spell checker thread. The user thread should never wait for the speller thread while it is spell checking a text. The speller thread should take pieces of text and highlight spelling mistakes. Solution: the document is composed of a sequence of text chunks. In order to edit text, the user thread locks the chunk containing the current editing position and marks the chunk as "busy", then it unlocks the chunk. When editing ends, the user thread locks the chunk again, sets a time-stamp that marks the completion of the editing, and marks the chunk as "not busy". Then, it unlocks the chunk. The spell checker looks for threads that are not busy. When it finds one, it locks the chunk, verify that it is indeed "not busy" and if so, makes a replica of the text contained in the chunk. The thread then finds the spelling mistakes and keeps a local records of them. Then, it locks the chunk again. If the chunk is busy or has been updated after the first lock, the spell checker discards its results. Otherwise, it highlights the erronous words and unlocks the chunk. 8

Provisional Action Only “pretending” to act, delaying commitment of effects until the possibility of failure has been ruled out Easier to manage for methods that only update instance variables Instead of making changes to an object, act on its shadow copy Once in a while commit our changes by replacing relevant objects with our copies Most of the time need to make sure the originals had not changed at all! Using immutable fields makes it easy to recognize changes by comparing references rather than data. 9

Provisional Action - Code class Optimistic { // State class is used instead of a few fields. // State should be immutable for this to be efficient (and safer) private State currentState; synchronized boolean commit(State assumed, State next) { // assumed is state before the action boolean success = (currentState == assumed); if (success) // no interference – safe to commit currentState = next; // the new state after action return success; } 10 Can a thread change the state during the commit of another?

Rollback / Recovery Undoing the effects of each performed action Every action performed within an optimistic method should have an inverse action Keeping a log of all actions performed so far to allow rolling back (undoing) in reverse order Periodically, verify that the actions were applied correctly (e.g. without interference) and discard parts of the log Allows concurrent modifications as long as we can check that they were applied correctly 11

Rollback Code class Optimistic { private State currentState; // State class is now mutable boolean synchronized verifyExecution(UndoList log) { boolean success = checkGuards(currentState); // check for indication that the operations applied correctly if (!success) // apply inverse of each operation in reverse order log.undo(currentState); else log.clear(); // operations effectively committed return success; } 12

Shadowing vs Rollback Acting on a copy is usually MUCH easier to implement Shadowing does not allow concurrent modifications, even if independent  Can be solved by improving state granularity or providing state merge operation In rollback there is no guarantee of progress  Without necessary precautions all threads might keep rolling back each other forever  In shadowing at least one thread makes progress 13

Helper Methods How to ensure safety when we perform one action from within another, or even recursion? Shadowing:  Operate on the state copy passed as a parameter: State func(State before)  Main method commit the copy when it is ready Rollback:  Helper method returns the list of undo actions for the modifications it performed  Caller adds those to its own list and verifies correctness of the object state periodically  Roll back when we discover a problem 14

Detecting Problems In Shadowing we only need to verify that the state of the target object has not changed since we started applying our modifications  Provide an efficient comparison operator (simple reference comparison on immutable objects)  If there is too much to compare, use versioning or timestamping In Rollback we must have a set of well defined verifiable conditions on the object’s state, so that  those conditions are true if and only if all actions performed on the objects so far were performed correctly and consistently 15

Volatile Members Marks for the compiler, that the field may be accessed without synchronization Unlike normal fields, reading / writing goes directly to memory True current values vs performance penalty C++’s equivalent to Java’s volatile is atomic - more on that next week. 16

Failure Handling Report failure or throw exception  Allows the caller to decide Internally retry the action until it succeeds  Are we sure it will eventually succeed? Retry a number of times, or until a timeout Drop the action because it is no longer relevant  For example avoid retrying to update a user entry if the user has just been deleted 17

Retrying Smartly What may cause an optimistic method to fail? Retrying an optimistic method immediately is not efficient  It is unlikely that object state has changed to a desirable or consistent state  It is unlikely that thread contention has passed When using shadowing, since we expect failures to be rare, it may be wiser to subscribe for necessary notifications and wait  May be done very efficiently  Additional programming overhead In either case, it is beneficial to yield and allow the other threads to execute 18

Exponential Backoff When using rollback, we may enter livelock, by causing several threads to rollback/retry continuously Most operations, when executed without interference for sufficiently long time, will complete successfully  Known as obstruction-freedom Use exponential backoff  After the first failure sleep for a bounded but random number of time units  After each consecutive failure, go to sleep for 1,2,4,8,16… time units 19