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 */
018
019package org.apache.hadoop.hbase.replication;
020
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.fail;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseConfiguration;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.Admin;
034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
035import org.apache.hadoop.hbase.client.Put;
036import org.apache.hadoop.hbase.client.Result;
037import org.apache.hadoop.hbase.client.ResultScanner;
038import org.apache.hadoop.hbase.client.Scan;
039import org.apache.hadoop.hbase.client.Table;
040import org.apache.hadoop.hbase.client.TableDescriptor;
041import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
042import org.apache.hadoop.hbase.mapreduce.replication.VerifyReplication;
043import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
044import org.apache.hadoop.hbase.testclassification.LargeTests;
045import org.apache.hadoop.hbase.testclassification.ReplicationTests;
046import org.apache.hadoop.hbase.util.Bytes;
047import org.apache.hadoop.hbase.util.CommonFSUtils;
048import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
049import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
050import org.apache.hadoop.mapreduce.Job;
051import org.junit.AfterClass;
052import org.junit.Assert;
053import org.junit.BeforeClass;
054import org.junit.ClassRule;
055import org.junit.Test;
056import org.junit.experimental.categories.Category;
057import org.slf4j.Logger;
058import org.slf4j.LoggerFactory;
059
060import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
061import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
062
063@Category({ ReplicationTests.class, LargeTests.class })
064public class TestVerifyReplicationCrossDiffHdfs {
065  @ClassRule
066  public static final HBaseClassTestRule CLASS_RULE =
067      HBaseClassTestRule.forClass(TestVerifyReplicationCrossDiffHdfs.class);
068
069  private static final Logger LOG =
070      LoggerFactory.getLogger(TestVerifyReplicationCrossDiffHdfs.class);
071
072  private static HBaseTestingUtil util1;
073  private static HBaseTestingUtil util2;
074  private static HBaseTestingUtil mapReduceUtil = new HBaseTestingUtil();
075
076  private static Configuration conf1 = HBaseConfiguration.create();
077  private static Configuration conf2;
078
079  private static final byte[] FAMILY = Bytes.toBytes("f");
080  private static final byte[] QUALIFIER = Bytes.toBytes("q");
081  private static final String PEER_ID = "1";
082  private static final TableName TABLE_NAME = TableName.valueOf("testVerifyRepCrossDiffHDFS");
083
084  @BeforeClass
085  public static void setUpBeforeClass() throws Exception {
086    conf1.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
087    util1 = new HBaseTestingUtil(conf1);
088    util1.startMiniZKCluster();
089    MiniZooKeeperCluster miniZK = util1.getZkCluster();
090    conf1 = util1.getConfiguration();
091
092    conf2 = HBaseConfiguration.create(conf1);
093    conf2.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/2");
094    util2 = new HBaseTestingUtil(conf2);
095    util2.setZkCluster(miniZK);
096
097    util1.startMiniCluster();
098    util2.startMiniCluster();
099
100    createTestingTable(util1.getAdmin());
101    createTestingTable(util2.getAdmin());
102    addTestingPeer();
103
104    LOG.info("Start to load some data to source cluster.");
105    loadSomeData();
106
107    LOG.info("Start mini MapReduce cluster.");
108    mapReduceUtil.setZkCluster(miniZK);
109    mapReduceUtil.startMiniMapReduceCluster();
110  }
111
112  private static void createTestingTable(Admin admin) throws IOException {
113    TableDescriptor table = TableDescriptorBuilder.newBuilder(TABLE_NAME)
114        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).setMaxVersions(100)
115            .setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build())
116        .build();
117    admin.createTable(table);
118  }
119
120  private static void addTestingPeer() throws IOException {
121    ReplicationPeerConfig rpc = ReplicationPeerConfig.newBuilder()
122        .setClusterKey(util2.getClusterKey()).setReplicateAllUserTables(false)
123        .setTableCFsMap(ImmutableMap.of(TABLE_NAME, ImmutableList.of())).build();
124    util1.getAdmin().addReplicationPeer(PEER_ID, rpc);
125  }
126
127  private static void loadSomeData() throws IOException, InterruptedException {
128    int numOfRows = 10;
129    try (Table table = util1.getConnection().getTable(TABLE_NAME)) {
130      for (int i = 0; i < numOfRows; i++) {
131        table.put(new Put(Bytes.toBytes(i)).addColumn(FAMILY, QUALIFIER, Bytes.toBytes(i)));
132      }
133    }
134    // Wait some time until the peer received those rows.
135    Result[] results = null;
136    try (Table table = util2.getConnection().getTable(TABLE_NAME)) {
137      for (int i = 0; i < 100; i++) {
138        try (ResultScanner rs = table.getScanner(new Scan())) {
139          results = rs.next(numOfRows);
140          if (results == null || results.length < numOfRows) {
141            LOG.info("Retrying, wait until the peer received all the rows, currentRows:"
142                + (results == null ? 0 : results.length));
143            Thread.sleep(100);
144          }
145        }
146      }
147    }
148    Assert.assertNotNull(results);
149    Assert.assertEquals(10, results.length);
150  }
151
152  @AfterClass
153  public static void tearDownClass() throws Exception {
154    if (mapReduceUtil != null) {
155      mapReduceUtil.shutdownMiniCluster();
156    }
157    if (util2 != null) {
158      util2.shutdownMiniCluster();
159    }
160    if (util1 != null) {
161      util1.shutdownMiniCluster();
162    }
163  }
164
165  @Test
166  public void testVerifyRepBySnapshot() throws Exception {
167    Path rootDir = CommonFSUtils.getRootDir(conf1);
168    FileSystem fs = rootDir.getFileSystem(conf1);
169    String sourceSnapshotName = "sourceSnapshot-" + EnvironmentEdgeManager.currentTime();
170    SnapshotTestingUtils.createSnapshotAndValidate(util1.getAdmin(), TABLE_NAME,
171        Bytes.toString(FAMILY), sourceSnapshotName, rootDir, fs, true);
172
173    // Take target snapshot
174    Path peerRootDir = CommonFSUtils.getRootDir(conf2);
175    FileSystem peerFs = peerRootDir.getFileSystem(conf2);
176    String peerSnapshotName = "peerSnapshot-" + EnvironmentEdgeManager.currentTime();
177    SnapshotTestingUtils.createSnapshotAndValidate(util2.getAdmin(), TABLE_NAME,
178        Bytes.toString(FAMILY), peerSnapshotName, peerRootDir, peerFs, true);
179
180    String peerFSAddress = peerFs.getUri().toString();
181    String temPath1 = new Path(fs.getUri().toString(), "/tmp1").toString();
182    String temPath2 = "/tmp2";
183
184    String[] args = new String[] { "--sourceSnapshotName=" + sourceSnapshotName,
185      "--sourceSnapshotTmpDir=" + temPath1, "--peerSnapshotName=" + peerSnapshotName,
186      "--peerSnapshotTmpDir=" + temPath2, "--peerFSAddress=" + peerFSAddress,
187      "--peerHBaseRootAddress=" + CommonFSUtils.getRootDir(conf2), PEER_ID, TABLE_NAME.toString() };
188
189    // Use the yarn's config override the source cluster's config.
190    Configuration newConf = HBaseConfiguration.create(conf1);
191    HBaseConfiguration.merge(newConf, mapReduceUtil.getConfiguration());
192    newConf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/1");
193    CommonFSUtils.setRootDir(newConf, CommonFSUtils.getRootDir(conf1));
194    Job job = new VerifyReplication().createSubmittableJob(newConf, args);
195    if (job == null) {
196      fail("Job wasn't created, see the log");
197    }
198    if (!job.waitForCompletion(true)) {
199      fail("Job failed, see the log");
200    }
201    assertEquals(10,
202      job.getCounters().findCounter(VerifyReplication.Verifier.Counters.GOODROWS).getValue());
203    assertEquals(0,
204      job.getCounters().findCounter(VerifyReplication.Verifier.Counters.BADROWS).getValue());
205  }
206}