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.replication.ReplicationException;
29 import org.apache.hadoop.hbase.testclassification.MediumTests;
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
71
72
73
74
75 @Test
76 public void testAddRemovePeer() throws Exception {
77
78 admin.addPeer(ID_ONE, KEY_ONE);
79
80 try {
81 admin.addPeer(ID_ONE, KEY_ONE);
82 } catch (IllegalArgumentException iae) {
83
84 }
85 assertEquals(1, admin.getPeersCount());
86
87 try {
88 admin.removePeer(ID_SECOND);
89 fail();
90 } catch (IllegalArgumentException iae) {
91
92 }
93 assertEquals(1, admin.getPeersCount());
94
95 try {
96 admin.addPeer(ID_SECOND, KEY_SECOND);
97 } catch (IllegalStateException iae) {
98 fail();
99 }
100 assertEquals(2, admin.getPeersCount());
101
102 admin.removePeer(ID_ONE);
103 assertEquals(1, admin.getPeersCount());
104 admin.removePeer(ID_SECOND);
105 assertEquals(0, admin.getPeersCount());
106 }
107
108
109
110
111
112 @Test
113 public void testEnableDisable() throws Exception {
114 admin.addPeer(ID_ONE, KEY_ONE);
115 assertEquals(1, admin.getPeersCount());
116 assertTrue(admin.getPeerState(ID_ONE));
117 admin.disablePeer(ID_ONE);
118
119 assertFalse(admin.getPeerState(ID_ONE));
120 try {
121 admin.getPeerState(ID_SECOND);
122 } catch (IllegalArgumentException iae) {
123
124 }
125 admin.removePeer(ID_ONE);
126 }
127
128 @Test
129 public void testGetTableCfsStr() {
130
131
132 Map<TableName, List<String>> tabCFsMap = null;
133
134
135 assertEquals(null, ReplicationAdmin.getTableCfsStr(tabCFsMap));
136
137
138
139 tabCFsMap = new TreeMap<TableName, List<String>>();
140 tabCFsMap.put(TableName.valueOf("tab1"), null);
141 assertEquals("tab1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
142
143 tabCFsMap = new TreeMap<TableName, List<String>>();
144 tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1"));
145 assertEquals("tab1:cf1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
146
147 tabCFsMap = new TreeMap<TableName, List<String>>();
148 tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1", "cf3"));
149 assertEquals("tab1:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
150
151
152 tabCFsMap = new TreeMap<TableName, List<String>>();
153 tabCFsMap.put(TableName.valueOf("tab1"), null);
154 tabCFsMap.put(TableName.valueOf("tab2"), Lists.newArrayList("cf1"));
155 tabCFsMap.put(TableName.valueOf("tab3"), Lists.newArrayList("cf1", "cf3"));
156 assertEquals("tab1;tab2:cf1;tab3:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
157 }
158
159 @Test
160 public void testAppendPeerTableCFs() throws Exception {
161
162 admin.addPeer(ID_ONE, KEY_ONE);
163
164 admin.appendPeerTableCFs(ID_ONE, "t1");
165 assertEquals("t1", admin.getPeerTableCFs(ID_ONE));
166
167
168 admin.appendPeerTableCFs(ID_ONE, "t2");
169 assertEquals("t2;t1", admin.getPeerTableCFs(ID_ONE));
170
171
172 admin.appendPeerTableCFs(ID_ONE, "t3:f1");
173 assertEquals("t3:f1;t2;t1", admin.getPeerTableCFs(ID_ONE));
174 admin.removePeer(ID_ONE);
175 }
176
177 @Test
178 public void testRemovePeerTableCFs() throws Exception {
179
180 admin.addPeer(ID_ONE, KEY_ONE);
181 try {
182 admin.removePeerTableCFs(ID_ONE, "t3");
183 assertTrue(false);
184 } catch (ReplicationException e) {
185 }
186 assertEquals("", admin.getPeerTableCFs(ID_ONE));
187
188 admin.setPeerTableCFs(ID_ONE, "t1;t2:cf1");
189 try {
190 admin.removePeerTableCFs(ID_ONE, "t3");
191 assertTrue(false);
192 } catch (ReplicationException e) {
193 }
194 assertEquals("t1;t2:cf1", admin.getPeerTableCFs(ID_ONE));
195
196 try {
197 admin.removePeerTableCFs(ID_ONE, "t1:f1");
198 assertTrue(false);
199 } catch (ReplicationException e) {
200 }
201 admin.removePeerTableCFs(ID_ONE, "t1");
202 assertEquals("t2:cf1", admin.getPeerTableCFs(ID_ONE));
203
204 try {
205 admin.removePeerTableCFs(ID_ONE, "t2");
206 assertTrue(false);
207 } catch (ReplicationException e) {
208 }
209 admin.removePeerTableCFs(ID_ONE, "t2:cf1");
210 assertEquals("", admin.getPeerTableCFs(ID_ONE));
211 admin.removePeer(ID_ONE);
212 }
213 }