1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master;
20
21 import static org.junit.Assert.fail;
22
23 import java.net.InetAddress;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.*;
29 import org.apache.hadoop.hbase.catalog.CatalogTracker;
30 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33
34 @Category(SmallTests.class)
35 public class TestClockSkewDetection {
36 private static final Log LOG =
37 LogFactory.getLog(TestClockSkewDetection.class);
38
39 @Test
40 public void testClockSkewDetection() throws Exception {
41 final Configuration conf = HBaseConfiguration.create();
42 ServerManager sm = new ServerManager(new Server() {
43 @Override
44 public CatalogTracker getCatalogTracker() {
45 return null;
46 }
47
48 @Override
49 public Configuration getConfiguration() {
50 return conf;
51 }
52
53 @Override
54 public ServerName getServerName() {
55 return null;
56 }
57
58 @Override
59 public ZooKeeperWatcher getZooKeeper() {
60 return null;
61 }
62
63 @Override
64 public void abort(String why, Throwable e) {}
65
66 @Override
67 public boolean isAborted() {
68 return false;
69 }
70
71 @Override
72 public boolean isStopped() {
73 return false;
74 }
75
76 @Override
77 public void stop(String why) {
78 }}, null, false);
79
80 LOG.debug("regionServerStartup 1");
81 InetAddress ia1 = InetAddress.getLocalHost();
82 sm.regionServerStartup(ia1, 1234, -1, System.currentTimeMillis());
83
84 final Configuration c = HBaseConfiguration.create();
85 long maxSkew = c.getLong("hbase.master.maxclockskew", 30000);
86 long warningSkew = c.getLong("hbase.master.warningclockskew", 1000);
87
88 try {
89
90 LOG.debug("Test: Master Time > Region Server Time");
91 LOG.debug("regionServerStartup 2");
92 InetAddress ia2 = InetAddress.getLocalHost();
93 sm.regionServerStartup(ia2, 1235, -1, System.currentTimeMillis() - maxSkew * 2);
94 fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
95 } catch(ClockOutOfSyncException e) {
96
97 LOG.info("Recieved expected exception: "+e);
98 }
99
100 try {
101
102 LOG.debug("Test: Master Time < Region Server Time");
103 LOG.debug("regionServerStartup 3");
104 InetAddress ia3 = InetAddress.getLocalHost();
105 sm.regionServerStartup(ia3, 1236, -1, System.currentTimeMillis() + maxSkew * 2);
106 fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
107 } catch (ClockOutOfSyncException e) {
108
109 LOG.info("Recieved expected exception: " + e);
110 }
111
112
113 LOG.debug("regionServerStartup 4");
114 InetAddress ia4 = InetAddress.getLocalHost();
115 sm.regionServerStartup(ia4, 1237, -1, System.currentTimeMillis() - warningSkew * 2);
116
117
118 LOG.debug("regionServerStartup 5");
119 InetAddress ia5 = InetAddress.getLocalHost();
120 sm.regionServerStartup(ia5, 1238, -1, System.currentTimeMillis() + warningSkew * 2);
121
122 }
123
124 }
125