Search in sources :

Example 91 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespaceAuditor method testRegionMerge.

@Test
public void testRegionMerge() throws Exception {
    String nsp1 = prefix + "_regiontest";
    final int initialRegions = 3;
    NamespaceDescriptor nspDesc = NamespaceDescriptor.create(nsp1).addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "" + initialRegions).addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2").build();
    ADMIN.createNamespace(nspDesc);
    final TableName tableTwo = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table2");
    byte[] columnFamily = Bytes.toBytes("info");
    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableTwo).setColumnFamily(ColumnFamilyDescriptorBuilder.of(columnFamily)).build();
    ADMIN.createTable(tableDescriptor, Bytes.toBytes("0"), Bytes.toBytes("9"), initialRegions);
    Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
    try (Table table = connection.getTable(tableTwo)) {
        UTIL.loadNumericRows(table, Bytes.toBytes("info"), 1000, 1999);
    }
    ADMIN.flush(tableTwo);
    List<RegionInfo> hris = ADMIN.getRegions(tableTwo);
    assertEquals(initialRegions, hris.size());
    Collections.sort(hris, RegionInfo.COMPARATOR);
    Future<?> f = ADMIN.mergeRegionsAsync(hris.get(0).getEncodedNameAsBytes(), hris.get(1).getEncodedNameAsBytes(), false);
    f.get(10, TimeUnit.SECONDS);
    hris = ADMIN.getRegions(tableTwo);
    assertEquals(initialRegions - 1, hris.size());
    Collections.sort(hris, RegionInfo.COMPARATOR);
    byte[] splitKey = Bytes.toBytes("3");
    HRegion regionToSplit = UTIL.getMiniHBaseCluster().getRegions(tableTwo).stream().filter(r -> r.getRegionInfo().containsRow(splitKey)).findFirst().get();
    regionToSplit.compact(true);
    // Waiting for compaction to finish
    UTIL.waitFor(30000, new Waiter.Predicate<Exception>() {

        @Override
        public boolean evaluate() throws Exception {
            return (CompactionState.NONE == ADMIN.getCompactionStateForRegion(regionToSplit.getRegionInfo().getRegionName()));
        }
    });
    // Cleaning compacted references for split to proceed
    regionToSplit.getStores().stream().forEach(s -> {
        try {
            s.closeAndArchiveCompactedFiles();
        } catch (IOException e1) {
            LOG.error("Error whiling cleaning compacted file");
        }
    });
    // the above compact may quit immediately if there is a compaction ongoing, so here we need to
    // wait a while to let the ongoing compaction finish.
    UTIL.waitFor(10000, regionToSplit::isSplittable);
    ADMIN.splitRegionAsync(regionToSplit.getRegionInfo().getRegionName(), splitKey).get(10, TimeUnit.SECONDS);
    hris = ADMIN.getRegions(tableTwo);
    assertEquals(initialRegions, hris.size());
    Collections.sort(hris, RegionInfo.COMPARATOR);
    // Fail region merge through Coprocessor hook
    SingleProcessHBaseCluster cluster = UTIL.getHBaseCluster();
    MasterCoprocessorHost cpHost = cluster.getMaster().getMasterCoprocessorHost();
    Coprocessor coprocessor = cpHost.findCoprocessor(CPMasterObserver.class);
    CPMasterObserver masterObserver = (CPMasterObserver) coprocessor;
    masterObserver.failMerge(true);
    f = ADMIN.mergeRegionsAsync(hris.get(1).getEncodedNameAsBytes(), hris.get(2).getEncodedNameAsBytes(), false);
    try {
        f.get(10, TimeUnit.SECONDS);
        fail("Merge was supposed to fail!");
    } catch (ExecutionException ee) {
    // Expected.
    }
    hris = ADMIN.getRegions(tableTwo);
    assertEquals(initialRegions, hris.size());
    Collections.sort(hris, RegionInfo.COMPARATOR);
    // verify that we cannot split
    try {
        ADMIN.split(tableTwo, Bytes.toBytes("6"));
        fail();
    } catch (DoNotRetryRegionException e) {
    // Expected
    }
    Thread.sleep(2000);
    assertEquals(initialRegions, ADMIN.getRegions(tableTwo).size());
}
Also used : SingleProcessHBaseCluster(org.apache.hadoop.hbase.SingleProcessHBaseCluster) MasterCoprocessorHost(org.apache.hadoop.hbase.master.MasterCoprocessorHost) Table(org.apache.hadoop.hbase.client.Table) Connection(org.apache.hadoop.hbase.client.Connection) RegionInfo(org.apache.hadoop.hbase.client.RegionInfo) IOException(java.io.IOException) TableDescriptor(org.apache.hadoop.hbase.client.TableDescriptor) RestoreSnapshotException(org.apache.hadoop.hbase.snapshot.RestoreSnapshotException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) DoNotRetryRegionException(org.apache.hadoop.hbase.client.DoNotRetryRegionException) QuotaExceededException(org.apache.hadoop.hbase.quotas.QuotaExceededException) ExecutionException(java.util.concurrent.ExecutionException) TableName(org.apache.hadoop.hbase.TableName) HRegion(org.apache.hadoop.hbase.regionserver.HRegion) Coprocessor(org.apache.hadoop.hbase.Coprocessor) RegionCoprocessor(org.apache.hadoop.hbase.coprocessor.RegionCoprocessor) MasterCoprocessor(org.apache.hadoop.hbase.coprocessor.MasterCoprocessor) RegionServerCoprocessor(org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessor) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) DoNotRetryRegionException(org.apache.hadoop.hbase.client.DoNotRetryRegionException) Waiter(org.apache.hadoop.hbase.Waiter) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 92 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespaceAuditor method testCloneSnapshotQuotaExceed.

@Test(expected = QuotaExceededException.class)
public void testCloneSnapshotQuotaExceed() throws Exception {
    String nsp = prefix + "_testTableQuotaExceedWithCloneSnapshot";
    NamespaceDescriptor nspDesc = NamespaceDescriptor.create(nsp).addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "1").build();
    ADMIN.createNamespace(nspDesc);
    assertNotNull("Namespace descriptor found null.", ADMIN.getNamespaceDescriptor(nsp));
    TableName tableName = TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table1");
    TableName cloneTableName = TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table2");
    ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("fam1")).build();
    TableDescriptorBuilder tableDescOne = TableDescriptorBuilder.newBuilder(tableName);
    tableDescOne.setColumnFamily(columnFamilyDescriptor);
    ADMIN.createTable(tableDescOne.build());
    String snapshot = "snapshot_testTableQuotaExceedWithCloneSnapshot";
    ADMIN.snapshot(snapshot, tableName);
    ADMIN.cloneSnapshot(snapshot, cloneTableName);
    ADMIN.deleteSnapshot(snapshot);
}
Also used : TableName(org.apache.hadoop.hbase.TableName) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) TableDescriptorBuilder(org.apache.hadoop.hbase.client.TableDescriptorBuilder) ColumnFamilyDescriptor(org.apache.hadoop.hbase.client.ColumnFamilyDescriptor) Test(org.junit.Test)

Example 93 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespaceAuditor method testRestoreSnapshotQuotaExceed.

@Test
public void testRestoreSnapshotQuotaExceed() throws Exception {
    String nsp = prefix + "_testRestoreSnapshotQuotaExceed";
    NamespaceDescriptor nspDesc = NamespaceDescriptor.create(nsp).addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "10").build();
    ADMIN.createNamespace(nspDesc);
    NamespaceDescriptor ndesc = ADMIN.getNamespaceDescriptor(nsp);
    assertNotNull("Namespace descriptor found null.", ndesc);
    TableName tableName1 = TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table1");
    ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("fam1")).build();
    TableDescriptorBuilder tableDescOne = TableDescriptorBuilder.newBuilder(tableName1);
    tableDescOne.setColumnFamily(columnFamilyDescriptor);
    ADMIN.createTable(tableDescOne.build(), Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 4);
    NamespaceTableAndRegionInfo nstate = getNamespaceState(nsp);
    assertEquals("Intial region count should be 4.", 4, nstate.getRegionCount());
    String snapshot = "snapshot_testRestoreSnapshotQuotaExceed";
    // snapshot has 4 regions
    ADMIN.snapshot(snapshot, tableName1);
    // recreate table with 1 region and set max regions to 3 for namespace
    ADMIN.disableTable(tableName1);
    ADMIN.deleteTable(tableName1);
    ADMIN.createTable(tableDescOne.build());
    ndesc.setConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "3");
    ADMIN.modifyNamespace(ndesc);
    ADMIN.disableTable(tableName1);
    try {
        ADMIN.restoreSnapshot(snapshot);
        fail("Region quota is exceeded so QuotaExceededException should be thrown but HBaseAdmin" + " wraps IOException into RestoreSnapshotException");
    } catch (RestoreSnapshotException ignore) {
        assertTrue(ignore.getCause() instanceof QuotaExceededException);
    }
    assertEquals(1, getNamespaceState(nsp).getRegionCount());
    ADMIN.enableTable(tableName1);
    ADMIN.deleteSnapshot(snapshot);
}
Also used : TableName(org.apache.hadoop.hbase.TableName) QuotaExceededException(org.apache.hadoop.hbase.quotas.QuotaExceededException) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) TableDescriptorBuilder(org.apache.hadoop.hbase.client.TableDescriptorBuilder) ColumnFamilyDescriptor(org.apache.hadoop.hbase.client.ColumnFamilyDescriptor) RestoreSnapshotException(org.apache.hadoop.hbase.snapshot.RestoreSnapshotException) Test(org.junit.Test)

Example 94 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespaceAuditor method testRecreateTableWithSameNameAfterFirstTimeFailure.

/*
   * Create a table and make sure that the table creation fails after adding this table entry into
   * namespace quota cache. Now correct the failure and recreate the table with same name.
   * HBASE-13394
   */
@Test
public void testRecreateTableWithSameNameAfterFirstTimeFailure() throws Exception {
    String nsp1 = prefix + "_testRecreateTable";
    NamespaceDescriptor nspDesc = NamespaceDescriptor.create(nsp1).addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "20").addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "1").build();
    ADMIN.createNamespace(nspDesc);
    final TableName tableOne = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table1");
    byte[] columnFamily = Bytes.toBytes("info");
    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableOne).setColumnFamily(ColumnFamilyDescriptorBuilder.of(columnFamily)).build();
    MasterSyncObserver.throwExceptionInPreCreateTableAction = true;
    try {
        try {
            ADMIN.createTable(tableDescriptor);
            fail("Table " + tableOne.toString() + "creation should fail.");
        } catch (Exception exp) {
            LOG.error(exp.toString(), exp);
        }
        assertFalse(ADMIN.tableExists(tableOne));
        NamespaceTableAndRegionInfo nstate = getNamespaceState(nsp1);
        assertEquals("First table creation failed in namespace so number of tables in namespace " + "should be 0.", 0, nstate.getTables().size());
        MasterSyncObserver.throwExceptionInPreCreateTableAction = false;
        try {
            ADMIN.createTable(tableDescriptor);
        } catch (Exception e) {
            fail("Table " + tableOne.toString() + "creation should succeed.");
            LOG.error(e.toString(), e);
        }
        assertTrue(ADMIN.tableExists(tableOne));
        nstate = getNamespaceState(nsp1);
        assertEquals("First table was created successfully so table size in namespace should " + "be one now.", 1, nstate.getTables().size());
    } finally {
        MasterSyncObserver.throwExceptionInPreCreateTableAction = false;
        if (ADMIN.tableExists(tableOne)) {
            ADMIN.disableTable(tableOne);
            deleteTable(tableOne);
        }
        ADMIN.deleteNamespace(nsp1);
    }
}
Also used : TableName(org.apache.hadoop.hbase.TableName) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) TableDescriptor(org.apache.hadoop.hbase.client.TableDescriptor) RestoreSnapshotException(org.apache.hadoop.hbase.snapshot.RestoreSnapshotException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) DoNotRetryRegionException(org.apache.hadoop.hbase.client.DoNotRetryRegionException) QuotaExceededException(org.apache.hadoop.hbase.quotas.QuotaExceededException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 95 with NamespaceDescriptor

use of org.apache.hadoop.hbase.NamespaceDescriptor in project hbase by apache.

the class TestNamespaceAuditor method testCloneSnapshot.

@Test
public void testCloneSnapshot() throws Exception {
    String nsp = prefix + "_testCloneSnapshot";
    NamespaceDescriptor nspDesc = NamespaceDescriptor.create(nsp).addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2").addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "20").build();
    ADMIN.createNamespace(nspDesc);
    assertNotNull("Namespace descriptor found null.", ADMIN.getNamespaceDescriptor(nsp));
    TableName tableName = TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table1");
    TableName cloneTableName = TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table2");
    ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("fam1")).build();
    TableDescriptorBuilder tableDescOne = TableDescriptorBuilder.newBuilder(tableName);
    tableDescOne.setColumnFamily(columnFamilyDescriptor);
    ADMIN.createTable(tableDescOne.build(), Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 4);
    String snapshot = "snapshot_testCloneSnapshot";
    ADMIN.snapshot(snapshot, tableName);
    ADMIN.cloneSnapshot(snapshot, cloneTableName);
    int tableLength;
    try (RegionLocator locator = ADMIN.getConnection().getRegionLocator(tableName)) {
        tableLength = locator.getStartKeys().length;
    }
    assertEquals(tableName.getNameAsString() + " should have four regions.", 4, tableLength);
    try (RegionLocator locator = ADMIN.getConnection().getRegionLocator(cloneTableName)) {
        tableLength = locator.getStartKeys().length;
    }
    assertEquals(cloneTableName.getNameAsString() + " should have four regions.", 4, tableLength);
    NamespaceTableAndRegionInfo nstate = getNamespaceState(nsp);
    assertEquals("Total tables count should be 2.", 2, nstate.getTables().size());
    assertEquals("Total regions count should be.", 8, nstate.getRegionCount());
    ADMIN.deleteSnapshot(snapshot);
}
Also used : TableName(org.apache.hadoop.hbase.TableName) RegionLocator(org.apache.hadoop.hbase.client.RegionLocator) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) TableDescriptorBuilder(org.apache.hadoop.hbase.client.TableDescriptorBuilder) ColumnFamilyDescriptor(org.apache.hadoop.hbase.client.ColumnFamilyDescriptor) Test(org.junit.Test)

Aggregations

NamespaceDescriptor (org.apache.hadoop.hbase.NamespaceDescriptor)97 Test (org.junit.Test)51 TableName (org.apache.hadoop.hbase.TableName)26 IOException (java.io.IOException)17 Admin (org.apache.hadoop.hbase.client.Admin)15 TableDescriptor (org.apache.hadoop.hbase.client.TableDescriptor)13 ColumnFamilyDescriptor (org.apache.hadoop.hbase.client.ColumnFamilyDescriptor)11 TableDescriptorBuilder (org.apache.hadoop.hbase.client.TableDescriptorBuilder)11 QuotaExceededException (org.apache.hadoop.hbase.quotas.QuotaExceededException)9 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)8 Table (org.apache.hadoop.hbase.client.Table)8 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)7 NamespaceNotFoundException (org.apache.hadoop.hbase.NamespaceNotFoundException)7 Connection (org.apache.hadoop.hbase.client.Connection)7 ConstraintException (org.apache.hadoop.hbase.constraint.ConstraintException)7 RestoreSnapshotException (org.apache.hadoop.hbase.snapshot.RestoreSnapshotException)7 KeeperException (org.apache.zookeeper.KeeperException)7 ArrayList (java.util.ArrayList)6 ExecutionException (java.util.concurrent.ExecutionException)5 NamespaceExistException (org.apache.hadoop.hbase.NamespaceExistException)5