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 junit.framework.Assert;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.ClockOutOfSyncException;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.HServerAddress;
30  import org.apache.hadoop.hbase.HServerInfo;
31  import org.apache.hadoop.hbase.Server;
32  import org.apache.hadoop.hbase.catalog.CatalogTracker;
33  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
34  import org.junit.Test;
35  
36  
37  public class TestClockSkewDetection {
38    private static final Log LOG =
39      LogFactory.getLog(TestClockSkewDetection.class);
40  
41    @Test
42    public void testClockSkewDetection() throws Exception {
43      final Configuration conf = HBaseConfiguration.create();
44      ServerManager sm = new ServerManager(new Server() {
45        @Override
46        public CatalogTracker getCatalogTracker() {
47          return null;
48        }
49  
50        @Override
51        public Configuration getConfiguration() {
52          return conf;
53        }
54  
55        @Override
56        public String getServerName() {
57          return null;
58        }
59  
60        @Override
61        public ZooKeeperWatcher getZooKeeper() {
62          return null;
63        }
64  
65        @Override
66        public void abort(String why, Throwable e) {}
67  
68        @Override
69        public boolean isStopped() {
70          return false;
71        }
72  
73        @Override
74        public void stop(String why) {
75        }}, null, null);
76  
77      LOG.debug("regionServerStartup 1");
78      HServerInfo hsi1 = new HServerInfo(new HServerAddress("example.org:1234"),
79          System.currentTimeMillis(), -1, "example.com");
80      sm.regionServerStartup(hsi1, System.currentTimeMillis());
81  
82      long maxSkew = 30000;
83  
84      try {
85        LOG.debug("regionServerStartup 2");
86        HServerInfo hsi2 = new HServerInfo(new HServerAddress("example.org:1235"),
87          System.currentTimeMillis(), -1, "example.com");
88        sm.regionServerStartup(hsi2, System.currentTimeMillis() - maxSkew * 2);
89        Assert.assertTrue("HMaster should have thrown an ClockOutOfSyncException "
90            + "but didn't.", false);
91      } catch(ClockOutOfSyncException e) {
92        //we want an exception
93        LOG.info("Recieved expected exception: "+e);
94      }
95    }
96  }