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.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    /**
71     * Simple testing of adding and removing peers, basically shows that
72     * all interactions with ZK work
73     * @throws Exception
74     */
75    @Test
76    public void testAddRemovePeer() throws Exception {
77      // Add a valid peer
78      admin.addPeer(ID_ONE, KEY_ONE);
79      // try adding the same (fails)
80      try {
81        admin.addPeer(ID_ONE, KEY_ONE);
82      } catch (IllegalArgumentException iae) {
83        // OK!
84      }
85      assertEquals(1, admin.getPeersCount());
86      // Try to remove an inexisting peer
87      try {
88        admin.removePeer(ID_SECOND);
89        fail();
90      } catch (IllegalArgumentException iae) {
91        // OK!
92      }
93      assertEquals(1, admin.getPeersCount());
94      // Add a second since multi-slave is supported
95      try {
96        admin.addPeer(ID_SECOND, KEY_SECOND);
97      } catch (IllegalStateException iae) {
98        fail();
99      }
100     assertEquals(2, admin.getPeersCount());
101     // Remove the first peer we added
102     admin.removePeer(ID_ONE);
103     assertEquals(1, admin.getPeersCount());
104     admin.removePeer(ID_SECOND);
105     assertEquals(0, admin.getPeersCount());
106   }
107 
108   /**
109    * basic checks that when we add a peer that it is enabled, and that we can disable
110    * @throws Exception
111    */
112   @Test
113   public void testEnableDisable() throws Exception {
114     admin.addPeer(ID_ONE, KEY_ONE);
115     assertEquals(1, admin.getPeersCount());
116     assertTrue(admin.getPeerState(ID_ONE));
117     admin.disablePeer(ID_ONE);
118 
119     assertFalse(admin.getPeerState(ID_ONE));
120     try {
121       admin.getPeerState(ID_SECOND);
122     } catch (IllegalArgumentException iae) {
123       // OK!
124     }
125     admin.removePeer(ID_ONE);
126   }
127 
128   @Test
129   public void testGetTableCfsStr() {
130     // opposite of TestPerTableCFReplication#testParseTableCFsFromConfig()
131 
132     Map<TableName, List<String>> tabCFsMap = null;
133 
134     // 1. null or empty string, result should be null
135     assertEquals(null, ReplicationAdmin.getTableCfsStr(tabCFsMap));
136 
137 
138     // 2. single table: "tab1" / "tab2:cf1" / "tab3:cf1,cf3"
139     tabCFsMap = new TreeMap<TableName, List<String>>();
140     tabCFsMap.put(TableName.valueOf("tab1"), null);   // its table name is "tab1"
141     assertEquals("tab1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
142 
143     tabCFsMap = new TreeMap<TableName, List<String>>();
144     tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1"));
145     assertEquals("tab1:cf1", ReplicationAdmin.getTableCfsStr(tabCFsMap));
146 
147     tabCFsMap = new TreeMap<TableName, List<String>>();
148     tabCFsMap.put(TableName.valueOf("tab1"), Lists.newArrayList("cf1", "cf3"));
149     assertEquals("tab1:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
150 
151     // 3. multiple tables: "tab1 ; tab2:cf1 ; tab3:cf1,cf3"
152     tabCFsMap = new TreeMap<TableName, List<String>>();
153     tabCFsMap.put(TableName.valueOf("tab1"), null);
154     tabCFsMap.put(TableName.valueOf("tab2"), Lists.newArrayList("cf1"));
155     tabCFsMap.put(TableName.valueOf("tab3"), Lists.newArrayList("cf1", "cf3"));
156     assertEquals("tab1;tab2:cf1;tab3:cf1,cf3", ReplicationAdmin.getTableCfsStr(tabCFsMap));
157   }
158 
159   @Test
160   public void testAppendPeerTableCFs() throws Exception {
161     // Add a valid peer
162     admin.addPeer(ID_ONE, KEY_ONE);
163 
164     admin.appendPeerTableCFs(ID_ONE, "t1");
165     assertEquals("t1", admin.getPeerTableCFs(ID_ONE));
166 
167     // append table t2 to replication
168     admin.appendPeerTableCFs(ID_ONE, "t2");
169     assertEquals("t2;t1", admin.getPeerTableCFs(ID_ONE));
170 
171     // append table column family: f1 of t3 to replication
172     admin.appendPeerTableCFs(ID_ONE, "t3:f1");
173     assertEquals("t3:f1;t2;t1", admin.getPeerTableCFs(ID_ONE));
174     admin.removePeer(ID_ONE);
175   }
176 
177   @Test
178   public void testRemovePeerTableCFs() throws Exception {
179     // Add a valid peer
180     admin.addPeer(ID_ONE, KEY_ONE);
181     try {
182       admin.removePeerTableCFs(ID_ONE, "t3");
183       assertTrue(false);
184     } catch (ReplicationException e) {
185     }
186     assertEquals("", admin.getPeerTableCFs(ID_ONE));
187 
188     admin.setPeerTableCFs(ID_ONE, "t1;t2:cf1");
189     try {
190       admin.removePeerTableCFs(ID_ONE, "t3");
191       assertTrue(false);
192     } catch (ReplicationException e) {
193     }
194     assertEquals("t1;t2:cf1", admin.getPeerTableCFs(ID_ONE));
195 
196     try {
197       admin.removePeerTableCFs(ID_ONE, "t1:f1");
198       assertTrue(false);
199     } catch (ReplicationException e) {
200     }
201     admin.removePeerTableCFs(ID_ONE, "t1");
202     assertEquals("t2:cf1", admin.getPeerTableCFs(ID_ONE));
203 
204     try {
205       admin.removePeerTableCFs(ID_ONE, "t2");
206       assertTrue(false);
207     } catch (ReplicationException e) {
208     }
209     admin.removePeerTableCFs(ID_ONE, "t2:cf1");
210     assertEquals("", admin.getPeerTableCFs(ID_ONE));
211     admin.removePeer(ID_ONE);
212   }
213 }