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