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