org.apache.qpid.test.framework.clocksynch
Class UDPClockSynchronizer

java.lang.Object
  extended by org.apache.qpid.test.framework.clocksynch.UDPClockSynchronizer
All Implemented Interfaces:
ClockSynchronizer

public class UDPClockSynchronizer
extends Object
implements ClockSynchronizer

UDPClockSynchronizer is a ClockSynchronizer that sends pings as UDP datagrams, and uses the following simple algorithm to perform clock synchronization:

  1. Slave initiates synchronization with a Reference clock.
  2. Slave stamps current local time on a "time request" message and sends to the Reference.
  3. Upon receipt by Reference, Reference stamps Reference-time and returns.
  4. Upon receipt by Slave, Slave subtracts current time from sent time and divides by two to compute latency. It subtracts current time from Reference time to determine Slave-Reference time delta and adds in the half-latency to get the correct clock delta.
  5. The first result is immediately used to update the clock since it will get the local clock into at least the right ballpark.
  6. The Slave repeats steps 2 through 4, 15 more times.
  7. The results of the packet receipts are accumulated and sorted in lowest-latency to highest-latency order. The median latency is determined by picking the mid-point sample from this ordered list.
  8. All samples outside 1 standard-deviation from the median are discarded and the remaining samples are averaged using an arithmetic mean.

The use of UDP datagrams, instead of TCP based communication eliminates the hidden delays that TCP can introduce, as it can transparently re-order or re-send packets, or introduce delays as packets are naggled.

CRC Card
Responsibilities Collaborations
Trigger a clock synchronziation.
Compute a clock delta to apply to the local clock.
Estimate the error in the synchronzation.


Constructor Summary
UDPClockSynchronizer(String address)
          Creates a clock synchronizer against the specified address for the reference.
 
Method Summary
 long getDelta()
          Gets the clock delta in nano seconds.
 long getEpsilon()
          Gets an estimate of the clock error in nan seconds.
static void main(String[] args)
          For testing purposes.
static long mean(long[] values)
          Computes the mean of a series of values.
static long median(long[] values)
          Computes the median of a series of values.
 long nanoTime()
          Gets the local clock time with any computed delta added in.
protected  long ping()
          Performs a single reference clock request cycle and returns the estimated delta relative to the local clock.
static long standardDeviation(long[] values)
          Computes the standard deviation of a series of values.
 void synch()
          The slave side should call this to compute a clock delta with the reference.
protected  void synch(int n)
          Updates the synchronization delta by performing the specified number of reference clock requests.
static long variance(long[] values)
          Computes the variance of series of values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UDPClockSynchronizer

public UDPClockSynchronizer(String address)
Creates a clock synchronizer against the specified address for the reference.

Parameters:
address - The address of the reference service.
Method Detail

synch

public void synch()
           throws ClockSynchFailureException
The slave side should call this to compute a clock delta with the reference.

Specified by:
synch in interface ClockSynchronizer
Throws:
ClockSynchFailureException - If synchronization cannot be achieved, due to unavailability of the reference time service.

synch

protected void synch(int n)
              throws ClockSynchFailureException
Updates the synchronization delta by performing the specified number of reference clock requests.

Parameters:
n - The number of reference clock request cycles to perform.
Throws:
ClockSynchFailureException - If synchronization cannot be achieved, due to unavailability of the reference time service.

ping

protected long ping()
             throws ClockSynchFailureException
Performs a single reference clock request cycle and returns the estimated delta relative to the local clock. This is computed as the half-latency of the requst cycle, plus the reference clock, minus the local clock.

Returns:
The estimated clock delta.
Throws:
ClockSynchFailureException - If the reference service is not responding.

getDelta

public long getDelta()
Gets the clock delta in nano seconds.

Specified by:
getDelta in interface ClockSynchronizer
Returns:
The clock delta in nano seconds.

getEpsilon

public long getEpsilon()
Gets an estimate of the clock error in nan seconds.

Specified by:
getEpsilon in interface ClockSynchronizer
Returns:
An estimate of the clock error in nan seconds.

nanoTime

public long nanoTime()
Gets the local clock time with any computed delta added in.

Specified by:
nanoTime in interface ClockSynchronizer
Returns:
The local clock time with any computed delta added in.

median

public static long median(long[] values)
Computes the median of a series of values.

Parameters:
values - The values.
Returns:
The median.

mean

public static long mean(long[] values)
Computes the mean of a series of values.

Parameters:
values - The values.
Returns:
The mean.

variance

public static long variance(long[] values)
Computes the variance of series of values.

Parameters:
values - The values.
Returns:
The variance of the values.

standardDeviation

public static long standardDeviation(long[] values)
Computes the standard deviation of a series of values.

Parameters:
values - The values.
Returns:
The standard deviation.

main

public static void main(String[] args)
For testing purposes. Supply address of reference clock as arg 1.

Parameters:
args - Address of reference clock as arg 1.


Licensed to the Apache Software Foundation