Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,48 @@
*/
package org.apache.hadoop.hbase.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.IOException;
import java.time.Duration;
import java.util.List;
import org.apache.hadoop.hbase.MetaTableAccessor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.master.RegionState;
import org.apache.hadoop.hbase.master.assignment.RegionStates;
import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.junit.Test;
import org.junit.jupiter.api.TestTemplate;

public class CloneSnapshotFromClientAfterSplittingRegionTestBase
extends CloneSnapshotFromClientTestBase {

protected CloneSnapshotFromClientAfterSplittingRegionTestBase(int numReplicas) {
super(numReplicas);
}

private void splitRegion() throws IOException {
int numRegions = admin.getRegions(tableName).size();
try (Table k = TEST_UTIL.getConnection().getTable(tableName);
ResultScanner scanner = k.getScanner(new Scan())) {
// Split on the second row to make sure that the snapshot contains reference files.
// We also disable the compaction so that the reference files are not compacted away.
scanner.next();
admin.split(tableName, scanner.next().getRow());
}
await().atMost(Duration.ofSeconds(30))
.untilAsserted(() -> assertEquals(numRegions + 1, admin.getRegions(tableName).size()));
}

@Test
@TestTemplate
public void testCloneSnapshotAfterSplittingRegion() throws IOException, InterruptedException {
// Turn off the CatalogJanitor
admin.catalogJanitorSwitch(false);

TableName clonedTableName =
TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
try {
List<RegionInfo> regionInfos = admin.getRegions(tableName);
RegionReplicaUtil.removeNonDefaultRegions(regionInfos);
Expand All @@ -59,8 +70,6 @@ public void testCloneSnapshotAfterSplittingRegion() throws IOException, Interrup
admin.snapshot(snapshotName2, tableName);

// Clone the snapshot to another table
TableName clonedTableName =
TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
admin.cloneSnapshot(snapshotName2, clonedTableName);
SnapshotTestingUtils.waitForTableToBeOnline(TEST_UTIL, clonedTableName);

Expand Down Expand Up @@ -90,26 +99,26 @@ public void testCloneSnapshotAfterSplittingRegion() throws IOException, Interrup
assertNotNull(daughter);
}
}

TEST_UTIL.deleteTable(clonedTableName);
} finally {
if (admin.tableExists(clonedTableName)) {
TEST_UTIL.deleteTable(clonedTableName);
}
admin.catalogJanitorSwitch(true);
}
}

@Test
@TestTemplate
public void testCloneSnapshotBeforeSplittingRegionAndDroppingTable()
throws IOException, InterruptedException {
// Turn off the CatalogJanitor
admin.catalogJanitorSwitch(false);

TableName clonedTableName =
TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
try {
// Take a snapshot
admin.snapshot(snapshotName2, tableName);

// Clone the snapshot to another table
TableName clonedTableName =
TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
admin.cloneSnapshot(snapshotName2, clonedTableName);
SnapshotTestingUtils.waitForTableToBeOnline(TEST_UTIL, clonedTableName);

Expand All @@ -129,6 +138,9 @@ public void testCloneSnapshotBeforeSplittingRegionAndDroppingTable()

verifyRowCount(TEST_UTIL, clonedTableName, snapshot1Rows);
} finally {
if (admin.tableExists(clonedTableName)) {
TEST_UTIL.deleteTable(clonedTableName);
}
admin.catalogJanitorSwitch(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@
import java.io.IOException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.junit.Test;

public class CloneSnapshotFromClientCloneLinksAfterDeleteTestBase
extends CloneSnapshotFromClientTestBase {

protected CloneSnapshotFromClientCloneLinksAfterDeleteTestBase(int numReplicas) {
super(numReplicas);
}

/**
* Verify that tables created from the snapshot are still alive after source table deletion.
*/
@Test
public void testCloneLinksAfterDelete() throws IOException, InterruptedException {
protected void testCloneLinksAfterDelete0() throws IOException, InterruptedException {
// Clone a table from the first snapshot
final TableName clonedTableName =
TableName.valueOf(getValidMethodName() + "1-" + EnvironmentEdgeManager.currentTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,34 @@
*/
package org.apache.hadoop.hbase.client;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import org.apache.hadoop.hbase.NamespaceNotFoundException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.snapshot.SnapshotDoesNotExistException;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.junit.Test;
import org.junit.jupiter.api.TestTemplate;

public class CloneSnapshotFromClientErrorTestBase extends CloneSnapshotFromClientTestBase {

@Test(expected = SnapshotDoesNotExistException.class)
protected CloneSnapshotFromClientErrorTestBase(int numReplicas) {
super(numReplicas);
}

@TestTemplate
public void testCloneNonExistentSnapshot() throws IOException, InterruptedException {
String snapshotName = "random-snapshot-" + EnvironmentEdgeManager.currentTime();
final TableName tableName =
TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
admin.cloneSnapshot(snapshotName, tableName);
assertThrows(SnapshotDoesNotExistException.class,
() -> admin.cloneSnapshot(snapshotName, tableName));
}

@Test(expected = NamespaceNotFoundException.class)
@TestTemplate
public void testCloneOnMissingNamespace() throws IOException, InterruptedException {
final TableName clonedTableName = TableName.valueOf("unknownNS:" + getValidMethodName());
admin.cloneSnapshot(snapshotName1, clonedTableName);
assertThrows(NamespaceNotFoundException.class,
() -> admin.cloneSnapshot(snapshotName1, clonedTableName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.junit.Test;
import org.junit.jupiter.api.TestTemplate;

public class CloneSnapshotFromClientNormalTestBase extends CloneSnapshotFromClientTestBase {

@Test
protected CloneSnapshotFromClientNormalTestBase(int numReplicas) {
super(numReplicas);
}

@TestTemplate
public void testCloneSnapshot() throws IOException, InterruptedException {
TableName clonedTableName =
TableName.valueOf(getValidMethodName() + "-" + EnvironmentEdgeManager.currentTime());
Expand All @@ -46,10 +50,10 @@ private void testCloneSnapshot(TableName tableName, String snapshotName, int sna
}

private void verifyReplicasCameOnline(TableName tableName) throws IOException {
SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, numReplicas);
}

@Test
@TestTemplate
public void testCloneSnapshotCrossNamespace() throws IOException, InterruptedException {
String nsName = getValidMethodName() + "_ns_" + EnvironmentEdgeManager.currentTime();
admin.createNamespace(NamespaceDescriptor.create(nsName).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
package org.apache.hadoop.hbase.client;

import java.io.IOException;
import java.util.stream.Stream;
import org.apache.hadoop.hbase.HBaseTestingUtil;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.params.provider.Arguments;

/**
* Base class for testing clone snapsot
Expand All @@ -50,8 +50,17 @@ public class CloneSnapshotFromClientTestBase {
protected int snapshot1Rows;
protected Admin admin;

@Rule
public TestName name = new TestName();
protected int numReplicas;

private String testName;

protected CloneSnapshotFromClientTestBase(int numReplicas) {
this.numReplicas = numReplicas;
}

public static Stream<Arguments> parameters() {
return Stream.of(Arguments.of(1), Arguments.of(3));
}

protected static void setupConfiguration() {
TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
Expand All @@ -63,30 +72,26 @@ protected static void setupConfiguration() {
TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.compaction.enabled", false);
}

@BeforeClass
public static void setUpBeforeClass() throws Exception {
setupConfiguration();
TEST_UTIL.startMiniCluster(3);
}

@AfterClass
@AfterAll
public static void tearDownAfterClass() throws Exception {
TEST_UTIL.shutdownMiniCluster();
}

protected final String getValidMethodName() {
return name.getMethodName().replaceAll("[^0-9A-Za-z_]", "_");
return testName;
}

/**
* Initialize the tests with a table filled with some data and two snapshots (snapshotName0,
* snapshotName1) of different states. The tableName, snapshotNames and the number of rows in the
* snapshot are initialized.
*/
@Before
public void setup() throws Exception {
@BeforeEach
public void setUp(TestInfo testInfo) throws Exception {
this.admin = TEST_UTIL.getAdmin();
long tid = EnvironmentEdgeManager.currentTime();
testName = testInfo.getTestMethod().get().getName()
+ testInfo.getDisplayName().replaceAll("[^0-9A-Za-z_]", "_");
tableName = TableName.valueOf(getValidMethodName() + tid);
emptySnapshot = "emptySnaptb-" + tid;
snapshotName0 = "snaptb0-" + tid;
Expand All @@ -97,15 +102,15 @@ public void setup() throws Exception {
}

protected void createTable() throws IOException, InterruptedException {
SnapshotTestingUtils.createTable(TEST_UTIL, tableName, getNumReplicas(), FAMILY);
SnapshotTestingUtils.createTable(TEST_UTIL, tableName, numReplicas, FAMILY);
}

protected int numRowsToLoad() {
return 500;
}

protected int countRows(Table table) throws IOException {
return TEST_UTIL.countRows(table);
return HBaseTestingUtil.countRows(table);
}

private void createTableAndSnapshots() throws Exception {
Expand Down Expand Up @@ -142,16 +147,12 @@ private void createTableAndSnapshots() throws Exception {
admin.enableTable(tableName);
}

protected int getNumReplicas() {
return 1;
}

protected void verifyRowCount(final HBaseTestingUtil util, final TableName tableName,
long expectedRows) throws IOException {
SnapshotTestingUtils.verifyRowCount(util, tableName, expectedRows);
}

@After
@AfterEach
public void tearDown() throws Exception {
if (admin.tableExists(tableName)) {
TEST_UTIL.deleteTable(tableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,25 @@
*/
package org.apache.hadoop.hbase.client;

import java.util.Arrays;
import java.util.List;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
import org.apache.hadoop.hbase.testclassification.ClientTests;
import org.apache.hadoop.hbase.testclassification.LargeTests;
import org.junit.ClassRule;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;

@RunWith(Parameterized.class)
@Category({ LargeTests.class, ClientTests.class })
@Tag(LargeTests.TAG)
@Tag(ClientTests.TAG)
@HBaseParameterizedTestTemplate(name = "{index}: regionReplication={0}")
public class TestCloneSnapshotFromClientAfterSplittingRegion
extends CloneSnapshotFromClientAfterSplittingRegionTestBase {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestCloneSnapshotFromClientAfterSplittingRegion.class);

@Parameter
public int numReplicas;

@Parameters(name = "{index}: regionReplication={0}")
public static List<Object[]> params() {
return Arrays.asList(new Object[] { 1 }, new Object[] { 3 });
public TestCloneSnapshotFromClientAfterSplittingRegion(int numReplicas) {
super(numReplicas);
}

@Override
protected int getNumReplicas() {
return numReplicas;
@BeforeAll
public static void setUpBeforeClass() throws Exception {
setupConfiguration();
TEST_UTIL.startMiniCluster(3);
}
}
Loading
Loading