/**
  * Calls {@link #getMockedConnection(Configuration)} and then mocks a few more of the popular
  * {@link ClusterConnection} methods so they do 'normal' operation (see return doc below for
  * list). Be sure to shutdown the connection when done by calling {@link Connection#close()} else
  * it will stick around; this is probably not what you want.
  *
  * @param conf Configuration to use
  * @param admin An AdminProtocol; can be null but is usually itself a mock.
  * @param client A ClientProtocol; can be null but is usually itself a mock.
  * @param sn ServerName to include in the region location returned by this <code>connection</code>
  * @param hri HRegionInfo to include in the location returned when getRegionLocator is called on
  *     the mocked connection
  * @return Mock up a connection that returns a {@link Configuration} when {@link
  *     ClusterConnection#getConfiguration()} is called, a 'location' when {@link
  *     ClusterConnection#getRegionLocation(org.apache.hadoop.hbase.TableName, byte[], boolean)} is
  *     called, and that returns the passed {@link AdminProtos.AdminService.BlockingInterface}
  *     instance when {@link ClusterConnection#getAdmin(ServerName)} is called, returns the passed
  *     {@link ClientProtos.ClientService.BlockingInterface} instance when {@link
  *     ClusterConnection#getClient(ServerName)} is called (Be sure to call {@link
  *     Connection#close()} when done with this mocked Connection.
  * @throws IOException
  */
 public static ClusterConnection getMockedConnectionAndDecorate(
     final Configuration conf,
     final AdminProtos.AdminService.BlockingInterface admin,
     final ClientProtos.ClientService.BlockingInterface client,
     final ServerName sn,
     final HRegionInfo hri)
     throws IOException {
   ConnectionImplementation c = Mockito.mock(ConnectionImplementation.class);
   Mockito.when(c.getConfiguration()).thenReturn(conf);
   Mockito.doNothing().when(c).close();
   // Make it so we return a particular location when asked.
   final HRegionLocation loc = new HRegionLocation(hri, sn);
   Mockito.when(
           c.getRegionLocation(
               (TableName) Mockito.any(), (byte[]) Mockito.any(), Mockito.anyBoolean()))
       .thenReturn(loc);
   Mockito.when(c.locateRegion((TableName) Mockito.any(), (byte[]) Mockito.any())).thenReturn(loc);
   Mockito.when(
           c.locateRegion(
               (TableName) Mockito.any(),
               (byte[]) Mockito.any(),
               Mockito.anyBoolean(),
               Mockito.anyBoolean(),
               Mockito.anyInt()))
       .thenReturn(new RegionLocations(loc));
   if (admin != null) {
     // If a call to getAdmin, return this implementation.
     Mockito.when(c.getAdmin(Mockito.any(ServerName.class))).thenReturn(admin);
   }
   if (client != null) {
     // If a call to getClient, return this client.
     Mockito.when(c.getClient(Mockito.any(ServerName.class))).thenReturn(client);
   }
   NonceGenerator ng = Mockito.mock(NonceGenerator.class);
   Mockito.when(c.getNonceGenerator()).thenReturn(ng);
   Mockito.when(c.getAsyncProcess())
       .thenReturn(
           new AsyncProcess(
               c,
               conf,
               null,
               RpcRetryingCallerFactory.instantiate(conf),
               false,
               RpcControllerFactory.instantiate(conf),
               conf.getInt(HConstants.HBASE_RPC_TIMEOUT_KEY, HConstants.DEFAULT_HBASE_RPC_TIMEOUT),
               conf.getInt(
                   HConstants.HBASE_CLIENT_OPERATION_TIMEOUT,
                   HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT)));
   Mockito.when(c.getNewRpcRetryingCallerFactory(conf))
       .thenReturn(
           RpcRetryingCallerFactory.instantiate(
               conf, RetryingCallerInterceptorFactory.NO_OP_INTERCEPTOR, null));
   Mockito.when(c.getRpcControllerFactory()).thenReturn(Mockito.mock(RpcControllerFactory.class));
   Table t = Mockito.mock(Table.class);
   Mockito.when(c.getTable((TableName) Mockito.any())).thenReturn(t);
   ResultScanner rs = Mockito.mock(ResultScanner.class);
   Mockito.when(t.getScanner((Scan) Mockito.any())).thenReturn(rs);
   return c;
 }
 public void doScan() throws IOException {
   Table tableRef = connection.getTable(TableName.valueOf(table));
   Scan scan = new Scan();
   ResultScanner scanner = tableRef.getScanner(scan);
   long now = System.currentTimeMillis();
   if (verbose) System.out.println("Starting scan");
   for (Result res : scanner) {
     if (verbose) System.out.println(res);
   }
   if (verbose) System.out.printf("Scan finished: %d ms\n\n", System.currentTimeMillis() - now);
   tableRef.close();
 }
 /** Return the number of rows in the given table. */
 public static int countMobRows(final Table table) throws IOException {
   Scan scan = new Scan();
   ResultScanner results = table.getScanner(scan);
   int count = 0;
   for (Result res : results) {
     count++;
     List<Cell> cells = res.listCells();
     for (Cell cell : cells) {
       // Verify the value
       Assert.assertTrue(CellUtil.cloneValue(cell).length > 0);
     }
   }
   results.close();
   return count;
 }
Exemple #4
0
 public void scan(
     Consumer<Result> callback, String tableName, String columnFamily, String... qualifiers) {
   if (callback != null && tableName != null && columnFamily != null && qualifiers != null) {
     Scan s = new Scan();
     byte[] family = Bytes.toBytes(columnFamily);
     for (String qualifier : qualifiers) {
       s.addColumn(family, Bytes.toBytes(qualifier));
     }
     try {
       final Table table = connection.getTable(TableName.valueOf(tableName));
       ResultScanner scanner = table.getScanner(s);
       for (Result r = scanner.next(); r != null; r = scanner.next()) {
         callback.accept(r);
       }
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
Exemple #5
0
  @Test(timeout = 300000)
  public void testDisableAndEnableTable() throws IOException {
    final byte[] row = Bytes.toBytes("row");
    final byte[] qualifier = Bytes.toBytes("qualifier");
    final byte[] value = Bytes.toBytes("value");
    final TableName table = TableName.valueOf("testDisableAndEnableTable");
    Table ht = TEST_UTIL.createTable(table, HConstants.CATALOG_FAMILY);
    Put put = new Put(row);
    put.add(HConstants.CATALOG_FAMILY, qualifier, value);
    ht.put(put);
    Get get = new Get(row);
    get.addColumn(HConstants.CATALOG_FAMILY, qualifier);
    ht.get(get);

    this.admin.disableTable(ht.getName());
    assertTrue(
        "Table must be disabled.",
        TEST_UTIL
            .getHBaseCluster()
            .getMaster()
            .getAssignmentManager()
            .getTableStateManager()
            .isTableState(ht.getName(), ZooKeeperProtos.Table.State.DISABLED));

    // Test that table is disabled
    get = new Get(row);
    get.addColumn(HConstants.CATALOG_FAMILY, qualifier);
    boolean ok = false;
    try {
      ht.get(get);
    } catch (TableNotEnabledException e) {
      ok = true;
    }
    ok = false;
    // verify that scan encounters correct exception
    Scan scan = new Scan();
    try {
      ResultScanner scanner = ht.getScanner(scan);
      Result res = null;
      do {
        res = scanner.next();
      } while (res != null);
    } catch (TableNotEnabledException e) {
      ok = true;
    }
    assertTrue(ok);
    this.admin.enableTable(table);
    assertTrue(
        "Table must be enabled.",
        TEST_UTIL
            .getHBaseCluster()
            .getMaster()
            .getAssignmentManager()
            .getTableStateManager()
            .isTableState(ht.getName(), ZooKeeperProtos.Table.State.ENABLED));

    // Test that table is enabled
    try {
      ht.get(get);
    } catch (RetriesExhaustedException e) {
      ok = false;
    }
    assertTrue(ok);
    ht.close();
  }