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.assertArrayEquals;
021
022import java.util.concurrent.BlockingQueue;
023import java.util.concurrent.LinkedBlockingQueue;
024import java.util.concurrent.TimeUnit;
025import java.util.concurrent.TimeoutException;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellUtil;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.client.Table;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.ReplicationTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.wal.WAL;
036import org.apache.hadoop.hbase.wal.WALEdit;
037import org.junit.jupiter.api.AfterAll;
038import org.junit.jupiter.api.BeforeAll;
039import org.junit.jupiter.api.Tag;
040import org.junit.jupiter.api.Test;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044/**
045 * Confirm that the empty replication endpoint can work.
046 */
047@Tag(ReplicationTests.TAG)
048@Tag(MediumTests.TAG)
049public class TestVerifyCellsReplicationEndpoint {
050
051  private static final Logger LOG =
052    LoggerFactory.getLogger(TestVerifyCellsReplicationEndpoint.class);
053
054  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
055
056  private static final TableName TABLE_NAME = TableName.valueOf("empty");
057
058  private static final byte[] CF = Bytes.toBytes("family");
059
060  private static final byte[] CQ = Bytes.toBytes("qualifier");
061
062  private static final String PEER_ID = "empty";
063
064  private static final BlockingQueue<Cell> CELLS = new LinkedBlockingQueue<>();
065
066  public static final class EndpointForTest extends VerifyWALEntriesReplicationEndpoint {
067
068    @Override
069    public boolean replicate(ReplicateContext replicateContext) {
070      LOG.info(replicateContext.getEntries().toString());
071      replicateContext.entries.stream().map(WAL.Entry::getEdit).map(WALEdit::getCells)
072        .forEachOrdered(CELLS::addAll);
073      return super.replicate(replicateContext);
074    }
075  }
076
077  @BeforeAll
078  public static void setUp() throws Exception {
079    UTIL.startMiniCluster(3);
080    // notice that we do not need to set replication scope here, EmptyReplicationEndpoint take all
081    // edits no matter what the replications scope is.
082    UTIL.createTable(TABLE_NAME, CF);
083    UTIL.getAdmin().addReplicationPeer(PEER_ID,
084      ReplicationPeerConfig.newBuilder().setClusterKey("zk1:8888:/hbase")
085        .setReplicationEndpointImpl(EndpointForTest.class.getName()).build());
086  }
087
088  @AfterAll
089  public static void tearDown() throws Exception {
090    UTIL.shutdownMiniCluster();
091  }
092
093  @Test
094  public void test() throws Exception {
095    try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) {
096      for (int i = 0; i < 100; i++) {
097        table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
098      }
099    }
100    long lastNoCellTime = -1;
101    for (int i = 0; i < 100;) {
102      Cell cell = CELLS.poll();
103      if (cell == null) {
104        if (lastNoCellTime < 0) {
105          lastNoCellTime = System.nanoTime();
106        } else {
107          if (System.nanoTime() - lastNoCellTime >= TimeUnit.SECONDS.toNanos(30)) {
108            throw new TimeoutException("Timeout waiting for wal edit");
109          }
110        }
111        Thread.sleep(1000);
112        continue;
113      }
114      lastNoCellTime = -1;
115      if (!Bytes.equals(CF, CellUtil.cloneFamily(cell))) {
116        // meta edits, such as open/close/flush, etc. skip
117        continue;
118      }
119      assertArrayEquals(Bytes.toBytes(i), CellUtil.cloneRow(cell));
120      assertArrayEquals(CQ, CellUtil.cloneQualifier(cell));
121      assertArrayEquals(Bytes.toBytes(i), CellUtil.cloneValue(cell));
122      i++;
123    }
124  }
125}