HBASE-26245 Addendum fix a naming issue
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestClockSkewDetection.java
blob0fe453e43d943626fc8997b4415171eecb0da28f
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.master;
20 import static org.junit.Assert.fail;
22 import java.net.InetAddress;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.ClockOutOfSyncException;
25 import org.apache.hadoop.hbase.HBaseClassTestRule;
26 import org.apache.hadoop.hbase.HBaseConfiguration;
27 import org.apache.hadoop.hbase.testclassification.MasterTests;
28 import org.apache.hadoop.hbase.testclassification.SmallTests;
29 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
30 import org.junit.ClassRule;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionServerStartupRequest;
38 @Category({MasterTests.class, SmallTests.class})
39 public class TestClockSkewDetection {
41 @ClassRule
42 public static final HBaseClassTestRule CLASS_RULE =
43 HBaseClassTestRule.forClass(TestClockSkewDetection.class);
45 private static final Logger LOG =
46 LoggerFactory.getLogger(TestClockSkewDetection.class);
48 @Test
49 public void testClockSkewDetection() throws Exception {
50 final Configuration conf = HBaseConfiguration.create();
51 ServerManager sm =
52 new ServerManager(new MockNoopMasterServices(conf), new DummyRegionServerList());
54 LOG.debug("regionServerStartup 1");
55 InetAddress ia1 = InetAddress.getLocalHost();
56 RegionServerStartupRequest.Builder request = RegionServerStartupRequest.newBuilder();
57 request.setPort(1234);
58 request.setServerStartCode(-1);
59 request.setServerCurrentTime(EnvironmentEdgeManager.currentTime());
60 sm.regionServerStartup(request.build(), 0, "0.0.0", ia1);
62 final Configuration c = HBaseConfiguration.create();
63 long maxSkew = c.getLong("hbase.master.maxclockskew", 30000);
64 long warningSkew = c.getLong("hbase.master.warningclockskew", 1000);
66 try {
67 //Master Time > Region Server Time
68 LOG.debug("Test: Master Time > Region Server Time");
69 LOG.debug("regionServerStartup 2");
70 InetAddress ia2 = InetAddress.getLocalHost();
71 request = RegionServerStartupRequest.newBuilder();
72 request.setPort(1235);
73 request.setServerStartCode(-1);
74 request.setServerCurrentTime(EnvironmentEdgeManager.currentTime() - maxSkew * 2);
75 sm.regionServerStartup(request.build(), 0, "0.0.0", ia2);
76 fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
77 } catch(ClockOutOfSyncException e) {
78 //we want an exception
79 LOG.info("Received expected exception: "+e);
82 try {
83 // Master Time < Region Server Time
84 LOG.debug("Test: Master Time < Region Server Time");
85 LOG.debug("regionServerStartup 3");
86 InetAddress ia3 = InetAddress.getLocalHost();
87 request = RegionServerStartupRequest.newBuilder();
88 request.setPort(1236);
89 request.setServerStartCode(-1);
90 request.setServerCurrentTime(EnvironmentEdgeManager.currentTime() + maxSkew * 2);
91 sm.regionServerStartup(request.build(), 0, "0.0.0", ia3);
92 fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
93 } catch (ClockOutOfSyncException e) {
94 // we want an exception
95 LOG.info("Received expected exception: " + e);
98 // make sure values above warning threshold but below max threshold don't kill
99 LOG.debug("regionServerStartup 4");
100 InetAddress ia4 = InetAddress.getLocalHost();
101 request = RegionServerStartupRequest.newBuilder();
102 request.setPort(1237);
103 request.setServerStartCode(-1);
104 request.setServerCurrentTime(EnvironmentEdgeManager.currentTime() - warningSkew * 2);
105 sm.regionServerStartup(request.build(), 0, "0.0.0", ia4);
107 // make sure values above warning threshold but below max threshold don't kill
108 LOG.debug("regionServerStartup 5");
109 InetAddress ia5 = InetAddress.getLocalHost();
110 request = RegionServerStartupRequest.newBuilder();
111 request.setPort(1238);
112 request.setServerStartCode(-1);
113 request.setServerCurrentTime(EnvironmentEdgeManager.currentTime() + warningSkew * 2);
114 sm.regionServerStartup(request.build(), 0, "0.0.0", ia5);