View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.replication.master;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.Abortable;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.replication.ReplicationSerDeHelper;
30  import org.apache.hadoop.hbase.exceptions.DeserializationException;
31  import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
32  import org.apache.hadoop.hbase.testclassification.ReplicationTests;
33  import org.apache.hadoop.hbase.testclassification.SmallTests;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
36  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
37  import org.apache.zookeeper.KeeperException;
38  import org.junit.AfterClass;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  import java.util.List;
44  import java.util.Map;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertNull;
48  import static org.junit.Assert.assertTrue;
49  
50  @Category({ReplicationTests.class, SmallTests.class})
51  public class TestTableCFsUpdater extends TableCFsUpdater {
52  
53    private static final Log LOG = LogFactory.getLog(TestTableCFsUpdater.class);
54    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55  
56    private static ZooKeeperWatcher zkw = null;
57    private static Abortable abortable = null;
58  
59    public TestTableCFsUpdater() {
60      super(zkw, TEST_UTIL.getConfiguration(), abortable);
61    }
62  
63    @BeforeClass
64    public static void setUpBeforeClass() throws Exception {
65      TEST_UTIL.startMiniZKCluster();
66      Configuration conf = TEST_UTIL.getConfiguration();
67      abortable = new Abortable() {
68        @Override
69        public void abort(String why, Throwable e) {
70          LOG.info(why, e);
71        }
72  
73        @Override
74        public boolean isAborted() {
75          return false;
76        }
77      };
78      zkw = new ZooKeeperWatcher(conf, "TableCFs", abortable, true);
79    }
80  
81    @AfterClass
82    public static void tearDownAfterClass() throws Exception {
83      TEST_UTIL.shutdownMiniZKCluster();
84    }
85  
86    @Test
87    public void testUpgrade() throws KeeperException, InterruptedException,
88        DeserializationException {
89      String peerId = "1";
90      TableName tab1 = TableName.valueOf("table1");
91      TableName tab2 = TableName.valueOf("table2");
92      TableName tab3 = TableName.valueOf("table3");
93  
94      ReplicationPeerConfig rpc = new ReplicationPeerConfig();
95      rpc.setClusterKey(zkw.getQuorum());
96      String peerNode = getPeerNode(peerId);
97      ZKUtil.createWithParents(zkw, peerNode, ReplicationSerDeHelper.toByteArray(rpc));
98  
99      String tableCFs = "table1:cf1,cf2;table2:cf3;table3";
100     String tableCFsNode = getTableCFsNode(peerId);
101     LOG.info("create tableCFs :" + tableCFsNode + " for peerId=" + peerId);
102     ZKUtil.createWithParents(zkw, tableCFsNode , Bytes.toBytes(tableCFs));
103 
104     ReplicationPeerConfig actualRpc = ReplicationSerDeHelper.parsePeerFrom(ZKUtil.getData(zkw, peerNode));
105     String actualTableCfs = Bytes.toString(ZKUtil.getData(zkw, tableCFsNode));
106 
107     assertEquals(rpc.getClusterKey(), actualRpc.getClusterKey());
108     assertNull(actualRpc.getTableCFsMap());
109     assertEquals(tableCFs, actualTableCfs);
110 
111     peerId = "2";
112     rpc = new ReplicationPeerConfig();
113     rpc.setClusterKey(zkw.getQuorum());
114     peerNode = getPeerNode(peerId);
115     ZKUtil.createWithParents(zkw, peerNode, ReplicationSerDeHelper.toByteArray(rpc));
116 
117     tableCFs = "table1:cf1,cf3;table2:cf2";
118     tableCFsNode = getTableCFsNode(peerId);
119     LOG.info("create tableCFs :" + tableCFsNode + " for peerId=" + peerId);
120     ZKUtil.createWithParents(zkw, tableCFsNode , Bytes.toBytes(tableCFs));
121 
122     actualRpc = ReplicationSerDeHelper.parsePeerFrom(ZKUtil.getData(zkw, peerNode));
123     actualTableCfs = Bytes.toString(ZKUtil.getData(zkw, tableCFsNode));
124 
125     assertEquals(rpc.getClusterKey(), actualRpc.getClusterKey());
126     assertNull(actualRpc.getTableCFsMap());
127     assertEquals(tableCFs, actualTableCfs);
128 
129 
130     update();
131 
132     peerId = "1";
133     peerNode = getPeerNode(peerId);
134     actualRpc = ReplicationSerDeHelper.parsePeerFrom(ZKUtil.getData(zkw, peerNode));
135     assertEquals(rpc.getClusterKey(), actualRpc.getClusterKey());
136     Map<TableName, List<String>> tableNameListMap = actualRpc.getTableCFsMap();
137     assertEquals(3, tableNameListMap.size());
138     assertTrue(tableNameListMap.containsKey(tab1));
139     assertTrue(tableNameListMap.containsKey(tab2));
140     assertTrue(tableNameListMap.containsKey(tab3));
141     assertEquals(2, tableNameListMap.get(tab1).size());
142     assertEquals("cf1", tableNameListMap.get(tab1).get(0));
143     assertEquals("cf2", tableNameListMap.get(tab1).get(1));
144     assertEquals(1, tableNameListMap.get(tab2).size());
145     assertEquals("cf3", tableNameListMap.get(tab2).get(0));
146     assertNull(tableNameListMap.get(tab3));
147 
148 
149     peerId = "2";
150     peerNode = getPeerNode(peerId);
151     actualRpc = ReplicationSerDeHelper.parsePeerFrom(ZKUtil.getData(zkw, peerNode));
152     assertEquals(rpc.getClusterKey(), actualRpc.getClusterKey());
153     tableNameListMap = actualRpc.getTableCFsMap();
154     assertEquals(2, tableNameListMap.size());
155     assertTrue(tableNameListMap.containsKey(tab1));
156     assertTrue(tableNameListMap.containsKey(tab2));
157     assertEquals(2, tableNameListMap.get(tab1).size());
158     assertEquals("cf1", tableNameListMap.get(tab1).get(0));
159     assertEquals("cf3", tableNameListMap.get(tab1).get(1));
160     assertEquals(1, tableNameListMap.get(tab2).size());
161     assertEquals("cf2", tableNameListMap.get(tab2).get(0));
162   }
163 
164 }