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