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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.when;
024
025import java.util.ArrayList;
026import java.util.List;
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.KeyValue;
032import org.apache.hadoop.hbase.KeyValue.Type;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.replication.ReplicationEndpoint.Context;
035import org.apache.hadoop.hbase.replication.ReplicationPeer;
036import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.testclassification.ReplicationTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
041import org.apache.hadoop.hbase.wal.WAL.Entry;
042import org.apache.hadoop.hbase.wal.WALEdit;
043import org.apache.hadoop.hbase.wal.WALKeyImpl;
044import org.junit.AfterClass;
045import org.junit.BeforeClass;
046import org.junit.ClassRule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049
050import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
051
052/**
053 * Tests {@link HBaseInterClusterReplicationEndpoint#filterNotExistColumnFamilyEdits(List)} and
054 * {@link HBaseInterClusterReplicationEndpoint#filterNotExistTableEdits(List)}
055 */
056@Category({ ReplicationTests.class, MediumTests.class })
057public class TestHBaseInterClusterReplicationEndpointFilterEdits {
058
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE =
061    HBaseClassTestRule.forClass(TestHBaseInterClusterReplicationEndpointFilterEdits.class);
062
063  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
064
065  private static HBaseInterClusterReplicationEndpoint endpoint;
066
067  private static final TableName TABLE1 = TableName.valueOf("T1");
068  private static final TableName TABLE2 = TableName.valueOf("T2");
069
070  private static final byte[] FAMILY = Bytes.toBytes("CF");
071  private static final byte[] NON_EXISTING_FAMILY = Bytes.toBytes("NECF");
072  private static final byte[] QUALIFIER = Bytes.toBytes("Q");
073  private static final byte[] ROW = Bytes.toBytes("r");
074  private static final byte[] VALUE = Bytes.toBytes("v");
075
076  @BeforeClass
077  public static void setUpBeforeClass() throws Exception {
078    UTIL.startMiniCluster();
079    ReplicationPeer replicationPeer = mock(ReplicationPeer.class);
080    ReplicationPeerConfig rpc = mock(ReplicationPeerConfig.class);
081    when(rpc.isSerial()).thenReturn(false);
082    when(replicationPeer.getPeerConfig()).thenReturn(rpc);
083    Context context = new Context(null, UTIL.getConfiguration(), UTIL.getConfiguration(), null,
084      null, null, replicationPeer, null, null, null);
085    endpoint = new HBaseInterClusterReplicationEndpoint();
086    endpoint.init(context);
087
088    UTIL.createTable(TABLE1, FAMILY);
089  }
090
091  @AfterClass
092  public static void tearDownAfterClass() throws Exception {
093    UTIL.shutdownMiniCluster();
094  }
095
096  @Test
097  public void testFilterNotExistColumnFamilyEdits() {
098    List<List<Entry>> entryList = new ArrayList<>();
099    // should be filtered
100    Cell c1 = new KeyValue(ROW, NON_EXISTING_FAMILY, QUALIFIER,
101      EnvironmentEdgeManager.currentTime(), Type.Put, VALUE);
102    Entry e1 = new Entry(new WALKeyImpl(new byte[32], TABLE1, EnvironmentEdgeManager.currentTime()),
103      new WALEdit().add(c1));
104    entryList.add(Lists.newArrayList(e1));
105    // should be kept
106    Cell c2 =
107      new KeyValue(ROW, FAMILY, QUALIFIER, EnvironmentEdgeManager.currentTime(), Type.Put, VALUE);
108    Entry e2 = new Entry(new WALKeyImpl(new byte[32], TABLE1, EnvironmentEdgeManager.currentTime()),
109      new WALEdit().add(c2));
110    entryList.add(Lists.newArrayList(e2, e1));
111    List<List<Entry>> filtered = endpoint.filterNotExistColumnFamilyEdits(entryList);
112    assertEquals(1, filtered.size());
113    assertEquals(1, filtered.get(0).get(0).getEdit().getCells().size());
114    Cell cell = filtered.get(0).get(0).getEdit().getCells().get(0);
115    assertTrue(CellUtil.matchingFamily(cell, FAMILY));
116  }
117
118  @Test
119  public void testFilterNotExistTableEdits() {
120    List<List<Entry>> entryList = new ArrayList<>();
121    // should be filtered
122    Cell c1 =
123      new KeyValue(ROW, FAMILY, QUALIFIER, EnvironmentEdgeManager.currentTime(), Type.Put, VALUE);
124    Entry e1 = new Entry(new WALKeyImpl(new byte[32], TABLE2, EnvironmentEdgeManager.currentTime()),
125      new WALEdit().add(c1));
126    entryList.add(Lists.newArrayList(e1));
127    // should be kept
128    Cell c2 =
129      new KeyValue(ROW, FAMILY, QUALIFIER, EnvironmentEdgeManager.currentTime(), Type.Put, VALUE);
130    Entry e2 = new Entry(new WALKeyImpl(new byte[32], TABLE1, EnvironmentEdgeManager.currentTime()),
131      new WALEdit().add(c2));
132    entryList.add(Lists.newArrayList(e2));
133    List<List<Entry>> filtered = endpoint.filterNotExistTableEdits(entryList);
134    assertEquals(1, filtered.size());
135    Entry entry = filtered.get(0).get(0);
136    assertEquals(1, entry.getEdit().getCells().size());
137    assertEquals(TABLE1, entry.getKey().getTableName());
138  }
139
140}