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.client;
019
020import java.io.IOException;
021import java.io.InterruptedIOException;
022import java.util.Optional;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.coprocessor.ObserverContext;
028import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
029import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
030import org.apache.hadoop.hbase.coprocessor.RegionObserver;
031import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
032import org.apache.hadoop.hbase.mob.MobConstants;
033import org.apache.hadoop.hbase.regionserver.FlushLifeCycleTracker;
034import org.apache.hadoop.hbase.snapshot.MobSnapshotTestingUtils;
035import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
036import org.apache.hadoop.hbase.testclassification.ClientTests;
037import org.apache.hadoop.hbase.testclassification.LargeTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045@Category({ LargeTests.class, ClientTests.class })
046public class TestMobCloneSnapshotFromClientCloneLinksAfterDelete
047    extends CloneSnapshotFromClientCloneLinksAfterDeleteTestBase {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestMobCloneSnapshotFromClientCloneLinksAfterDelete.class);
052
053  private static boolean delayFlush = false;
054
055  /**
056   * This coprocessor is used to delay the flush.
057   */
058  public static class DelayFlushCoprocessor implements RegionCoprocessor, RegionObserver {
059
060    @Override
061    public Optional<RegionObserver> getRegionObserver() {
062      return Optional.of(this);
063    }
064
065    @Override
066    public void preFlush(ObserverContext<RegionCoprocessorEnvironment> e,
067        FlushLifeCycleTracker tracker) throws IOException {
068      if (delayFlush) {
069        try {
070          if (Bytes.compareTo(e.getEnvironment().getRegionInfo().getStartKey(),
071            HConstants.EMPTY_START_ROW) != 0) {
072            Thread.sleep(100);
073          }
074        } catch (InterruptedException e1) {
075          throw new InterruptedIOException(e1.getMessage());
076        }
077      }
078    }
079  }
080
081  protected static void setupConfiguration() {
082    CloneSnapshotFromClientTestBase.setupConfiguration();
083    TEST_UTIL.getConfiguration().setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, 0);
084    TEST_UTIL.getConfiguration().setInt(MobConstants.MOB_FILE_CACHE_SIZE_KEY, 0);
085  }
086
087  @BeforeClass
088  public static void setUpBeforeClass() throws Exception {
089    setupConfiguration();
090    TEST_UTIL.startMiniCluster(3);
091  }
092
093  @Override
094  protected void createTable() throws IOException, InterruptedException {
095    MobSnapshotTestingUtils.createMobTable(TEST_UTIL, tableName,
096      SnapshotTestingUtils.getSplitKeys(), getNumReplicas(), DelayFlushCoprocessor.class.getName(),
097      FAMILY);
098  }
099
100  @Override
101  protected int numRowsToLoad() {
102    return 20;
103  }
104
105  @Override
106  protected int countRows(Table table) throws IOException {
107    return MobSnapshotTestingUtils.countMobRows(table);
108  }
109
110  @Test
111  @Override
112  public void testCloneLinksAfterDelete() throws IOException, InterruptedException {
113    // delay the flush to make sure
114    delayFlush = true;
115    SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 20, FAMILY);
116    long tid = EnvironmentEdgeManager.currentTime();
117    String snapshotName3 = "snaptb3-" + tid;
118    TableName clonedTableName3 =
119      TableName.valueOf(name.getMethodName() + EnvironmentEdgeManager.currentTime());
120    admin.snapshot(snapshotName3, tableName);
121    delayFlush = false;
122    int snapshot3Rows = -1;
123    try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
124      snapshot3Rows = HBaseTestingUtil.countRows(table);
125    }
126    admin.cloneSnapshot(snapshotName3, clonedTableName3);
127    admin.deleteSnapshot(snapshotName3);
128    super.testCloneLinksAfterDelete();
129    verifyRowCount(TEST_UTIL, clonedTableName3, snapshot3Rows);
130    admin.disableTable(clonedTableName3);
131    admin.deleteTable(clonedTableName3);
132  }
133}