View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.replication.ReplicationException;
31  import org.apache.hadoop.hbase.replication.ReplicationPeer;
32  import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
33  import org.apache.hadoop.hbase.replication.ReplicationQueuesZKImpl;
34  import org.apache.hadoop.hbase.testclassification.MediumTests;
35  import org.junit.AfterClass;
36  import org.junit.BeforeClass;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  import com.google.common.collect.Lists;
41  
42  import static org.junit.Assert.assertEquals;
43  import static org.junit.Assert.assertFalse;
44  import static org.junit.Assert.assertNotNull;
45  import static org.junit.Assert.assertTrue;
46  import static org.junit.Assert.fail;
47  
48  
49  /**
50   * Unit testing of ReplicationAdmin
51   */
52  @Category(MediumTests.class)
53  public class TestReplicationAdmin {
54  
55    private static final Log LOG =
56        LogFactory.getLog(TestReplicationAdmin.class);
57    private final static HBaseTestingUtility TEST_UTIL =
58        new HBaseTestingUtility();
59  
60    private final String ID_ONE = "1";
61    private final String KEY_ONE = "127.0.0.1:2181:/hbase";
62    private final String ID_SECOND = "2";
63    private final String KEY_SECOND = "127.0.0.1:2181:/hbase2";
64  
65    private static ReplicationAdmin admin;
66  
67    /**
68     * @throws java.lang.Exception
69     */
70    @BeforeClass
71    public static void setUpBeforeClass() throws Exception {
72      TEST_UTIL.startMiniZKCluster();
73      Configuration conf = TEST_UTIL.getConfiguration();
74      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
75      admin = new ReplicationAdmin(conf);
76    }
77  
78    @AfterClass
79    public static void tearDownAfterClass() throws Exception {
80      if (admin != null) {
81        admin.close();
82      }
83      TEST_UTIL.shutdownMiniZKCluster();
84    }
85  
86    /**
87     * Simple testing of adding and removing peers, basically shows that
88     * all interactions with ZK work
89     * @throws Exception
90     */
91    @Test
92    public void testAddRemovePeer() throws Exception {
93      // Add a valid peer
94      admin.addPeer(ID_ONE, KEY_ONE);
95      // try adding the same (fails)
96      try {
97        admin.addPeer(ID_ONE, KEY_ONE);
98      } catch (IllegalArgumentException iae) {
99        // OK!
100     }
101     assertEquals(1, admin.getPeersCount());
102 
103     // try adding a peer contains "-"
104     try {
105       admin.addPeer(ID_ONE + "-" + ID_SECOND, KEY_ONE);
106     } catch (IllegalArgumentException iae) {
107       // OK!
108     }
109     assertEquals(1, admin.getPeersCount());
110     // try adding a peer named "lock"
111     try {
112       admin.addPeer(ReplicationQueuesZKImpl.RS_LOCK_ZNODE, KEY_ONE);
113     } catch (IllegalArgumentException iae) {
114       // OK!
115     }
116     assertEquals(1, admin.getPeersCount());
117 
118     // Try to remove an inexisting peer
119     try {
120       admin.removePeer(ID_SECOND);
121       fail();
122     } catch (IllegalArgumentException iae) {
123       // OK!
124     }
125     assertEquals(1, admin.getPeersCount());
126     // Add a second since multi-slave is supported
127     try {
128       admin.addPeer(ID_SECOND, KEY_SECOND);
129     } catch (IllegalStateException iae) {
130       fail();
131     }
132     assertEquals(2, admin.getPeersCount());
133     // Remove the first peer we added
134     admin.removePeer(ID_ONE);
135     assertEquals(1, admin.getPeersCount());
136     admin.removePeer(ID_SECOND);
137     assertEquals(0, admin.getPeersCount());
138   }
139 
140   /**
141    * Tests that the peer configuration used by ReplicationAdmin contains all
142    * the peer's properties.
143    */
144   @Test
145   public void testPeerConfig() throws Exception {
146     ReplicationPeerConfig config = new ReplicationPeerConfig();
147     config.setClusterKey(KEY_ONE);
148     config.getConfiguration().put("key1", "value1");
149     config.getConfiguration().put("key2", "value2");
150     admin.addPeer(ID_ONE, config, null);
151 
152     List<ReplicationPeer> peers = admin.listReplicationPeers();
153     assertEquals(1, peers.size());
154     ReplicationPeer peerOne = peers.get(0);
155     assertNotNull(peerOne);
156     assertEquals("value1", peerOne.getConfiguration().get("key1"));
157     assertEquals("value2", peerOne.getConfiguration().get("key2"));
158 
159     admin.removePeer(ID_ONE);
160   }
161 
162   /**
163    * basic checks that when we add a peer that it is enabled, and that we can disable
164    * @throws Exception
165    */
166   @Test
167   public void testEnableDisable() throws Exception {
168     admin.addPeer(ID_ONE, KEY_ONE);
169     assertEquals(1, admin.getPeersCount());
170     assertTrue(admin.getPeerState(ID_ONE));
171     admin.disablePeer(ID_ONE);
172 
173     assertFalse(admin.getPeerState(ID_ONE));
174     try {
175       admin.getPeerState(ID_SECOND);
176     } catch (IllegalArgumentException iae) {
177       // OK!
178     }
179     admin.removePeer(ID_ONE);
180   }
181 
182   @Test
183   public void testGetTableCfsStr() {
184     // opposite of TestPerTableCFReplication#testParseTableCFsFromConfig()
185 
186     Map<TableName, List<String>> tabCFsMap = null;
187 
188     // 1. null or empty string, result should be null
189     assertEquals(null, ReplicationAdmin.getTableCfsStr(tabCFsMap));
190 
191 
192     // 2. single table: "tab1" / "tab2:cf1" / "tab3:cf1,cf3"
193     tabCFsMap = new TreeMap<TableName, List<String>>();
194     tabCFsMap.put(TableName.valueOf("tab1"), null);   // its table name is "tab1"
195     assertEquals("tab1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
196 
197     tabCFsMap = new TreeMap<TableName, List<String>>();
198     tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1"));
199     assertEquals("tab1:cf1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
200 
201     tabCFsMap = new TreeMap<TableName, List<String>>();
202     tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1", "cf3"));
203     assertEquals("tab1:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
204 
205     // 3. multiple tables: "tab1 ; tab2:cf1 ; tab3:cf1,cf3"
206     tabCFsMap = new TreeMap<TableName, List<String>>();
207     tabCFsMap.put(TableName.valueOf("tab1"), null);
208     tabCFsMap.put(TableName.valueOf("tab2"), Lists.newArrayList("cf1"));
209     tabCFsMap.put(TableName.valueOf("tab3"), Lists.newArrayList("cf1", "cf3"));
210     assertEquals("tab1;tab2:cf1;tab3:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
211   }
212 
213   @Test
214   public void testAppendPeerTableCFs() throws Exception {
215     // Add a valid peer
216     admin.addPeer(ID_ONE, KEY_ONE);
217 
218     admin.appendPeerTableCFs(ID_ONE, "t1");
219     assertEquals("t1", admin.getPeerTableCFs(ID_ONE));
220 
221     // append table t2 to replication
222     admin.appendPeerTableCFs(ID_ONE, "t2");
223     String peerTablesOne = admin.getPeerTableCFs(ID_ONE);
224 
225     // Different jdk's return different sort order for the tables. ( Not sure on why exactly )
226     //
227     // So instead of asserting that the string is exactly we
228     // assert that the string contains all tables and the needed separator.
229     assertTrue("Should contain t1", peerTablesOne.contains("t1"));
230     assertTrue("Should contain t2", peerTablesOne.contains("t2"));
231     assertTrue("Should contain ; as the seperator", peerTablesOne.contains(";"));
232 
233     // append table column family: f1 of t3 to replication
234     admin.appendPeerTableCFs(ID_ONE, "t3:f1");
235     String peerTablesTwo = admin.getPeerTableCFs(ID_ONE);
236     assertTrue("Should contain t1", peerTablesTwo.contains("t1"));
237     assertTrue("Should contain t2", peerTablesTwo.contains("t2"));
238     assertTrue("Should contain t3:f1", peerTablesTwo.contains("t3:f1"));
239     assertTrue("Should contain ; as the seperator", peerTablesTwo.contains(";"));
240     admin.removePeer(ID_ONE);
241   }
242 
243   @Test
244   public void testRemovePeerTableCFs() throws Exception {
245     // Add a valid peer
246     admin.addPeer(ID_ONE, KEY_ONE);
247     try {
248       admin.removePeerTableCFs(ID_ONE, "t3");
249       assertTrue(false);
250     } catch (ReplicationException e) {
251     }
252     assertEquals("", admin.getPeerTableCFs(ID_ONE));
253 
254     admin.setPeerTableCFs(ID_ONE, "t1;t2:cf1");
255     try {
256       admin.removePeerTableCFs(ID_ONE, "t3");
257       assertTrue(false);
258     } catch (ReplicationException e) {
259     }
260     assertEquals("t1;t2:cf1", admin.getPeerTableCFs(ID_ONE));
261 
262     try {
263       admin.removePeerTableCFs(ID_ONE, "t1:f1");
264       assertTrue(false);
265     } catch (ReplicationException e) {
266     }
267     admin.removePeerTableCFs(ID_ONE, "t1");
268     assertEquals("t2:cf1", admin.getPeerTableCFs(ID_ONE));
269 
270     try {
271       admin.removePeerTableCFs(ID_ONE, "t2");
272       assertTrue(false);
273     } catch (ReplicationException e) {
274     }
275     admin.removePeerTableCFs(ID_ONE, "t2:cf1");
276     assertEquals("", admin.getPeerTableCFs(ID_ONE));
277     admin.removePeer(ID_ONE);
278   }
279 }