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