View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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.*;
29  import org.apache.hadoop.hbase.catalog.CatalogTracker;
30  import org.apache.hadoop.hbase.testclassification.SmallTests;
31  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  @Category(SmallTests.class)
36  public class TestClockSkewDetection {
37    private static final Log LOG =
38      LogFactory.getLog(TestClockSkewDetection.class);
39  
40    @Test
41    public void testClockSkewDetection() throws Exception {
42      final Configuration conf = HBaseConfiguration.create();
43      ServerManager sm = new ServerManager(new Server() {
44        @Override
45        public CatalogTracker getCatalogTracker() {
46          return null;
47        }
48  
49        @Override
50        public Configuration getConfiguration() {
51          return conf;
52        }
53  
54        @Override
55        public ServerName getServerName() {
56          return null;
57        }
58  
59        @Override
60        public ZooKeeperWatcher getZooKeeper() {
61          return null;
62        }
63  
64        @Override
65        public void abort(String why, Throwable e) {}
66        
67        @Override
68        public boolean isAborted() {
69          return false;
70        }
71  
72        @Override
73        public boolean isStopped() {
74          return false;
75        }
76  
77        @Override
78        public void stop(String why) {
79        }}, null, false);
80  
81      LOG.debug("regionServerStartup 1");
82      InetAddress ia1 = InetAddress.getLocalHost();
83      sm.regionServerStartup(ia1, 1234, -1, System.currentTimeMillis());
84  
85      final Configuration c = HBaseConfiguration.create();
86      long maxSkew = c.getLong("hbase.master.maxclockskew", 30000);
87      long warningSkew = c.getLong("hbase.master.warningclockskew", 1000);
88  
89      try {
90      	//Master Time > Region Server Time
91        LOG.debug("Test: Master Time > Region Server Time");
92        LOG.debug("regionServerStartup 2");
93        InetAddress ia2 = InetAddress.getLocalHost();
94        sm.regionServerStartup(ia2, 1235, -1, System.currentTimeMillis() - maxSkew * 2);
95        fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
96      } catch(ClockOutOfSyncException e) {
97        //we want an exception
98        LOG.info("Recieved expected exception: "+e);
99      }
100     
101     try {
102       // Master Time < Region Server Time
103       LOG.debug("Test: Master Time < Region Server Time");
104       LOG.debug("regionServerStartup 3");
105       InetAddress ia3 = InetAddress.getLocalHost();
106       sm.regionServerStartup(ia3, 1236, -1, System.currentTimeMillis() + maxSkew * 2);
107       fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
108     } catch (ClockOutOfSyncException e) {
109       // we want an exception
110       LOG.info("Recieved expected exception: " + e);
111     }
112     
113     // make sure values above warning threshold but below max threshold don't kill
114     LOG.debug("regionServerStartup 4");
115     InetAddress ia4 = InetAddress.getLocalHost();
116     sm.regionServerStartup(ia4, 1237, -1, System.currentTimeMillis() - warningSkew * 2);
117     
118     // make sure values above warning threshold but below max threshold don't kill
119     LOG.debug("regionServerStartup 5");
120     InetAddress ia5 = InetAddress.getLocalHost();
121     sm.regionServerStartup(ia5, 1238, -1, System.currentTimeMillis() + warningSkew * 2);
122     
123   }
124 
125 }
126