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