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