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.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
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 TEST_UTIL.getConfiguration().setBoolean(ShutdownHook.RUN_SHUTDOWN_HOOK, false);
59 }
60
61 @After
62 public void tearDown() throws Exception {
63 TEST_UTIL.shutdownMiniCluster();
64 if(rst != null && rst.getRegionServer() != null) {
65 rst.getRegionServer().stop("end of test");
66 rst.join();
67 }
68 }
69
70 @Test
71 public void testClusterId() throws Exception {
72 TEST_UTIL.startMiniZKCluster();
73 TEST_UTIL.startMiniDFSCluster(1);
74
75 Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
76 CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
77
78
79 rst = JVMClusterUtil.createRegionServerThread(conf,cp,
80 HRegionServer.class, 0);
81 rst.start();
82
83 Thread.sleep(10000);
84
85 TEST_UTIL.startMiniHBaseCluster(1, 1);
86
87 rst.waitForServerOnline();
88
89 String clusterId = ZKClusterId.readClusterIdZNode(TEST_UTIL.getZooKeeperWatcher());
90 assertNotNull(clusterId);
91 assertEquals(clusterId, rst.getRegionServer().getClusterId());
92 }
93
94 @Test
95 public void testRewritingClusterIdToPB() throws Exception {
96 TEST_UTIL.startMiniZKCluster();
97 TEST_UTIL.startMiniDFSCluster(1);
98 TEST_UTIL.createRootDir();
99 TEST_UTIL.getConfiguration().setBoolean("hbase.replication", true);
100 Path rootDir = FSUtils.getRootDir(TEST_UTIL.getConfiguration());
101 FileSystem fs = rootDir.getFileSystem(TEST_UTIL.getConfiguration());
102 Path filePath = new Path(rootDir, HConstants.CLUSTER_ID_FILE_NAME);
103 FSDataOutputStream s = null;
104 try {
105 s = fs.create(filePath);
106 s.writeUTF(UUID.randomUUID().toString());
107 } finally {
108 if (s != null) {
109 s.close();
110 }
111 }
112 TEST_UTIL.startMiniHBaseCluster(1, 1);
113 HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
114 assertEquals(1, master.getServerManager().getOnlineServersList().size());
115 }
116
117 }
118