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