View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.FileSystem;
31  import org.apache.hadoop.fs.Path;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.MediumTests;
35  import org.apache.hadoop.hbase.MiniHBaseCluster;
36  import org.apache.hadoop.hbase.ServerName;
37  import org.apache.hadoop.hbase.SplitLogTask;
38  import org.apache.hadoop.hbase.util.FSUtils;
39  import org.apache.hadoop.hbase.zookeeper.ZKSplitLog;
40  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
41  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
42  import org.apache.zookeeper.CreateMode;
43  import org.apache.zookeeper.ZooDefs.Ids;
44  import org.junit.AfterClass;
45  import org.junit.BeforeClass;
46  import org.junit.Test;
47  import org.junit.experimental.categories.Category;
48  
49  /**
50   * Test the master filesystem in a local cluster
51   */
52  @Category(MediumTests.class)
53  public class TestMasterFileSystem {
54  
55    private static final Log LOG = LogFactory.getLog(TestMasterFileSystem.class);
56    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
57  
58    @BeforeClass
59    public static void setupTest() throws Exception {
60      UTIL.startMiniCluster();
61    }
62  
63    @AfterClass
64    public static void teardownTest() throws Exception {
65      UTIL.shutdownMiniCluster();
66    }
67  
68    @Test
69    public void testFsUriSetProperly() throws Exception {
70      HMaster master = UTIL.getMiniHBaseCluster().getMaster();
71      MasterFileSystem fs = master.getMasterFileSystem();
72      Path masterRoot = FSUtils.getRootDir(fs.conf);
73      Path rootDir = FSUtils.getRootDir(fs.getFileSystem().getConf());
74      // make sure the fs and the found root dir have the same scheme
75      LOG.debug("from fs uri:" + FileSystem.getDefaultUri(fs.getFileSystem().getConf()));
76      LOG.debug("from configuration uri:" + FileSystem.getDefaultUri(fs.conf));
77      // make sure the set uri matches by forcing it.
78      assertEquals(masterRoot, rootDir);
79    }
80  
81    @Test
82    public void testRemoveStaleRecoveringRegionsDuringMasterInitialization() throws Exception {
83      // this test is for when distributed log replay is enabled
84      if (!UTIL.getConfiguration().getBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, false)) return;
85      
86      LOG.info("Starting testRemoveStaleRecoveringRegionsDuringMasterInitialization");
87      HMaster master = UTIL.getMiniHBaseCluster().getMaster();
88      MasterFileSystem fs = master.getMasterFileSystem();
89  
90      String failedRegion = "failedRegoin1";
91      String staleRegion = "staleRegion";
92      ServerName inRecoveryServerName = new ServerName("mgr,1,1");
93      ServerName previouselyFaildServerName = new ServerName("previous,1,1");
94      String walPath = "/hbase/data/.logs/" + inRecoveryServerName.getServerName()
95          + "-splitting/test";
96      // Create a ZKW to use in the test
97      ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
98      zkw.getRecoverableZooKeeper().create(ZKSplitLog.getEncodedNodeName(zkw, walPath),
99        new SplitLogTask.Owned(inRecoveryServerName).toByteArray(), Ids.OPEN_ACL_UNSAFE,
100       CreateMode.PERSISTENT);
101     String staleRegionPath = ZKUtil.joinZNode(zkw.recoveringRegionsZNode, staleRegion);
102     ZKUtil.createWithParents(zkw, staleRegionPath);
103     String inRecoveringRegionPath = ZKUtil.joinZNode(zkw.recoveringRegionsZNode, failedRegion);
104     inRecoveringRegionPath = ZKUtil.joinZNode(inRecoveringRegionPath, 
105       inRecoveryServerName.getServerName());
106     ZKUtil.createWithParents(zkw, inRecoveringRegionPath);
107     Set<ServerName> servers = new HashSet<ServerName>();
108     servers.add(previouselyFaildServerName);
109     fs.removeStaleRecoveringRegionsFromZK(servers);
110 
111     // verification
112     assertFalse(ZKUtil.checkExists(zkw, staleRegionPath) != -1);
113     assertTrue(ZKUtil.checkExists(zkw, inRecoveringRegionPath) != -1);
114       
115     ZKUtil.deleteChildrenRecursively(zkw, zkw.recoveringRegionsZNode);
116     ZKUtil.deleteChildrenRecursively(zkw, zkw.splitLogZNode);
117     zkw.close();
118   }
119 
120 }