Java Code Examples for org.apache.hadoop.hbase.client.Admin#getDescriptor()

The following examples show how to use org.apache.hadoop.hbase.client.Admin#getDescriptor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Action.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Apply a transform to all columns in a given table. If there are no columns in a table
 * or if the context is stopping does nothing.
 * @param tableName the table to modify
 * @param transform the modification to perform. Callers will have the
 *                  column name as a string and a column family builder available to them
 */
protected void modifyAllTableColumns(TableName tableName,
  BiConsumer<String, ColumnFamilyDescriptorBuilder> transform) throws IOException {
  HBaseTestingUtility util = this.context.getHBaseIntegrationTestingUtility();
  Admin admin = util.getAdmin();

  TableDescriptor tableDescriptor = admin.getDescriptor(tableName);
  ColumnFamilyDescriptor[] columnDescriptors = tableDescriptor.getColumnFamilies();

  if (columnDescriptors == null || columnDescriptors.length == 0) {
    return;
  }

  TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableDescriptor);
  for (ColumnFamilyDescriptor descriptor : columnDescriptors) {
    ColumnFamilyDescriptorBuilder cfd = ColumnFamilyDescriptorBuilder.newBuilder(descriptor);
    transform.accept(descriptor.getNameAsString(), cfd);
    builder.modifyColumnFamily(cfd.build());
  }

  // Don't try the modify if we're stopping
  if (this.context.isStopping()) {
    return;
  }
  admin.modifyTable(builder.build());
}
 
Example 2
Source File: IndexCoprocIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateOnExistingHBaseTable() throws Exception {
    String schemaName = generateUniqueName();
    String tableName = generateUniqueName();
    String indexName = generateUniqueName();
    byte[] cf = Bytes.toBytes("f");
    try (PhoenixConnection conn = getConnection()){
        TableName table = TableName.valueOf(SchemaUtil.getPhysicalHBaseTableName(schemaName,
            tableName, isNamespaceMapped).getString());
        TableDescriptorBuilder originalDescBuilder = TableDescriptorBuilder.newBuilder(table);
        ColumnFamilyDescriptorBuilder familyDescBuilder =
            ColumnFamilyDescriptorBuilder.newBuilder(cf);
        originalDescBuilder.setColumnFamily(familyDescBuilder.build());
        Admin admin = conn.getQueryServices().getAdmin();
        admin.createTable(originalDescBuilder.build());
        createBaseTable(schemaName, tableName, isMultiTenant, 0, null);
        TableDescriptor baseDescriptor = admin.getDescriptor(table);
        assertUsingNewCoprocs(baseDescriptor);
        createIndexTable(schemaName, tableName, indexName);
        baseDescriptor = admin.getDescriptor(table);
        TableName indexTable = TableName.valueOf(SchemaUtil.getPhysicalHBaseTableName(schemaName,
            indexName, isNamespaceMapped).getString());
        TableDescriptor indexDescriptor = admin.getDescriptor(indexTable);
        assertUsingNewCoprocs(baseDescriptor, indexDescriptor);
    }
}
 
Example 3
Source File: LocalIndexIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testLocalIndexTableRegionSplitPolicyAndSplitKeys() throws Exception {
    String tableName = schemaName + "." + generateUniqueName();
    String indexName = "IDX_" + generateUniqueName();
    TableName physicalTableName = SchemaUtil.getPhysicalTableName(tableName.getBytes(), isNamespaceMapped);
    String indexPhysicalTableName = physicalTableName.getNameAsString();

    createBaseTable(tableName, null,"('e','i','o')");
    Connection conn1 = getConnection();
    Connection conn2 = getConnection();
    conn1.createStatement().execute("CREATE LOCAL INDEX " + indexName + " ON " + tableName + "(v1)");
    conn2.createStatement().executeQuery("SELECT * FROM " + tableName).next();
    Admin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
    TableDescriptor htd = admin
            .getDescriptor(TableName.valueOf(indexPhysicalTableName));
    assertEquals(IndexRegionSplitPolicy.class.getName(), htd.getValue(TableDescriptorBuilder.SPLIT_POLICY));
    try(org.apache.hadoop.hbase.client.Connection c = ConnectionFactory.createConnection(admin.getConfiguration())) {
        try (RegionLocator userTable= c.getRegionLocator(SchemaUtil.getPhysicalTableName(tableName.getBytes(), isNamespaceMapped))) {
            try (RegionLocator indxTable = c.getRegionLocator(TableName.valueOf(indexPhysicalTableName))) {
                assertArrayEquals("Both user table and index table should have same split keys.",
                    userTable.getStartKeys(), indxTable.getStartKeys());
            }
        }
    }
}
 
Example 4
Source File: IntegrationTestIngestWithEncryption.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Before
@Override
public void setUp() throws Exception {
  // Initialize the cluster. This invokes LoadTestTool -init_only, which
  // will create the test table, appropriately pre-split
  super.setUp();

  if (!initialized) {
    return;
  }

  // Update the test table schema so HFiles from this point will be written with
  // encryption features enabled.
  final Admin admin = util.getAdmin();
  TableDescriptor tableDescriptor = admin.getDescriptor(getTablename());
  for (ColumnFamilyDescriptor columnDescriptor : tableDescriptor.getColumnFamilies()) {
    ColumnFamilyDescriptor updatedColumn = ColumnFamilyDescriptorBuilder
        .newBuilder(columnDescriptor).setEncryptionType("AES").build();
    LOG.info(
      "Updating CF schema for " + getTablename() + "." + columnDescriptor.getNameAsString());
    admin.disableTable(getTablename());
    admin.modifyColumnFamily(getTablename(), updatedColumn);
    admin.enableTable(getTablename());
    util.waitFor(30000, 1000, true, new Predicate<IOException>() {
      @Override
      public boolean evaluate() throws IOException {
        return admin.isTableAvailable(getTablename());
      }
    });
  }
}
 
Example 5
Source File: ExpiredMobFileCleaner.java    From hbase with Apache License 2.0 5 votes vote down vote up
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION",
    justification="Intentional")
@Override
public int run(String[] args) throws Exception {
  if (args.length != 2) {
    printUsage();
    return 1;
  }
  String tableName = args[0];
  String familyName = args[1];
  TableName tn = TableName.valueOf(tableName);
  Connection connection = ConnectionFactory.createConnection(getConf());
  Admin admin = connection.getAdmin();
  try {
    TableDescriptor htd = admin.getDescriptor(tn);
    ColumnFamilyDescriptor family = htd.getColumnFamily(Bytes.toBytes(familyName));
    if (family == null || !family.isMobEnabled()) {
      throw new IOException("Column family " + familyName + " is not a MOB column family");
    }
    if (family.getMinVersions() > 0) {
      throw new IOException(
          "The minVersions of the column family is not 0, could not be handled by this cleaner");
    }
    cleanExpiredMobFiles(tableName, family);
    return 0;
  } finally {
    admin.close();
    try {
      connection.close();
    } catch (IOException e) {
      LOG.error("Failed to close the connection.", e);
    }
  }
}
 
Example 6
Source File: IndexCoprocIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCoprocs() throws Exception {
    String schemaName = "S" + generateUniqueName();
    String tableName = "T_" + generateUniqueName();
    String indexName = "I_" + generateUniqueName();
    String physicalTableName = SchemaUtil.getPhysicalHBaseTableName(schemaName, tableName,
        isNamespaceMapped).getString();
    String physicalIndexName = SchemaUtil.getPhysicalHBaseTableName(schemaName,
        indexName, isNamespaceMapped).getString();
    Admin admin = ((PhoenixConnection) getConnection()).getQueryServices().getAdmin();

    createBaseTable(schemaName, tableName, isMultiTenant, 0, null);
    createIndexTable(schemaName, tableName, indexName);

    TableDescriptor baseDescriptor = admin.getDescriptor(TableName.valueOf(physicalTableName));
    TableDescriptorBuilder baseDescBuilder = TableDescriptorBuilder.newBuilder(baseDescriptor);
    TableDescriptor indexDescriptor = admin.getDescriptor(TableName.valueOf(physicalIndexName));
    TableDescriptorBuilder indexDescBuilder = TableDescriptorBuilder.newBuilder(indexDescriptor);

    assertCoprocsContains(IndexRegionObserver.class, baseDescriptor);
    assertCoprocsContains(GlobalIndexChecker.class, indexDescriptor);

    removeCoproc(IndexRegionObserver.class, baseDescBuilder, admin);
    removeCoproc(IndexRegionObserver.class, indexDescBuilder, admin);
    removeCoproc(GlobalIndexChecker.class, indexDescBuilder, admin);

    Map<String, String> props = new HashMap<String, String>();
    props.put(NonTxIndexBuilder.CODEC_CLASS_NAME_KEY, PhoenixIndexCodec.class.getName());
    Indexer.enableIndexing(baseDescBuilder, PhoenixIndexBuilder.class,
        props, QueryServicesOptions.DEFAULT_COPROCESSOR_PRIORITY);
    admin.modifyTable(baseDescBuilder.build());
    baseDescriptor = admin.getDescriptor(TableName.valueOf(physicalTableName));
    indexDescriptor = admin.getDescriptor(TableName.valueOf(physicalIndexName));
    assertUsingOldCoprocs(baseDescriptor, indexDescriptor);

    createBaseTable(schemaName, tableName, true, 0, null);
    baseDescriptor = admin.getDescriptor(TableName.valueOf(physicalTableName));
    indexDescriptor = admin.getDescriptor(TableName.valueOf(physicalIndexName));
    assertUsingOldCoprocs(baseDescriptor, indexDescriptor);
}
 
Example 7
Source File: IntegrationTestBulkLoad.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Modify table {@code getTableName()} to carry {@link SlowMeCoproScanOperations}.
 */
private void installSlowingCoproc() throws IOException, InterruptedException {
  int replicaCount = conf.getInt(NUM_REPLICA_COUNT_KEY, NUM_REPLICA_COUNT_DEFAULT);
  if (replicaCount == NUM_REPLICA_COUNT_DEFAULT) return;

  TableName t = getTablename();
  Admin admin = util.getAdmin();
  TableDescriptor desc = admin.getDescriptor(t);
  TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(desc);
  builder.setCoprocessor(SlowMeCoproScanOperations.class.getName());
  HBaseTestingUtility.modifyTableSync(admin, builder.build());
}
 
Example 8
Source File: IntegrationTestDDLMasterFailover.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
void perform() throws IOException {
  Admin admin = connection.getAdmin();
  try {
    TableDescriptor td = createTableDesc();
    TableName tableName = td.getTableName();
    if ( admin.tableExists(tableName)){
      return;
    }
    String numRegionKey = String.format(NUM_REGIONS_KEY, this.getClass().getSimpleName());
    numRegions = getConf().getInt(numRegionKey, DEFAULT_NUM_REGIONS);
    byte[] startKey = Bytes.toBytes("row-0000000000");
    byte[] endKey = Bytes.toBytes("row-" + Integer.MAX_VALUE);
    LOG.info("Creating table:" + td);
    admin.createTable(td, startKey, endKey, numRegions);
    Assert.assertTrue("Table: " + td + " was not created", admin.tableExists(tableName));
    TableDescriptor freshTableDesc = admin.getDescriptor(tableName);
    Assert.assertTrue(
      "After create, Table: " + tableName + " in not enabled", admin.isTableEnabled(tableName));
    enabledTables.put(tableName, freshTableDesc);
    LOG.info("Created table:" + freshTableDesc);
  } catch (Exception e) {
    LOG.warn("Caught exception in action: " + this.getClass());
    throw e;
  } finally {
    admin.close();
  }
}
 
Example 9
Source File: FlappingLocalIndexIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void testBuildingLocalIndexShouldHandleNoSuchColumnFamilyException(boolean coveredIndex) throws Exception {
    String tableName = schemaName + "." + generateUniqueName();
    String indexName = "IDX_" + generateUniqueName();
    String indexTableName = schemaName + "." + indexName;
    TableName physicalTableName = SchemaUtil.getPhysicalTableName(tableName.getBytes(), isNamespaceMapped);

    createBaseTable(tableName, null, null, coveredIndex ? "cf" : null);
    Connection conn1 = DriverManager.getConnection(getUrl());
    conn1.createStatement().execute("UPSERT INTO "+tableName+" values('b',1,2,4,'z')");
    conn1.createStatement().execute("UPSERT INTO "+tableName+" values('f',1,2,3,'z')");
    conn1.createStatement().execute("UPSERT INTO "+tableName+" values('j',2,4,2,'a')");
    conn1.createStatement().execute("UPSERT INTO "+tableName+" values('q',3,1,1,'c')");
    conn1.commit();
    Admin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
    TableDescriptor tableDescriptor = admin.getDescriptor(physicalTableName);
    tableDescriptor=TableDescriptorBuilder.newBuilder(tableDescriptor).addCoprocessor(DeleyOpenRegionObserver.class.getName(), null,
        QueryServicesOptions.DEFAULT_COPROCESSOR_PRIORITY - 1, null).build();
    admin.disableTable(physicalTableName);
    admin.modifyTable(tableDescriptor);
    admin.enableTable(physicalTableName);
    DeleyOpenRegionObserver.DELAY_OPEN = true;
    conn1.createStatement().execute(
        "CREATE LOCAL INDEX " + indexName + " ON " + tableName + "(k3)"
                + (coveredIndex ? " include(cf.v1)" : ""));
    DeleyOpenRegionObserver.DELAY_OPEN = false;
    ResultSet rs = conn1.createStatement().executeQuery("SELECT COUNT(*) FROM " + indexTableName);
    assertTrue(rs.next());
    assertEquals(4, rs.getInt(1));
}
 
Example 10
Source File: TestTableDescriptorModificationFromClient.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void verifyTableDescriptor(final TableName tableName, final byte[]... families)
    throws IOException {
  Admin admin = TEST_UTIL.getAdmin();

  // Verify descriptor from master
  HTableDescriptor htd = new HTableDescriptor(admin.getDescriptor(tableName));
  verifyTableDescriptor(htd, tableName, families);

  // Verify descriptor from HDFS
  MasterFileSystem mfs = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
  Path tableDir = CommonFSUtils.getTableDir(mfs.getRootDir(), tableName);
  TableDescriptor td =
      FSTableDescriptors.getTableDescriptorFromFs(mfs.getFileSystem(), tableDir);
  verifyTableDescriptor(td, tableName, families);
}
 
Example 11
Source File: AlterTableIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlterTableOnGlobalIndex() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl());
         Statement stmt = conn.createStatement()) {
        conn.setAutoCommit(false);
        Admin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
        String tableName = generateUniqueName();
        String globalIndexTableName = generateUniqueName();

        stmt.execute("CREATE TABLE " + tableName +
            " (ID INTEGER PRIMARY KEY, COL1 VARCHAR(10), COL2 BOOLEAN)");

        stmt.execute("CREATE INDEX " + globalIndexTableName + " on " + tableName + " (COL2)");
        TableDescriptor originalDesc = admin.getDescriptor(TableName.valueOf(globalIndexTableName));
        int expectedErrorCode = 0;
        try {
            stmt.execute("ALTER TABLE " + globalIndexTableName + " ADD CF1.AGE INTEGER ");
            conn.commit();
            fail("The alter table did not fail as expected");
        } catch (SQLException e) {
            assertEquals(e.getErrorCode(), CANNOT_MUTATE_TABLE.getErrorCode());
        }

        TableDescriptor finalDesc = admin.getDescriptor(TableName.valueOf(globalIndexTableName));
        assertTrue(finalDesc.equals(originalDesc));

        // drop the table
        stmt.execute("DROP TABLE " + tableName);
    }
}
 
Example 12
Source File: ChangeSplitPolicyAction.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void perform() throws Exception {
  HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
  Admin admin = util.getAdmin();

  getLogger().info("Performing action: Change split policy of table " + tableName);
  TableDescriptor tableDescriptor = admin.getDescriptor(tableName);
  TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableDescriptor);
  String chosenPolicy = possiblePolicies[random.nextInt(possiblePolicies.length)];
  builder.setRegionSplitPolicyClassName(chosenPolicy);
  getLogger().info("Changing "  + tableName + " split policy to " + chosenPolicy);
  admin.modifyTable(builder.build());
}
 
Example 13
Source File: TestRestoreSnapshotProcedure.java    From hbase with Apache License 2.0 4 votes vote down vote up
private void setupSnapshotAndUpdateTable() throws Exception {
  long tid = System.currentTimeMillis();
  final String snapshotName = "snapshot-" + tid;
  Admin admin = UTIL.getAdmin();
  // create Table
  SnapshotTestingUtils.createTable(UTIL, snapshotTableName, getNumReplicas(), CF1, CF2);
  // Load data
  SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF1, CF1);
  SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF2, CF2);
  SnapshotTestingUtils.verifyRowCount(UTIL, snapshotTableName, rowCountCF1 + rowCountCF2);

  snapshotHTD = admin.getDescriptor(snapshotTableName);

  admin.disableTable(snapshotTableName);
  // take a snapshot
  admin.snapshot(snapshotName, snapshotTableName);

  List<SnapshotDescription> snapshotList = admin.listSnapshots();
  snapshot = ProtobufUtil.createHBaseProtosSnapshotDesc(snapshotList.get(0));

  // modify the table
  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor columnFamilyDescriptor3 =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(CF3);
  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor columnFamilyDescriptor4 =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(CF4);
  admin.addColumnFamily(snapshotTableName, columnFamilyDescriptor3);
  admin.addColumnFamily(snapshotTableName, columnFamilyDescriptor4);
  admin.deleteColumnFamily(snapshotTableName, CF2);
  // enable table and insert data
  admin.enableTable(snapshotTableName);
  SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF3, CF3);
  SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF4, CF4);
  SnapshotTestingUtils.loadData(UTIL, snapshotTableName, rowCountCF1addition, CF1);
  HTableDescriptor currentHTD = new HTableDescriptor(admin.getDescriptor(snapshotTableName));
  assertTrue(currentHTD.hasFamily(CF1));
  assertFalse(currentHTD.hasFamily(CF2));
  assertTrue(currentHTD.hasFamily(CF3));
  assertTrue(currentHTD.hasFamily(CF4));
  assertNotEquals(currentHTD.getFamiliesKeys().size(), snapshotHTD.getColumnFamilies().length);
  SnapshotTestingUtils.verifyRowCount(
    UTIL, snapshotTableName, rowCountCF1 + rowCountCF3 + rowCountCF4 + rowCountCF1addition);
  admin.disableTable(snapshotTableName);
}
 
Example 14
Source File: IntegrationTestDDLMasterFailover.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
void perform() throws IOException {

  TableDescriptor selected = selectTable(enabledTables);
  if (selected == null) {
    return;
  }

  Admin admin = connection.getAdmin();
  try {
    TableName tableName = selected.getTableName();
    LOG.info("Disabling table :" + selected);
    admin.disableTable(tableName);
    Assert.assertTrue("Table: " + selected + " was not disabled",
        admin.isTableDisabled(tableName));
    TableDescriptor freshTableDesc = admin.getDescriptor(tableName);
    Assert.assertTrue(
      "After disable, Table: " + tableName + " is not disabled",
      admin.isTableDisabled(tableName));
    disabledTables.put(tableName, freshTableDesc);
    LOG.info("Disabled table :" + freshTableDesc);
  } catch (Exception e){
    LOG.warn("Caught exception in action: " + this.getClass());
    // TODO workaround
    // loose restriction for TableNotDisabledException/TableNotEnabledException thrown in sync
    // operations
    // 1) when enable/disable starts, the table state is changed to ENABLING/DISABLING (ZK node
    // in 1.x), which will be further changed to ENABLED/DISABLED once the operation completes
    // 2) if master failover happens in the middle of the enable/disable operation, the new
    // master will try to recover the tables in ENABLING/DISABLING state, as programmed in
    // AssignmentManager#recoverTableInEnablingState() and
    // AssignmentManager#recoverTableInDisablingState()
    // 3) after the new master initialization completes, the procedure tries to re-do the
    // enable/disable operation, which was already done. Ignore those exceptions before change
    // of behaviors of AssignmentManager in presence of PV2
    if (e instanceof TableNotEnabledException) {
      LOG.warn("Caught TableNotEnabledException in action: " + this.getClass());
      e.printStackTrace();
    } else {
      throw e;
    }
  } finally {
    admin.close();
  }
}
 
Example 15
Source File: IntegrationTestDDLMasterFailover.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
void perform() throws IOException {

  TableDescriptor selected = selectTable(disabledTables);
  if (selected == null ) {
    return;
  }

  Admin admin = connection.getAdmin();
  try {
    TableName tableName = selected.getTableName();
    LOG.info("Enabling table :" + selected);
    admin.enableTable(tableName);
    Assert.assertTrue("Table: " + selected + " was not enabled",
        admin.isTableEnabled(tableName));
    TableDescriptor freshTableDesc = admin.getDescriptor(tableName);
    Assert.assertTrue(
      "After enable, Table: " + tableName + " in not enabled", admin.isTableEnabled(tableName));
    enabledTables.put(tableName, freshTableDesc);
    LOG.info("Enabled table :" + freshTableDesc);
  } catch (Exception e){
    LOG.warn("Caught exception in action: " + this.getClass());
    // TODO workaround
    // loose restriction for TableNotDisabledException/TableNotEnabledException thrown in sync
    // operations 1) when enable/disable starts, the table state is changed to
    // ENABLING/DISABLING (ZK node in 1.x), which will be further changed to ENABLED/DISABLED
    // once the operation completes 2) if master failover happens in the middle of the
    // enable/disable operation, the new master will try to recover the tables in
    // ENABLING/DISABLING state, as programmed in
    // AssignmentManager#recoverTableInEnablingState() and
    // AssignmentManager#recoverTableInDisablingState()
    // 3) after the new master initialization completes, the procedure tries to re-do the
    // enable/disable operation, which was already done. Ignore those exceptions before
    // change of behaviors of AssignmentManager in presence of PV2
    if (e instanceof TableNotDisabledException) {
      LOG.warn("Caught TableNotDisabledException in action: " + this.getClass());
      e.printStackTrace();
    } else {
      throw e;
    }
  } finally {
    admin.close();
  }
}
 
Example 16
Source File: IntegrationTestDDLMasterFailover.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
void perform() throws IOException {
  TableDescriptor selected = selectTable(disabledTables);
  if (selected == null) {
    return;
  }
  ColumnFamilyDescriptor columnDesc = selectFamily(selected);
  if (columnDesc == null){
    return;
  }

  Admin admin = connection.getAdmin();
  int versions = RandomUtils.nextInt(0, 10) + 3;
  try {
    TableName tableName = selected.getTableName();
    LOG.info("Altering versions of column family: " + columnDesc + " to: " + versions +
        " in table: " + tableName);

    ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.newBuilder(columnDesc)
        .setMinVersions(versions)
        .setMaxVersions(versions)
        .build();
    TableDescriptor td = TableDescriptorBuilder.newBuilder(selected)
        .modifyColumnFamily(cfd)
        .build();
    admin.modifyTable(td);

    // assertion
    TableDescriptor freshTableDesc = admin.getDescriptor(tableName);
    ColumnFamilyDescriptor freshColumnDesc = freshTableDesc.getColumnFamily(columnDesc.getName());
    Assert.assertEquals("Column family: " + columnDesc + " was not altered",
        freshColumnDesc.getMaxVersions(), versions);
    Assert.assertEquals("Column family: " + freshColumnDesc + " was not altered",
        freshColumnDesc.getMinVersions(), versions);
    Assert.assertTrue(
      "After alter versions of column family, Table: " + tableName + " is not disabled",
      admin.isTableDisabled(tableName));
    disabledTables.put(tableName, freshTableDesc);
    LOG.info("Altered versions of column family: " + columnDesc + " to: " + versions +
      " in table: " + tableName);
  } catch (Exception e) {
    LOG.warn("Caught exception in action: " + this.getClass());
    throw e;
  } finally {
    admin.close();
  }
}
 
Example 17
Source File: IntegrationTestDDLMasterFailover.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
void perform() throws IOException {
  TableDescriptor selected = selectTable(disabledTables);
  if (selected == null) {
    return;
  }
  ColumnFamilyDescriptor columnDesc = selectFamily(selected);
  if (columnDesc == null){
    return;
  }

  Admin admin = connection.getAdmin();
  try {
    TableName tableName = selected.getTableName();
    // possible DataBlockEncoding ids
    DataBlockEncoding[] possibleIds = {DataBlockEncoding.NONE, DataBlockEncoding.PREFIX,
            DataBlockEncoding.DIFF, DataBlockEncoding.FAST_DIFF, DataBlockEncoding.ROW_INDEX_V1};
    short id = possibleIds[RandomUtils.nextInt(0, possibleIds.length)].getId();
    LOG.info("Altering encoding of column family: " + columnDesc + " to: " + id +
        " in table: " + tableName);

    ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.newBuilder(columnDesc)
        .setDataBlockEncoding(DataBlockEncoding.getEncodingById(id))
        .build();
    TableDescriptor td = TableDescriptorBuilder.newBuilder(selected)
        .modifyColumnFamily(cfd)
        .build();
    admin.modifyTable(td);

    // assertion
    TableDescriptor freshTableDesc = admin.getDescriptor(tableName);
    ColumnFamilyDescriptor freshColumnDesc = freshTableDesc.getColumnFamily(columnDesc.getName());
    Assert.assertEquals("Encoding of column family: " + columnDesc + " was not altered",
        freshColumnDesc.getDataBlockEncoding().getId(), id);
    Assert.assertTrue(
      "After alter encoding of column family, Table: " + tableName + " is not disabled",
      admin.isTableDisabled(tableName));
    disabledTables.put(tableName, freshTableDesc);
    LOG.info("Altered encoding of column family: " + freshColumnDesc + " to: " + id +
      " in table: " + tableName);
  } catch (Exception e) {
    LOG.warn("Caught exception in action: " + this.getClass());
    throw e;
  } finally {
    admin.close();
  }
}
 
Example 18
Source File: TestAccessController.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testSnapshotWithOwner() throws Exception {
  Admin admin = TEST_UTIL.getAdmin();
  final HTableDescriptor htd = new HTableDescriptor(admin.getDescriptor(TEST_TABLE));
  final SnapshotDescription snapshot = new SnapshotDescription(
      TEST_TABLE.getNameAsString() + "-snapshot", TEST_TABLE, null, USER_OWNER.getName());

  AccessTestAction snapshotAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV),
          snapshot, htd);
      return null;
    }
  };
  verifyAllowed(snapshotAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
  verifyDenied(snapshotAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
    USER_GROUP_WRITE, USER_GROUP_CREATE);

  AccessTestAction deleteAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preDeleteSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV),
        snapshot);
      return null;
    }
  };
  verifyAllowed(deleteAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
  verifyDenied(deleteAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
    USER_GROUP_WRITE, USER_GROUP_CREATE);

  AccessTestAction restoreAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preRestoreSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV),
        snapshot, htd);
      return null;
    }
  };
  verifyAllowed(restoreAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
  verifyDenied(restoreAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
    USER_GROUP_WRITE, USER_GROUP_CREATE);

  AccessTestAction cloneAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preCloneSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV),
        snapshot, htd);
      return null;
    }
  };
  verifyAllowed(cloneAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN, USER_OWNER);
  verifyDenied(cloneAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
    USER_GROUP_WRITE, USER_GROUP_CREATE);
}
 
Example 19
Source File: IntegrationTestDDLMasterFailover.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
void perform() throws IOException {
  TableDescriptor selected = selectTable(enabledTables);
  if (selected == null ) {
    return;
  }

  Admin admin = connection.getAdmin();
  TableName tableName = selected.getTableName();
  try (Table table = connection.getTable(tableName)){
    ArrayList<RegionInfo> regionInfos = new ArrayList<>(admin.getRegions(
        selected.getTableName()));
    int numRegions = regionInfos.size();
    // average number of rows to be added per action to each region
    int average_rows = 1;
    int numRows = average_rows * numRegions;
    LOG.info("Adding " + numRows + " rows to table: " + selected);
    for (int i = 0; i < numRows; i++){
      // nextInt(Integer.MAX_VALUE)) to return positive numbers only
      byte[] rowKey = Bytes.toBytes(
          "row-" + String.format("%010d", RandomUtils.nextInt()));
      ColumnFamilyDescriptor cfd = selectFamily(selected);
      if (cfd == null){
        return;
      }
      byte[] family = cfd.getName();
      byte[] qualifier = Bytes.toBytes("col-" + RandomUtils.nextInt() % 10);
      byte[] value = Bytes.toBytes("val-" + RandomStringUtils.randomAlphanumeric(10));
      Put put = new Put(rowKey);
      put.addColumn(family, qualifier, value);
      table.put(put);
    }
    TableDescriptor freshTableDesc = admin.getDescriptor(tableName);
    Assert.assertTrue(
      "After insert, Table: " + tableName + " in not enabled", admin.isTableEnabled(tableName));
    enabledTables.put(tableName, freshTableDesc);
    LOG.info("Added " + numRows + " rows to table: " + selected);
  } catch (Exception e) {
    LOG.warn("Caught exception in action: " + this.getClass());
    throw e;
  } finally {
    admin.close();
  }
}
 
Example 20
Source File: TestAccessController.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testSnapshot() throws Exception {
  Admin admin = TEST_UTIL.getAdmin();
  final HTableDescriptor htd = new HTableDescriptor(admin.getDescriptor(TEST_TABLE));
  final SnapshotDescription snapshot = new SnapshotDescription(
      TEST_TABLE.getNameAsString() + "-snapshot", TEST_TABLE);
  AccessTestAction snapshotAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV),
        snapshot, htd);
      return null;
    }
  };

  AccessTestAction deleteAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preDeleteSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV),
        snapshot);
      return null;
    }
  };

  AccessTestAction restoreAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preRestoreSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV),
        snapshot, htd);
      return null;
    }
  };

  AccessTestAction cloneAction = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preCloneSnapshot(ObserverContextImpl.createAndPrepare(CP_ENV),
        snapshot, null);
      return null;
    }
  };

  verifyAllowed(snapshotAction, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
  verifyDenied(snapshotAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
    USER_GROUP_WRITE, USER_GROUP_CREATE);

  verifyAllowed(cloneAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
  verifyDenied(deleteAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
    USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);

  verifyAllowed(restoreAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
  verifyDenied(restoreAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
    USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);

  verifyAllowed(deleteAction, SUPERUSER, USER_ADMIN, USER_GROUP_ADMIN);
  verifyDenied(cloneAction, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_OWNER,
    USER_GROUP_READ, USER_GROUP_WRITE, USER_GROUP_CREATE);
}