summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/test/framework/clocksynch/UDPClockSynchronizer.java
blob: 226c84611d01822e914105b12e05c6fb8460f2da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.test.framework.clocksynch;

import org.apache.qpid.junit.extensions.util.CommandLineParser;
import org.apache.qpid.junit.extensions.util.ParsedProperties;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * UDPClockSynchronizer is a {@link ClockSynchronizer} that sends pings as UDP datagrams, and uses the following simple
 * algorithm to perform clock synchronization:
 *
 * <ol>
 * <li>Slave initiates synchronization with a Reference clock.</li>
 * <li>Slave stamps current local time on a "time request" message and sends to the Reference.</li>
 * <li>Upon receipt by Reference, Reference stamps Reference-time and returns.</li>
 * <li>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.</li>
 * <li>The first result is immediately used to update the clock since it will get the local clock into at least
 *     the right ballpark.</li>
 * <li>The Slave repeats steps 2 through 4, 15 more times.</li>
 * <li>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.</li>
 * <li>All samples outside 1 standard-deviation from the median are discarded and the remaining samples
 *     are averaged using an arithmetic mean.</li>
 * </ol>
 *
 * <p/>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.
 *
 * <p/><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Trigger a clock synchronziation.
 * <tr><td> Compute a clock delta to apply to the local clock.
 * <tr><td> Estimate the error in the synchronzation.
 * </table>
 */
public class UDPClockSynchronizer implements ClockSynchronizer
{
    /** Used for debugging. */
    // private static final Logger log = Logger.getLogger(UDPClockSynchronizer.class);

    /** Defines the timeout to use when waiting for responses to time requests. */
    private static final int TIMEOUT = 50;

    /** The clock delta. */
    private long delta = 0L;

    /** Holds an estimate of the clock error relative to the reference clock. */
    private long epsilon = 0L;

    /** Holds the address of the reference clock. */
    private InetAddress referenceAddress;

    /** Holds the socket to communicate with the reference service over. */
    private DatagramSocket socket;

    /** Used to control the shutdown in the main test loop. */
    private static boolean doSynch = true;

    /**
     * Creates a clock synchronizer against the specified address for the reference.
     *
     * @param address The address of the reference service.
     */
    public UDPClockSynchronizer(String address)
    {
        try
        {
            referenceAddress = InetAddress.getByName(address);
        }
        catch (UnknownHostException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * The slave side should call this to compute a clock delta with the reference.
     *
     * @throws ClockSynchFailureException If synchronization cannot be achieved, due to unavailability of the reference
     *                                    time service.
     */
    public void synch() throws ClockSynchFailureException
    {
        try
        {
            socket = new DatagramSocket();
            socket.setSoTimeout(TIMEOUT);

            // Synchronize on a single ping, to get the clock into the right ball-park.
            synch(1);

            // Synchronize on 15 pings.
            synch(15);

            // And again, for greater accuracy, on 31.
            synch(31);

            socket.close();
        }
        catch (SocketException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * Updates the synchronization delta by performing the specified number of reference clock requests.
     *
     * @param 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.
     */
    protected void synch(int n) throws ClockSynchFailureException
    {
        // log.debug("protected void synch(int n = " + n + "): called");

        // Create an array of deltas by performing n reference pings.
        long[] delta = new long[n];

        for (int i = 0; i < n; i++)
        {
            delta[i] = ping();
        }

        // Reject any deltas that are larger than 1 s.d. above the median.
        long median = median(delta);
        long sd = standardDeviation(delta);

        // log.debug("median = " + median);
        // log.debug("sd = " + sd);

        long[] tempDeltas = new long[n];
        int count = 0;

        for (int i = 0; i < n; i++)
        {
            if ((delta[i] <= (median + sd)) && (delta[i] >= (median - sd)))
            {
                tempDeltas[count] = delta[i];
                count++;
            }
            else
            {
                // log.debug("Rejected: " + delta[i]);
            }
        }

        System.arraycopy(tempDeltas, 0, delta, 0, count);

        // Estimate the delta as the mean of the remaining deltas.
        this.delta += mean(delta);

        // Estimate the error as the standard deviation of the remaining deltas.
        this.epsilon = standardDeviation(delta);

        // log.debug("this.delta = " + this.delta);
        // log.debug("this.epsilon = " + this.epsilon);
    }

    /**
     * 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.
     *
     * @return The estimated clock delta.
     *
     * @throws ClockSynchFailureException If the reference service is not responding.
     */
    protected long ping() throws ClockSynchFailureException
    {
        // log.debug("protected long ping(): called");

        try
        {
            byte[] buf = new byte[256];

            boolean timedOut = false;
            long start = 0L;
            long refTime = 0L;
            long localTime = 0L;
            long latency = 0L;
            int failCount = 0;

            // Keep trying the ping until it gets a response, or 10 tries in a row all time out.
            do
            {
                // Start timing the request latency.
                start = nanoTime();

                // Get the reference time.
                DatagramPacket packet =
                    new DatagramPacket(buf, buf.length, referenceAddress, UDPClockReference.REFERENCE_PORT);
                socket.send(packet);
                packet = new DatagramPacket(buf, buf.length);

                timedOut = false;

                try
                {
                    socket.receive(packet);
                }
                catch (SocketTimeoutException e)
                {
                    timedOut = true;
                    failCount++;

                    continue;
                }

                ByteBuffer bbuf = ByteBuffer.wrap(packet.getData());
                refTime = bbuf.getLong();

                // Stop timing the request latency.
                localTime = nanoTime();
                latency = localTime - start;

                // log.debug("refTime = " + refTime);
                // log.debug("localTime = " + localTime);
                // log.debug("start = " + start);
                // log.debug("latency = " + latency);
                // log.debug("delta = " + ((latency / 2) + (refTime - localTime)));

            }
            while (timedOut && (failCount < 10));

            // Fail completely if the fail count is too high.
            if (failCount >= 10)
            {
                throw new ClockSynchFailureException("Clock reference not responding.", null);
            }

            // Estimate delta as (ref clock + half-latency) - local clock.
            return (latency / 2) + (refTime - localTime);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * Gets the clock delta in nano seconds.
     *
     * @return The clock delta in nano seconds.
     */
    public long getDelta()
    {
        return delta;
    }

    /**
     * Gets an estimate of the clock error in nan seconds.
     *
     * @return An estimate of the clock error in nan seconds.
     */
    public long getEpsilon()
    {
        return epsilon;
    }

    /**
     * Gets the local clock time with any computed delta added in.
     *
     * @return The local clock time with any computed delta added in.
     */
    public long nanoTime()
    {
        return System.nanoTime() + delta;
    }

    /**
     * Computes the median of a series of values.
     *
     * @param values The values.
     *
     * @return The median.
     */
    public static long median(long[] values)
    {
        // log.debug("public static long median(long[] values = " + Arrays.toString(values) + "): called");

        long median;

        // Order the list of values.
        long[] orderedValues = new long[values.length];
        System.arraycopy(values, 0, orderedValues, 0, values.length);
        Arrays.sort(orderedValues);

        // Check if the median is computed from a pair of middle value.
        if ((orderedValues.length % 2) == 0)
        {
            int middle = orderedValues.length / 2;

            median = (orderedValues[middle] + orderedValues[middle - 1]) / 2;
        }
        // The median is computed from a single middle value.
        else
        {
            median = orderedValues[orderedValues.length / 2];
        }

        // log.debug("median = " + median);

        return median;
    }

    /**
     * Computes the mean of a series of values.
     *
     * @param values The values.
     *
     * @return The mean.
     */
    public static long mean(long[] values)
    {
        // log.debug("public static long mean(long[] values = " + Arrays.toString(values) + "): called");

        long total = 0L;

        for (long value : values)
        {
            total += value;
        }

        long mean = total / values.length;

        // log.debug("mean = " + mean);

        return mean;
    }

    /**
     * Computes the variance of series of values.
     *
     * @param values The values.
     *
     * @return The variance of the values.
     */
    public static long variance(long[] values)
    {
        // log.debug("public static long variance(long[] values = " + Arrays.toString(values) + "): called");

        long mean = mean(values);

        long totalVariance = 0;

        for (long value : values)
        {
            long diff = (value - mean);
            totalVariance += diff * diff;
        }

        long variance = totalVariance / values.length;

        // log.debug("variance = " + variance);

        return variance;
    }

    /**
     * Computes the standard deviation of a series of values.
     *
     * @param values The values.
     *
     * @return The standard deviation.
     */
    public static long standardDeviation(long[] values)
    {
        // log.debug("public static long standardDeviation(long[] values = " + Arrays.toString(values) + "): called");

        long sd = Double.valueOf(Math.sqrt(variance(values))).longValue();

        // log.debug("sd = " + sd);

        return sd;
    }

    /**
     * For testing purposes. Supply address of reference clock as arg 1.
     *
     * @param args Address of reference clock as arg 1.
     */
    public static void main(String[] args)
    {
        ParsedProperties options =
            new ParsedProperties(CommandLineParser.processCommandLine(args,
                    new CommandLineParser(
                        new String[][]
                        {
                            { "1", "Address of clock reference service.", "address", "true" }
                        }), System.getProperties()));

        String address = options.getProperty("1");

        // Create a clock synchronizer.
        UDPClockSynchronizer clockSyncher = new UDPClockSynchronizer(address);

        // Set up a shutdown hook for it.
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable()
                {
                    public void run()
                    {
                        doSynch = false;
                    }
                }));

        // Repeat the clock synching until the user kills the progam.
        while (doSynch)
        {
            // Perform a clock clockSynch.
            try
            {
                clockSyncher.synch();

                // Print out the clock delta and estimate of the error.
                System.out.println("Delta = " + clockSyncher.getDelta());
                System.out.println("Epsilon = " + clockSyncher.getEpsilon());

                try
                {
                    Thread.sleep(250);
                }
                catch (InterruptedException e)
                {
                    // Restore the interrupted status and terminate the loop.
                    Thread.currentThread().interrupt();
                    doSynch = false;
                }
            }
            // Terminate if the reference time service is unavailable.
            catch (ClockSynchFailureException e)
            {
                doSynch = false;
            }
        }
    }
}