1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.fs.FSDataOutputStream;
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.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
47
48 @Category(MediumTests.class)
49 public class TestClusterId {
50
51 private static final Log LOG =
52 LogFactory.getLog(TestClusterId.class.getName());
53
54 private final HBaseTestingUtility TEST_UTIL =
55 new HBaseTestingUtility();
56
57 private JVMClusterUtil.RegionServerThread rst;
58 private JVMClusterUtil.MasterThread mst;
59
60 @Before
61 public void setUp() throws Exception {
62 }
63
64 @After
65 public void tearDown() throws Exception {
66 TEST_UTIL.shutdownMiniCluster();
67 if(rst != null && rst.getRegionServer() != null) {
68 rst.getRegionServer().stop("end of test");
69 rst.join();
70 }
71 }
72
73 @Test
74 public void testClusterId() throws Exception {
75 TEST_UTIL.startMiniZKCluster();
76 TEST_UTIL.startMiniDFSCluster(1);
77
78 Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
79
80
81 rst = JVMClusterUtil.createRegionServerThread(conf,
82 HRegionServer.class, 0);
83 rst.start();
84
85 Thread.sleep(10000);
86
87 TEST_UTIL.startMiniHBaseCluster(1, 0);
88
89 rst.waitForServerOnline();
90
91 String clusterId = ZKClusterId.readClusterIdZNode(TEST_UTIL.getZooKeeperWatcher());
92 assertNotNull(clusterId);
93 assertEquals(clusterId, rst.getRegionServer().getClusterId());
94 }
95
96 @Test
97 public void testRewritingClusterIdToPB() throws Exception {
98 TEST_UTIL.startMiniZKCluster();
99 TEST_UTIL.startMiniDFSCluster(1);
100 TEST_UTIL.createRootDir();
101 TEST_UTIL.getConfiguration().setBoolean("hbase.replication", true);
102 Path rootDir = FSUtils.getRootDir(TEST_UTIL.getConfiguration());
103 FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration());
104 Path filePath = new Path(rootDir, HConstants.CLUSTER_ID_FILE_NAME);
105 FSDataOutputStream s = null;
106 try {
107 s = fs.create(filePath);
108 s.writeUTF(UUID.randomUUID().toString());
109 } finally {
110 if (s != null) {
111 s.close();
112 }
113 }
114 TEST_UTIL.startMiniHBaseCluster(1, 1);
115 HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
116 assertEquals(1, master.getServerManager().getOnlineServersList().size());
117 }
118
119 }
120