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.ReplicationPeer;
31  import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
32  import org.apache.hadoop.hbase.testclassification.MediumTests;
33  import org.junit.AfterClass;
34  import org.junit.BeforeClass;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  import com.google.common.collect.Lists;
39  
40  import static org.junit.Assert.assertEquals;
41  import static org.junit.Assert.assertFalse;
42  import static org.junit.Assert.assertNotNull;
43  import static org.junit.Assert.assertTrue;
44  import static org.junit.Assert.fail;
45  
46  
47  /**
48   * Unit testing of ReplicationAdmin
49   */
50  @Category(MediumTests.class)
51  public class TestReplicationAdmin {
52  
53    private static final Log LOG =
54        LogFactory.getLog(TestReplicationAdmin.class);
55    private final static HBaseTestingUtility TEST_UTIL =
56        new HBaseTestingUtility();
57  
58    private final String ID_ONE = "1";
59    private final String KEY_ONE = "127.0.0.1:2181:/hbase";
60    private final String ID_SECOND = "2";
61    private final String KEY_SECOND = "127.0.0.1:2181:/hbase2";
62  
63    private static ReplicationAdmin admin;
64  
65    /**
66     * @throws java.lang.Exception
67     */
68    @BeforeClass
69    public static void setUpBeforeClass() throws Exception {
70      TEST_UTIL.startMiniZKCluster();
71      Configuration conf = TEST_UTIL.getConfiguration();
72      conf.setBoolean(HConstants.REPLICATION_ENABLE_KEY, HConstants.REPLICATION_ENABLE_DEFAULT);
73      admin = new ReplicationAdmin(conf);
74    }
75  
76    @AfterClass
77    public static void tearDownAfterClass() throws Exception {
78      if (admin != null) {
79        admin.close();
80      }
81      TEST_UTIL.shutdownMiniZKCluster();
82    }
83  
84    /**
85     * Simple testing of adding and removing peers, basically shows that
86     * all interactions with ZK work
87     * @throws Exception
88     */
89    @Test
90    public void testAddRemovePeer() throws Exception {
91      // Add a valid peer
92      admin.addPeer(ID_ONE, KEY_ONE);
93      // try adding the same (fails)
94      try {
95        admin.addPeer(ID_ONE, KEY_ONE);
96      } catch (IllegalArgumentException iae) {
97        // OK!
98      }
99      assertEquals(1, admin.getPeersCount());
100     // Try to remove an inexisting peer
101     try {
102       admin.removePeer(ID_SECOND);
103       fail();
104     } catch (IllegalArgumentException iae) {
105       // OK!
106     }
107     assertEquals(1, admin.getPeersCount());
108     // Add a second since multi-slave is supported
109     try {
110       admin.addPeer(ID_SECOND, KEY_SECOND);
111     } catch (IllegalStateException iae) {
112       fail();
113     }
114     assertEquals(2, admin.getPeersCount());
115     // Remove the first peer we added
116     admin.removePeer(ID_ONE);
117     assertEquals(1, admin.getPeersCount());
118     admin.removePeer(ID_SECOND);
119     assertEquals(0, admin.getPeersCount());
120   }
121 
122   /**
123    * Tests that the peer configuration used by ReplicationAdmin contains all
124    * the peer's properties.
125    */
126   @Test
127   public void testPeerConfig() throws Exception {
128     ReplicationPeerConfig config = new ReplicationPeerConfig();
129     config.setClusterKey(KEY_ONE);
130     config.getConfiguration().put("key1", "value1");
131     config.getConfiguration().put("key2", "value2");
132     admin.addPeer(ID_ONE, config, null);
133 
134     List<ReplicationPeer> peers = admin.listReplicationPeers();
135     assertEquals(1, peers.size());
136     ReplicationPeer peerOne = peers.get(0);
137     assertNotNull(peerOne);
138     assertEquals("value1", peerOne.getConfiguration().get("key1"));
139     assertEquals("value2", peerOne.getConfiguration().get("key2"));
140 
141     admin.removePeer(ID_ONE);
142   }
143 
144   /**
145    * basic checks that when we add a peer that it is enabled, and that we can disable
146    * @throws Exception
147    */
148   @Test
149   public void testEnableDisable() throws Exception {
150     admin.addPeer(ID_ONE, KEY_ONE);
151     assertEquals(1, admin.getPeersCount());
152     assertTrue(admin.getPeerState(ID_ONE));
153     admin.disablePeer(ID_ONE);
154 
155     assertFalse(admin.getPeerState(ID_ONE));
156     try {
157       admin.getPeerState(ID_SECOND);
158     } catch (IllegalArgumentException iae) {
159       // OK!
160     }
161     admin.removePeer(ID_ONE);
162   }
163 
164   @Test
165   public void testGetTableCfsStr() {
166     // opposite of TestPerTableCFReplication#testParseTableCFsFromConfig()
167 
168     Map<TableName, List<String>> tabCFsMap = null;
169 
170     // 1. null or empty string, result should be null
171     assertEquals(null, ReplicationAdmin.getTableCfsStr(tabCFsMap));
172 
173 
174     // 2. single table: "tab1" / "tab2:cf1" / "tab3:cf1,cf3"
175     tabCFsMap = new TreeMap<TableName, List<String>>();
176     tabCFsMap.put(TableName.valueOf("tab1"), null);   // its table name is "tab1"
177     assertEquals("tab1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
178 
179     tabCFsMap = new TreeMap<TableName, List<String>>();
180     tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1"));
181     assertEquals("tab1:cf1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
182 
183     tabCFsMap = new TreeMap<TableName, List<String>>();
184     tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1", "cf3"));
185     assertEquals("tab1:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
186 
187     // 3. multiple tables: "tab1 ; tab2:cf1 ; tab3:cf1,cf3"
188     tabCFsMap = new TreeMap<TableName, List<String>>();
189     tabCFsMap.put(TableName.valueOf("tab1"), null);
190     tabCFsMap.put(TableName.valueOf("tab2"), Lists.newArrayList("cf1"));
191     tabCFsMap.put(TableName.valueOf("tab3"), Lists.newArrayList("cf1", "cf3"));
192     assertEquals("tab1;tab2:cf1;tab3:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
193   }
194 
195 }
196