org.apache.hadoop.hbase.master.HMaster Java Examples

The following examples show how to use org.apache.hadoop.hbase.master.HMaster. 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: WALRecoveryRegionPostOpenIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void moveRegionAndWait(MiniHBaseCluster miniHBaseCluster,HRegion destRegion, HRegionServer destRegionServer) throws IOException, InterruptedException {
    HMaster master = miniHBaseCluster.getMaster();
    getUtility().getHBaseAdmin().move(
            destRegion.getRegionInfo().getEncodedNameAsBytes(),
            Bytes.toBytes(destRegionServer.getServerName().getServerName()));
    while (true) {
        ServerName currentRegionServerName =
                master.getAssignmentManager().getRegionStates().getRegionServerOfRegion(destRegion.getRegionInfo());
        if (currentRegionServerName != null && currentRegionServerName.equals(destRegionServer.getServerName())) {
            getUtility().assertRegionOnServer(
                    destRegion.getRegionInfo(), currentRegionServerName, 200);
            break;
        }
        Thread.sleep(10);
    }
}
 
Example #2
Source File: TestTransitRegionStateProcedure.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecoveryAndDoubleExecutionUnassignAndAssign() throws Exception {
  HMaster master = UTIL.getMiniHBaseCluster().getMaster();
  MasterProcedureEnv env = master.getMasterProcedureExecutor().getEnvironment();
  HRegion region = UTIL.getMiniHBaseCluster().getRegions(tableName).get(0);
  RegionInfo regionInfo = region.getRegionInfo();
  long openSeqNum = region.getOpenSeqNum();
  TransitRegionStateProcedure unassign = TransitRegionStateProcedure.unassign(env, regionInfo);
  testRecoveryAndDoubleExcution(unassign);
  AssignmentManager am = master.getAssignmentManager();
  assertTrue(am.getRegionStates().getRegionState(regionInfo).isClosed());

  TransitRegionStateProcedure assign = TransitRegionStateProcedure.assign(env, regionInfo, null);
  testRecoveryAndDoubleExcution(assign);

  HRegion region2 = UTIL.getMiniHBaseCluster().getRegions(tableName).get(0);
  long openSeqNum2 = region2.getOpenSeqNum();
  // confirm that the region is successfully opened
  assertTrue(openSeqNum2 > openSeqNum);
}
 
Example #3
Source File: TestFlushSnapshotFromClient.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for testing async snapshot operations. Just waits for the given snapshot to
 * complete on the server by repeatedly checking the master.
 * @param master the master running the snapshot
 * @param snapshot the snapshot to check
 * @param timeoutNanos the timeout in nano between checks to see if the snapshot is done
 */
private static void waitForSnapshotToComplete(HMaster master,
    SnapshotProtos.SnapshotDescription snapshot, long timeoutNanos) throws Exception {
  final IsSnapshotDoneRequest request =
    IsSnapshotDoneRequest.newBuilder().setSnapshot(snapshot).build();
  long start = System.nanoTime();
  while (System.nanoTime() - start < timeoutNanos) {
    try {
      IsSnapshotDoneResponse done = master.getMasterRpcServices().isSnapshotDone(null, request);
      if (done.getDone()) {
        return;
      }
    } catch (ServiceException e) {
      // ignore UnknownSnapshotException, this is possible as for AsyncAdmin, the method will
      // return immediately after sending out the request, no matter whether the master has
      // processed the request or not.
      if (!(e.getCause() instanceof UnknownSnapshotException)) {
        throw e;
      }
    }

    Thread.sleep(200);
  }
  throw new TimeoutException("Timeout waiting for snapshot " + snapshot + " to complete");
}
 
Example #4
Source File: TestWALFiltering.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testFlushedSequenceIdsSentToHMaster()
throws IOException, InterruptedException,
org.apache.hbase.thirdparty.com.google.protobuf.ServiceException, ServiceException {
  SortedMap<byte[], Long> allFlushedSequenceIds = new TreeMap<>(Bytes.BYTES_COMPARATOR);
  for (int i = 0; i < NUM_RS; ++i) {
    flushAllRegions(i);
  }
  Thread.sleep(10000);
  HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
  for (int i = 0; i < NUM_RS; ++i) {
    for (byte[] regionName : getRegionsByServer(i)) {
      if (allFlushedSequenceIds.containsKey(regionName)) {
        GetLastFlushedSequenceIdRequest req =
          RequestConverter.buildGetLastFlushedSequenceIdRequest(regionName);

        assertEquals((long)allFlushedSequenceIds.get(regionName),
          master.getMasterRpcServices().getLastFlushedSequenceId(
            null, req).getLastFlushedSequenceId());
      }
    }
  }
}
 
Example #5
Source File: TestMasterObserver.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableDescriptorsEnumeration() throws Exception {
  MiniHBaseCluster cluster = UTIL.getHBaseCluster();

  HMaster master = cluster.getMaster();
  MasterCoprocessorHost host = master.getMasterCoprocessorHost();
  CPMasterObserver cp = host.findCoprocessor(CPMasterObserver.class);
  cp.resetStates();

  GetTableDescriptorsRequest req =
      RequestConverter.buildGetTableDescriptorsRequest((List<TableName>)null);
  master.getMasterRpcServices().getTableDescriptors(null, req);

  assertTrue("Coprocessor should be called on table descriptors request",
    cp.wasGetTableDescriptorsCalled());
}
 
Example #6
Source File: StartMiniClusterOption.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Private constructor. Use {@link Builder#build()}.
 */
private StartMiniClusterOption(int numMasters, int numAlwaysStandByMasters,
    Class<? extends HMaster> masterClass, int numRegionServers, List<Integer> rsPorts,
    Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> rsClass, int numDataNodes,
    String[] dataNodeHosts, int numZkServers, boolean createRootDir, boolean createWALDir) {
  this.numMasters = numMasters;
  this.numAlwaysStandByMasters = numAlwaysStandByMasters;
  this.masterClass = masterClass;
  this.numRegionServers = numRegionServers;
  this.rsPorts = rsPorts;
  this.rsClass = rsClass;
  this.numDataNodes = numDataNodes;
  this.dataNodeHosts = dataNodeHosts;
  this.numZkServers = numZkServers;
  this.createRootDir = createRootDir;
  this.createWALDir = createWALDir;
}
 
Example #7
Source File: TestAssignmentManagerLoadMetaRegionState.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testRestart() throws InterruptedException, IOException {
  ServerName sn = UTIL.getMiniHBaseCluster().getRegionServer(0).getServerName();
  AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
  Set<RegionInfo> regions = new HashSet<>(am.getRegionsOnServer(sn));

  UTIL.getMiniHBaseCluster().stopMaster(0).join();
  HMaster newMaster = UTIL.getMiniHBaseCluster().startMaster().getMaster();
  UTIL.waitFor(30000, () -> newMaster.isInitialized());

  am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
  List<RegionInfo> newRegions = am.getRegionsOnServer(sn);
  assertEquals(regions.size(), newRegions.size());
  for (RegionInfo region : newRegions) {
    assertTrue(regions.contains(region));
  }
}
 
Example #8
Source File: HBaseTestingUtility.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@link Predicate} for checking that there are no regions in transition in master
 */
public ExplainingPredicate<IOException> predicateNoRegionsInTransition() {
  return new ExplainingPredicate<IOException>() {
    @Override
    public String explainFailure() throws IOException {
      final RegionStates regionStates = getMiniHBaseCluster().getMaster()
          .getAssignmentManager().getRegionStates();
      return "found in transition: " + regionStates.getRegionsInTransition().toString();
    }

    @Override
    public boolean evaluate() throws IOException {
      HMaster master = getMiniHBaseCluster().getMaster();
      if (master == null) return false;
      AssignmentManager am = master.getAssignmentManager();
      if (am == null) return false;
      return !am.hasRegionsInTransition();
    }
  };
}
 
Example #9
Source File: TestClientClusterMetrics.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test public void testMasterAndBackupMastersStatus() throws Exception {
  // get all the master threads
  List<MasterThread> masterThreads = CLUSTER.getMasterThreads();
  int numActive = 0;
  int activeIndex = 0;
  ServerName activeName = null;
  HMaster active = null;
  for (int i = 0; i < masterThreads.size(); i++) {
    if (masterThreads.get(i).getMaster().isActiveMaster()) {
      numActive++;
      activeIndex = i;
      active = masterThreads.get(activeIndex).getMaster();
      activeName = active.getServerName();
    }
  }
  Assert.assertNotNull(active);
  Assert.assertEquals(1, numActive);
  Assert.assertEquals(MASTERS, masterThreads.size());
  // Retrieve master and backup masters infos only.
  EnumSet<Option> options = EnumSet.of(Option.MASTER, Option.BACKUP_MASTERS);
  ClusterMetrics metrics = ADMIN.getClusterMetrics(options);
  Assert.assertTrue(metrics.getMasterName().equals(activeName));
  Assert.assertEquals(MASTERS - 1, metrics.getBackupMasterNames().size());
}
 
Example #10
Source File: TestRegionServerCrashDisableWAL.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws InterruptedException, IOException {
  HMaster master = UTIL.getMiniHBaseCluster().stopMaster(0).getMaster();
  // Shutdown master before shutting down rs
  UTIL.waitFor(30000, () -> !master.isAlive());
  RegionServerThread thread = null;
  for (RegionServerThread t : UTIL.getMiniHBaseCluster().getRegionServerThreads()) {
    if (!t.getRegionServer().getRegions(TABLE_NAME).isEmpty()) {
      thread = t;
      break;
    }
  }
  // shutdown rs
  thread.getRegionServer().abort("For testing");
  thread.join();
  // restart master
  UTIL.getMiniHBaseCluster().startMaster();
  // make sure that we can schedule a SCP for the crashed server which WAL is disabled and bring
  // the region online.
  try (Table table =
    UTIL.getConnection().getTableBuilder(TABLE_NAME, null).setOperationTimeout(30000).build()) {
    table.put(new Put(Bytes.toBytes(1)).addColumn(CF, CQ, Bytes.toBytes(1)));
    assertEquals(1, Bytes.toInt(table.get(new Get(Bytes.toBytes(1))).getValue(CF, CQ)));
  }
}
 
Example #11
Source File: MasterProcedureTestingUtility.java    From hbase with Apache License 2.0 6 votes vote down vote up
public static void validateTableDeletion(
    final HMaster master, final TableName tableName) throws IOException {
  // check filesystem
  final FileSystem fs = master.getMasterFileSystem().getFileSystem();
  final Path tableDir =
    CommonFSUtils.getTableDir(master.getMasterFileSystem().getRootDir(), tableName);
  assertFalse(fs.exists(tableDir));

  // check meta
  assertFalse(MetaTableAccessor.tableExists(master.getConnection(), tableName));
  assertEquals(0, countMetaRegions(master, tableName));

  // check htd
  assertTrue("found htd of deleted table",
    master.getTableDescriptors().get(tableName) == null);
}
 
Example #12
Source File: SnapshotTestingUtils.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Expect the snapshot to throw an error when checking if the snapshot is
 * complete
 *
 * @param master master to check
 * @param snapshot the {@link SnapshotDescription} request to pass to the master
 * @param clazz expected exception from the master
 */
public static void expectSnapshotDoneException(HMaster master,
    IsSnapshotDoneRequest snapshot,
    Class<? extends HBaseSnapshotException> clazz) {
  try {
    master.getMasterRpcServices().isSnapshotDone(null, snapshot);
    Assert.fail("didn't fail to lookup a snapshot");
  } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) {
    try {
      throw ProtobufUtil.handleRemoteException(se);
    } catch (HBaseSnapshotException e) {
      assertEquals("Threw wrong snapshot exception!", clazz, e.getClass());
    } catch (Throwable t) {
      Assert.fail("Threw an unexpected exception:" + t);
    }
  }
}
 
Example #13
Source File: TestAsyncRegionAdminApi.java    From hbase with Apache License 2.0 6 votes vote down vote up
RegionInfo createTableAndGetOneRegion(final TableName tableName)
    throws IOException, InterruptedException, ExecutionException {
  TableDescriptor desc =
      TableDescriptorBuilder.newBuilder(tableName)
          .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build();
  admin.createTable(desc, Bytes.toBytes("A"), Bytes.toBytes("Z"), 5).get();

  // wait till the table is assigned
  HMaster master = TEST_UTIL.getHBaseCluster().getMaster();
  long timeoutTime = System.currentTimeMillis() + 3000;
  while (true) {
    List<RegionInfo> regions =
        master.getAssignmentManager().getRegionStates().getRegionsOfTable(tableName);
    if (regions.size() > 3) {
      return regions.get(2);
    }
    long now = System.currentTimeMillis();
    if (now > timeoutTime) {
      fail("Could not find an online region");
    }
    Thread.sleep(10);
  }
}
 
Example #14
Source File: TestReopenTableRegionsProcedureInfiniteLoop.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testInfiniteLoop() throws IOException {
  HMaster master = UTIL.getMiniHBaseCluster().getMaster();
  AssignmentManager am = master.getAssignmentManager();
  ProcedureExecutor<MasterProcedureEnv> exec = master.getMasterProcedureExecutor();
  RegionInfo regionInfo = UTIL.getAdmin().getRegions(TABLE_NAME).get(0);
  RegionStateNode regionNode = am.getRegionStates().getRegionStateNode(regionInfo);
  long procId;
  ReopenTableRegionsProcedure proc = new ReopenTableRegionsProcedure(TABLE_NAME);
  regionNode.lock();
  try {
    procId = exec.submitProcedure(proc);
    UTIL.waitFor(30000, () -> proc.hasLock());
    TransitRegionStateProcedure trsp =
      TransitRegionStateProcedure.reopen(exec.getEnvironment(), regionInfo);
    regionNode.setProcedure(trsp);
    exec.submitProcedure(trsp);
  } finally {
    regionNode.unlock();
  }
  UTIL.waitFor(60000, () -> exec.isFinished(procId));
}
 
Example #15
Source File: IndexLoadBalancerIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public boolean checkForColocation(HMaster master, String tableName, String indexTableName)
        throws IOException, InterruptedException {
    List<Pair<byte[], ServerName>> uTableStartKeysAndLocations =
            getStartKeysAndLocations(master, tableName);
    List<Pair<byte[], ServerName>> iTableStartKeysAndLocations =
            getStartKeysAndLocations(master, indexTableName);

    boolean regionsColocated = true;
    if (uTableStartKeysAndLocations.size() != iTableStartKeysAndLocations.size()) {
        regionsColocated = false;
    } else {
        for (int i = 0; i < uTableStartKeysAndLocations.size(); i++) {
            Pair<byte[], ServerName> uStartKeyAndLocation = uTableStartKeysAndLocations.get(i);
            Pair<byte[], ServerName> iStartKeyAndLocation = iTableStartKeysAndLocations.get(i);

            if (Bytes.compareTo(uStartKeyAndLocation.getFirst(), iStartKeyAndLocation
                    .getFirst()) == 0) {
                if (uStartKeyAndLocation.getSecond().equals(iStartKeyAndLocation.getSecond())) {
                    continue;
                }
            }
            regionsColocated = false;
        }
    }
    return regionsColocated;
}
 
Example #16
Source File: IndexLoadBalancerIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private List<Pair<byte[], ServerName>> getStartKeysAndLocations(HMaster master, String tableName)
        throws IOException, InterruptedException {

    List<Pair<HRegionInfo, ServerName>> tableRegionsAndLocations =
            MetaTableAccessor.getTableRegionsAndLocations(master.getZooKeeper(), master.getConnection(),
                    TableName.valueOf(tableName));
    List<Pair<byte[], ServerName>> startKeyAndLocationPairs =
            new ArrayList<Pair<byte[], ServerName>>(tableRegionsAndLocations.size());
    Pair<byte[], ServerName> startKeyAndLocation = null;
    for (Pair<HRegionInfo, ServerName> regionAndLocation : tableRegionsAndLocations) {
        startKeyAndLocation =
                new Pair<byte[], ServerName>(regionAndLocation.getFirst().getStartKey(),
                        regionAndLocation.getSecond());
        startKeyAndLocationPairs.add(startKeyAndLocation);
    }
    return startKeyAndLocationPairs;

}
 
Example #17
Source File: TestMasterRegistry.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testRegistryRPCs() throws Exception {
  Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
  HMaster activeMaster = TEST_UTIL.getHBaseCluster().getMaster();
  final int size =
    activeMaster.getMetaRegionLocationCache().getMetaRegionLocations().get().size();
  for (int numHedgedReqs = 1; numHedgedReqs <= size; numHedgedReqs++) {
    conf.setInt(MasterRegistry.MASTER_REGISTRY_HEDGED_REQS_FANOUT_KEY, numHedgedReqs);
    try (MasterRegistry registry = new MasterRegistry(conf)) {
      // Add wait on all replicas being assigned before proceeding w/ test. Failed on occasion
      // because not all replicas had made it up before test started.
      RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, registry);
      assertEquals(registry.getClusterId().get(), activeMaster.getClusterId());
      assertEquals(registry.getActiveMaster().get(), activeMaster.getServerName());
      List<HRegionLocation> metaLocations =
        Arrays.asList(registry.getMetaRegionLocations().get().getRegionLocations());
      List<HRegionLocation> actualMetaLocations =
        activeMaster.getMetaRegionLocationCache().getMetaRegionLocations().get();
      Collections.sort(metaLocations);
      Collections.sort(actualMetaLocations);
      assertEquals(actualMetaLocations, metaLocations);
    }
  }
}
 
Example #18
Source File: TestClientClusterStatus.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testMasterAndBackupMastersStatus() throws Exception {
  // get all the master threads
  List<MasterThread> masterThreads = CLUSTER.getMasterThreads();
  int numActive = 0;
  int activeIndex = 0;
  ServerName activeName = null;
  HMaster active = null;
  for (int i = 0; i < masterThreads.size(); i++) {
    if (masterThreads.get(i).getMaster().isActiveMaster()) {
      numActive++;
      activeIndex = i;
      active = masterThreads.get(activeIndex).getMaster();
      activeName = active.getServerName();
    }
  }
  Assert.assertNotNull(active);
  Assert.assertEquals(1, numActive);
  Assert.assertEquals(MASTERS, masterThreads.size());
  // Retrieve master and backup masters infos only.
  EnumSet<Option> options = EnumSet.of(Option.MASTER, Option.BACKUP_MASTERS);
  ClusterMetrics status = ADMIN.getClusterMetrics(options);
  Assert.assertTrue(status.getMasterName().equals(activeName));
  Assert.assertEquals(MASTERS - 1, status.getBackupMasterNames().size());
}
 
Example #19
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 #20
Source File: TestRegionServerAbort.java    From hbase with Apache License 2.0 6 votes vote down vote up
@After
public void tearDown() throws Exception {
  String className = StopBlockingRegionObserver.class.getName();
  for (JVMClusterUtil.RegionServerThread t : cluster.getRegionServerThreads()) {
    HRegionServer rs = t.getRegionServer();
    RegionServerCoprocessorHost cpHost = rs.getRegionServerCoprocessorHost();
    StopBlockingRegionObserver cp = (StopBlockingRegionObserver)cpHost.findCoprocessor(className);
    cp.setStopAllowed(true);
  }
  HMaster master = cluster.getMaster();
  RegionServerCoprocessorHost host = master.getRegionServerCoprocessorHost();
  if (host != null) {
    StopBlockingRegionObserver obs = (StopBlockingRegionObserver) host.findCoprocessor(className);
    if (obs != null) obs.setStopAllowed(true);
  }
  testUtil.shutdownMiniCluster();
}
 
Example #21
Source File: TestMasterObserverPostCalls.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostDisableTable() throws IOException {
  final Admin admin = UTIL.getAdmin();
  final TableName tn = TableName.valueOf("postdisabletable");
  final TableDescriptor td = TableDescriptorBuilder.newBuilder(tn).setColumnFamily(
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f1")).build()).build();

  HMaster master = UTIL.getMiniHBaseCluster().getMaster();
  MasterObserverForTest observer = master.getMasterCoprocessorHost().findCoprocessor(
      MasterObserverForTest.class);

  // Create the table and disable it
  admin.createTable(td);

  // Validate that the post hook is called
  int preCount = observer.postHookCalls.get();
  admin.disableTable(td.getTableName());
  int postCount = observer.postHookCalls.get();
  assertEquals("Expected 1 invocation of postDisableTable", preCount + 1, postCount);

  // Then, validate that it's not called when the call fails
  preCount = observer.postHookCalls.get();
  try {
    admin.disableTable(TableName.valueOf("Missing"));
    fail("Disabling a missing table should fail");
  } catch (IOException e) {
    // Pass
  }
  postCount = observer.postHookCalls.get();
  assertEquals("Expected no invocations of postDisableTable when the operation fails",
      preCount, postCount);
}
 
Example #22
Source File: MasterProcedureTestingUtility.java    From hbase with Apache License 2.0 5 votes vote down vote up
private static int countMetaRegions(final HMaster master, final TableName tableName)
    throws IOException {
  final AtomicInteger actualRegCount = new AtomicInteger(0);
  final ClientMetaTableAccessor.Visitor visitor = new ClientMetaTableAccessor.Visitor() {
    @Override
    public boolean visit(Result rowResult) throws IOException {
      RegionLocations list = CatalogFamilyFormat.getRegionLocations(rowResult);
      if (list == null) {
        LOG.warn("No serialized RegionInfo in " + rowResult);
        return true;
      }
      HRegionLocation l = list.getRegionLocation();
      if (l == null) {
        return true;
      }
      if (!l.getRegion().getTable().equals(tableName)) {
        return false;
      }
      if (l.getRegion().isOffline() || l.getRegion().isSplit()) {
        return true;
      }

      HRegionLocation[] locations = list.getRegionLocations();
      for (HRegionLocation location : locations) {
        if (location == null) continue;
        ServerName serverName = location.getServerName();
        // Make sure that regions are assigned to server
        if (serverName != null && serverName.getAddress() != null) {
          actualRegCount.incrementAndGet();
        }
      }
      return true;
    }
  };
  MetaTableAccessor.scanMetaForTableRegions(master.getConnection(), visitor, tableName);
  return actualRegCount.get();
}
 
Example #23
Source File: TestZooKeeper.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the master does not call retainAssignment after recovery from expired zookeeper
 * session. Without the HBASE-6046 fix master always tries to assign all the user regions by
 * calling retainAssignment.
 */
@Test
public void testRegionAssignmentAfterMasterRecoveryDueToZKExpiry() throws Exception {
  MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
  cluster.startRegionServer();
  cluster.waitForActiveAndReadyMaster(10000);
  HMaster m = cluster.getMaster();
  final ZKWatcher zkw = m.getZooKeeper();
  // now the cluster is up. So assign some regions.
  try (Admin admin = TEST_UTIL.getAdmin()) {
    byte[][] SPLIT_KEYS = new byte[][] { Bytes.toBytes("a"), Bytes.toBytes("b"),
        Bytes.toBytes("c"), Bytes.toBytes("d"), Bytes.toBytes("e"), Bytes.toBytes("f"),
        Bytes.toBytes("g"), Bytes.toBytes("h"), Bytes.toBytes("i"), Bytes.toBytes("j") };
    TableDescriptor htd =
        TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
            .setColumnFamily(ColumnFamilyDescriptorBuilder.of(HConstants.CATALOG_FAMILY)).build();
    admin.createTable(htd, SPLIT_KEYS);
    TEST_UTIL.waitUntilNoRegionsInTransition(60000);
    m.getZooKeeper().close();
    MockLoadBalancer.retainAssignCalled = false;
    final int expectedNumOfListeners = countPermanentListeners(zkw);
    m.abort("Test recovery from zk session expired",
        new KeeperException.SessionExpiredException());
    assertTrue(m.isStopped()); // Master doesn't recover any more
    // The recovered master should not call retainAssignment, as it is not a
    // clean startup.
    assertFalse("Retain assignment should not be called", MockLoadBalancer.retainAssignCalled);
    // number of listeners should be same as the value before master aborted
    // wait for new master is initialized
    cluster.waitForActiveAndReadyMaster(120000);
    final HMaster newMaster = cluster.getMasterThread().getMaster();
    assertEquals(expectedNumOfListeners, countPermanentListeners(newMaster.getZooKeeper()));
  }
}
 
Example #24
Source File: TestInfoServers.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testMasterServerReadOnly() throws Exception {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  byte[] cf = Bytes.toBytes("d");
  UTIL.createTable(tableName, cf);
  UTIL.waitTableAvailable(tableName);
  HMaster master = UTIL.getHBaseCluster().getMaster();
  int port = master.getRegionServerInfoPort(master.getServerName());
  assertDoesNotContainContent(new URL("http://localhost:" + port + "/table.jsp?name=" +
    tableName + "&action=split&key="), "Table action request accepted");
  assertDoesNotContainContent(
    new URL("http://localhost:" + port + "/table.jsp?name=" + tableName), "Actions:");
}
 
Example #25
Source File: TestRegionAssignedToMultipleRegionServers.java    From hbase with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
  UTIL
    .startMiniCluster(StartMiniClusterOption.builder().numMasters(2).numRegionServers(2).build());
  UTIL.createTable(NAME, CF);
  UTIL.waitTableAvailable(NAME);
  UTIL.getAdmin().balancerSwitch(false, true);
}
 
Example #26
Source File: TestZooKeeper.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the logs are split when master recovers from a expired zookeeper session and an
 * RS goes down.
 */
@Test
public void testLogSplittingAfterMasterRecoveryDueToZKExpiry() throws Exception {
  MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
  cluster.startRegionServer();
  TableName tableName = TableName.valueOf(name.getMethodName());
  byte[] family = Bytes.toBytes("col");
  try (Admin admin = TEST_UTIL.getAdmin()) {
    byte[][] SPLIT_KEYS = new byte[][] { Bytes.toBytes("1"), Bytes.toBytes("2"),
      Bytes.toBytes("3"), Bytes.toBytes("4"), Bytes.toBytes("5") };
    TableDescriptor htd = TableDescriptorBuilder.newBuilder(tableName)
        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)).build();
    admin.createTable(htd, SPLIT_KEYS);
  }
  TEST_UTIL.waitUntilNoRegionsInTransition(60000);
  HMaster m = cluster.getMaster();
  try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
    int numberOfPuts;
    for (numberOfPuts = 0; numberOfPuts < 6; numberOfPuts++) {
      Put p = new Put(Bytes.toBytes(numberOfPuts));
      p.addColumn(Bytes.toBytes("col"), Bytes.toBytes("ql"),
        Bytes.toBytes("value" + numberOfPuts));
      table.put(p);
    }
    m.abort("Test recovery from zk session expired",
      new KeeperException.SessionExpiredException());
    assertTrue(m.isStopped()); // Master doesn't recover any more
    cluster.killRegionServer(TEST_UTIL.getRSForFirstRegionInTable(tableName).getServerName());
    // Without patch for HBASE-6046 this test case will always timeout
    // with patch the test case should pass.
    int numberOfRows = 0;
    try (ResultScanner scanner = table.getScanner(new Scan())) {
      while (scanner.next() != null) {
        numberOfRows++;
      }
    }
    assertEquals("Number of rows should be equal to number of puts.", numberOfPuts, numberOfRows);
  }
}
 
Example #27
Source File: TestHBCKSCP.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @return True if we find reference to <code>sn</code> in meta table.
 */
private boolean searchMeta(HMaster master, ServerName sn) throws IOException {
  List<Pair<RegionInfo, ServerName>> ps =
    MetaTableAccessor.getTableRegionsAndLocations(master.getConnection(), null);
  for (Pair<RegionInfo, ServerName> p: ps) {
    if (p.getSecond().equals(sn)) {
      return true;
    }
  }
  return false;
}
 
Example #28
Source File: MiniHBaseCluster.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void init(final int nMasterNodes, final int numAlwaysStandByMasters,
    final int nRegionNodes, List<Integer> rsPorts, Class<? extends HMaster> masterClass,
    Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass)
throws IOException, InterruptedException {
  try {
    if (masterClass == null){
      masterClass =  HMaster.class;
    }
    if (regionserverClass == null){
      regionserverClass = MiniHBaseCluster.MiniHBaseClusterRegionServer.class;
    }

    // start up a LocalHBaseCluster
    hbaseCluster = new LocalHBaseCluster(conf, nMasterNodes, numAlwaysStandByMasters, 0,
        masterClass, regionserverClass);

    // manually add the regionservers as other users
    for (int i = 0; i < nRegionNodes; i++) {
      Configuration rsConf = HBaseConfiguration.create(conf);
      if (rsPorts != null) {
        rsConf.setInt(HConstants.REGIONSERVER_PORT, rsPorts.get(i));
      }
      User user = HBaseTestingUtility.getDifferentUser(rsConf,
          ".hfs."+index++);
      hbaseCluster.addRegionServer(rsConf, i, user);
    }

    hbaseCluster.startup();
  } catch (IOException e) {
    shutdown();
    throw e;
  } catch (Throwable t) {
    LOG.error("Error starting cluster", t);
    shutdown();
    throw new IOException("Shutting down", t);
  }
}
 
Example #29
Source File: MiniHBaseCluster.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Start a MiniHBaseCluster.
 * @param conf Configuration to be used for cluster
 * @param numMasters initial number of masters to start.
 * @param numRegionServers initial number of region servers to start.
 */
public MiniHBaseCluster(Configuration conf, int numMasters, int numRegionServers,
       Class<? extends HMaster> masterClass,
       Class<? extends MiniHBaseCluster.MiniHBaseClusterRegionServer> regionserverClass)
    throws IOException, InterruptedException {
  this(conf, numMasters, 0, numRegionServers, null, masterClass, regionserverClass);
}
 
Example #30
Source File: TestAsyncClusterAdminApi2.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testStop() throws Exception {
  HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
  assertFalse(rs.isStopped());
  admin.stopRegionServer(rs.getServerName()).join();
  assertTrue(rs.isStopped());

  HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
  assertFalse(master.isStopped());
  admin.stopMaster().join();
  assertTrue(master.isStopped());
}