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.util;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027import org.apache.hadoop.hbase.ClientMetaTableAccessor;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.MetaTableAccessor;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
034import org.apache.hadoop.hbase.client.Put;
035import org.apache.hadoop.hbase.client.RegionInfo;
036import org.apache.hadoop.hbase.client.RegionInfoBuilder;
037import org.apache.hadoop.hbase.client.Result;
038import org.apache.hadoop.hbase.client.ResultScanner;
039import org.apache.hadoop.hbase.client.Scan;
040import org.apache.hadoop.hbase.client.Table;
041import org.apache.hadoop.hbase.client.TableDescriptor;
042import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
043import org.apache.hadoop.hbase.master.RegionState;
044import org.apache.hadoop.hbase.replication.ReplicationBarrierFamilyFormat;
045import org.apache.hadoop.hbase.replication.ReplicationException;
046import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
047import org.apache.hadoop.hbase.replication.ReplicationQueueStorage;
048import org.apache.hadoop.hbase.replication.ReplicationStorageFactory;
049import org.apache.hadoop.hbase.testclassification.MediumTests;
050import org.apache.hadoop.hbase.testclassification.ReplicationTests;
051import org.apache.hadoop.hbase.util.hbck.HbckTestingUtil;
052import org.junit.AfterClass;
053import org.junit.BeforeClass;
054import org.junit.ClassRule;
055import org.junit.Test;
056import org.junit.experimental.categories.Category;
057
058import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableMap;
059
060@Category({ ReplicationTests.class, MediumTests.class })
061public class TestHBaseFsckCleanReplicationBarriers {
062  @ClassRule
063  public static final HBaseClassTestRule CLASS_RULE =
064    HBaseClassTestRule.forClass(TestHBaseFsckCleanReplicationBarriers.class);
065
066  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
067
068  private static String PEER_1 = "1", PEER_2 = "2";
069
070  private static ReplicationQueueStorage QUEUE_STORAGE;
071
072  private static String WAL_FILE_NAME = "test.wal";
073
074  private static String TABLE_NAME = "test";
075
076  private static String COLUMN_FAMILY = "info";
077
078  @BeforeClass
079  public static void setUp() throws Exception {
080    UTIL.startMiniCluster(1);
081    QUEUE_STORAGE = ReplicationStorageFactory.getReplicationQueueStorage(UTIL.getZooKeeperWatcher(),
082      UTIL.getConfiguration());
083    createPeer();
084    QUEUE_STORAGE.addWAL(UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName(), PEER_1,
085      WAL_FILE_NAME);
086    QUEUE_STORAGE.addWAL(UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName(), PEER_2,
087      WAL_FILE_NAME);
088  }
089
090  @AfterClass
091  public static void tearDown() throws Exception {
092    UTIL.shutdownMiniCluster();
093  }
094
095  @Test
096  public void testCleanReplicationBarrierWithNonExistTable()
097    throws ClassNotFoundException, IOException {
098    TableName tableName = TableName.valueOf(TABLE_NAME + "_non");
099    boolean cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);
100    assertFalse(cleaned);
101  }
102
103  @Test
104  public void testCleanReplicationBarrierWithDeletedTable() throws Exception {
105    TableName tableName = TableName.valueOf(TABLE_NAME + "_deleted");
106    List<RegionInfo> regionInfos = new ArrayList<>();
107    // only write some barriers into meta table
108
109    for (int i = 0; i < 110; i++) {
110      RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes(i))
111        .setEndKey(Bytes.toBytes(i + 1)).build();
112      regionInfos.add(regionInfo);
113      addStateAndBarrier(regionInfo, RegionState.State.OPEN, 10, 100);
114      updatePushedSeqId(regionInfo, 10);
115      assertEquals("check if there is lastPushedId", 10,
116        QUEUE_STORAGE.getLastSequenceId(regionInfo.getEncodedName(), PEER_1));
117      assertEquals("check if there is lastPushedId", 10,
118        QUEUE_STORAGE.getLastSequenceId(regionInfo.getEncodedName(), PEER_2));
119    }
120    Scan barrierScan = new Scan();
121    barrierScan.setCaching(100);
122    barrierScan.addFamily(HConstants.REPLICATION_BARRIER_FAMILY);
123    barrierScan
124      .withStartRow(ClientMetaTableAccessor.getTableStartRowForMeta(tableName,
125        ClientMetaTableAccessor.QueryType.REGION))
126      .withStopRow(ClientMetaTableAccessor.getTableStopRowForMeta(tableName,
127        ClientMetaTableAccessor.QueryType.REGION));
128    Result result;
129    try (ResultScanner scanner =
130      MetaTableAccessor.getMetaHTable(UTIL.getConnection()).getScanner(barrierScan)) {
131      while ((result = scanner.next()) != null) {
132        assertTrue(ReplicationBarrierFamilyFormat.getReplicationBarriers(result).length > 0);
133      }
134    }
135    boolean cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);
136    assertTrue(cleaned);
137    for (RegionInfo regionInfo : regionInfos) {
138      assertEquals("check if there is lastPushedId", -1,
139        QUEUE_STORAGE.getLastSequenceId(regionInfo.getEncodedName(), PEER_1));
140      assertEquals("check if there is lastPushedId", -1,
141        QUEUE_STORAGE.getLastSequenceId(regionInfo.getEncodedName(), PEER_2));
142    }
143    cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);
144    assertFalse(cleaned);
145    for (RegionInfo region : regionInfos) {
146      assertEquals(0, ReplicationBarrierFamilyFormat.getReplicationBarriers(UTIL.getConnection(),
147        region.getRegionName()).length);
148    }
149  }
150
151  @Test
152  public void testCleanReplicationBarrierWithExistTable() throws Exception {
153    TableName tableName = TableName.valueOf(TABLE_NAME);
154    String cf = COLUMN_FAMILY;
155    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
156      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build())
157      .setReplicationScope(HConstants.REPLICATION_SCOPE_LOCAL).build();
158    UTIL.createTable(tableDescriptor, Bytes.split(Bytes.toBytes(1), Bytes.toBytes(256), 123));
159    assertTrue(UTIL.getAdmin().getRegions(tableName).size() > 0);
160    for (RegionInfo region : UTIL.getAdmin().getRegions(tableName)) {
161      addStateAndBarrier(region, RegionState.State.OFFLINE, 10, 100);
162      updatePushedSeqId(region, 10);
163      assertEquals("check if there is lastPushedId", 10,
164        QUEUE_STORAGE.getLastSequenceId(region.getEncodedName(), PEER_1));
165      assertEquals("check if there is lastPushedId", 10,
166        QUEUE_STORAGE.getLastSequenceId(region.getEncodedName(), PEER_2));
167    }
168    boolean cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);
169    assertTrue(cleaned);
170    for (RegionInfo region : UTIL.getAdmin().getRegions(tableName)) {
171      assertEquals("check if there is lastPushedId", -1,
172        QUEUE_STORAGE.getLastSequenceId(region.getEncodedName(), PEER_1));
173      assertEquals("check if there is lastPushedId", -1,
174        QUEUE_STORAGE.getLastSequenceId(region.getEncodedName(), PEER_2));
175    }
176    cleaned = HbckTestingUtil.cleanReplicationBarrier(UTIL.getConfiguration(), tableName);
177    assertFalse(cleaned);
178    for (RegionInfo region : UTIL.getAdmin().getRegions(tableName)) {
179      assertEquals(0, ReplicationBarrierFamilyFormat.getReplicationBarriers(UTIL.getConnection(),
180        region.getRegionName()).length);
181    }
182  }
183
184  public static void createPeer() throws IOException {
185    ReplicationPeerConfig rpc = ReplicationPeerConfig.newBuilder()
186      .setClusterKey(UTIL.getClusterKey() + "-test").setSerial(true).build();
187    UTIL.getAdmin().addReplicationPeer(PEER_1, rpc);
188    UTIL.getAdmin().addReplicationPeer(PEER_2, rpc);
189  }
190
191  private void addStateAndBarrier(RegionInfo region, RegionState.State state, long... barriers)
192    throws IOException {
193    Put put = new Put(region.getRegionName(), EnvironmentEdgeManager.currentTime());
194    if (state != null) {
195      put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,
196        Bytes.toBytes(state.name()));
197    }
198    for (int i = 0; i < barriers.length; i++) {
199      put.addColumn(HConstants.REPLICATION_BARRIER_FAMILY, HConstants.SEQNUM_QUALIFIER,
200        put.getTimestamp() - barriers.length + i, Bytes.toBytes(barriers[i]));
201    }
202    try (Table table = UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {
203      table.put(put);
204    }
205  }
206
207  private void updatePushedSeqId(RegionInfo region, long seqId) throws ReplicationException {
208    QUEUE_STORAGE.setWALPosition(UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName(),
209      PEER_1, WAL_FILE_NAME, 10, ImmutableMap.of(region.getEncodedName(), seqId));
210    QUEUE_STORAGE.setWALPosition(UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName(),
211      PEER_2, WAL_FILE_NAME, 10, ImmutableMap.of(region.getEncodedName(), seqId));
212  }
213}