HBASE-25032 Do not assign regions to region server which has not called regionServerR...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestClockSkewDetection.java
blob0deea155404de36c4254e2ac8e7847b3d2d9943f
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.junit.ClassRule;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionServerStartupRequest;
37 @Category({MasterTests.class, SmallTests.class})
38 public class TestClockSkewDetection {
40 @ClassRule
41 public static final HBaseClassTestRule CLASS_RULE =
42 HBaseClassTestRule.forClass(TestClockSkewDetection.class);
44 private static final Logger LOG =
45 LoggerFactory.getLogger(TestClockSkewDetection.class);
47 @Test
48 public void testClockSkewDetection() throws Exception {
49 final Configuration conf = HBaseConfiguration.create();
50 ServerManager sm = new ServerManager(new MockNoopMasterServices(conf));
52 LOG.debug("regionServerStartup 1");
53 InetAddress ia1 = InetAddress.getLocalHost();
54 RegionServerStartupRequest.Builder request = RegionServerStartupRequest.newBuilder();
55 request.setPort(1234);
56 request.setServerStartCode(-1);
57 request.setServerCurrentTime(System.currentTimeMillis());
58 sm.regionServerStartup(request.build(), 0, "0.0.0", ia1);
60 final Configuration c = HBaseConfiguration.create();
61 long maxSkew = c.getLong("hbase.master.maxclockskew", 30000);
62 long warningSkew = c.getLong("hbase.master.warningclockskew", 1000);
64 try {
65 //Master Time > Region Server Time
66 LOG.debug("Test: Master Time > Region Server Time");
67 LOG.debug("regionServerStartup 2");
68 InetAddress ia2 = InetAddress.getLocalHost();
69 request = RegionServerStartupRequest.newBuilder();
70 request.setPort(1235);
71 request.setServerStartCode(-1);
72 request.setServerCurrentTime(System.currentTimeMillis() - maxSkew * 2);
73 sm.regionServerStartup(request.build(), 0, "0.0.0", ia2);
74 fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
75 } catch(ClockOutOfSyncException e) {
76 //we want an exception
77 LOG.info("Received expected exception: "+e);
80 try {
81 // Master Time < Region Server Time
82 LOG.debug("Test: Master Time < Region Server Time");
83 LOG.debug("regionServerStartup 3");
84 InetAddress ia3 = InetAddress.getLocalHost();
85 request = RegionServerStartupRequest.newBuilder();
86 request.setPort(1236);
87 request.setServerStartCode(-1);
88 request.setServerCurrentTime(System.currentTimeMillis() + maxSkew * 2);
89 sm.regionServerStartup(request.build(), 0, "0.0.0", ia3);
90 fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
91 } catch (ClockOutOfSyncException e) {
92 // we want an exception
93 LOG.info("Received expected exception: " + e);
96 // make sure values above warning threshold but below max threshold don't kill
97 LOG.debug("regionServerStartup 4");
98 InetAddress ia4 = InetAddress.getLocalHost();
99 request = RegionServerStartupRequest.newBuilder();
100 request.setPort(1237);
101 request.setServerStartCode(-1);
102 request.setServerCurrentTime(System.currentTimeMillis() - warningSkew * 2);
103 sm.regionServerStartup(request.build(), 0, "0.0.0", ia4);
105 // make sure values above warning threshold but below max threshold don't kill
106 LOG.debug("regionServerStartup 5");
107 InetAddress ia5 = InetAddress.getLocalHost();
108 request = RegionServerStartupRequest.newBuilder();
109 request.setPort(1238);
110 request.setServerStartCode(-1);
111 request.setServerCurrentTime(System.currentTimeMillis() + warningSkew * 2);
112 sm.regionServerStartup(request.build(), 0, "0.0.0", ia5);