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