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.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HConstants;
33  import org.apache.hadoop.hbase.testclassification.MediumTests;
34  import org.apache.hadoop.hbase.ServerName;
35  import org.apache.hadoop.hbase.SplitLogTask;
36  import org.apache.hadoop.hbase.util.FSUtils;
37  import org.apache.hadoop.hbase.zookeeper.ZKSplitLog;
38  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
39  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
40  import org.apache.zookeeper.CreateMode;
41  import org.apache.zookeeper.ZooDefs.Ids;
42  import org.junit.AfterClass;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  
47  /**
48   * Test the master filesystem in a local cluster
49   */
50  @Category(MediumTests.class)
51  public class TestMasterFileSystem {
52  
53    private static final Log LOG = LogFactory.getLog(TestMasterFileSystem.class);
54    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
55  
56    @BeforeClass
57    public static void setupTest() throws Exception {
58      UTIL.startMiniCluster();
59    }
60  
61    @AfterClass
62    public static void teardownTest() throws Exception {
63      UTIL.shutdownMiniCluster();
64    }
65  
66    @Test
67    public void testFsUriSetProperly() throws Exception {
68      HMaster master = UTIL.getMiniHBaseCluster().getMaster();
69      MasterFileSystem fs = master.getMasterFileSystem();
70      Path masterRoot = FSUtils.getRootDir(fs.conf);
71      Path rootDir = FSUtils.getRootDir(fs.getFileSystem().getConf());
72      // make sure the fs and the found root dir have the same scheme
73      LOG.debug("from fs uri:" + FileSystem.getDefaultUri(fs.getFileSystem().getConf()));
74      LOG.debug("from configuration uri:" + FileSystem.getDefaultUri(fs.conf));
75      // make sure the set uri matches by forcing it.
76      assertEquals(masterRoot, rootDir);
77    }
78  
79    @Test
80    public void testRemoveStaleRecoveringRegionsDuringMasterInitialization() throws Exception {
81      // this test is for when distributed log replay is enabled
82      if (!UTIL.getConfiguration().getBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, false)) return;
83      
84      LOG.info("Starting testRemoveStaleRecoveringRegionsDuringMasterInitialization");
85      HMaster master = UTIL.getMiniHBaseCluster().getMaster();
86      MasterFileSystem fs = master.getMasterFileSystem();
87  
88      String failedRegion = "failedRegoin1";
89      String staleRegion = "staleRegion";
90      ServerName inRecoveryServerName = ServerName.valueOf("mgr,1,1");
91      ServerName previouselyFaildServerName = ServerName.valueOf("previous,1,1");
92      String walPath = "/hbase/data/.logs/" + inRecoveryServerName.getServerName()
93          + "-splitting/test";
94      // Create a ZKW to use in the test
95      ZooKeeperWatcher zkw = HBaseTestingUtility.getZooKeeperWatcher(UTIL);
96      zkw.getRecoverableZooKeeper().create(ZKSplitLog.getEncodedNodeName(zkw, walPath),
97        new SplitLogTask.Owned(inRecoveryServerName, fs.getLogRecoveryMode()).toByteArray(), 
98          Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
99      String staleRegionPath = ZKUtil.joinZNode(zkw.recoveringRegionsZNode, staleRegion);
100     ZKUtil.createWithParents(zkw, staleRegionPath);
101     String inRecoveringRegionPath = ZKUtil.joinZNode(zkw.recoveringRegionsZNode, failedRegion);
102     inRecoveringRegionPath = ZKUtil.joinZNode(inRecoveringRegionPath, 
103       inRecoveryServerName.getServerName());
104     ZKUtil.createWithParents(zkw, inRecoveringRegionPath);
105     Set<ServerName> servers = new HashSet<ServerName>();
106     servers.add(previouselyFaildServerName);
107     fs.removeStaleRecoveringRegionsFromZK(servers);
108 
109     // verification
110     assertFalse(ZKUtil.checkExists(zkw, staleRegionPath) != -1);
111     assertTrue(ZKUtil.checkExists(zkw, inRecoveringRegionPath) != -1);
112       
113     ZKUtil.deleteChildrenRecursively(zkw, zkw.recoveringRegionsZNode);
114     ZKUtil.deleteChildrenRecursively(zkw, zkw.splitLogZNode);
115     zkw.close();
116   }
117 
118 }