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.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023import static org.junit.Assert.fail;
024
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.Cell;
028import org.apache.hadoop.hbase.CellUtil;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
034import org.apache.hadoop.hbase.client.Connection;
035import org.apache.hadoop.hbase.client.ConnectionFactory;
036import org.apache.hadoop.hbase.client.Delete;
037import org.apache.hadoop.hbase.client.Put;
038import org.apache.hadoop.hbase.client.Result;
039import org.apache.hadoop.hbase.client.ResultScanner;
040import org.apache.hadoop.hbase.client.Scan;
041import org.apache.hadoop.hbase.client.Table;
042import org.apache.hadoop.hbase.client.TableDescriptor;
043import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
044import org.apache.hadoop.hbase.mapreduce.replication.VerifyReplication;
045import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
046import org.apache.hadoop.hbase.testclassification.LargeTests;
047import org.apache.hadoop.hbase.testclassification.ReplicationTests;
048import org.apache.hadoop.hbase.util.Bytes;
049import org.apache.hadoop.hbase.util.CommonFSUtils;
050import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
051import org.junit.AfterClass;
052import org.junit.Before;
053import org.junit.BeforeClass;
054import org.junit.ClassRule;
055import org.junit.Rule;
056import org.junit.Test;
057import org.junit.experimental.categories.Category;
058import org.junit.rules.TestName;
059import org.slf4j.Logger;
060import org.slf4j.LoggerFactory;
061
062import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
063
064/**
065 * We moved some of {@link TestVerifyReplication}'s tests here because it could take too long to
066 * complete. In here we have miscellaneous.
067 */
068@Category({ ReplicationTests.class, LargeTests.class })
069public class TestVerifyReplicationAdjunct extends TestReplicationBase {
070
071  @ClassRule
072  public static final HBaseClassTestRule CLASS_RULE =
073      HBaseClassTestRule.forClass(TestVerifyReplicationAdjunct.class);
074
075  private static final Logger LOG = LoggerFactory.getLogger(TestVerifyReplicationAdjunct.class);
076
077  private static final String PEER_ID = "2";
078  private static final TableName peerTableName = TableName.valueOf("peerTest");
079  private static Table htable3;
080
081  @Rule
082  public TestName name = new TestName();
083
084  @Before
085  public void setUp() throws Exception {
086    cleanUp();
087    UTIL2.deleteTableData(peerTableName);
088  }
089
090  @BeforeClass
091  public static void setUpBeforeClass() throws Exception {
092    TestReplicationBase.setUpBeforeClass();
093    TableDescriptor peerTable = TableDescriptorBuilder.newBuilder(peerTableName).setColumnFamily(
094                    ColumnFamilyDescriptorBuilder.newBuilder(noRepfamName).setMaxVersions(100)
095                            .build()).build();
096    Connection connection2 = ConnectionFactory.createConnection(CONF2);
097    try (Admin admin2 = connection2.getAdmin()) {
098      admin2.createTable(peerTable, HBaseTestingUtil.KEYS_FOR_HBA_CREATE_TABLE);
099    }
100    htable3 = connection2.getTable(peerTableName);
101  }
102
103  // VerifyReplication should honor versions option
104  @Test
105  public void testHBase14905() throws Exception {
106    // normal Batch tests
107    byte[] qualifierName = Bytes.toBytes("f1");
108    Put put = new Put(Bytes.toBytes("r1"));
109    long ts = EnvironmentEdgeManager.currentTime();
110    put.addColumn(famName, qualifierName, ts + 1, Bytes.toBytes("v1002"));
111    htable1.put(put);
112    put.addColumn(famName, qualifierName, ts + 2, Bytes.toBytes("v1001"));
113    htable1.put(put);
114    put.addColumn(famName, qualifierName, ts + 3, Bytes.toBytes("v1112"));
115    htable1.put(put);
116
117    Scan scan = new Scan();
118    scan.readVersions(100);
119    ResultScanner scanner1 = htable1.getScanner(scan);
120    Result[] res1 = scanner1.next(1);
121    scanner1.close();
122
123    assertEquals(1, res1.length);
124    assertEquals(3, res1[0].getColumnCells(famName, qualifierName).size());
125
126    for (int i = 0; i < NB_RETRIES; i++) {
127      scan = new Scan();
128      scan.readVersions(100);
129      scanner1 = htable2.getScanner(scan);
130      res1 = scanner1.next(1);
131      scanner1.close();
132      if (res1.length != 1) {
133        LOG.info("Only got " + res1.length + " rows");
134        Thread.sleep(SLEEP_TIME);
135      } else {
136        int cellNumber = res1[0].getColumnCells(famName, Bytes.toBytes("f1")).size();
137        if (cellNumber != 3) {
138          LOG.info("Only got " + cellNumber + " cells");
139          Thread.sleep(SLEEP_TIME);
140        } else {
141          break;
142        }
143      }
144      if (i == NB_RETRIES - 1) {
145        fail("Waited too much time for normal batch replication");
146      }
147    }
148
149    put.addColumn(famName, qualifierName, ts + 4, Bytes.toBytes("v1111"));
150    htable2.put(put);
151    put.addColumn(famName, qualifierName, ts + 5, Bytes.toBytes("v1112"));
152    htable2.put(put);
153
154    scan = new Scan();
155    scan.readVersions(100);
156    scanner1 = htable2.getScanner(scan);
157    res1 = scanner1.next(NB_ROWS_IN_BATCH);
158    scanner1.close();
159
160    assertEquals(1, res1.length);
161    assertEquals(5, res1[0].getColumnCells(famName, qualifierName).size());
162
163    String[] args = new String[] { "--versions=100", PEER_ID, tableName.getNameAsString() };
164    TestVerifyReplication.runVerifyReplication(args, 0, 1);
165  }
166
167  // VerifyReplication should honor versions option
168  @Test
169  public void testVersionMismatchHBase14905() throws Exception {
170    // normal Batch tests
171    byte[] qualifierName = Bytes.toBytes("f1");
172    Put put = new Put(Bytes.toBytes("r1"));
173    long ts = EnvironmentEdgeManager.currentTime();
174    put.addColumn(famName, qualifierName, ts + 1, Bytes.toBytes("v1"));
175    htable1.put(put);
176    put.addColumn(famName, qualifierName, ts + 2, Bytes.toBytes("v2"));
177    htable1.put(put);
178    put.addColumn(famName, qualifierName, ts + 3, Bytes.toBytes("v3"));
179    htable1.put(put);
180
181    Scan scan = new Scan();
182    scan.readVersions(100);
183    ResultScanner scanner1 = htable1.getScanner(scan);
184    Result[] res1 = scanner1.next(1);
185    scanner1.close();
186
187    assertEquals(1, res1.length);
188    assertEquals(3, res1[0].getColumnCells(famName, qualifierName).size());
189
190    for (int i = 0; i < NB_RETRIES; i++) {
191      scan = new Scan();
192      scan.readVersions(100);
193      scanner1 = htable2.getScanner(scan);
194      res1 = scanner1.next(1);
195      scanner1.close();
196      if (res1.length != 1) {
197        LOG.info("Only got " + res1.length + " rows");
198        Thread.sleep(SLEEP_TIME);
199      } else {
200        int cellNumber = res1[0].getColumnCells(famName, Bytes.toBytes("f1")).size();
201        if (cellNumber != 3) {
202          LOG.info("Only got " + cellNumber + " cells");
203          Thread.sleep(SLEEP_TIME);
204        } else {
205          break;
206        }
207      }
208      if (i == NB_RETRIES - 1) {
209        fail("Waited too much time for normal batch replication");
210      }
211    }
212
213    try {
214      // Disabling replication and modifying the particular version of the cell to validate the
215      // feature.
216      hbaseAdmin.disableReplicationPeer(PEER_ID);
217      Put put2 = new Put(Bytes.toBytes("r1"));
218      put2.addColumn(famName, qualifierName, ts + 2, Bytes.toBytes("v99"));
219      htable2.put(put2);
220
221      scan = new Scan();
222      scan.readVersions(100);
223      scanner1 = htable2.getScanner(scan);
224      res1 = scanner1.next(NB_ROWS_IN_BATCH);
225      scanner1.close();
226      assertEquals(1, res1.length);
227      assertEquals(3, res1[0].getColumnCells(famName, qualifierName).size());
228
229      String[] args = new String[] { "--versions=100", PEER_ID, tableName.getNameAsString() };
230      TestVerifyReplication.runVerifyReplication(args, 0, 1);
231    } finally {
232      hbaseAdmin.enableReplicationPeer(PEER_ID);
233    }
234  }
235
236  @Test
237  public void testVerifyReplicationPrefixFiltering() throws Exception {
238    final byte[] prefixRow = Bytes.toBytes("prefixrow");
239    final byte[] prefixRow2 = Bytes.toBytes("secondrow");
240    loadData("prefixrow", prefixRow);
241    loadData("secondrow", prefixRow2);
242    loadData("aaa", row);
243    loadData("zzz", row);
244    waitForReplication(NB_ROWS_IN_BATCH * 4, NB_RETRIES * 4);
245    String[] args =
246        new String[] { "--row-prefixes=prefixrow,secondrow", PEER_ID, tableName.getNameAsString() };
247    TestVerifyReplication.runVerifyReplication(args, NB_ROWS_IN_BATCH * 2, 0);
248  }
249
250  @Test
251  public void testVerifyReplicationSnapshotArguments() {
252    String[] args =
253        new String[] { "--sourceSnapshotName=snapshot1", "2", tableName.getNameAsString() };
254    assertFalse(Lists.newArrayList(args).toString(), new VerifyReplication().doCommandLine(args));
255
256    args = new String[] { "--sourceSnapshotTmpDir=tmp", "2", tableName.getNameAsString() };
257    assertFalse(Lists.newArrayList(args).toString(), new VerifyReplication().doCommandLine(args));
258
259    args = new String[] { "--sourceSnapshotName=snapshot1", "--sourceSnapshotTmpDir=tmp", "2",
260        tableName.getNameAsString() };
261    assertTrue(Lists.newArrayList(args).toString(), new VerifyReplication().doCommandLine(args));
262
263    args = new String[] { "--peerSnapshotName=snapshot1", "2", tableName.getNameAsString() };
264    assertFalse(Lists.newArrayList(args).toString(), new VerifyReplication().doCommandLine(args));
265
266    args = new String[] { "--peerSnapshotTmpDir=/tmp/", "2", tableName.getNameAsString() };
267    assertFalse(Lists.newArrayList(args).toString(), new VerifyReplication().doCommandLine(args));
268
269    args = new String[] { "--peerSnapshotName=snapshot1", "--peerSnapshotTmpDir=/tmp/",
270        "--peerFSAddress=tempfs", "--peerHBaseRootAddress=hdfs://tempfs:50070/hbase/", "2",
271        tableName.getNameAsString() };
272    assertTrue(Lists.newArrayList(args).toString(), new VerifyReplication().doCommandLine(args));
273
274    args = new String[] { "--sourceSnapshotName=snapshot1", "--sourceSnapshotTmpDir=/tmp/",
275        "--peerSnapshotName=snapshot2", "--peerSnapshotTmpDir=/tmp/", "--peerFSAddress=tempfs",
276        "--peerHBaseRootAddress=hdfs://tempfs:50070/hbase/", "2", tableName.getNameAsString() };
277
278    assertTrue(Lists.newArrayList(args).toString(), new VerifyReplication().doCommandLine(args));
279  }
280
281  @Test
282  public void testVerifyReplicationWithSnapshotSupport() throws Exception {
283    // Populate the tables, at the same time it guarantees that the tables are
284    // identical since it does the check
285    runSmallBatchTest();
286
287    // Take source and target tables snapshot
288    Path rootDir = CommonFSUtils.getRootDir(CONF1);
289    FileSystem fs = rootDir.getFileSystem(CONF1);
290    String sourceSnapshotName = "sourceSnapshot-" + EnvironmentEdgeManager.currentTime();
291    SnapshotTestingUtils.createSnapshotAndValidate(UTIL1.getAdmin(), tableName,
292      Bytes.toString(famName), sourceSnapshotName, rootDir, fs, true);
293
294    // Take target snapshot
295    Path peerRootDir = CommonFSUtils.getRootDir(CONF2);
296    FileSystem peerFs = peerRootDir.getFileSystem(CONF2);
297    String peerSnapshotName = "peerSnapshot-" + EnvironmentEdgeManager.currentTime();
298    SnapshotTestingUtils.createSnapshotAndValidate(UTIL2.getAdmin(), tableName,
299      Bytes.toString(famName), peerSnapshotName, peerRootDir, peerFs, true);
300
301    String peerFSAddress = peerFs.getUri().toString();
302    String temPath1 = UTIL1.getRandomDir().toString();
303    String temPath2 = "/tmp" + EnvironmentEdgeManager.currentTime();
304
305    String[] args = new String[] { "--sourceSnapshotName=" + sourceSnapshotName,
306      "--sourceSnapshotTmpDir=" + temPath1, "--peerSnapshotName=" + peerSnapshotName,
307      "--peerSnapshotTmpDir=" + temPath2, "--peerFSAddress=" + peerFSAddress,
308      "--peerHBaseRootAddress=" + CommonFSUtils.getRootDir(CONF2), "2",
309      tableName.getNameAsString() };
310    TestVerifyReplication.runVerifyReplication(args, NB_ROWS_IN_BATCH, 0);
311    TestVerifyReplication.checkRestoreTmpDir(CONF1, temPath1, 1);
312    TestVerifyReplication.checkRestoreTmpDir(CONF2, temPath2, 1);
313
314    Scan scan = new Scan();
315    ResultScanner rs = htable2.getScanner(scan);
316    Put put = null;
317    for (Result result : rs) {
318      put = new Put(result.getRow());
319      Cell firstVal = result.rawCells()[0];
320      put.addColumn(CellUtil.cloneFamily(firstVal), CellUtil.cloneQualifier(firstVal),
321        Bytes.toBytes("diff data"));
322      htable2.put(put);
323    }
324    Delete delete = new Delete(put.getRow());
325    htable2.delete(delete);
326
327    sourceSnapshotName = "sourceSnapshot-" + EnvironmentEdgeManager.currentTime();
328    SnapshotTestingUtils.createSnapshotAndValidate(UTIL1.getAdmin(), tableName,
329      Bytes.toString(famName), sourceSnapshotName, rootDir, fs, true);
330
331    peerSnapshotName = "peerSnapshot-" + EnvironmentEdgeManager.currentTime();
332    SnapshotTestingUtils.createSnapshotAndValidate(UTIL2.getAdmin(), tableName,
333      Bytes.toString(famName), peerSnapshotName, peerRootDir, peerFs, true);
334
335    args = new String[] { "--sourceSnapshotName=" + sourceSnapshotName,
336      "--sourceSnapshotTmpDir=" + temPath1, "--peerSnapshotName=" + peerSnapshotName,
337      "--peerSnapshotTmpDir=" + temPath2, "--peerFSAddress=" + peerFSAddress,
338      "--peerHBaseRootAddress=" + CommonFSUtils.getRootDir(CONF2), "2",
339      tableName.getNameAsString() };
340    TestVerifyReplication.runVerifyReplication(args, 0, NB_ROWS_IN_BATCH);
341    TestVerifyReplication.checkRestoreTmpDir(CONF1, temPath1, 2);
342    TestVerifyReplication.checkRestoreTmpDir(CONF2, temPath2, 2);
343  }
344
345  @AfterClass
346  public static void tearDownAfterClass() throws Exception {
347    htable3.close();
348    TestReplicationBase.tearDownAfterClass();
349  }
350}