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.HBaseParameterizedTestTemplate;
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.jupiter.api.BeforeAll;
042import org.junit.jupiter.api.Tag;
043import org.junit.jupiter.api.TestTemplate;
044
045@Tag(LargeTests.TAG)
046@Tag(ClientTests.TAG)
047@HBaseParameterizedTestTemplate(name = "{index}: regionReplication={0}")
048public class TestMobCloneSnapshotFromClientCloneLinksAfterDelete
049  extends CloneSnapshotFromClientCloneLinksAfterDeleteTestBase {
050
051  public TestMobCloneSnapshotFromClientCloneLinksAfterDelete(int numReplicas) {
052    super(numReplicas);
053  }
054
055  private static boolean delayFlush = false;
056
057  /**
058   * This coprocessor is used to delay the flush.
059   */
060  public static class DelayFlushCoprocessor implements RegionCoprocessor, RegionObserver {
061
062    @Override
063    public Optional<RegionObserver> getRegionObserver() {
064      return Optional.of(this);
065    }
066
067    @Override
068    public void preFlush(ObserverContext<? extends RegionCoprocessorEnvironment> e,
069      FlushLifeCycleTracker tracker) throws IOException {
070      if (delayFlush) {
071        try {
072          if (
073            Bytes.compareTo(e.getEnvironment().getRegionInfo().getStartKey(),
074              HConstants.EMPTY_START_ROW) != 0
075          ) {
076            Thread.sleep(100);
077          }
078        } catch (InterruptedException e1) {
079          throw new InterruptedIOException(e1.getMessage());
080        }
081      }
082    }
083  }
084
085  protected static void setupConfiguration() {
086    CloneSnapshotFromClientTestBase.setupConfiguration();
087    TEST_UTIL.getConfiguration().setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, 0);
088    TEST_UTIL.getConfiguration().setInt(MobConstants.MOB_FILE_CACHE_SIZE_KEY, 0);
089  }
090
091  @BeforeAll
092  public static void setUpBeforeClass() throws Exception {
093    setupConfiguration();
094    TEST_UTIL.startMiniCluster(3);
095  }
096
097  @Override
098  protected void createTable() throws IOException, InterruptedException {
099    MobSnapshotTestingUtils.createMobTable(TEST_UTIL, tableName,
100      SnapshotTestingUtils.getSplitKeys(), numReplicas,
101      StoreFileTrackerFactory.Trackers.DEFAULT.name(), DelayFlushCoprocessor.class.getName(),
102      FAMILY);
103  }
104
105  @Override
106  protected int numRowsToLoad() {
107    return 20;
108  }
109
110  @Override
111  protected int countRows(Table table) throws IOException {
112    return MobSnapshotTestingUtils.countMobRows(table);
113  }
114
115  @TestTemplate
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(getValidMethodName() + 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    testCloneLinksAfterDelete0();
133    verifyRowCount(TEST_UTIL, clonedTableName3, snapshot3Rows);
134    admin.disableTable(clonedTableName3);
135    admin.deleteTable(clonedTableName3);
136  }
137}