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.concurrent.atomic.AtomicBoolean;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.FileSystem;
26 import org.apache.hadoop.fs.Path;
27 import org.apache.hadoop.hbase.*;
28 import org.apache.hadoop.hbase.replication.regionserver.ReplicationSourceManager;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32
33 import static org.junit.Assert.fail;
34 import static org.junit.Assert.assertEquals;
35
36
37
38
39 @Category(MediumTests.class)
40 public class TestReplicationAdmin {
41
42 private static final Log LOG =
43 LogFactory.getLog(TestReplicationAdmin.class);
44 private final static HBaseTestingUtility TEST_UTIL =
45 new HBaseTestingUtility();
46
47 private final String ID_ONE = "1";
48 private final String KEY_ONE = "127.0.0.1:2181:/hbase";
49 private final String ID_SECOND = "2";
50 private final String KEY_SECOND = "127.0.0.1:2181:/hbase2";
51
52 private static ReplicationSourceManager manager;
53 private static ReplicationAdmin admin;
54 private static AtomicBoolean replicating = new AtomicBoolean(true);
55
56
57
58
59 @BeforeClass
60 public static void setUpBeforeClass() throws Exception {
61 TEST_UTIL.startMiniZKCluster();
62 Configuration conf = TEST_UTIL.getConfiguration();
63 conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, true);
64 admin = new ReplicationAdmin(conf);
65 Path oldLogDir = new Path(TEST_UTIL.getDataTestDir(),
66 HConstants.HREGION_OLDLOGDIR_NAME);
67 Path logDir = new Path(TEST_UTIL.getDataTestDir(),
68 HConstants.HREGION_LOGDIR_NAME);
69 manager = new ReplicationSourceManager(admin.getReplicationZk(), conf,
70
71
72 new Stoppable() {
73 @Override
74 public void stop(String why) {}
75 @Override
76 public boolean isStopped() {return false;}
77 }, FileSystem.get(conf), replicating, logDir, oldLogDir);
78 }
79
80
81
82
83
84
85 @Test
86 public void testAddRemovePeer() throws Exception {
87 assertEquals(0, manager.getSources().size());
88
89 admin.addPeer(ID_ONE, KEY_ONE);
90
91 try {
92 admin.addPeer(ID_ONE, KEY_ONE);
93 } catch (IllegalArgumentException iae) {
94
95 }
96 assertEquals(1, admin.getPeersCount());
97
98 try {
99 admin.removePeer(ID_SECOND);
100 fail();
101 } catch (IllegalArgumentException iae) {
102
103 }
104 assertEquals(1, admin.getPeersCount());
105
106 try {
107 admin.addPeer(ID_SECOND, KEY_SECOND);
108 } catch (IllegalStateException iae) {
109 fail();
110
111 }
112 assertEquals(2, admin.getPeersCount());
113
114 admin.removePeer(ID_ONE);
115 assertEquals(1, admin.getPeersCount());
116 }
117
118 @org.junit.Rule
119 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
120 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
121 }
122