SlideShare une entreprise Scribd logo
1  sur  57
Fundamentals of Distributed
Systems
Dr. Erunika Dayaratna
2024 (2022/2023)
Semester I
Time
• What does time mean for you?
• What does time mean to “software” ?
• What does time mean to “Operating Systems”?
• What does time mean to “Distributed Systems”?
Discuss!
Clocks
• What does clock mean for you?
• What does clock mean to “software” ?
• What does clock mean to “Operating Systems”?
• What does clock mean to “Distributed Systems”?
Discuss!
Use of time in Distributed Systems
• Distributed Systems rely on time for ;
• Scheduler (also in operating systems)
For Scheduling, Timeouts, Failure Detectors, Retry time , etc..
• Performance measurement statistics, profiling
Time a process had been running, CPU usage, etc
• Log files & databases
Records when an event occurs
• Date with timelimited validity
cache entries DNS / TLS / etc
DNS Time-to-live
TLS information – Expiry
Clocks
Types
of
Clocks
Physical Clocks Counts numbers of seconds elapsed
Logical Clocks Counts events (messages sent, etc)
Clocks in DS is not an oscillator (digital electronics)
In DS -> it means source of timestamp
Physical Clocks
• Physical clocks are needed to adjust time of nodes
• All nodes in the system can share their local time with all other
nodes in the system
• In physical synchronization, physical clocks are used to time
stamp an event on that computer
• Keeps the time of the day
• Consistent across systems
Implementations of Clocks
• Quartz crystal clocks
• Quartz oscillator is a part of self feedback loop
• It typically oscillates at 32KHz
• Higher frequency will be generated by dividing the clock
• The clock drift is + or -15 seconds per month (6 ppm)
• The regular quartz clock is not suitable for large distributed systems
• Resonator shaped liked turning fork
• Good resonator can have accuracy of 1 second in 10 years
(Frequency changes with age, temperature and acceleration)
Temperature Impact on Quartz Clocks
Implementations of Clocks
• Atomic Clocks
• Caesium 133 used as an oscillator
• Uses a similar feedback based on circuit as the quartz clock
• Accuracy: 10^8 ppm
GPS
• 31 satellites each carrying an atomic clock
• Satellite broadcast current time and location
• Calculate position from speed of light delay between the satellite and receiver
• Corrections for atmospheric effects, relativity, etc
• In data centers, may need antennas outside the data center ??
Uses of Atomic Clocks
GPS
• Each satellite broadcast its position (x1,y1,z1) and time t1
obtained through an atomic clock
Finding the Position through GPS
• Current position (x, y, z)
• Drift between the receiver clock and the atomic clocks is d.
• Time at which the receiver receives the message is t
• Setup equation:
p (x − xi) 2 + (y − yi) 2 + (z − zi) 2 = (tr − ti + d) × c [ where c = speed of
light ]
Sync Problems of clocks
• Getting two system agree upon time
• Two clocks hardly ever agree
• Quartz oscillators oscillates at sightly different frequencies
• Create ever-widening gap in perceived time
called as Clock drift
• Difference between the two clocks at one point of time is called
as Clock skew
Distributed Systems
• Location is not important , Time is -> GPS can be used
Standard Time Systems
Coordinated Universal Time (UTC)
based on the Greenwich Mean Time (GMT)
GMT -> Solar time
** It’s the noon when the sun is in the south, as seen from the
Greenwich meridian
International Atomic Time (TAI – French temps atomique international)
1 day is 24 * 60 * 60 * 9,192,631,770 periods of caesium-133’s
resonant frequency
Problem and
Compromise
• Speed of Earth’s rotation is
not constant
• UTC is ATI with corrections to
account for Earth’s rotation
• Time zones, daylight savings
time are offsets to UTC
Correction to TAI to UTC
• Makes use of Leap Seconds (Similar to leap years)
• Every year, on 30th June and 31st December at 23:59:59 UTC,
• Option 1 : Clock immediately jumps forward to 00:00:00 skipping one second
(negative leap second)
• Option 2 : Clock moves to 23:59:60 after one second and then moves to
00:00:00 after one further second (neutral)
• Option 3 : Clock moves to 23:59:60 after one second and then moves to
00:00:00 after one further second (positive leap second)
This is announced before hand (few months)
Positive Leap Second
• Case Study : Leap Second Glitch (Read : URL)
Screenshot of the UTC clock from time.gov during the leap second on 31 December 2016.
Computers Representation of Timestamps
• Unix Time
number of second since a given point in time
1st January 1970 00:00:00 UTC (the “epoch”)
not counting leap seconds
• ISO 8601
year month, day, hour, minute. Second, and timezone offset relative to UTC
eg : 2024-03-19T10:00:00+05:30
https://www.unixtimestamp.com/
Conversion Unix to ISO 8601
• Gregorian Calendar
365 days in a year except for the leap years
(year% 4 == 0 && (year % 100 != 0 ) || year % 400 == 0))
Knowledge of past and future seconds ??
(is it taken into consideration?)
Software and Leap Seconds
• Software ignores leap seconds
• Most of the applications are not sensitive of the time accuracy
• Operation systems and distributed systems are not the same !
• Time stamps and their accuracies are crucial in OS and DS
Example Case Study
30th June 2012 incident (similar events occurred in 2016)
• In the night from 30th June to 1st July 2012, many online services and
systems around the globe crashed simultaneous
• Rebooting didn’t help the situation
• Bug in Linux kernel caused livelock on leap second, causing many internet
services to go down
servers locked up and stopped responding
• Solution : Smear (Spread out)
leap second over the course of a period (likely a day)
more of a hack not a solid solution
https://www.wired.com/2012/07/leap-second-glitch-explained/
Clock Synchronization (Sync)
• Computers track physical time/ UTC with a quartz clock
(with Battery, continues running when power is off)
Due to clock drift, clock error gradually increases
(clock skew → difference between two clocks at a point in time)
Reduce the skew as much as possible, not possible to get rid of it totally
Solution
• Periodically get current time from a (reliable source) server that has a
more accurate time source (atomic clock / GPS receiver)
• Protocols :
Network Time Protocol (NTP)
Precision Time Protocol (PTP)
Sync
• Client will adjust
its own time using
a standard NTP
server
Linux – Time Server Selection
Network Time Protocol (NTP) Basics
• Many operating systems vendors
run NTP servers
• Usually configure OS to use
them by default
• Hierarchy of clock servers
arranged into Strata
Stratum 0 : Atomic clock or GPS receiver
Stratum 1 : synced directly with stratum 0 device
Stratum 2 : ser that sync with stratum 1, …. They can
go on as required
https://ntp.org/
Network Time Protocol (NTP)
• May contact multiple servers,
discard the outliers, average
rest
• Makes multiple requests to
the same server, use statistics
to reduce random error due to
variations in network latency
• Reduce clock skew to a few
milliseconds in good network
conditions, but, can be worse!
Estimating Time over a Network
Round-trip network delay :
∂ = (𝑡4 − 𝑡1) - (𝑡3 − 𝑡2)
estimated server time when client receives response : 𝑡3 +
∂
2
estimated clock skew 𝜃 = 𝑡3 +
∂
2
- 𝑡4 =
𝑡2 − 𝑡1 + 𝑡3 − 𝑡4
2
Precision Clock Protocol – Used in Networks
• Compared with NTP, PTP allows hosts to be synchronized to one
common source of time with much higher precision.
Additional Material
Dealing with Clock Skew / Drift
• Go for gradual clock correction when possible
• Fast
Make sure that the clocks run slower until synchronized
• Slow
Make sure that the clocks run faster until synchronized
Correct the Clock Skew
• When the client estimates clock skew 𝜃
need to apply clock correction
If clock skew 𝜃 < 125ms, slew
slightly speedup or slowdown by upto 500 ppm (sync within ~ 5 m)
If 125ms ≤ clock skew 𝜃 < 1000 ms, step
suddenly reset client clock to estimated server timestamp
If clock skew 𝜃 > 1000 ms, panic (DO NOTHING)
Human operator should intervene
Systems relying on clock sync should monitor clock skew !!!
https://www.ntp.org/ntpfaq/ntp-s-algo/#5131-how-accurate-will-my-clock-be
Monotonic and time-of-day Clocks
• Usual procedure (Java Example)
• Get the time stamp at the beginning and the end of the process to measure
the time it takes
long startTime = System.currentTimeMillis();
doSomething();
long endTime = System.currentTimeMillis();
long elapsedMillis = endTime – startTime;
Elapse Time of a Programme
• Usual procedure (Java Example)
• Get the time stamp at the beginning and the end of the process to measure
the time it takes
long startTime = System.currentTimeMillis();
doSomething(); (NTP syncs the time here ?? )
long endTime = System.currentTimeMillis();
long elapsedMillis = endTime – startTime;
What if the value is negative or too large
Appropriate Mode
long startTime = System.nanoTime();
doSmething();
long endTime=System.nanoTime();
long elapsedNanos = endTime-StartTime;
This will always result in a monotonic value (positive here)
C example : https://www.tutorialspoint.com/c_standard_library/c_function_time.htm
Monotonic and Time-of-day Clocks
• Time-of-day Clocks
• Time since a fixed date (Unix : 1st January 1970 epoch)
• Monotonic Clock
• Time since arbitrary point (eg: since the machine / server booted up)
Monotonic and Time-of-day Clocks
• Time-of-day Clocks
• Time since a fixed date (Unix : 1st January 1970 epoch)
• May Suddenly move forward or backward → NTP Stepping
• Subject to leap second adjustment
• Monotonic Clock
• Time since arbitrary point (eg: since the machine / server booted up)
• Always moves forward at near constant rate
Usage of Monotonic and Time-of-day Clocks
• Time-of-day
• Comparison across nodes (if synced)
• Linux : clock_gettime(CLOCK_REALTIME)
• Java : system.currentTimeMillis()
• C : time() – returns seconds since 1st January 1970, 0:0:0, +0.0
• Monotonic
• Good for measuring elapse time on a single node
• Linux : clock_gettime(CLOCK_MONOTONIC)
• C : clock() - approximate processor time consumed by the program
(in <ctime> header file)
Ordering
• Ordering of messages
C sees m2 first and m1 second
though the logical order is m1 happened before m2
Formalizing Ordering of Events
m1 = (t1, message of A)
m2 = (t2, message of B)
Still the problem is not resolved if t1 and t2 are not synced
Happens-before Relation
• Event is something happening at one node
sending / receiving a message / local execution steps
• event a happens before event b ( a → b) iff :
• a and b occurred at the same node , and a occurred before b in that node’s
local execution order; or
• event a is the sending of some message m, and event b is the receipt of that
same message m (assuming sent messages are unique ); or
• There exists an event c such that a → c and c → b
Happen-before relation is a partial order:
It is possible that neither a → b nor b → a. In that case, a and b are
concurrent (a ǁ b)
Happens-before Relationship
a → b, c → d, and e → f due to process order
b → c and d → f due to messages m1 and m2
a → c, a → d, a → f, b → d, b → f, and c → f due to transitivity
a ǁ e , b ǁ e, c ǁ e, and d ǁ e (independent)
Causality
• When a → b, then a might have caused b
• When a ǁ b, known a cannot have caused b
Happens-before relation encodes potential causality
Relativity
Formalization of Causality
Let be a strict total order on events
if ( a → b )  (a b) ; then is a causal order
is “consistent with causality”
There is a causal relationship
ϒ
ϒ
ϒ
ϒ
Logical Time
• Physical clocks (recap) -> seconds elapsed
• Physical time stamp –> useful / but, inconsistent with causality
• Logical clock count number of events occurred
• Captures the causality (dependencies) a → b  T(a) < T(b)
Logical Clocks
Lamport clocks
Vector clocks
Lamport Clock
• Each node maintains a counter (t)
counter is incremented on every local events (e)
• Let L(e) be value of t after increment
• Every messages sent over network is appended with current t
• Recent adjusts its internal t to the received t (iff its greater) and then
increment the event count
If a → b  L(a) < L(b)
But, L(a) < L(b) does not imply a → b
L(a) < L(b) might/might not a ≠ b ()
Lamport Clock Algorithm
on initialization do
t:=0 (each node maintains local variable t)
end initilization
on any event occurring at the local node do
t := t+1
end any event
on request to send message m do
t := t+1; send (t,m) via network link
end request to send message
on receiving (t’,m) via network link do
t := max(t’,t) +1
deliver m to the application
end receiving
Lamport Clock Example
• Let N(e) be node at which event e occurred
• Then pair (L(e),N(e)) uniquely identified event e.
A B C
A,1
A,2
A,3
B,1
B,3
B,4
m(2,m1)
m(4,m2)
C,1
C,5
Total order using Lamport timestamps:
a b  (L(a) < L(b)) V (L(a) = L(b) Λ N(a) < N(b))
(N naming of nodes )
This order is causal (consistent with the causality)
a → b  a b
A B C
A,1
A,2
A,3
B,1
B,3
B,4
m(2,m1)
m(4,m2)
C,1
C,5
ϒ
ϒ
ϒ
Limitations of Lamport Clocks
• Lamport clocks
Given Lamport timestamps L(a) and L(b) with L(a) < L(b)
We cannot deduce whether a → b or a ǁ b
Vector clocks are used to mitigate this issue
Vector Clocks
• Nodes as Vectors ;
N = <𝑁1, 𝑁2, … … 𝑁𝑛>
• Vector timestamp of event a ;
V(a)= <𝑡1, 𝑡2, … … 𝑡𝑛>
• 𝑡𝑖 is number of events observed by node 𝑁𝑖
• Each node has a current vector timestamp T
• On event at node𝑁𝑖, increment vector element T[i]
• Attach current vector timestamp to each message
• Recipient merges message vector into its local vector
Vector Clock Algorithm
on initialization at Node 𝑁𝑖 do
T := <0,0,…….,0> local variable at 𝑁𝑖
end initialization
on anyevent occurring at node 𝑁𝑖 no
T[i] := T[i] + 1
end anyevent
on request to send message m at the node 𝑁𝑖 do
T[i] := T[i] +1 ; send (T,m) via network
end request to send message
on receiving (T’,m) at node 𝑁𝑖 via network do
T[j] := max(T[j],T’[j]) for every j ϵ {1,2,….n}
T[i] := T[i] +1; deliver m to the application
end receiving (T’,m)
Vector Clock Example
A B C
<1,0,0>
(<2,0,0>,m1)
<2,0,0>
<3,0,0>
<0,1,0> <0,0,1>
<2,3,0>
<2,2,0>
(<2,3,0>,m1)
<2,3,2>
• Vector timestamp of an event e represents a set of events;
e and its causal dependencies : {e} υ {a | a → e }
Vector clock ordering
Define following order on vector timestamps
(in a system with n nodes )
T = T’ iff T[i] = T’[i] for all i ϵ {1,…….,n}
T ≤ T’ iff T[i] ≤ T’[i] for all i ϵ {1,…….,n}
T < T’ iff T < T’[i] && T ≠ T’[i]
T ǁ T’ iff T ! ≤ T’[i] && T’[i] ! ≤ T
V(a) ≤ V(b) iff ({a} ᴜ {e | e → a }) ({b} ᴜ {e | e → b } )
Ul
(V(a) < V(b))  (a → b )
(V(a) = V(b))  (a = b )
(V(a) ǁ V(b))  (a ǁ b )
Lesson 05 - Time in Distrributed System.pptx

Contenu connexe

Similaire à Lesson 05 - Time in Distrributed System.pptx

Week 1 Time_measuremenjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjt.pdf
Week 1 Time_measuremenjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjt.pdfWeek 1 Time_measuremenjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjt.pdf
Week 1 Time_measuremenjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjt.pdf
devinezekiel2003
 
Resource Management in (Embedded) Real-Time Systems
Resource Management in (Embedded) Real-Time SystemsResource Management in (Embedded) Real-Time Systems
Resource Management in (Embedded) Real-Time Systems
jeronimored
 

Similaire à Lesson 05 - Time in Distrributed System.pptx (20)

Unit iii-Synchronization
Unit iii-SynchronizationUnit iii-Synchronization
Unit iii-Synchronization
 
L12.FA20.ppt
L12.FA20.pptL12.FA20.ppt
L12.FA20.ppt
 
Synchronisation
SynchronisationSynchronisation
Synchronisation
 
Shoaib
ShoaibShoaib
Shoaib
 
6.Distributed Operating Systems
6.Distributed Operating Systems6.Distributed Operating Systems
6.Distributed Operating Systems
 
Clock synchronization
Clock synchronizationClock synchronization
Clock synchronization
 
Real-Time Systems Intro.pptx
Real-Time Systems Intro.pptxReal-Time Systems Intro.pptx
Real-Time Systems Intro.pptx
 
Week 1 Time_measuremenjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjt.pdf
Week 1 Time_measuremenjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjt.pdfWeek 1 Time_measuremenjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjt.pdf
Week 1 Time_measuremenjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjt.pdf
 
Shoaib
ShoaibShoaib
Shoaib
 
A brief history of Leap Seconds
A brief history of Leap SecondsA brief history of Leap Seconds
A brief history of Leap Seconds
 
Whats the Time
Whats the TimeWhats the Time
Whats the Time
 
Getting date and time from ntp server with esp8266 node mcu
Getting date and time from ntp server with esp8266 node mcuGetting date and time from ntp server with esp8266 node mcu
Getting date and time from ntp server with esp8266 node mcu
 
clock synchronization in Distributed System
clock synchronization in Distributed System clock synchronization in Distributed System
clock synchronization in Distributed System
 
09-time+synch.ppt
09-time+synch.ppt09-time+synch.ppt
09-time+synch.ppt
 
Distributed systems scheduling
Distributed systems schedulingDistributed systems scheduling
Distributed systems scheduling
 
Physical and Logical Clocks
Physical and Logical ClocksPhysical and Logical Clocks
Physical and Logical Clocks
 
CS6601-Unit 4 Distributed Systems
CS6601-Unit 4 Distributed SystemsCS6601-Unit 4 Distributed Systems
CS6601-Unit 4 Distributed Systems
 
Real Time Systems
Real Time SystemsReal Time Systems
Real Time Systems
 
Instrumenting the real-time web: Node.js in production
Instrumenting the real-time web: Node.js in productionInstrumenting the real-time web: Node.js in production
Instrumenting the real-time web: Node.js in production
 
Resource Management in (Embedded) Real-Time Systems
Resource Management in (Embedded) Real-Time SystemsResource Management in (Embedded) Real-Time Systems
Resource Management in (Embedded) Real-Time Systems
 

Dernier

一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
c6eb683559b3
 
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
AS
 
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
AS
 
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
AS
 
原版定制美国加州大学河滨分校毕业证原件一模一样
原版定制美国加州大学河滨分校毕业证原件一模一样原版定制美国加州大学河滨分校毕业证原件一模一样
原版定制美国加州大学河滨分校毕业证原件一模一样
A
 
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
Fir
 
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
AS
 
原版定制(LBS毕业证书)英国伦敦商学院毕业证原件一模一样
原版定制(LBS毕业证书)英国伦敦商学院毕业证原件一模一样原版定制(LBS毕业证书)英国伦敦商学院毕业证原件一模一样
原版定制(LBS毕业证书)英国伦敦商学院毕业证原件一模一样
AS
 
一比一定制波士顿学院毕业证学位证书
一比一定制波士顿学院毕业证学位证书一比一定制波士顿学院毕业证学位证书
一比一定制波士顿学院毕业证学位证书
A
 
一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理
A
 
如何办理(UCLA毕业证)加州大学洛杉矶分校毕业证成绩单本科硕士学位证留信学历认证
如何办理(UCLA毕业证)加州大学洛杉矶分校毕业证成绩单本科硕士学位证留信学历认证如何办理(UCLA毕业证)加州大学洛杉矶分校毕业证成绩单本科硕士学位证留信学历认证
如何办理(UCLA毕业证)加州大学洛杉矶分校毕业证成绩单本科硕士学位证留信学历认证
hfkmxufye
 
一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书
A
 

Dernier (20)

一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
一比一原版(NYU毕业证书)美国纽约大学毕业证学位证书
 
Free scottie t shirts Free scottie t shirts
Free scottie t shirts Free scottie t shirtsFree scottie t shirts Free scottie t shirts
Free scottie t shirts Free scottie t shirts
 
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
 
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
一比一原版(Design毕业证书)新加坡科技设计大学毕业证原件一模一样
 
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
 
原版定制美国加州大学河滨分校毕业证原件一模一样
原版定制美国加州大学河滨分校毕业证原件一模一样原版定制美国加州大学河滨分校毕业证原件一模一样
原版定制美国加州大学河滨分校毕业证原件一模一样
 
HUMANIZE YOUR BRAND - FREE E-WORKBOOK Download Now
HUMANIZE YOUR BRAND - FREE E-WORKBOOK Download NowHUMANIZE YOUR BRAND - FREE E-WORKBOOK Download Now
HUMANIZE YOUR BRAND - FREE E-WORKBOOK Download Now
 
Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303
 
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
[Hackersuli] Élő szövet a fémvázon: Python és gépi tanulás a Zeek platformon
 
APNIC Updates presented by Paul Wilson at CaribNOG 27
APNIC Updates presented by Paul Wilson at  CaribNOG 27APNIC Updates presented by Paul Wilson at  CaribNOG 27
APNIC Updates presented by Paul Wilson at CaribNOG 27
 
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
一比一定制(USC毕业证书)美国南加州大学毕业证学位证书
 
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
 
Washington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers ShirtWashington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers Shirt
 
原版定制(LBS毕业证书)英国伦敦商学院毕业证原件一模一样
原版定制(LBS毕业证书)英国伦敦商学院毕业证原件一模一样原版定制(LBS毕业证书)英国伦敦商学院毕业证原件一模一样
原版定制(LBS毕业证书)英国伦敦商学院毕业证原件一模一样
 
Down bad crying at the gym t shirtsDown bad crying at the gym t shirts
Down bad crying at the gym t shirtsDown bad crying at the gym t shirtsDown bad crying at the gym t shirtsDown bad crying at the gym t shirts
Down bad crying at the gym t shirtsDown bad crying at the gym t shirts
 
一比一定制波士顿学院毕业证学位证书
一比一定制波士顿学院毕业证学位证书一比一定制波士顿学院毕业证学位证书
一比一定制波士顿学院毕业证学位证书
 
一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理
 
如何办理(UCLA毕业证)加州大学洛杉矶分校毕业证成绩单本科硕士学位证留信学历认证
如何办理(UCLA毕业证)加州大学洛杉矶分校毕业证成绩单本科硕士学位证留信学历认证如何办理(UCLA毕业证)加州大学洛杉矶分校毕业证成绩单本科硕士学位证留信学历认证
如何办理(UCLA毕业证)加州大学洛杉矶分校毕业证成绩单本科硕士学位证留信学历认证
 
Subdomain enumeration is a crucial phase in cybersecurity, particularly durin...
Subdomain enumeration is a crucial phase in cybersecurity, particularly durin...Subdomain enumeration is a crucial phase in cybersecurity, particularly durin...
Subdomain enumeration is a crucial phase in cybersecurity, particularly durin...
 
一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书一比一定制加州大学欧文分校毕业证学位证书
一比一定制加州大学欧文分校毕业证学位证书
 

Lesson 05 - Time in Distrributed System.pptx

  • 1. Fundamentals of Distributed Systems Dr. Erunika Dayaratna 2024 (2022/2023) Semester I
  • 2. Time • What does time mean for you? • What does time mean to “software” ? • What does time mean to “Operating Systems”? • What does time mean to “Distributed Systems”? Discuss!
  • 3. Clocks • What does clock mean for you? • What does clock mean to “software” ? • What does clock mean to “Operating Systems”? • What does clock mean to “Distributed Systems”? Discuss!
  • 4. Use of time in Distributed Systems • Distributed Systems rely on time for ; • Scheduler (also in operating systems) For Scheduling, Timeouts, Failure Detectors, Retry time , etc.. • Performance measurement statistics, profiling Time a process had been running, CPU usage, etc • Log files & databases Records when an event occurs • Date with timelimited validity cache entries DNS / TLS / etc
  • 7. Clocks Types of Clocks Physical Clocks Counts numbers of seconds elapsed Logical Clocks Counts events (messages sent, etc) Clocks in DS is not an oscillator (digital electronics) In DS -> it means source of timestamp
  • 8. Physical Clocks • Physical clocks are needed to adjust time of nodes • All nodes in the system can share their local time with all other nodes in the system • In physical synchronization, physical clocks are used to time stamp an event on that computer • Keeps the time of the day • Consistent across systems
  • 9. Implementations of Clocks • Quartz crystal clocks • Quartz oscillator is a part of self feedback loop • It typically oscillates at 32KHz • Higher frequency will be generated by dividing the clock • The clock drift is + or -15 seconds per month (6 ppm) • The regular quartz clock is not suitable for large distributed systems • Resonator shaped liked turning fork • Good resonator can have accuracy of 1 second in 10 years (Frequency changes with age, temperature and acceleration)
  • 10. Temperature Impact on Quartz Clocks
  • 11. Implementations of Clocks • Atomic Clocks • Caesium 133 used as an oscillator • Uses a similar feedback based on circuit as the quartz clock • Accuracy: 10^8 ppm GPS • 31 satellites each carrying an atomic clock • Satellite broadcast current time and location • Calculate position from speed of light delay between the satellite and receiver • Corrections for atmospheric effects, relativity, etc • In data centers, may need antennas outside the data center ??
  • 12. Uses of Atomic Clocks GPS • Each satellite broadcast its position (x1,y1,z1) and time t1 obtained through an atomic clock Finding the Position through GPS • Current position (x, y, z) • Drift between the receiver clock and the atomic clocks is d. • Time at which the receiver receives the message is t • Setup equation: p (x − xi) 2 + (y − yi) 2 + (z − zi) 2 = (tr − ti + d) × c [ where c = speed of light ]
  • 13. Sync Problems of clocks • Getting two system agree upon time • Two clocks hardly ever agree • Quartz oscillators oscillates at sightly different frequencies • Create ever-widening gap in perceived time called as Clock drift • Difference between the two clocks at one point of time is called as Clock skew
  • 14. Distributed Systems • Location is not important , Time is -> GPS can be used
  • 15. Standard Time Systems Coordinated Universal Time (UTC) based on the Greenwich Mean Time (GMT) GMT -> Solar time ** It’s the noon when the sun is in the south, as seen from the Greenwich meridian International Atomic Time (TAI – French temps atomique international) 1 day is 24 * 60 * 60 * 9,192,631,770 periods of caesium-133’s resonant frequency
  • 16. Problem and Compromise • Speed of Earth’s rotation is not constant • UTC is ATI with corrections to account for Earth’s rotation • Time zones, daylight savings time are offsets to UTC
  • 17. Correction to TAI to UTC • Makes use of Leap Seconds (Similar to leap years) • Every year, on 30th June and 31st December at 23:59:59 UTC, • Option 1 : Clock immediately jumps forward to 00:00:00 skipping one second (negative leap second) • Option 2 : Clock moves to 23:59:60 after one second and then moves to 00:00:00 after one further second (neutral) • Option 3 : Clock moves to 23:59:60 after one second and then moves to 00:00:00 after one further second (positive leap second) This is announced before hand (few months)
  • 18. Positive Leap Second • Case Study : Leap Second Glitch (Read : URL) Screenshot of the UTC clock from time.gov during the leap second on 31 December 2016.
  • 19. Computers Representation of Timestamps • Unix Time number of second since a given point in time 1st January 1970 00:00:00 UTC (the “epoch”) not counting leap seconds • ISO 8601 year month, day, hour, minute. Second, and timezone offset relative to UTC eg : 2024-03-19T10:00:00+05:30 https://www.unixtimestamp.com/
  • 20. Conversion Unix to ISO 8601 • Gregorian Calendar 365 days in a year except for the leap years (year% 4 == 0 && (year % 100 != 0 ) || year % 400 == 0)) Knowledge of past and future seconds ?? (is it taken into consideration?)
  • 21. Software and Leap Seconds • Software ignores leap seconds • Most of the applications are not sensitive of the time accuracy • Operation systems and distributed systems are not the same ! • Time stamps and their accuracies are crucial in OS and DS
  • 22. Example Case Study 30th June 2012 incident (similar events occurred in 2016) • In the night from 30th June to 1st July 2012, many online services and systems around the globe crashed simultaneous • Rebooting didn’t help the situation • Bug in Linux kernel caused livelock on leap second, causing many internet services to go down servers locked up and stopped responding • Solution : Smear (Spread out) leap second over the course of a period (likely a day) more of a hack not a solid solution https://www.wired.com/2012/07/leap-second-glitch-explained/
  • 23. Clock Synchronization (Sync) • Computers track physical time/ UTC with a quartz clock (with Battery, continues running when power is off) Due to clock drift, clock error gradually increases (clock skew → difference between two clocks at a point in time) Reduce the skew as much as possible, not possible to get rid of it totally
  • 24. Solution • Periodically get current time from a (reliable source) server that has a more accurate time source (atomic clock / GPS receiver) • Protocols : Network Time Protocol (NTP) Precision Time Protocol (PTP)
  • 25. Sync • Client will adjust its own time using a standard NTP server
  • 26. Linux – Time Server Selection
  • 27. Network Time Protocol (NTP) Basics • Many operating systems vendors run NTP servers • Usually configure OS to use them by default • Hierarchy of clock servers arranged into Strata Stratum 0 : Atomic clock or GPS receiver Stratum 1 : synced directly with stratum 0 device Stratum 2 : ser that sync with stratum 1, …. They can go on as required https://ntp.org/
  • 28. Network Time Protocol (NTP) • May contact multiple servers, discard the outliers, average rest • Makes multiple requests to the same server, use statistics to reduce random error due to variations in network latency • Reduce clock skew to a few milliseconds in good network conditions, but, can be worse!
  • 29. Estimating Time over a Network
  • 30. Round-trip network delay : ∂ = (𝑡4 − 𝑡1) - (𝑡3 − 𝑡2) estimated server time when client receives response : 𝑡3 + ∂ 2 estimated clock skew 𝜃 = 𝑡3 + ∂ 2 - 𝑡4 = 𝑡2 − 𝑡1 + 𝑡3 − 𝑡4 2
  • 31. Precision Clock Protocol – Used in Networks • Compared with NTP, PTP allows hosts to be synchronized to one common source of time with much higher precision. Additional Material
  • 32. Dealing with Clock Skew / Drift • Go for gradual clock correction when possible • Fast Make sure that the clocks run slower until synchronized • Slow Make sure that the clocks run faster until synchronized
  • 33. Correct the Clock Skew • When the client estimates clock skew 𝜃 need to apply clock correction If clock skew 𝜃 < 125ms, slew slightly speedup or slowdown by upto 500 ppm (sync within ~ 5 m) If 125ms ≤ clock skew 𝜃 < 1000 ms, step suddenly reset client clock to estimated server timestamp If clock skew 𝜃 > 1000 ms, panic (DO NOTHING) Human operator should intervene Systems relying on clock sync should monitor clock skew !!!
  • 35. Monotonic and time-of-day Clocks • Usual procedure (Java Example) • Get the time stamp at the beginning and the end of the process to measure the time it takes long startTime = System.currentTimeMillis(); doSomething(); long endTime = System.currentTimeMillis(); long elapsedMillis = endTime – startTime;
  • 36. Elapse Time of a Programme • Usual procedure (Java Example) • Get the time stamp at the beginning and the end of the process to measure the time it takes long startTime = System.currentTimeMillis(); doSomething(); (NTP syncs the time here ?? ) long endTime = System.currentTimeMillis(); long elapsedMillis = endTime – startTime; What if the value is negative or too large
  • 37. Appropriate Mode long startTime = System.nanoTime(); doSmething(); long endTime=System.nanoTime(); long elapsedNanos = endTime-StartTime; This will always result in a monotonic value (positive here) C example : https://www.tutorialspoint.com/c_standard_library/c_function_time.htm
  • 38. Monotonic and Time-of-day Clocks • Time-of-day Clocks • Time since a fixed date (Unix : 1st January 1970 epoch) • Monotonic Clock • Time since arbitrary point (eg: since the machine / server booted up)
  • 39. Monotonic and Time-of-day Clocks • Time-of-day Clocks • Time since a fixed date (Unix : 1st January 1970 epoch) • May Suddenly move forward or backward → NTP Stepping • Subject to leap second adjustment • Monotonic Clock • Time since arbitrary point (eg: since the machine / server booted up) • Always moves forward at near constant rate
  • 40. Usage of Monotonic and Time-of-day Clocks • Time-of-day • Comparison across nodes (if synced) • Linux : clock_gettime(CLOCK_REALTIME) • Java : system.currentTimeMillis() • C : time() – returns seconds since 1st January 1970, 0:0:0, +0.0 • Monotonic • Good for measuring elapse time on a single node • Linux : clock_gettime(CLOCK_MONOTONIC) • C : clock() - approximate processor time consumed by the program (in <ctime> header file)
  • 41. Ordering • Ordering of messages C sees m2 first and m1 second though the logical order is m1 happened before m2
  • 42. Formalizing Ordering of Events m1 = (t1, message of A) m2 = (t2, message of B) Still the problem is not resolved if t1 and t2 are not synced
  • 43. Happens-before Relation • Event is something happening at one node sending / receiving a message / local execution steps • event a happens before event b ( a → b) iff : • a and b occurred at the same node , and a occurred before b in that node’s local execution order; or • event a is the sending of some message m, and event b is the receipt of that same message m (assuming sent messages are unique ); or • There exists an event c such that a → c and c → b Happen-before relation is a partial order: It is possible that neither a → b nor b → a. In that case, a and b are concurrent (a ǁ b)
  • 44. Happens-before Relationship a → b, c → d, and e → f due to process order b → c and d → f due to messages m1 and m2 a → c, a → d, a → f, b → d, b → f, and c → f due to transitivity a ǁ e , b ǁ e, c ǁ e, and d ǁ e (independent)
  • 45. Causality • When a → b, then a might have caused b • When a ǁ b, known a cannot have caused b Happens-before relation encodes potential causality Relativity
  • 46. Formalization of Causality Let be a strict total order on events if ( a → b )  (a b) ; then is a causal order is “consistent with causality” There is a causal relationship ϒ ϒ ϒ ϒ
  • 47. Logical Time • Physical clocks (recap) -> seconds elapsed • Physical time stamp –> useful / but, inconsistent with causality • Logical clock count number of events occurred • Captures the causality (dependencies) a → b  T(a) < T(b) Logical Clocks Lamport clocks Vector clocks
  • 48. Lamport Clock • Each node maintains a counter (t) counter is incremented on every local events (e) • Let L(e) be value of t after increment • Every messages sent over network is appended with current t • Recent adjusts its internal t to the received t (iff its greater) and then increment the event count If a → b  L(a) < L(b) But, L(a) < L(b) does not imply a → b L(a) < L(b) might/might not a ≠ b ()
  • 49. Lamport Clock Algorithm on initialization do t:=0 (each node maintains local variable t) end initilization on any event occurring at the local node do t := t+1 end any event on request to send message m do t := t+1; send (t,m) via network link end request to send message on receiving (t’,m) via network link do t := max(t’,t) +1 deliver m to the application end receiving
  • 50. Lamport Clock Example • Let N(e) be node at which event e occurred • Then pair (L(e),N(e)) uniquely identified event e. A B C A,1 A,2 A,3 B,1 B,3 B,4 m(2,m1) m(4,m2) C,1 C,5
  • 51. Total order using Lamport timestamps: a b  (L(a) < L(b)) V (L(a) = L(b) Λ N(a) < N(b)) (N naming of nodes ) This order is causal (consistent with the causality) a → b  a b A B C A,1 A,2 A,3 B,1 B,3 B,4 m(2,m1) m(4,m2) C,1 C,5 ϒ ϒ ϒ
  • 52. Limitations of Lamport Clocks • Lamport clocks Given Lamport timestamps L(a) and L(b) with L(a) < L(b) We cannot deduce whether a → b or a ǁ b Vector clocks are used to mitigate this issue
  • 53. Vector Clocks • Nodes as Vectors ; N = <𝑁1, 𝑁2, … … 𝑁𝑛> • Vector timestamp of event a ; V(a)= <𝑡1, 𝑡2, … … 𝑡𝑛> • 𝑡𝑖 is number of events observed by node 𝑁𝑖 • Each node has a current vector timestamp T • On event at node𝑁𝑖, increment vector element T[i] • Attach current vector timestamp to each message • Recipient merges message vector into its local vector
  • 54. Vector Clock Algorithm on initialization at Node 𝑁𝑖 do T := <0,0,…….,0> local variable at 𝑁𝑖 end initialization on anyevent occurring at node 𝑁𝑖 no T[i] := T[i] + 1 end anyevent on request to send message m at the node 𝑁𝑖 do T[i] := T[i] +1 ; send (T,m) via network end request to send message on receiving (T’,m) at node 𝑁𝑖 via network do T[j] := max(T[j],T’[j]) for every j ϵ {1,2,….n} T[i] := T[i] +1; deliver m to the application end receiving (T’,m)
  • 55. Vector Clock Example A B C <1,0,0> (<2,0,0>,m1) <2,0,0> <3,0,0> <0,1,0> <0,0,1> <2,3,0> <2,2,0> (<2,3,0>,m1) <2,3,2> • Vector timestamp of an event e represents a set of events; e and its causal dependencies : {e} υ {a | a → e }
  • 56. Vector clock ordering Define following order on vector timestamps (in a system with n nodes ) T = T’ iff T[i] = T’[i] for all i ϵ {1,…….,n} T ≤ T’ iff T[i] ≤ T’[i] for all i ϵ {1,…….,n} T < T’ iff T < T’[i] && T ≠ T’[i] T ǁ T’ iff T ! ≤ T’[i] && T’[i] ! ≤ T V(a) ≤ V(b) iff ({a} ᴜ {e | e → a }) ({b} ᴜ {e | e → b } ) Ul (V(a) < V(b))  (a → b ) (V(a) = V(b))  (a = b ) (V(a) ǁ V(b))  (a ǁ b )