1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client.replication;
19
20 import java.util.List;
21 import java.util.Map;
22 import java.util.TreeMap;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.*;
28 import org.apache.hadoop.hbase.testclassification.MediumTests;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33
34 import com.google.common.collect.Lists;
35
36 import static org.junit.Assert.fail;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertTrue;
39 import static org.junit.Assert.assertFalse;
40
41
42
43
44 @Category(MediumTests.class)
45 public class TestReplicationAdmin {
46
47 private static final Log LOG =
48 LogFactory.getLog(TestReplicationAdmin.class);
49 private final static HBaseTestingUtility TEST_UTIL =
50 new HBaseTestingUtility();
51
52 private final String ID_ONE = "1";
53 private final String KEY_ONE = "127.0.0.1:2181:/hbase";
54 private final String ID_SECOND = "2";
55 private final String KEY_SECOND = "127.0.0.1:2181:/hbase2";
56
57 private static ReplicationAdmin admin;
58
59
60
61
62 @BeforeClass
63 public static void setUpBeforeClass() throws Exception {
64 TEST_UTIL.startMiniZKCluster();
65 Configuration conf = TEST_UTIL.getConfiguration();
66 conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
67 admin = new ReplicationAdmin(conf);
68 }
69
70 @AfterClass
71 public static void tearDownAfterClass() throws Exception {
72 if (admin != null) {
73 admin.close();
74 }
75 TEST_UTIL.shutdownMiniZKCluster();
76 }
77
78
79
80
81
82
83 @Test
84 public void testAddRemovePeer() throws Exception {
85
86 admin.addPeer(ID_ONE, KEY_ONE);
87
88 try {
89 admin.addPeer(ID_ONE, KEY_ONE);
90 } catch (IllegalArgumentException iae) {
91
92 }
93 assertEquals(1, admin.getPeersCount());
94
95 try {
96 admin.removePeer(ID_SECOND);
97 fail();
98 } catch (IllegalArgumentException iae) {
99
100 }
101 assertEquals(1, admin.getPeersCount());
102
103 try {
104 admin.addPeer(ID_SECOND, KEY_SECOND);
105 } catch (IllegalStateException iae) {
106 fail();
107 }
108 assertEquals(2, admin.getPeersCount());
109
110 admin.removePeer(ID_ONE);
111 assertEquals(1, admin.getPeersCount());
112 admin.removePeer(ID_SECOND);
113 assertEquals(0, admin.getPeersCount());
114 }
115
116
117
118
119
120 @Test
121 public void testEnableDisable() throws Exception {
122 admin.addPeer(ID_ONE, KEY_ONE);
123 assertEquals(1, admin.getPeersCount());
124 assertTrue(admin.getPeerState(ID_ONE));
125 admin.disablePeer(ID_ONE);
126
127 assertFalse(admin.getPeerState(ID_ONE));
128 try {
129 admin.getPeerState(ID_SECOND);
130 } catch (IllegalArgumentException iae) {
131
132 }
133 admin.removePeer(ID_ONE);
134 }
135
136 @Test
137 public void testGetTableCfsStr() {
138
139
140 Map<TableName, List<String>> tabCFsMap = null;
141
142
143 assertEquals(null, ReplicationAdmin.getTableCfsStr(tabCFsMap));
144
145
146
147 tabCFsMap = new TreeMap<TableName, List<String>>();
148 tabCFsMap.put(TableName.valueOf("tab1"), null);
149 assertEquals("tab1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
150
151 tabCFsMap = new TreeMap<TableName, List<String>>();
152 tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1"));
153 assertEquals("tab1:cf1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
154
155 tabCFsMap = new TreeMap<TableName, List<String>>();
156 tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1", "cf3"));
157 assertEquals("tab1:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
158
159
160 tabCFsMap = new TreeMap<TableName, List<String>>();
161 tabCFsMap.put(TableName.valueOf("tab1"), null);
162 tabCFsMap.put(TableName.valueOf("tab2"), Lists.newArrayList("cf1"));
163 tabCFsMap.put(TableName.valueOf("tab3"), Lists.newArrayList("cf1", "cf3"));
164 assertEquals("tab1;tab2:cf1;tab3:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
165 }
166
167 }
168