001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.testing;
019
020import static org.junit.Assert.assertArrayEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.Waiter;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.Connection;
029import org.apache.hadoop.hbase.client.ConnectionFactory;
030import org.apache.hadoop.hbase.client.Get;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.client.TableDescriptor;
034import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
035import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.hadoop.hbase.zookeeper.ZKConfig;
038import org.junit.After;
039import org.junit.Before;
040import org.junit.Test;
041
042import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
043
044/**
045 * Setup two clusters for replication.
046 */
047public abstract class TestingHBaseClusterReplicationTestBase {
048
049  protected TestingHBaseCluster sourceCluster;
050
051  protected TestingHBaseCluster peerCluster;
052
053  private Connection sourceConn;
054
055  private Connection peerConn;
056
057  private TableName tableName = TableName.valueOf("test_rep");
058
059  private byte[] family = Bytes.toBytes("family");
060
061  private String peerId = "peer_id";
062
063  private String getPeerClusterKey() {
064    return ZKConfig.getZooKeeperClusterKey(peerCluster.getConf());
065  }
066
067  @Before
068  public void setUp() throws Exception {
069    startClusters();
070    sourceConn = ConnectionFactory.createConnection(sourceCluster.getConf());
071    peerConn = ConnectionFactory.createConnection(peerCluster.getConf());
072    TableDescriptor desc =
073      TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder
074        .newBuilder(family).setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build()).build();
075    try (Admin admin = sourceConn.getAdmin()) {
076      admin.createTable(desc);
077      admin.addReplicationPeer(peerId, ReplicationPeerConfig.newBuilder()
078        .setClusterKey(getPeerClusterKey()).setReplicateAllUserTables(true).build());
079    }
080    try (Admin admin = peerConn.getAdmin()) {
081      admin.createTable(desc);
082    }
083  }
084
085  @After
086  public void tearDown() throws Exception {
087    Closeables.close(sourceConn, true);
088    Closeables.close(peerConn, true);
089    if (sourceCluster != null) {
090      sourceCluster.stop();
091    }
092    if (peerCluster != null) {
093      peerCluster.stop();
094    }
095    stopClusters();
096  }
097
098  @Test
099  public void testReplication() throws IOException {
100    byte[] row = Bytes.toBytes("row");
101    byte[] qual = Bytes.toBytes("qual");
102    byte[] value = Bytes.toBytes("value");
103    try (Table sourceTable = sourceConn.getTable(tableName);
104      Table peerTable = peerConn.getTable(tableName);) {
105      sourceTable.put(new Put(row).addColumn(family, qual, value));
106      Waiter.waitFor(sourceCluster.getConf(), 30000,
107        () -> peerTable.exists(new Get(row).addColumn(family, qual)));
108      byte[] actual = peerTable.get(new Get(row)).getValue(family, qual);
109      assertArrayEquals(value, actual);
110    }
111  }
112
113  protected abstract void startClusters() throws Exception;
114
115  protected abstract void stopClusters() throws Exception;
116}