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