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