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