MC714: Sistemas Distribu´ ıdos Prof. Lucas Wanner Instituto de Computac ¸ ˜ ao, Unicamp ¸ ˜ Coordenac ao Aula 9: Sincronizac ¸ ˜ ao de rel´ ogios Aula 10: Rel´ ogios l´ ogicos e vetoriais Aula 11: Exclus˜ ao m´ utua Aula 12: Eleic ¸ ˜ ao de lider
Clock Synchronization Physical clocks Logical clocks Vector clocks Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 2 / 47
Time and Frequency Types of Information Time of day Time interval Frequency Question How is time defined? 3 / 47
Time and Frequency Definition Second: 1 31 , 556 , 925 . 9747 of the tropical year for 1900. The duration of 9192631770 periods of the radiation corresponding to the transition between the two hyperfine levels of the ground state of the cesium 133 atom Frequency: Events per second (Hz). Generation Oscillators can create signals that alternate periodically at a certain frequency Inductor-Capacitor (LC circuit) Resistor-Capacitor (RC circuit) Crystal Oscillator 5 / 47
Crystal Oscillators Principles Crystal resonator that strains (expands or contracts) when a voltage is applied When the voltage is reversed, the strain is reversed Voltage signal is taken from the resonator, amplified, and fed back to it. Rate of expansion and contraction is determined by size and cut of the crystal Sources of innaccuracy Crystal oscillators may deviate from their nominal frequency Cut (manufacturing) Environmental: temperature, pressure, vibration Accuracy is measured in PPM–parts per million 6 / 47
Clock Innacuracies Observation Two unsynchronized clocks will drift apart. 7 / 47
Physical clocks Problem Sometimes we simply need the exact time, not just an ordering. Solution Universal Coordinated Time (UTC): Based on the number of transitions per second of the cesium 133 atom (pretty accurate). At present, the real time is taken as the average of some 50 cesium-clocks around the world. Introduces a leap second from time to time to compensate that days are getting longer. Note UTC is broadcast through short wave radio and satellite. Satellites can give an accuracy of about ± 0 . 5 ms. Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 8 / 47
Physical clocks Problem Suppose we have a distributed system with a UTC-receiver somewhere in it ⇒ we still have to distribute its time to each machine. Basic principle Every machine has a timer that generates an interrupt H times per second. There is a clock in machine p that ticks on each timer interrupt. Denote the value of that clock by C p ( t ) , where t is UTC time. Ideally, we have that for each machine p , C p ( t ) = t , or, in other words, dC / dt = 1. Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 9 / 47
Physical clocks dC dt > 1 dC Clock time, C dt = 1 Fast clock k c o l c t c dC e dt < 1 f r k e c P o l c w o l S UTC, t In practice: 1 − ρ ≤ dC dt ≤ 1 + ρ . Goal Never let two clocks in any system differ by more than δ time units ⇒ synchronize at least every δ / ( 2 ρ ) seconds. Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 10 / 47
Global positioning system Basic idea You can get an accurate account of time as a side-effect of GPS. Height Point to be� ignored (14,14) r = 16 (-6,6) x r = 10 Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 11 / 47
Global positioning system Problem Assuming that the clocks of the satellites are accurate and synchronized: It takes a while before a signal reaches the receiver The receiver’s clock is definitely out of synch with the satellite Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 12 / 47
Global positioning system Principal operation ∆ r : unknown deviation of the receiver’s clock. x r , y r , z r : unknown coordinates of the receiver. T i : timestamp on a message from satellite i ∆ i = ( T now − T i )+∆ r : measured delay of the message sent by satellite i . Measured distance to satellite i : c × ∆ i ( c is speed of light) Real distance is � ( x i − x r ) 2 +( y i − y r ) 2 +( z i − z r ) 2 d i = c ∆ i − c ∆ r = Observation 4 satellites ⇒ 4 equations in 4 unknowns (with ∆ r as one of them) Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 13 / 47
Clock synchronization principles Principle I Every machine asks a time server for the accurate time at least once every δ / ( 2 ρ ) seconds (Network Time Protocol). Note Okay, but you need an accurate measure of round trip delay, including interrupt handling and processing incoming messages. Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 14 / 47
Clock synchronization principles Principle II Let the time server scan all machines periodically, calculate an average, and inform each machine how it should adjust its time relative to its present time. Note Okay, you’ll probably get every machine in sync. You don’t even need to propagate UTC time. Fundamental You’ll have to take into account that setting the time back is never allowed ⇒ smooth adjustments. Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 15 / 47
Cristian’s algorithm Assuming we have a process P a server S with UTC Algorithm P requests the time from S and measures T 1 locally 1 After receiving the request from P , S prepares a response and appends the time T s 2 from its own clock. P receives the response, measures T 2 locally and then sets its time to be 3 T new = T s +( T 2 − T 1 ) / 2 Problems Single point of failure / bottleneck Impostor or faulty server 16 / 47
NTP – Network Time Protocol 17 / 47
NTP – Network Time Protocol Algorithm A reads local timestamp t 0 , sends to A 1 B reads local timestamp t 1 at reception 2 B reads local timestamp t 2 at transmision, sends t 1 and t 2 to B 3 B computes round trip delay δ and offset θ . 4 18 / 47
NTP – Network Time Protocol Round trip delay δ = ( t 3 − t 0 ) − ( t 2 − t 1 ) Offset θ = ( t 1 − t 0 )+( t 2 − t 3 ) 2 19 / 47
The Happened-before relationship Problem We first need to introduce a notion of ordering before we can order anything. The happened-before relation If a and b are two events in the same process, and a comes before b , then a → b . If a is the sending of a message, and b is the receipt of that message, then a → b If a → b and b → c , then a → c Note This introduces a partial ordering of events in a system with concurrently operating processes. Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 20 / 47
Logical clocks Problem How do we maintain a global view on the system’s behavior that is consistent with the happened-before relation? Solution Attach a timestamp C ( e ) to each event e , satisfying the following properties: P1 If a and b are two events in the same process, and a → b , then we demand that C ( a ) < C ( b ) . P2 If a corresponds to sending a message m , and b to the receipt of that message, then also C ( a ) < C ( b ) . Problem How to attach a timestamp to an event when there’s no global clock ⇒ maintain a consistent set of logical clocks, one per process. Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 21 / 47
Logical clocks Solution Each process P i maintains a local counter C i and adjusts this counter according to the following rules: 1: For any two successive events that take place within P i , C i is incremented by 1. 2: Each time a message m is sent by process P i , the message receives a timestamp ts ( m ) = C i . 3: Whenever a message m is received by a process P j , P j adjusts its local counter C j to max { C j , ts ( m ) } ; then executes step 1 before passing m to the application. Notes Property P1 is satisfied by (1); Property P2 by (2) and (3). It can still occur that two events happen at the same time. Avoid this by breaking ties through process IDs. Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 22 / 47
Logical clocks – example P 1 P 2 P 3 P 1 P 2 P 3 0� 0� 0� 0� 0� 0� 6� m 1 8� 10� 6� m 1 8� 10� 12� 16� 20� 12� 16� 20� 18� 24� m 2 30� 18� 24� m 2 30� 24� 32� 40� 24� 32� 40� 30� 40� 50� 30� P adjusts� 40� 50� 2 its clock 36� 48� 60� 36� 48� 60� m 3 m 3 42� 56� 70� 42� 61� 70� 48� 64� 80� 48� 69� 80� m 4 m 4 54� 72� 90� 70� 77� 90� 60 80 100 76 85 100 P adjusts� 1 its clock (a) (b) Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 23 / 47
Logical clocks – example Note Adjustments take place in the middleware layer Application layer Application sends message Message is delivered to application Adjust local clock� Adjust local clock Middleware layer and timestamp message Middleware sends message Message is received Network layer Source: Maarten van Steen, Distributed Systems: Principles and Paradigms 24 / 47
Recommend
More recommend