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.zookeeper.ZooKeeperWatcher;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(SmallTests.class)
35  public class TestClockSkewDetection {
36    private static final Log LOG =
37      LogFactory.getLog(TestClockSkewDetection.class);
38  
39    @Test
40    public void testClockSkewDetection() throws Exception {
41      final Configuration conf = HBaseConfiguration.create();
42      ServerManager sm = new ServerManager(new Server() {
43        @Override
44        public CatalogTracker getCatalogTracker() {
45          return null;
46        }
47  
48        @Override
49        public Configuration getConfiguration() {
50          return conf;
51        }
52  
53        @Override
54        public ServerName getServerName() {
55          return null;
56        }
57  
58        @Override
59        public ZooKeeperWatcher getZooKeeper() {
60          return null;
61        }
62  
63        @Override
64        public void abort(String why, Throwable e) {}
65        
66        @Override
67        public boolean isAborted() {
68          return false;
69        }
70  
71        @Override
72        public boolean isStopped() {
73          return false;
74        }
75  
76        @Override
77        public void stop(String why) {
78        }}, null, false);
79  
80      LOG.debug("regionServerStartup 1");
81      InetAddress ia1 = InetAddress.getLocalHost();
82      sm.regionServerStartup(ia1, 1234, -1, System.currentTimeMillis());
83  
84      final Configuration c = HBaseConfiguration.create();
85      long maxSkew = c.getLong("hbase.master.maxclockskew", 30000);
86      long warningSkew = c.getLong("hbase.master.warningclockskew", 1000);
87  
88      try {
89      	//Master Time > Region Server Time
90        LOG.debug("Test: Master Time > Region Server Time");
91        LOG.debug("regionServerStartup 2");
92        InetAddress ia2 = InetAddress.getLocalHost();
93        sm.regionServerStartup(ia2, 1235, -1, System.currentTimeMillis() - maxSkew * 2);
94        fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
95      } catch(ClockOutOfSyncException e) {
96        //we want an exception
97        LOG.info("Recieved expected exception: "+e);
98      }
99      
100     try {
101       // Master Time < Region Server Time
102       LOG.debug("Test: Master Time < Region Server Time");
103       LOG.debug("regionServerStartup 3");
104       InetAddress ia3 = InetAddress.getLocalHost();
105       sm.regionServerStartup(ia3, 1236, -1, System.currentTimeMillis() + maxSkew * 2);
106       fail("HMaster should have thrown a ClockOutOfSyncException but didn't.");
107     } catch (ClockOutOfSyncException e) {
108       // we want an exception
109       LOG.info("Recieved expected exception: " + e);
110     }
111     
112     // make sure values above warning threshold but below max threshold don't kill
113     LOG.debug("regionServerStartup 4");
114     InetAddress ia4 = InetAddress.getLocalHost();
115     sm.regionServerStartup(ia4, 1237, -1, System.currentTimeMillis() - warningSkew * 2);
116     
117     // make sure values above warning threshold but below max threshold don't kill
118     LOG.debug("regionServerStartup 5");
119     InetAddress ia5 = InetAddress.getLocalHost();
120     sm.regionServerStartup(ia5, 1238, -1, System.currentTimeMillis() + warningSkew * 2);
121     
122   }
123 
124 }
125