View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * 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, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  
24  import java.util.UUID;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FSDataOutputStream;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.CoordinatedStateManager;
31  import org.apache.hadoop.hbase.CoordinatedStateManagerFactory;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.testclassification.MediumTests;
35  import org.apache.hadoop.hbase.master.HMaster;
36  import org.apache.hadoop.hbase.util.FSUtils;
37  import org.apache.hadoop.hbase.util.JVMClusterUtil;
38  import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  
45  /**
46   * Test metrics incremented on region server operations.
47   */
48  @Category(MediumTests.class)
49  public class TestClusterId {
50  
51    private final HBaseTestingUtility TEST_UTIL =
52        new HBaseTestingUtility();
53  
54    private JVMClusterUtil.RegionServerThread rst;
55  
56    @Before
57    public void setUp() throws Exception {
58    }
59  
60    @After
61    public void tearDown() throws Exception {
62      TEST_UTIL.shutdownMiniCluster();
63      if(rst != null && rst.getRegionServer() != null) {
64        rst.getRegionServer().stop("end of test");
65        rst.join();
66      }
67    }
68  
69    @Test
70    public void testClusterId() throws Exception  {
71      TEST_UTIL.startMiniZKCluster();
72      TEST_UTIL.startMiniDFSCluster(1);
73  
74      Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
75      CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
76      //start region server, needs to be separate
77      //so we get an unset clusterId
78      rst = JVMClusterUtil.createRegionServerThread(conf,cp,
79          HRegionServer.class, 0);
80      rst.start();
81      //Make sure RS is in blocking state
82      Thread.sleep(10000);
83  
84      TEST_UTIL.startMiniHBaseCluster(1, 1);
85  
86      rst.waitForServerOnline();
87  
88      String clusterId = ZKClusterId.readClusterIdZNode(TEST_UTIL.getZooKeeperWatcher());
89      assertNotNull(clusterId);
90      assertEquals(clusterId, rst.getRegionServer().getClusterId());
91    }
92    
93    @Test
94    public void testRewritingClusterIdToPB() throws Exception {
95      TEST_UTIL.startMiniZKCluster();
96      TEST_UTIL.startMiniDFSCluster(1);
97      TEST_UTIL.createRootDir();
98      TEST_UTIL.getConfiguration().setBoolean("hbase.replication", true);
99      Path rootDir = FSUtils.getRootDir(TEST_UTIL.getConfiguration());
100     FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration());
101     Path filePath = new Path(rootDir, HConstants.CLUSTER_ID_FILE_NAME);
102     FSDataOutputStream s = null;
103     try {
104       s = fs.create(filePath);
105       s.writeUTF(UUID.randomUUID().toString());
106     } finally {
107       if (s != null) {
108         s.close();
109       }
110     }
111     TEST_UTIL.startMiniHBaseCluster(1, 1);
112     HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
113     assertEquals(1, master.getServerManager().getOnlineServersList().size());
114   }
115   
116 }
117