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

The following examples show how to use org.apache.hadoop.hbase.client.Admin#move() . 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: TestRegionServerOnlineConfigChange.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void removeClosedRegionFromConfigurationManager() throws Exception {
  try (Connection connection = ConnectionFactory.createConnection(conf)) {
    Admin admin = connection.getAdmin();
    assertTrue("The open region doesn't register as a ConfigurationObserver",
      rs1.getConfigurationManager().containsObserver(r1));
    admin.move(r1name);
    hbaseTestingUtility.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
      @Override public boolean evaluate() throws Exception {
        return rs1.getOnlineRegion(r1name) == null;
      }
    });
    assertFalse("The closed region is not removed from ConfigurationManager",
      rs1.getConfigurationManager().containsObserver(r1));
    admin.move(r1name, rs1.getServerName());
    hbaseTestingUtility.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
      @Override public boolean evaluate() throws Exception {
        return rs1.getOnlineRegion(r1name) != null;
      }
    });
  }
}
 
Example 2
Source File: BaseTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures each region of SYSTEM.CATALOG is on a different region server
 */
private static void moveRegion(HRegionInfo regionInfo, ServerName srcServerName, ServerName dstServerName) throws Exception  {
    Admin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
    HBaseTestingUtility util = getUtility();
    MiniHBaseCluster cluster = util.getHBaseCluster();
    HMaster master = cluster.getMaster();
    AssignmentManager am = master.getAssignmentManager();
   
    HRegionServer dstServer = util.getHBaseCluster().getRegionServer(dstServerName);
    HRegionServer srcServer = util.getHBaseCluster().getRegionServer(srcServerName);
    byte[] encodedRegionNameInBytes = regionInfo.getEncodedNameAsBytes();
    admin.move(encodedRegionNameInBytes, Bytes.toBytes(dstServer.getServerName().getServerName()));
    while (dstServer.getOnlineRegion(regionInfo.getRegionName()) == null
            || dstServer.getRegionsInTransitionInRS().containsKey(encodedRegionNameInBytes)
            || srcServer.getRegionsInTransitionInRS().containsKey(encodedRegionNameInBytes)) {
        // wait for the move to be finished
        Thread.sleep(100);
    }
}
 
Example 3
Source File: Action.java    From hbase with Apache License 2.0 5 votes vote down vote up
protected void unbalanceRegions(ClusterMetrics clusterStatus,
    List<ServerName> fromServers, List<ServerName> toServers,
    double fractionOfRegions) throws Exception {
  List<byte[]> victimRegions = new LinkedList<>();
  for (Map.Entry<ServerName, ServerMetrics> entry
    : clusterStatus.getLiveServerMetrics().entrySet()) {
    ServerName sn = entry.getKey();
    ServerMetrics serverLoad = entry.getValue();
    // Ugh.
    List<byte[]> regions = new LinkedList<>(serverLoad.getRegionMetrics().keySet());
    int victimRegionCount = (int)Math.ceil(fractionOfRegions * regions.size());
    getLogger().debug("Removing {} regions from {}", victimRegionCount, sn);
    for (int i = 0; i < victimRegionCount; ++i) {
      int victimIx = RandomUtils.nextInt(0, regions.size());
      String regionId = RegionInfo.encodeRegionName(regions.remove(victimIx));
      victimRegions.add(Bytes.toBytes(regionId));
    }
  }

  getLogger().info("Moving {} regions from {} servers to {} different servers",
    victimRegions.size(), fromServers.size(), toServers.size());
  Admin admin = this.context.getHBaseIntegrationTestingUtility().getAdmin();
  for (byte[] victimRegion : victimRegions) {
    // Don't keep moving regions if we're
    // trying to stop the monkey.
    if (context.isStopping()) {
      break;
    }
    int targetIx = RandomUtils.nextInt(0, toServers.size());
    admin.move(victimRegion, toServers.get(targetIx));
  }
}
 
Example 4
Source File: MoveRegionsOfTableAction.java    From hbase with Apache License 2.0 5 votes vote down vote up
static void moveRegion(Admin admin, ServerName [] servers, RegionInfo regionInfo, Logger logger) {
  try {
    ServerName destServerName = servers[RandomUtils.nextInt(0, servers.length)];
    logger.debug("Moving {} to {}", regionInfo.getRegionNameAsString(), destServerName);
    admin.move(regionInfo.getEncodedNameAsBytes(), destServerName);
  } catch (Exception ex) {
    logger.warn("Move failed, might be caused by other chaos: {}", ex.getMessage());
  }
}
 
Example 5
Source File: TestAccessController.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testGlobalAuthorizationForNewRegisteredRS() throws Exception {
  LOG.debug("Test for global authorization for a new registered RegionServer.");
  MiniHBaseCluster hbaseCluster = TEST_UTIL.getHBaseCluster();

  final Admin admin = TEST_UTIL.getAdmin();
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TEST_TABLE2);
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(TEST_FAMILY));
  createTable(TEST_UTIL, tableDescriptor);

  // Starting a new RegionServer.
  JVMClusterUtil.RegionServerThread newRsThread = hbaseCluster
      .startRegionServer();
  final HRegionServer newRs = newRsThread.getRegionServer();

  // Move region to the new RegionServer.
  List<HRegionLocation> regions;
  try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE2)) {
    regions = locator.getAllRegionLocations();
  }
  HRegionLocation location = regions.get(0);
  final RegionInfo hri = location.getRegion();
  final ServerName server = location.getServerName();
  try (Table table = systemUserConnection.getTable(TEST_TABLE2)) {
    AccessTestAction moveAction = new AccessTestAction() {
      @Override
      public Object run() throws Exception {
        admin.move(hri.getEncodedNameAsBytes(), newRs.getServerName());
        return null;
      }
    };
    SUPERUSER.runAs(moveAction);

    final int RETRIES_LIMIT = 10;
    int retries = 0;
    while (newRs.getRegions(TEST_TABLE2).size() < 1 && retries < RETRIES_LIMIT) {
      LOG.debug("Waiting for region to be opened. Already retried " + retries
          + " times.");
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
      }
      retries++;
      if (retries == RETRIES_LIMIT - 1) {
        fail("Retry exhaust for waiting region to be opened.");
      }
    }
    // Verify write permission for user "admin2" who has the global
    // permissions.
    AccessTestAction putAction = new AccessTestAction() {
      @Override
      public Object run() throws Exception {
        Put put = new Put(Bytes.toBytes("test"));
        put.addColumn(TEST_FAMILY, Bytes.toBytes("qual"), Bytes.toBytes("value"));
        table.put(put);
        return null;
      }
    };
    USER_ADMIN.runAs(putAction);
  }
}
 
Example 6
Source File: TestSplitTransactionOnCluster.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure single table region is not on same server as the single hbase:meta table
 * region.
 * @return Index of the server hosting the single table region
 * @throws UnknownRegionException
 * @throws MasterNotRunningException
 * @throws org.apache.hadoop.hbase.ZooKeeperConnectionException
 * @throws InterruptedException
 */
private int ensureTableRegionNotOnSameServerAsMeta(final Admin admin,
    final RegionInfo hri)
throws IOException, MasterNotRunningException,
ZooKeeperConnectionException, InterruptedException {
  // Now make sure that the table region is not on same server as that hosting
  // hbase:meta  We don't want hbase:meta replay polluting our test when we later crash
  // the table region serving server.
  int metaServerIndex = cluster.getServerWithMeta();
  boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TESTING_UTIL.getConfiguration());
  if (tablesOnMaster) {
    // Need to check master is supposed to host meta... perhaps it is not.
    throw new UnsupportedOperationException();
    // TODO: assertTrue(metaServerIndex == -1); // meta is on master now
  }
  HRegionServer metaRegionServer = tablesOnMaster?
    cluster.getMaster(): cluster.getRegionServer(metaServerIndex);
  int tableRegionIndex = cluster.getServerWith(hri.getRegionName());
  assertTrue(tableRegionIndex != -1);
  HRegionServer tableRegionServer = cluster.getRegionServer(tableRegionIndex);
  LOG.info("MetaRegionServer=" + metaRegionServer.getServerName() +
    ", other=" + tableRegionServer.getServerName());
  if (metaRegionServer.getServerName().equals(tableRegionServer.getServerName())) {
    HRegionServer hrs = getOtherRegionServer(cluster, metaRegionServer);
    assertNotNull(hrs);
    assertNotNull(hri);
    LOG.info("Moving " + hri.getRegionNameAsString() + " from " +
      metaRegionServer.getServerName() + " to " +
      hrs.getServerName() + "; metaServerIndex=" + metaServerIndex);
    admin.move(hri.getEncodedNameAsBytes(), hrs.getServerName());
  }
  // Wait till table region is up on the server that is NOT carrying hbase:meta.
  for (int i = 0; i < 100; i++) {
    tableRegionIndex = cluster.getServerWith(hri.getRegionName());
    if (tableRegionIndex != -1 && tableRegionIndex != metaServerIndex) break;
    LOG.debug("Waiting on region move off the hbase:meta server; current index " +
      tableRegionIndex + " and metaServerIndex=" + metaServerIndex);
    Thread.sleep(100);
  }
  assertTrue("Region not moved off hbase:meta server, tableRegionIndex=" + tableRegionIndex,
    tableRegionIndex != -1 && tableRegionIndex != metaServerIndex);
  // Verify for sure table region is not on same server as hbase:meta
  tableRegionIndex = cluster.getServerWith(hri.getRegionName());
  assertTrue(tableRegionIndex != -1);
  assertNotSame(metaServerIndex, tableRegionIndex);
  return tableRegionIndex;
}
 
Example 7
Source File: TestHRegionOnCluster.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testDataCorrectnessReplayingRecoveredEdits() throws Exception {
  final int NUM_RS = 3;
  Admin hbaseAdmin = null;
  TEST_UTIL.startMiniCluster(NUM_RS);

  try {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    final byte[] FAMILY = Bytes.toBytes("family");
    MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
    HMaster master = cluster.getMaster();

    // Create table
    TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
      new TableDescriptorBuilder.ModifyableTableDescriptor(tableName);
    tableDescriptor.setColumnFamily(
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY));
    hbaseAdmin = master.getConnection().getAdmin();
    hbaseAdmin.createTable(tableDescriptor);

    assertTrue(hbaseAdmin.isTableAvailable(tableName));

    // Put data: r1->v1
    LOG.info("Loading r1 to v1 into " + tableName);
    Table table = TEST_UTIL.getConnection().getTable(tableName);
    putDataAndVerify(table, "r1", FAMILY, "v1", 1);

    TEST_UTIL.waitUntilAllRegionsAssigned(table.getName());
    // Move region to target server

    RegionInfo regionInfo;
    try (RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
      regionInfo = locator.getRegionLocation(Bytes.toBytes("r1")).getRegion();
    }

    int originServerNum = cluster.getServerWith(regionInfo.getRegionName());
    HRegionServer originServer = cluster.getRegionServer(originServerNum);
    int targetServerNum = (originServerNum + 1) % NUM_RS;
    HRegionServer targetServer = cluster.getRegionServer(targetServerNum);
    assertFalse(originServer.equals(targetServer));

    TEST_UTIL.waitUntilAllRegionsAssigned(table.getName());
    LOG.info("Moving " + regionInfo.getEncodedName() + " to " + targetServer.getServerName());
    hbaseAdmin.move(regionInfo.getEncodedNameAsBytes(), targetServer.getServerName());
    do {
      Thread.sleep(1);
    } while (cluster.getServerWith(regionInfo.getRegionName()) == originServerNum);

    // Put data: r2->v2
    LOG.info("Loading r2 to v2 into " + tableName);
    putDataAndVerify(table, "r2", FAMILY, "v2", 2);

    TEST_UTIL.waitUntilAllRegionsAssigned(table.getName());
    // Move region to origin server
    LOG.info("Moving " + regionInfo.getEncodedName() + " to " + originServer.getServerName());
    hbaseAdmin.move(regionInfo.getEncodedNameAsBytes(), originServer.getServerName());
    do {
      Thread.sleep(1);
    } while (cluster.getServerWith(regionInfo.getRegionName()) == targetServerNum);

    // Put data: r3->v3
    LOG.info("Loading r3 to v3 into " + tableName);
    putDataAndVerify(table, "r3", FAMILY, "v3", 3);

    // Kill target server
    LOG.info("Killing target server " + targetServer.getServerName());
    targetServer.kill();
    cluster.getRegionServerThreads().get(targetServerNum).join();
    // Wait until finish processing of shutdown
    while (master.getServerManager().areDeadServersInProgress()) {
      Thread.sleep(5);
    }
    // Kill origin server
    LOG.info("Killing origin server " + targetServer.getServerName());
    originServer.kill();
    cluster.getRegionServerThreads().get(originServerNum).join();

    // Put data: r4->v4
    LOG.info("Loading r4 to v4 into " + tableName);
    putDataAndVerify(table, "r4", FAMILY, "v4", 4);

  } finally {
    if (hbaseAdmin != null) hbaseAdmin.close();
    TEST_UTIL.shutdownMiniCluster();
  }
}
 
Example 8
Source File: PhoenixServerRpcIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Verifies that the given tables each have a single region and are on
 * different region servers. If they are on the same server moves tableName2
 * to the other region server.
 */
private void ensureTablesOnDifferentRegionServers(String tableName1, String tableName2) throws Exception  {
	byte[] table1 = Bytes.toBytes(tableName1);
	byte[] table2 = Bytes.toBytes(tableName2);
	Admin admin = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES).getAdmin();
	HBaseTestingUtility util = getUtility();
	MiniHBaseCluster cluster = util.getHBaseCluster();
	HMaster master = cluster.getMaster();
	AssignmentManager am = master.getAssignmentManager();
  
	// verify there is only a single region for data table
	List<RegionInfo> tableRegions = admin.getRegions(TableName.valueOf(table1));
	assertEquals("Expected single region for " + table1, tableRegions.size(), 1);
	RegionInfo hri1 = tableRegions.get(0);
  
	// verify there is only a single region for index table
	tableRegions = admin.getRegions(TableName.valueOf(table2));
	RegionInfo hri2 = tableRegions.get(0);
	assertEquals("Expected single region for " + table2, tableRegions.size(), 1);
  
	ServerName serverName1 = am.getRegionStates().getRegionServerOfRegion(hri1);
	ServerName serverName2 = am.getRegionStates().getRegionServerOfRegion(hri2);
  
	// if data table and index table are on same region server, move the index table to the other region server
	if (serverName1.equals(serverName2)) {
	    HRegionServer server1 = util.getHBaseCluster().getRegionServer(0);
	    HRegionServer server2 = util.getHBaseCluster().getRegionServer(1);
	    HRegionServer dstServer = null;
	    HRegionServer srcServer = null;
	    if (server1.getServerName().equals(serverName2)) {
	        dstServer = server2;
	        srcServer = server1;
	    } else {
	        dstServer = server1;
	        srcServer = server2;
	    }
	    byte[] encodedRegionNameInBytes = hri2.getEncodedNameAsBytes();
	    admin.move(encodedRegionNameInBytes, Bytes.toBytes(dstServer.getServerName().getServerName()));
	    while (dstServer.getOnlineRegion(hri2.getRegionName()) == null
	            || dstServer.getRegionsInTransitionInRS().containsKey(encodedRegionNameInBytes)
	            || srcServer.getRegionsInTransitionInRS().containsKey(encodedRegionNameInBytes)
	            || master.getAssignmentManager().getRegionStates().isRegionInTransition(hri2)) {
	        // wait for the move to be finished
	        Thread.sleep(1);
	    }
	}
  
	hri1 = admin.getRegions(TableName.valueOf(table1)).get(0);
	serverName1 = am.getRegionStates().getRegionServerOfRegion(hri1);
	hri2 = admin.getRegions(TableName.valueOf(table2)).get(0);
	serverName2 = am.getRegionStates().getRegionServerOfRegion(hri2);

	// verify index and data tables are on different servers
	assertNotEquals("Tables " + tableName1 + " and " + tableName2 + " should be on different region servers", serverName1, serverName2);
}