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