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.mapreduce;
019
020import static org.junit.Assert.assertFalse;
021
022import org.apache.commons.lang3.ArrayUtils;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Table;
026import org.apache.hadoop.hbase.client.TableDescriptor;
027import org.junit.AfterClass;
028import org.junit.BeforeClass;
029import org.junit.Test;
030
031/**
032 * Test CopyTable between clusters
033 */
034public abstract class CopyTableToPeerClusterTestBase extends CopyTableTestBase {
035
036  protected static final HBaseTestingUtil UTIL1 = new HBaseTestingUtil();
037
038  protected static final HBaseTestingUtil UTIL2 = new HBaseTestingUtil();
039
040  @BeforeClass
041  public static void beforeClass() throws Exception {
042    UTIL1.startMiniCluster(3);
043    UTIL2.startMiniCluster(3);
044  }
045
046  @AfterClass
047  public static void afterClass() throws Exception {
048    UTIL1.shutdownMiniCluster();
049    UTIL2.shutdownMiniCluster();
050  }
051
052  @Override
053  protected Table createSourceTable(TableDescriptor desc) throws Exception {
054    return UTIL1.createTable(desc, null);
055  }
056
057  @Override
058  protected Table createTargetTable(TableDescriptor desc) throws Exception {
059    return UTIL2.createTable(desc, null);
060  }
061
062  @Override
063  protected void dropSourceTable(TableName tableName) throws Exception {
064    UTIL1.deleteTable(tableName);
065  }
066
067  @Override
068  protected void dropTargetTable(TableName tableName) throws Exception {
069    UTIL2.deleteTable(tableName);
070  }
071
072  @Override
073  protected String[] getPeerClusterOptions() throws Exception {
074    return new String[] { "--peer.uri=" + UTIL2.getRpcConnnectionURI() };
075  }
076
077  /**
078   * Simple end-to-end test
079   */
080  @Test
081  public void testCopyTable() throws Exception {
082    doCopyTableTest(UTIL1.getConfiguration(), false);
083  }
084
085  /**
086   * Simple end-to-end test on table with MOB
087   */
088  @Test
089  public void testCopyTableWithMob() throws Exception {
090    doCopyTableTestWithMob(UTIL1.getConfiguration(), false);
091  }
092
093  @Test
094  public void testStartStopRow() throws Exception {
095    testStartStopRow(UTIL1.getConfiguration());
096  }
097
098  /**
099   * Test copy of table from sourceTable to targetTable all rows from family a
100   */
101  @Test
102  public void testRenameFamily() throws Exception {
103    testRenameFamily(UTIL1.getConfiguration());
104  }
105
106  @Test
107  public void testBulkLoadNotSupported() throws Exception {
108    TableName tableName1 = TableName.valueOf(name.getMethodName() + "1");
109    TableName tableName2 = TableName.valueOf(name.getMethodName() + "2");
110    try (Table t1 = UTIL1.createTable(tableName1, FAMILY_A);
111      Table t2 = UTIL2.createTable(tableName2, FAMILY_A)) {
112      String[] args = ArrayUtils.addAll(getPeerClusterOptions(),
113        "--new.name=" + tableName2.getNameAsString(), "--bulkload", tableName1.getNameAsString());
114      assertFalse(runCopy(UTIL1.getConfiguration(), args));
115    } finally {
116      UTIL1.deleteTable(tableName1);
117      UTIL2.deleteTable(tableName2);
118    }
119  }
120
121  @Test
122  public void testSnapshotNotSupported() throws Exception {
123    TableName tableName1 = TableName.valueOf(name.getMethodName() + "1");
124    TableName tableName2 = TableName.valueOf(name.getMethodName() + "2");
125    String snapshot = tableName1.getNameAsString() + "_snapshot";
126    try (Table t1 = UTIL1.createTable(tableName1, FAMILY_A);
127      Table t2 = UTIL2.createTable(tableName2, FAMILY_A)) {
128      UTIL1.getAdmin().snapshot(snapshot, tableName1);
129      String[] args = ArrayUtils.addAll(getPeerClusterOptions(),
130        "--new.name=" + tableName2.getNameAsString(), "--snapshot", snapshot);
131      assertFalse(runCopy(UTIL1.getConfiguration(), args));
132    } finally {
133      UTIL1.getAdmin().deleteSnapshot(snapshot);
134      UTIL1.deleteTable(tableName1);
135      UTIL2.deleteTable(tableName2);
136    }
137  }
138}