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