org.apache.hadoop.hbase.client.Table Java Examples

The following examples show how to use org.apache.hadoop.hbase.client.Table. 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: HfileBulkImporter.java    From super-cloudops with Apache License 2.0 7 votes vote down vote up
/**
 * e.g.</br>
 * 
 * <pre>
 *  yarn jar super-devops-tool-hbase-migrator-master.jar \
 *  com.wl4g.devops.tool.hbase.migrator.HfileBulkImporter \
 *  -z emr-header-1:2181 \
 *  -t safeclound.tb_elec_power \
 *  -p /tmp-devops/safeclound.tb_elec_power
 * </pre>
 * 
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
	HbaseMigrateUtils.showBanner();

	CommandLine line = new Builder().option("z", "zkaddr", null, "Zookeeper address.")
			.option("t", "tabname", null, "Hbase table name.")
			.option("p", "path", null, "Data hdfs path to be import. e.g. hdfs://localhost:9000/bak/safeclound.tb_air")
			.build(args);

	Configuration cfg = HBaseConfiguration.create();
	cfg.set("hbase.zookeeper.quorum", line.getOptionValue("z"));
	Connection conn = ConnectionFactory.createConnection(cfg);
	Admin admin = conn.getAdmin();
	Table table = conn.getTable(TableName.valueOf(line.getOptionValue("t")));
	LoadIncrementalHFiles load = new LoadIncrementalHFiles(cfg);
	load.doBulkLoad(new Path(line.getOptionValue("p")), admin, table,
			conn.getRegionLocator(TableName.valueOf(line.getOptionValue("t"))));
}
 
Example #2
Source File: TestDeleteMobTable.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteNonMobTable() throws Exception {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  TableDescriptor htd = createTableDescriptor(tableName, false);
  ColumnFamilyDescriptor hcd = htd.getColumnFamily(FAMILY);

  Table table = createTableWithOneFile(htd);
  try {
    // the mob file doesn't exist
    Assert.assertEquals(0, countMobFiles(tableName, hcd.getNameAsString()));
    Assert.assertEquals(0, countArchiveMobFiles(tableName, hcd.getNameAsString()));
    Assert.assertFalse(mobTableDirExist(tableName));
  } finally {
    table.close();
    TEST_UTIL.deleteTable(tableName);
  }

  Assert.assertFalse(TEST_UTIL.getAdmin().tableExists(tableName));
  Assert.assertEquals(0, countMobFiles(tableName, hcd.getNameAsString()));
  Assert.assertEquals(0, countArchiveMobFiles(tableName, hcd.getNameAsString()));
  Assert.assertFalse(mobTableDirExist(tableName));
}
 
Example #3
Source File: ServerUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private static Table getTableFromSingletonPool(RegionCoprocessorEnvironment env, TableName tableName) throws IOException {
    // It's ok to not ever do a pool.close() as we're storing a single
    // table only. The HTablePool holds no other resources that this table
    // which will be closed itself when it's no longer needed.
    Connection conn = ConnectionFactory.getConnection(ConnectionType.DEFAULT_SERVER_CONNECTION, env);
    try {
        return conn.getTable(tableName);
    } catch (RuntimeException t) {
        // handle cases that an IOE is wrapped inside a RuntimeException like HTableInterface#createHTableInterface
        if(t.getCause() instanceof IOException) {
            throw (IOException)t.getCause();
        } else {
            throw t;
        }
    }
}
 
Example #4
Source File: Reads.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static void readRow(String projectId, String instanceId, String tableId) {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
    Table table = connection.getTable(TableName.valueOf(tableId));

    byte[] rowkey = Bytes.toBytes("phone#4c410523#20190501");

    Result row = table.get(new Get(rowkey));
    printRow(row);

  } catch (IOException e) {
    System.out.println(
        "Unable to initialize service client, as a network error occurred: \n" + e.toString());
  }
}
 
Example #5
Source File: TestEndToEndSplitTransaction.java    From hbase with Apache License 2.0 6 votes vote down vote up
/** verify region boundaries obtained from HTable.getStartEndKeys() */
void verifyRegionsUsingHTable() throws IOException {
  Table table = null;
  try {
    // HTable.getStartEndKeys()
    table = connection.getTable(tableName);

    try (RegionLocator rl = connection.getRegionLocator(tableName)) {
      Pair<byte[][], byte[][]> keys = rl.getStartEndKeys();
      verifyStartEndKeys(keys);

      Set<RegionInfo> regions = new TreeSet<>(RegionInfo.COMPARATOR);
      for (HRegionLocation loc : rl.getAllRegionLocations()) {
        regions.add(loc.getRegion());
      }
      verifyTableRegions(regions);
    }

  } finally {
    IOUtils.closeQuietly(table);
  }
}
 
Example #6
Source File: HBaseResourceStore.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void deleteResourceImpl(String resPath, long timestamp) throws IOException {
    Table table = getConnection().getTable(TableName.valueOf(tableName));
    try {
        boolean hdfsResourceExist = isHdfsResourceExist(table, resPath);
        long origLastModified = getResourceLastModified(table, resPath);
        if (checkTimeStampBeforeDelete(origLastModified, timestamp)) {
            Delete del = new Delete(Bytes.toBytes(resPath));
            table.delete(del);

            if (hdfsResourceExist) { // remove hdfs cell value
                deletePushdown(resPath);
            }
        } else {
            throw new IOException("Resource " + resPath + " timestamp not match, [originLastModified: "
                    + origLastModified + ", timestampToDelete: " + timestamp + "]");
        }

    } finally {
        IOUtils.closeQuietly(table);
    }
}
 
Example #7
Source File: TestHBase_2_ClientService.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testScanWithInvalidFilter() throws InitializationException, IOException {
    final String tableName = "nifi";
    final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);

    // Mock an HBase Table so we can verify the put operations later
    final Table table = Mockito.mock(Table.class);
    when(table.getName()).thenReturn(TableName.valueOf(tableName));

    // create the controller service and link it to the test processor
    final MockHBaseClientService service = configureHBaseClientService(runner, table);
    runner.assertValid(service);

    // perform a scan and verify the four rows were returned
    final CollectingResultHandler handler = new CollectingResultHandler();
    final HBaseClientService hBaseClientService = runner.getProcessContext().getProperty(TestProcessor.HBASE_CLIENT_SERVICE)
            .asControllerService(HBaseClientService.class);

    // this should throw IllegalArgumentException
    final String filter = "this is not a filter";
    hBaseClientService.scan(tableName, new ArrayList<Column>(), filter, System.currentTimeMillis(), handler);
}
 
Example #8
Source File: IndexToolForDeleteBeforeRebuildIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
/**
 * Test delete before rebuild
 */
public void testDeleteBeforeRebuildForGlobalIndex() throws Exception {
    conn.createStatement().execute(String.format(INDEX_GLOBAL_DDL, globalIndexName, dataTableFullName));
    String globalIndexUpsert = String.format(UPSERT_SQL, globalIndexFullName);
    PreparedStatement stmt = conn.prepareStatement(globalIndexUpsert);
    upsertRow(stmt, "tenantID1",11, "name11", 99911);
    conn.commit();

    ConnectionQueryServices queryServices = conn.unwrap(PhoenixConnection.class).getQueryServices();
    PTable physicalTable = PhoenixRuntime.getTable(conn, globalIndexFullName);
    Table hIndexTable= queryServices.getTable(physicalTable.getPhysicalName().getBytes());
    int count = getUtility().countRows(hIndexTable);
    // Confirm index has rows.
    assertEquals(4, count);

    runIndexTool(schemaName, dataTableName, globalIndexName, 0);

    count = getUtility().countRows(hIndexTable);

    // Confirm index has all the data rows
    assertEquals(3, count);
}
 
Example #9
Source File: TephraTransactionContext.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public Table getTransactionalTableWriter(PhoenixConnection connection, PTable table, Table htable, boolean isIndex) throws SQLException {
    // If we have indexes, wrap the HTable in a delegate HTable that
    // will attach the necessary index meta data in the event of a
    // rollback
    TransactionAwareHTable transactionAwareHTable;
    // Don't add immutable indexes (those are the only ones that would participate
    // during a commit), as we don't need conflict detection for these.
    if (isIndex) {
        transactionAwareHTable = new TransactionAwareHTable(htable, TxConstants.ConflictDetection.NONE);
        transactionAwareHTable.startTx(getTransaction());
    } else {
        htable = new RollbackHookHTableWrapper(htable, table, connection);
        transactionAwareHTable = new TransactionAwareHTable(htable, table.isImmutableRows() ? TxConstants.ConflictDetection.NONE : TxConstants.ConflictDetection.ROW);
        // Even for immutable, we need to do this so that an abort has the state
        // necessary to generate the rows to delete.
        this.addTransactionAware(transactionAwareHTable);
    }
    return transactionAwareHTable;
}
 
Example #10
Source File: UngroupedAggregateRegionObserver.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void separateLocalAndRemoteMutations(Table targetHTable, Region region, List<Mutation> mutations,
                                             List<Mutation> localRegionMutations, List<Mutation> remoteRegionMutations,
                                             boolean isPKChanging){
    boolean areMutationsInSameTable = areMutationsInSameTable(targetHTable, region);
    //if we're writing to the same table, but the PK can change, that means that some
    //mutations might be in our current region, and others in a different one.
    if (areMutationsInSameTable && isPKChanging) {
        RegionInfo regionInfo = region.getRegionInfo();
        for (Mutation mutation : mutations){
            if (regionInfo.containsRow(mutation.getRow())){
                localRegionMutations.add(mutation);
            } else {
                remoteRegionMutations.add(mutation);
            }
        }
    } else if (areMutationsInSameTable && !isPKChanging) {
        localRegionMutations.addAll(mutations);
    } else {
        remoteRegionMutations.addAll(mutations);
    }
}
 
Example #11
Source File: DataJanitorState.java    From phoenix-tephra with Apache License 2.0 6 votes vote down vote up
/**
 * Return regions that were recorded as empty after the given time.
 *
 * @param time time in milliseconds
 * @param includeRegions If not null, the returned set will be an intersection of the includeRegions set
 *                       and the empty regions after the given time
 */
public SortedSet<byte[]> getEmptyRegionsAfterTime(long time, @Nullable SortedSet<byte[]> includeRegions)
  throws IOException {
  SortedSet<byte[]> emptyRegions = new TreeSet<>(Bytes.BYTES_COMPARATOR);
  try (Table stateTable = stateTableSupplier.get()) {
    Scan scan = new Scan(makeEmptyRegionTimeKey(Bytes.toBytes(time + 1), EMPTY_BYTE_ARRAY),
                         EMPTY_REGION_TIME_KEY_PREFIX_STOP);
    scan.addColumn(FAMILY, EMPTY_REGION_TIME_COL);

    try (ResultScanner scanner = stateTable.getScanner(scan)) {
      Result next;
      while ((next = scanner.next()) != null) {
        byte[] emptyRegion = getEmptyRegionFromKey(next.getRow());
        if (includeRegions == null || includeRegions.contains(emptyRegion)) {
          emptyRegions.add(emptyRegion);
        }
      }
    }
  }
  return Collections.unmodifiableSortedSet(emptyRegions);
}
 
Example #12
Source File: TestShadedHBaseTestingUtility.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateTable() throws Exception {
  TableName tableName = TableName.valueOf("test");

  Table table = TEST_UTIL.createTable(tableName, "cf");

  Put put1 = new Put(Bytes.toBytes("r1"));
  put1.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("c"), Bytes.toBytes(1));
  table.put(put1);

  Put put2 = new Put(Bytes.toBytes("r2"));
  put2.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("c"), Bytes.toBytes(2));
  table.put(put2);

  int rows = TEST_UTIL.countRows(tableName);
  assertEquals(2, rows);
}
 
Example #13
Source File: TestMetaTableAccessor.java    From hbase with Apache License 2.0 6 votes vote down vote up
public static void assertMetaLocation(Table meta, byte[] row, ServerName serverName, long seqNum,
  int replicaId, boolean checkSeqNum) throws IOException {
  Get get = new Get(row);
  Result result = meta.get(get);
  assertTrue(Bytes.equals(
    result.getValue(HConstants.CATALOG_FAMILY, CatalogFamilyFormat.getServerColumn(replicaId)),
    Bytes.toBytes(serverName.getAddress().toString())));
  assertTrue(Bytes.equals(
    result.getValue(HConstants.CATALOG_FAMILY, CatalogFamilyFormat.getStartCodeColumn(replicaId)),
    Bytes.toBytes(serverName.getStartcode())));
  if (checkSeqNum) {
    assertTrue(Bytes.equals(
      result.getValue(HConstants.CATALOG_FAMILY, CatalogFamilyFormat.getSeqNumColumn(replicaId)),
      Bytes.toBytes(seqNum)));
  }
}
 
Example #14
Source File: TestChangingEncoding.java    From hbase with Apache License 2.0 6 votes vote down vote up
static void writeTestDataBatch(TableName tableName,
    int batchId) throws Exception {
  LOG.debug("Writing test data batch " + batchId);
  List<Put> puts = new ArrayList<>();
  for (int i = 0; i < NUM_ROWS_PER_BATCH; ++i) {
    Put put = new Put(getRowKey(batchId, i));
    for (int j = 0; j < NUM_COLS_PER_ROW; ++j) {
      put.addColumn(CF_BYTES, getQualifier(j), getValue(batchId, i, j));
    }
    put.setDurability(Durability.SKIP_WAL);
    puts.add(put);
  }
  try (Connection conn = ConnectionFactory.createConnection(conf);
      Table table = conn.getTable(tableName)) {
    table.put(puts);
  }
}
 
Example #15
Source File: HBaseIndexTest.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@Test
public void testPopulateVertexIndex() throws Exception {
    assertEquals(0, count(graph.vertices()));
    graph.addVertex(T.id, id(10), T.label, "a", "key1", 11);
    graph.addVertex(T.id, id(11), T.label, "a", "key1", 12);
    graph.addVertex(T.id, id(12), T.label, "a", "key2", 12);
    graph.addVertex(T.id, id(13), T.label, "a", "key1", 11);
    graph.addVertex(T.id, id(14), T.label, "b", "key1", 11);


    HBaseGraphConfiguration hconf = graph.configuration();
    Connection conn = graph.connection();
    Table table = conn.getTable(HBaseGraphUtils.getTableName(hconf, Constants.VERTEX_INDICES));

    graph.createIndex(ElementType.VERTEX, "a", "key1", false, true, false);
    verifyTableCount(table, 3);

    table.close();
}
 
Example #16
Source File: TestCompactionWithThroughputController.java    From hbase with Apache License 2.0 6 votes vote down vote up
private HStore prepareData() throws IOException {
  Admin admin = TEST_UTIL.getAdmin();
  if (admin.tableExists(tableName)) {
    admin.disableTable(tableName);
    admin.deleteTable(tableName);
  }
  Table table = TEST_UTIL.createTable(tableName, family);
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      byte[] value = new byte[128 * 1024];
      ThreadLocalRandom.current().nextBytes(value);
      table.put(new Put(Bytes.toBytes(i * 10 + j)).addColumn(family, qualifier, value));
    }
    admin.flush(tableName);
  }
  return getStoreWithName(tableName);
}
 
Example #17
Source File: TestReplicationStuckWithDeletedTableCFs.java    From hbase with Apache License 2.0 6 votes vote down vote up
private void verifyReplicationStuck() throws Exception {
  try (Table normalTable = utility1.getConnection().getTable(TABLE)) {
    Put put = new Put(ROW);
    put.addColumn(NORMAL_FAMILY, QUALIFIER, VALUE);
    normalTable.put(put);
  }
  try (Table normalTable = utility2.getConnection().getTable(TABLE)) {
    for (int i = 0; i < NB_RETRIES; i++) {
      Result result = normalTable.get(new Get(ROW).addColumn(NORMAL_FAMILY, QUALIFIER));
      if (result != null && !result.isEmpty()) {
        fail("Edit should have been stuck behind dropped tables, but value is " + Bytes
            .toString(result.getValue(NORMAL_FAMILY, QUALIFIER)));
      } else {
        LOG.info("Row not replicated, let's wait a bit more...");
        Thread.sleep(SLEEP_TIME);
      }
    }
  }
}
 
Example #18
Source File: TestVisibilityLabelsWithACL.java    From hbase with Apache License 2.0 6 votes vote down vote up
private static Table createTableAndWriteDataWithLabels(TableName tableName, String... labelExps)
    throws Exception {
  Table table = null;
  try {
    table = TEST_UTIL.createTable(tableName, fam);
    int i = 1;
    List<Put> puts = new ArrayList<>(labelExps.length);
    for (String labelExp : labelExps) {
      Put put = new Put(Bytes.toBytes("row" + i));
      put.addColumn(fam, qual, HConstants.LATEST_TIMESTAMP, value);
      put.setCellVisibility(new CellVisibility(labelExp));
      puts.add(put);
      i++;
    }
    table.put(puts);
  } finally {
    if (table != null) {
      table.close();
    }
  }
  return table;
}
 
Example #19
Source File: SMRecordReaderImplIT.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void twoRegionsWithMemstores() throws Exception {
  List<String> names = new ArrayList<String>();
  names.add("COL1");
  names.add("COL2");
  config.set(MRConstants.SPLICE_SCAN_INFO, sqlUtil.getTableScannerBuilder(SMRecordReaderImplIT.class.getSimpleName() + ".C", names).base64Encode());
  SMRecordReaderImpl rr = new SMRecordReaderImpl(config);
  TableName tableName = TableName.valueOf(sqlUtil.getConglomID(SMRecordReaderImplIT.class.getSimpleName() + ".C"));
  try (Connection connection = ConnectionFactory.createConnection(config)) {
    Table table = connection.getTable(tableName);
    Scan scan = new Scan();
    rr.setHTable(table);
    rr.setScan(scan);
    SMSplit tableSplit = new SMSplit(new TableSplit(tableName, scan.getStartRow(), scan.getStopRow(), "sdfsdf"));
    rr.initialize(tableSplit, null);
    int i = 0;
    while (rr.nextKeyValue()) {
      i++;
      Assert.assertNotNull("Column 1 is null", rr.getCurrentValue().getColumn(1));
      Assert.assertNotNull("Column 2 is null", rr.getCurrentValue().getColumn(2));
      Assert.assertNotNull("Current Key is null", rr.getCurrentKey());
    }
    Assert.assertEquals("incorrect results returned", 10000, i);
  }
}
 
Example #20
Source File: TestVisibilityLabelsWithACL.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetForSuperUserWithFewerLabelAuths() throws Throwable {
  String[] auths = { SECRET };
  String user = "admin";
  VisibilityClient.setAuths(TEST_UTIL.getConnection(), auths, user);
  TableName tableName = TableName.valueOf(TEST_NAME.getMethodName());
  final Table table = createTableAndWriteDataWithLabels(tableName, SECRET + "&" + CONFIDENTIAL
      + "&!" + PRIVATE, SECRET + "&!" + PRIVATE);
  PrivilegedExceptionAction<Void> scanAction = new PrivilegedExceptionAction<Void>() {
    @Override
    public Void run() throws Exception {
      Get g = new Get(row1);
      g.setAuthorizations(new Authorizations(SECRET, CONFIDENTIAL));
      try (Connection connection = ConnectionFactory.createConnection(conf);
           Table t = connection.getTable(table.getName())) {
        Result result = t.get(g);
        assertTrue(!result.isEmpty());
      }
      return null;
    }
  };
  SUPERUSER.runAs(scanAction);
}
 
Example #21
Source File: JobHistoryRawService.java    From hraven with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the raw job configuration stored for the given cluster and job ID
 * @param jobId the cluster and job ID to look up
 * @return the stored job configuration
 * @throws IOException
 */
public Configuration getRawJobConfiguration(QualifiedJobId jobId)
    throws IOException {
  Configuration conf = null;
  byte[] rowKey = idConv.toBytes(jobId);
  Get get = new Get(rowKey);
  get.addColumn(Constants.RAW_FAM_BYTES, Constants.JOBCONF_COL_BYTES);
  Table rawTable = null;
  try {
    rawTable = hbaseConnection
        .getTable(TableName.valueOf(Constants.HISTORY_RAW_TABLE));
    Result result = rawTable.get(get);
    if (result != null && !result.isEmpty()) {
      conf = createConfigurationFromResult(result);
    }
  } catch (MissingColumnInResultException e) {
    LOG.error(
        "Failed to retrieve configuration from row returned for " + jobId, e);
  } finally {
    if (rawTable != null) {
      rawTable.close();
    }
  }
  return conf;
}
 
Example #22
Source File: HBaseResourceStore.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
protected void deleteResourceImpl(String resPath) throws IOException {
    Table table = getConnection().getTable(TableName.valueOf(tableName));
    try {
        boolean hdfsResourceExist = isHdfsResourceExist(table, resPath);

        Delete del = new Delete(Bytes.toBytes(resPath));
        table.delete(del);

        if (hdfsResourceExist) { // remove hdfs cell value
            deletePushdown(resPath);
        }
    } finally {
        IOUtils.closeQuietly(table);
    }
}
 
Example #23
Source File: TestSplitWALProcedure.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testHandleDeadWorker() throws Exception {
  Table table = TEST_UTIL.createTable(TABLE_NAME, FAMILY, TEST_UTIL.KEYS_FOR_HBA_CREATE_TABLE);
  for (int i = 0; i < 10; i++) {
    TEST_UTIL.loadTable(table, FAMILY);
  }
  HRegionServer testServer = TEST_UTIL.getHBaseCluster().getRegionServer(0);
  ProcedureExecutor<MasterProcedureEnv> masterPE = master.getMasterProcedureExecutor();
  List<FileStatus> wals = splitWALManager.getWALsToSplit(testServer.getServerName(), false);
  Assert.assertEquals(1, wals.size());
  TEST_UTIL.getHBaseCluster().killRegionServer(testServer.getServerName());
  TEST_UTIL.waitFor(30000, () -> master.getProcedures().stream()
      .anyMatch(procedure -> procedure instanceof SplitWALProcedure));
  Procedure splitWALProcedure = master.getProcedures().stream()
      .filter(procedure -> procedure instanceof SplitWALProcedure).findAny().get();
  Assert.assertNotNull(splitWALProcedure);
  TEST_UTIL.waitFor(5000, () -> ((SplitWALProcedure) splitWALProcedure).getWorker() != null);
  TEST_UTIL.getHBaseCluster()
      .killRegionServer(((SplitWALProcedure) splitWALProcedure).getWorker());
  ProcedureTestingUtility.waitProcedure(masterPE, splitWALProcedure.getProcId());
  Assert.assertTrue(splitWALProcedure.isSuccess());
  ProcedureTestingUtility.waitAllProcedures(masterPE);
}
 
Example #24
Source File: BackupSystemTable.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Exclusive operations are: create, delete, merge
 * @throws IOException if a table operation fails or an active backup exclusive operation is
 *           already underway
 */
public void startBackupExclusiveOperation() throws IOException {
  LOG.debug("Start new backup exclusive operation");

  try (Table table = connection.getTable(tableName)) {
    Put put = createPutForStartBackupSession();
    // First try to put if row does not exist
    if (!table.checkAndMutate(ACTIVE_SESSION_ROW, SESSIONS_FAMILY).qualifier(ACTIVE_SESSION_COL)
        .ifNotExists().thenPut(put)) {
      // Row exists, try to put if value == ACTIVE_SESSION_NO
      if (!table.checkAndMutate(ACTIVE_SESSION_ROW, SESSIONS_FAMILY).qualifier(ACTIVE_SESSION_COL)
          .ifEquals(ACTIVE_SESSION_NO).thenPut(put)) {
        throw new ExclusiveOperationException();
      }
    }
  }
}
 
Example #25
Source File: TestConstraint.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Check to make sure a constraint is unloaded when it fails
 * @throws Exception
 */
@Test
public void testIsUnloaded() throws Exception {
  // create the table
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);

  // add a family to the table
  for (byte[] family : new byte[][]{dummy, test}) {
    tableDescriptor.setColumnFamily(
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(family));
  }
  // make sure that constraints are unloaded
  Constraints.add(tableDescriptor, RuntimeFailConstraint.class);
  // add a constraint to check to see if is run
  Constraints.add(tableDescriptor, CheckWasRunConstraint.class);
  CheckWasRunConstraint.wasRun = false;

  util.getAdmin().createTable(tableDescriptor);
  Table table = util.getConnection().getTable(tableName);

  // test that we do fail on violation
  Put put = new Put(row1);
  byte[] qualifier = new byte[0];
  put.addColumn(dummy, qualifier, Bytes.toBytes("pass"));

  try{
  table.put(put);
  fail("RuntimeFailConstraint wasn't triggered - this put shouldn't work!");
  } catch (Exception e) {// NOOP
  }

  // try the put again, this time constraints are not used, so it works
  table.put(put);
  // and we make sure that constraints were not run...
  assertFalse(CheckWasRunConstraint.wasRun);
  table.close();
}
 
Example #26
Source File: TestRowProcessorEndpoint.java    From hbase with Apache License 2.0 5 votes vote down vote up
private int incrementCounter(Table table) throws Throwable {
  CoprocessorRpcChannel channel = table.coprocessorService(ROW);
  RowProcessorEndpoint.IncrementCounterProcessor processor =
      new RowProcessorEndpoint.IncrementCounterProcessor(ROW);
  RowProcessorService.BlockingInterface service =
      RowProcessorService.newBlockingStub(channel);
  ProcessRequest request = RowProcessorClient.getRowProcessorPB(processor);
  ProcessResponse protoResult = service.process(null, request);
  IncCounterProcessorResponse response = IncCounterProcessorResponse
      .parseFrom(protoResult.getRowProcessorResult());
  Integer result = response.getResponse();
  return result;
}
 
Example #27
Source File: TestCoprocessorWhitelistMasterObserver.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Test a table modification adding a coprocessor path
 * which is not whitelisted.
 * @exception Exception should be thrown and caught
 *         to show coprocessor is working as desired
 * @param whitelistedPaths A String array of paths to add in
 *         for the whitelisting configuration
 * @param coprocessorPath A String to use as the
 *         path for a mock coprocessor
 */
private static void positiveTestCase(String[] whitelistedPaths,
    String coprocessorPath) throws Exception {
  Configuration conf = UTIL.getConfiguration();
  // load coprocessor under test
  conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
      CoprocessorWhitelistMasterObserver.class.getName());
  conf.setStrings(
      CoprocessorWhitelistMasterObserver.CP_COPROCESSOR_WHITELIST_PATHS_KEY,
      whitelistedPaths);
  // set retries low to raise exception quickly
  conf.setInt("hbase.client.retries.number", 5);
  UTIL.startMiniCluster();
  UTIL.createTable(TEST_TABLE, new byte[][] { TEST_FAMILY });
  UTIL.waitUntilAllRegionsAssigned(TEST_TABLE);
  Connection connection = ConnectionFactory.createConnection(conf);
  Table t = connection.getTable(TEST_TABLE);
  HTableDescriptor htd = new HTableDescriptor(t.getDescriptor());
  htd.addCoprocessor("net.clayb.hbase.coprocessor.NotWhitelisted",
    new Path(coprocessorPath),
    Coprocessor.PRIORITY_USER, null);
  LOG.info("Modifying Table");
  try {
    connection.getAdmin().modifyTable(htd);
    fail("Expected coprocessor to raise IOException");
  } catch (IOException e) {
    // swallow exception from coprocessor
  }
  LOG.info("Done Modifying Table");
  assertEquals(0, t.getDescriptor().getCoprocessorDescriptors().size());
}
 
Example #28
Source File: TestRegionServerAbortTimeout.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testAbortTimeout() throws Exception {
  Thread writer = new Thread(() -> {
    try {
      try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) {
        for (int i = 0; i < 10000; i++) {
          table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
        }
      }
    } catch (IOException e) {
      LOG.warn("Failed to load data");
    }
  });
  writer.setDaemon(true);
  writer.start();

  // Abort one region server
  UTIL.getMiniHBaseCluster().getRegionServer(0).abort("Abort RS for test");

  long startTime = System.currentTimeMillis();
  long timeout = REGIONS_NUM * SLEEP_TIME_WHEN_CLOSE_REGION * 10;
  while (System.currentTimeMillis() - startTime < timeout) {
    if (UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size() == 1) {
      assertTrue("Abort timer task should be scheduled", abortTimeoutTaskScheduled);
      return;
    }
    Threads.sleep(SLEEP_TIME_WHEN_CLOSE_REGION);
  }
  fail("Failed to abort a region server in " + timeout + " ms");
}
 
Example #29
Source File: TestMetaTableAccessor.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static void assertEmptyMetaLocation(Table meta, byte[] row, int replicaId)
  throws IOException {
  Get get = new Get(row);
  Result result = meta.get(get);
  Cell serverCell = result.getColumnLatestCell(HConstants.CATALOG_FAMILY,
    CatalogFamilyFormat.getServerColumn(replicaId));
  Cell startCodeCell = result.getColumnLatestCell(HConstants.CATALOG_FAMILY,
    CatalogFamilyFormat.getStartCodeColumn(replicaId));
  assertNotNull(serverCell);
  assertNotNull(startCodeCell);
  assertEquals(0, serverCell.getValueLength());
  assertEquals(0, startCodeCell.getValueLength());
}
 
Example #30
Source File: VisibilityClient.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param connection the Connection instance to use.
 * @param user
 * @return labels, the given user is globally authorized for.
 * @throws Throwable
 */
public static GetAuthsResponse getAuths(Connection connection, final String user)
    throws Throwable {
  try (Table table = connection.getTable(LABELS_TABLE_NAME)) {
    Batch.Call<VisibilityLabelsService, GetAuthsResponse> callable =
        new Batch.Call<VisibilityLabelsService, GetAuthsResponse>() {
      ServerRpcController controller = new ServerRpcController();
      CoprocessorRpcUtils.BlockingRpcCallback<GetAuthsResponse> rpcCallback =
          new CoprocessorRpcUtils.BlockingRpcCallback<>();

      @Override
      public GetAuthsResponse call(VisibilityLabelsService service) throws IOException {
        GetAuthsRequest.Builder getAuthReqBuilder = GetAuthsRequest.newBuilder();
        getAuthReqBuilder.setUser(UnsafeByteOperations.unsafeWrap(Bytes.toBytes(user)));
        service.getAuths(controller, getAuthReqBuilder.build(), rpcCallback);
        GetAuthsResponse response = rpcCallback.get();
        if (controller.failedOnException()) {
          throw controller.getFailedOn();
        }
        return response;
      }
    };
    Map<byte[], GetAuthsResponse> result =
        table.coprocessorService(VisibilityLabelsService.class, HConstants.EMPTY_BYTE_ARRAY,
            HConstants.EMPTY_BYTE_ARRAY, callable);
    return result.values().iterator().next(); // There will be exactly one region for labels
    // table and so one entry in result Map.
  }
}