Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

Synchronization

VIEWS: 8 PAGES: 29

									                                                               Synchronization




                        Synchronization
Synchronization or synchronisation is timekeeping which requires the
coordination of events to operate a system in unison. Systems operating with
all their parts in synchrony are said to be synchronous or in sync. Some
systems may be only approximately synchronized, or plesiochronous. For
some applications relative offsets between events need to be determined, for
others only the order of the event is important.

   • Stand-alone System
        – Exclusive access to shared resources

   • Distributes System
        – Exclusive access to shared resources
        – Ordering of Events


   Difficulties in clock synchronization in DS

         – Not all sites have direct access to accurate time source such as a
           GPS receivers due to the cost.
         – Sites has to synchronize their local clocks with those have more
           accurate time.
         – Synchronization needs to be done periodically due to clock
           drift: rate is change in the offset (difference in reading) between
           the clock and a normal perfect reference clock per unit of time
           measured (about 10-6 or 1 sec. in 11.6 days).




                                                                            1
                                                                 Synchronization



             Clock Synchronization: Introduction
Clock synchronization is a problem from computer science and engineering
which deals with the idea that internal clocks of several computers may
differ. Even when initially set accurately, real clocks will differ after some
amount of time due to clock drift, caused by clocks counting time at slightly
different rates. There are several problems that occur as a repercussion of
rate differences and several solutions, some being more appropriate than
others in certain contexts.
       Each node in a distributed system has separate local clock. Notion of
physical time might differ among various nodes of the system. Problem due
to different time values at different nodes. When each machine has its own
clock, an event that occurred after another event may nevertheless be
assigned an earlier time.



Need for Clock Synchronization
   We need to measure time accurately to know the time an event occurred
at a computer. To do this we need to synchronize its clock with an
authoritative external clock. Algorithms for clock synchronization are useful
for concurrency control based on timestamp ordering. There is no global
clock in a distributed system. Logical time is an alternative. It gives ordering
of events - also useful for consistency of replicated data

    If a site‘s clock is ahead of the reference (time server) to which it
     synchronizes to, it cannot be simply set back (why?).
    This may have effect that a event that has happened but its time-stamp
     is in the future (Example: compiler and UNIX make).
    For process pi
         – Ci(t) – software clock
         – Hi(t) – Hardware clock, I.e., the time given by hardware clock
         Thus: Ci(t) = H(t)+ 
    Various notions of correctness for clocks have been suggested.
    It is common to define a hardware clock H to be correct if its drifts
     fall within a known bound  > 0 (e.g. for real-time t’ and t, t’>t).
      (1-  )(t’-t) ≤ H(t’) – H(t) ≤ (1+ )(t’-t)
   • Problem due to different time values at different nodes

                                                                              2
                                                              Synchronization


When each machine has its own clock, an event that occurred after another
event may nevertheless be assigned an earlier time




There are two types of Synchronization

    External synchronization
       – A computer‘s clock Ci is synchronized with an external
          authoritative time source S, so that:
       – |S(t) - Ci(t)| < D for i = 1, 2, … N over an interval, I of real
          time
       – The clocks Ci are accurate to within the bound D.


    Internal synchronization
        – The clocks of a pair of computers are synchronized with one
           another so that:
        – | Ci(t) - Cj(t)| < D for i = 1, 2, … N over an interval, I of real
           time
        – The clocks Ci and Cj agree within the bound D.
            Internally synchronized clocks are not necessarily
                externally synchronized, as they may drift collectively.
            if the set of processes P is synchronized externally within a
                bound D, it is also internally synchronized within bound
                2D.

Clock Synchronization can be done in of three ways:

• Physical clocks
• Logical clocks
• Vector clocks

                                                                           3
                                                          Synchronization


                        Physical Clocks


Some methods to measure time are:
       – Mean solar second: measuring a large numbers of day --
          taking average -- dividing by 86400
       – TAI (International Atomic Time): the mean number of ticks of
          the cesium 133 clocks (since 1/1/1958) divided by
          9,192,631,770
              • Very small drift rate ~ 10-13 seconds/second
       – UTC: broadcast by NIST from Fort Collins, Colorado over
          shortwave radio station WWV.




                      Computation of the mean solar day




  The relation between clock time and UTC when clocks tick at different
  rates

        – maximum drift rate ()


                                                                       4
                                                   Synchronization


– every t seconds, the worst case drift between two clocks will
  be at most 2t
– to guarantee two clocks never differ by more than , the clocks
  must re-synchronize every /2 seconds




                                                                5
                                                                  Synchronization


             Clock Synchronaization Algorithems


                 In a centralized system the solution is trivial; the centralized
server will dictate the system time. Cristian's algorithm and the Berkeley
Algorithm are some solutions to the clock synchronization problem in a
centralized server environment. In a distributed system the problem takes on
more complexity because a global time is not easily known. The most used
clock synchronization solution on the Internet is the Network Time Protocol
(NTP) which is a layered client-server architecture based on UDP message
passing. Lamport timestamps and Vector clocks are concepts of the logical
clocks in distributed systems.



   • Centralized Algorithms
       – Cristian‘s Algorithm (1989)
       – Berkeley Algorithm (1989)


   • Decentralized Algorithms
       – Averaging Algorithms (e.g. NTP)
       – Multiple External Time Sources


Cristian's Algorithm

             Cristian's Algorithm is a method for clock synchronisation
which can be used in many fields of distributive computer science but is
primarily used in low-latency intranets. Cristian observed that this simple
algorithm is probabilistic, in that it only achieves synchronisation if the
round-trip time (RTT) of the request is short compared to required accuracy.
It also suffers in implementations using a single server, making it unsuitable
for many distributive applications where redundancy may be crucial.

        Cristian's Algorithm works between a process P, and a time server
S — connected to a source of UTC (Coordinated Universal Time). Put
simply:



                                                                               6
                                                               Synchronization


   1. P requests the time from S
   2. After receiving the request from P, S prepares a response and appends
      the time T, from its own clock at the last possible moment before
      dispatch.
   3. P then sets its time to be T + RTT/2

      It is important to note that the time is attached at the last possible
moment before being returned to P. This is to eliminate inaccuracies caused
by network delay.

       P needs to record the Round Trip Time (RTT) of the request it made
to S so that it can set its clock to T + RTT/2. This method assumes that the
RTT is split equally between both request and response, which may not
always be the case but is a reasonable assumption on a LAN connection.

        Further accuracy can be gained by making multiple requests to S and
using the response with the shortest RTT. We can estimate the accuracy of
the system by taking RTT/2 from the fastest response as a value we call min.
The earliest point at which S could have placed the time T was min after P
sent its request. Therefore the time at S when the message is received by P is
in the range (T + min) to (T + RTT - min). The width of this range is (RTT -
2*min). This gives an accuracy of (RTT/2 - min).


    Client C requests time in Request message sent to time server S, then
      it receives time value t in message CUTC from S. t is the current time
      in S before sending back CUTC to C.
    Let Ttrans be time taken for CUTC message from S to C, then C
      should set its time to t + Ttrans .
    Ttrans can be variant. You may say: Ttrans = min+x, x ≥ 0 and min =
      time of message transmission if no interference of other process and
      no other messages, but x is unknown!
   Solution:
    (1) Record total round trip time as Tround (between sending Request
      and receiving CUTC,, i.e., T1-T0)
    (2) If the time received in Request message is t, then C can estimate
      its time as: t + Tround/2.

   • Problem
        – time must never run backward


                                                                            7
                                                               Synchronization




                            Cristian's Algorithm



Berkeley algorithm
         The Berkeley algorithm is a method of clock synchronisation in
distributed computing which assumes no machine has an accurate time
source. It was developed by Gusella and Zatti at the University of California,
Berkeley in 1989 and like Cristian's algorithm is intended for use within
intranets.

Unlike Cristian's algorithm the server process in Berkeley algorithm, called
the master periodically polls other slave process. Generally speaking the
algorithm is as follows:

   1. A master is chosen via an election process such as Chang and Roberts
      algorithm.
   2. The master polls the slaves who reply with their time in a similar way
      to Cristian's algorithm.
   3. The master observes the round-trip time (RTT) of the messages and
      estimates the time of each slave and its own.
   4. The master then averages the clock times, ignoring any values it
      receives far outside the values of the others.
   5. Instead of sending the updated current time back to the other process,
      the master then sends out the amount (positive or negative) that each
      slave must adjust its clock. This avoids further uncertainty due to RTT
      at the slave processes.


                                                                            8
                                                                Synchronization


           Computer systems normally avoid rewinding their clock when
they receive a negative clock alteration from the master. Doing so would
break the property of monotonic time, which is a fundamental assumption in
certain algorithms in the system itself or in programs such as make. A
simple solution to this problem is to halt the clock for the duration specified
by the master, but this simplistic solution can also cause problems, although
they are less severe. For minor corrections, most systems slow the clock
(known as "clock slew"), applying the correction over a longer period of
time.




                             Berkeley algorithm

   a) The time daemon asks all the other machines for their clock values
   b) The machines answer
   c) The time daemon tells everyone how to adjust their clock


Averaging Algorithm
            At the beginning of each interval, every machine broadcasts the
   current time according to its clock. Then it starts a local timer to collect
   all other broadcasts that arrive during some interval S.

   The simplest algorithm is just to average the values from all other
   machines
        – A slightly more sophisticated algorithm :: Discard the m
            highest and m lowest to reduce the effect of a set of faulty
            clocks


                                                                             9
                                                              Synchronization


           – Another improved algorithm :: Correct each message by
             adding to the received time an estimate of the propagation time
             from the ith source
           Problem:
                • extra probe messages are needed to use this scheme




Network Time Protocol (NTP)

  NTP is used in the Internet. Time servers are arranged in levels called
  strata. NTP intends to:
          – provide a service enabling clients in Internet to be synchronized
            accurately to UTC.
          – provide reliable service to losses of connectivity.
          – enable clients to resynchronize sufficiently and frequently.
          – provide protection against interference with the time service.

     Synchronization clock: Network Time Protocol (NTP)
     Stratum 1: Primary server (PS) sync directly with UTC sources.
     Stratum 2: Secondary servers (SS) sync directly to PS.
     Stratum 3: Lowest servers (LS) execute in user sites sync with SS.
     Accuracy: the number of levers (strata).



                                      1

                      2                           2


       3                          3                            3




                                                                           10
                                                          Synchronization


Multiple servers provide for redundancies and hence availability of time
sources and fault-tolerance.NTP synchronizes with one another in one of
three modes:

      – Multicast: Used on LAN. One or more servers periodically
        broadcast the time to servers running in workstations which set
        their clocks assuming a small delay. Accuracy is low.
      – Procedure-call: Similar to Cristian‘s method. Workstations ask
        the server the time. Accuracy is higher than multicast.
      – Symmetric mode: Used by high level to achieve highest
        accuracy. A pair of servers exchange messages bearing timing
        information.




                                                                      11
                                                               Synchronization



                           Logical Clocks
                     For many purposes, it is sufficient that all machines
   agree on the same time. It is not essential that this time also agrees with
   the real time as announced on the radio every hour.What usually matters
   is not that all processes agree on exactly what time it is, but rather that
   they agree on the order in which events occur.

    Mostly ‗absolute‘ time is not important rather ‗relative‘ time is of
   significance
          – Internal consistency and ordering of events
          – If two process don‘t interact  no need for synchronization
             between them
   A logical clock is a Monotonically increasing SW counters
   (COULOURIS) Clocks on different computers that are somehow
   consistent (LAMPORT)

    Potential Requirements for logical clocks:
         – Timestamps : C(a), C(b)
         – If a happens before b in the same process, C(a) < C(b).
               • a  b => C(a) < C(b)
         – If a and b represent the sending and receiving of a message,
            respectively, C(a) < C(b).
         – For all distinctive events a and b, C(a) ≠ C(b).


          Logical clock can capture happened-before ordering by logical
clock. A logical clock need not bear particularly relationship with physical
clock. Each process just keeps its own clock Cp. It is used to timestamp
events. Use Cp(a) and Cp(b) to timestamp events a and b happen in process
p and C(b) for event b at whatever process it occurred.

    Logical clock update and transmission rules:
         – LC1:
     Cp is incremented before each event is issued at p: Cp:= Cp+1;

         – LC2:




                                                                           12
                                                               Synchronization


      When p sends a message m, it piggybacks on m the value t=Cp. On
receiving (m, t), a process q computes Cq:=max(Cq, t) and then applies LC1
before time-stamping the event receive(m).

   Totally ordered logical clocks for pair of distinct events and we take
   process id into consideration. If a is an event occurring at pa with local
   timestamp Ta, and b is an event occurring at pb with local timestamp Tb.
   Define the global logical timestamp for those events as (Ta, a) and (Tb,
   b), respectively.(Ta, a) < (Tb, b) if and only if either Ta < Tb or (Ta =Tb
   and pa < pb)

   Two methods for assigning logical timestamps

         – Lamport‘s Timestamps
         – Vector Timestamps




Lamport’s Timestamps

           Lamport defined a relation ‖happens before‖. a  b ‗a happens
   before b‘. Each Process has local clock LCi with each local event e: LCi
   := LCi + 1; with each sending of a message by process Pi: LCi := LCi +1;
   send (LCi,m) with each reception of a message ―(M,LCm)‖ by Pj: LCj
   := MAX(LCm, LCj ); LCj := LCj +1




                     Example: Totally-Ordered Multicasting




                                                                           13
                                                               Synchronization




For a group of processes multicasting messages to each other, we assume:

  * Each message is timestamped with current logical time of its sender;
  * The sender is also a receiver of its own sending message;
  * The messages from the same sender are received in the order they were
    sent, and no messages are lost.


              When process receives a message, it is put into a local queue,
ordered according to its timestamp. The receiver multicasts an
acknowledgement to the other processes. A process can deliver a queued
message to the application it is running only when the message is at the head
of the queue and has been acknowledged by each other process.All
processes will eventually have the same copy of the local queue by using
Lamport‘s timestamp.. Lamport‘s clock ensures that no two messages have
the same timestamp, and timestamps reflect a consistent global order of
events. Therefore, all message are delivered in the same order everywhere.
That is, we have established totally-ordered multicasting.




                                                                           14
                                                              Synchronization




                      The positioning of Lamport‘s logical
                         clocks in distributed systems



Vector Timestamps

     Lamport timestamps do not guarantee that if C(a) < C(b) then a indeed
happened before b. We need vector timestamps for that. Each process Pi has
an array Vi [1..n], where Vi [j] denotes the number of events that process Pi
knows have taken place at process Pj. When Pi sends a message m, it adds 1
to Vi [i], and sends Vi along with m as vector timestamp vt(m). upon arrival,
each other process knows Pi‘s timestamp.


      When a process Pj receives a message m from Pi with vector
timestamp vt(m), it updates each Vj [k] to max {Vj [k], V(m)[k]}, and
increments Vj [j] by 1 your own component only when sending a message.
Then, Pj postpones delivery of m until:

– Vt (m)[i] = Vj [i] + 1
– Vt (m)[k] <= Vj [k] for k != i
Example: Take V3 = [0,2,2], vt (m) = [1,3,0].




                                                                          15
                                                                Synchronization




Initially:
all c[i] = 0

Increment C[i]:
-Events
   - Send msg
   - Receive msg




Global State

   It is often useful to know the global state in which a distributed system is
   currently residing, e.g., to detect the termination of a distributed
   computation, or deadlock etc.

   Global state of a DS: The local state of each process, and the messages
   that are currently in transit

    Distributed snapshot: reflects a (consistent global) state in which the
   DS might have been. It is not desirable that a snapshot contains the
   recording of messages that have been received but never sent.

   Cut: A graphical representation of global state, as shown in the next
   slide. Like a ‗distributed snapshot‘ reflecting a state in which the system
   might have been represents the last event recorded for each process
   graphically represented by a cut.


                                                                            16
                                                                  Synchronization


   Consistent : for every received message, the sender can be identified
                Cause  Effect




                                 (a)A consistent cut
                                (b)An inconsistent cut


Global State Algorithm for Taking Snapshot
            Assume that the DS can be represented as a collection of
processes connected to each other through unidirectional point-to-point
communication channels (e.g., TCP connection). Any initiating process, say
P, may start by recording its own local state; then it sends a marker along
each of its outgoing channels; When a process Q receives a marker from an
incoming channel C, If Q has not saved its local state, it first saves the state,
then sends a marker along each of its own outgoing channels.

               If Q has recorded the local state, it records the state of channel
C: the sequence of       messages that have been received by Q since the last
time Q recorded its local state, and before it received the marker.When a
process has received a marker along each of its incoming channels, and
processed each one, its recorded local state and state of each channel are
collected and sent to process P. Because any process can initiate the
algorithm, several snapshots can be constructed at the same time. To identify
different processes of snapshot construction, a marker can be tagged with
identifier (even version) of process that initiates the snapshot.



                                                                              17
                                                                    Synchronization




      a) Organization of a process and channels for a distributed snapshot
   b) Process Q receives a marker for the first time and records its local state
                      c) Q records all incoming message
d) Q receives a marker for its incoming channel and finishes recording the state of
                                 the incoming channel




                                                                                  18
                                                                Synchronization




Global State: Algorithm for Termination Detection

                         The termination of a distributed computation can be
detected using the algorithm of taking a snapshot.A snapshot may show a
global state in which message are still in transit, which means the distributed
computation has not terminated.The termination detection algorithm is a
simple modification of the snapshot algorithm. Assume that a process Q
receives the marker from process P, requesting a snapshot for the first time,
then P is said to be the predecessor of Q. When a process Q finishes its part
of the snapshot, it either returns DONE or CONTINUE message to its
predecessor:

DONE: all of Q‘s successors have returned a DONE message; and Q has
not received any message between the point it recorded its state and the
point it had received the marker along each of its incoming channel;

CONTINUE: In all other cases.

               When the original initiator of the snapshot, say P, receives
only the DONE messages, it concludes that the distributed computation has
terminated; Otherwise, P initiates another snapshot, and continues to do so
until only DONE message are eventually returned.




                                                                            19
                                                            Synchronization




                    Election Algorithms


               Election algorithms are algorithms for electing a
coordinator (using this as a generic name for the special process) attempt
to locate the process with the highest process number and designate it as
coordinator .There are two types of algorithms,

             • Bully Algorithm
             • Ring Algorithm

                Goal of these algorithms is to ensure that when an election
starts, it concludes with all processes agreeing on who the new
coordinator is to be.

                    Many distributed applications require one processor
act coordinator, such as Berkeley algorithm for clock synchronization, or
otherwise perform some special role. If all processes are exactly the
same, with no distinguishing characteristics, there is no way to select one
of them to be special. Assume that each processor has a unique number.
The process with the highest number often could be elected. The goal of
an election algorithm is to ensure that when election starts, it concludes
with all processes agreeing on who the new coordinator is to be.




                                                                        20
                                                              Synchronization


Bully Algorithm

              It is assumed that every process knows the processor numbers
  of every other process, but does not know which one is currently up or
  down.When any process P notices that the current coordinator is no
  longer responding the request, it initiates an election:

        1. P sends an ELECTION message to all processes with higher
             numbers;
        2. If no one responds, P wins the lection and becomes
           coordinator;
        3. If one of the highest-ups answers, it takes over. P‘s job is done.

                   When a process gets a message form one of its lower-
  numbered processes, it sends an OK message back to the sender to
  indicate that it is alive and will take over, and it then holds an election
  (unless it is already doing so).Eventually, all processes but one give up,
  and that one is the new coordinator.The new coordinator announces its
  victory by sending all processes a message telling them that starting
  immediately it is the new coordinator.If a process that was previously
  down comes back up, it holds an election, and may win the election if it
  happens to be highest number process among all the running processes.

          A process P detects failure of coordinator and holds an election to
  be the coordinator
         – All process with ID > P response
         – If P receives such a response, it will step back
         – Processes having ID > P can hold elections
            and repeat same procedure
         – If no response from any process with higher ID,
            election holder becomes the new coordinator




                                                                          21
                                          Synchronization




Example
   – Process 4 holds election
   – Process 5 and 6 respond, telling 4 to stop
   – Now 5 and 6 each hold an election
   – Process 6 tells 5 to stop
   – Process 6 wins and tells everyone




                                                      22
                                                                    Synchronization



    Ring Algorithm

                Assume that the processes are physically or logically
ordered, so that each process knows who its successor is.When any
process notices that the coordinator is not functioning, it builds an
ELECTION message containing its process number and sends the
message to its successor. If the successor is down, the sender skips over it
and goes to the next number along the ring, or the next after that, until a
running process is located. At each step, the sender adds its own process
number (as a candidate for coordinator) to the list in the message.
Eventually, the message gets back to the process that started it all. The
process can recognize this event. The process changes the message type
to COORDINATOR and circulates it once again, to inform everyone else
who the new coordinator is (the list number with the highest number) and
who the numbers of the new ring are. When the COORDINATOR
message has circulated once, it is removed and everyone goes back to
work.

   Process detects failure of coordinator
   Send message to neighbor with its ID
   Neighbor adds its ID and pass along
   When all process have added their ID, the one with highest ID becomes the
    coordinator
   The message is rotated once again so everyone knows




                                                                                23
                                                                      Synchronization


   Elections in Wireless Environments

               Bully or Ring algorithms are not realistic in wireless
environments since they assume message passing is reliable which is not
true in wireless environments. Purpose is to select the best node as
coordinator not just any node (e.g. best capacity based on remaining
battery). Leader, called Source, can initiate an election by sending a
message to its immediate neighbors




     Election algorithm in a wireless network, with node a as the source. (a) Initial
                      network. (b)–(e) The build-tree phase




     Election algorithm in a wireless network, with node a as the source. (a) Initial
                      network. (b)–(e) The build-tree phase




                                                                                    24
                                        Synchronization




          (e) The build-tree phase.
(f) Reporting of best node to source.




                                                    25
                                                                Synchronization




                             Mutual Exclusion
       There are three types of algorithems used to achive mutual
exclusion
         –   Centralized Algorithm
         –   Distributed Algorithm
         –   Token Ring Algorithm




      Centralized Algorithm




   a) Process 1 asks the coordinator for permission to enter a critical region.
      Permission is granted
   b) Process 2 then asks permission to enter the same critical region. The
      coordinator does not reply
   c) When process 1 exits the critical region, it tells the coordinator, when
      then replies to 2




                                                                             26
                                                             Synchronization




   Distributed Algorithm




a) Two processes want to enter the same critical region at the same
   moment
b) Process 0 has the lowest timestamp, so it wins
c) When process 0 is done, it sends an OK also, so 2 can now enter the
   critical region



   Token Ring Algorithm


   A token is passed along the ring to allow access to the critical section




             a) unordered group of processes on a network
                 b) logical ring constructed in software




                                                                          27
                                                             Synchronization




Comparison between algorithms used for mutual exclusion:


               Messages Delay before
    Algorithm     per      entry (in             Problems
               entry/exit message times)
   Centralized     3            2            Coordinator crash
                                            Crash of any process
    Distributed 2 ( n – 1 )   2(n–1)               Group
                                              communication
                                            Lost token, process
    Token ring    1 to       0 to n – 1
                                                   crash


             A comparison of three mutual exclusion algorithms




                                                                         28
                                                             Synchronization




                               Summary


                 There is no notion of a globally shared clock. Accurate
timekeeping is important for distributed systems. There are various ways
to synchronize clocks in a DS, essentially all are based on exchanging
clock values. The accuracy of clock synchronization depends on the
variation in communication delays and how they are dealt with. For
ordering of an arbitrary pair of events at different computers, clock
synchronization is not always practical. The happened-before relation is
a partial order on events that reflects a flow of information between them.
Lamport clocks are counters that are updated according to the happened-
before relationship between events. There is no globally shared memory
in DS. It is often hard to determine the system‘s current state, which can
be done by synchronizing all processes so that each collects its local state
and the messages currently in transit (using snapshot). Synchronization
between processes often requires that one process acts as a coordinator. A
decision on who is going to be the new coordinator is taken by means of
election algorithms. These algorithms are primarily used in cases where
the coordinator can crash.




                                                                         29

								
To top