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