下面列出了怎么用org.apache.hadoop.hbase.master.HMaster的API类实例代码及写法,或者点击链接到github查看源代码。
@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();
}
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;
}
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;
}
@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());
}
@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));
}
}
/**
* 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");
}
/**
* 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;
}
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);
}
}
/**
* 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);
}
}
@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());
}
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);
}
@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));
}
@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);
}
@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());
}
}
}
}
@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());
}
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);
}
}
@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);
}
}
}
/**
* 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);
}
}
}
@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)));
}
}
/**
* 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();
}
};
}
@Test(timeout = 180000)
public void testRoundRobinAssignmentDuringIndexTableCreation() throws Exception {
MiniHBaseCluster cluster = UTIL.getHBaseCluster();
HMaster master = cluster.getMaster();
TableName tableName = TableName.valueOf("testRoundRobinAssignmentDuringIndexTableCreation");
TableName indexTableName =
TableName.valueOf("testRoundRobinAssignmentDuringIndexTableCreation_index");
createUserAndIndexTable(tableName, indexTableName);
boolean isRegionColocated =
checkForColocation(master, tableName.getNameAsString(), indexTableName
.getNameAsString());
assertTrue("User regions and index regions should colocate.", isRegionColocated);
}
@Test(timeout = 180000)
public void testBalanceCluster() throws Exception {
MiniHBaseCluster cluster = UTIL.getHBaseCluster();
HMaster master = cluster.getMaster();
master.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", false);
master.getConfiguration().setBoolean("hbase.master.startup.retainassign", false);
master.getConfiguration().setBoolean("hbase.master.loadbalance.bytable", false);
TableName tableName = TableName.valueOf("testBalanceCluster");
TableName indexTableName = TableName.valueOf("testBalanceCluster_index");
createUserAndIndexTable(tableName, indexTableName);
HTableDescriptor htd1 = new HTableDescriptor(TableName.valueOf("testBalanceCluster1"));
htd1.addFamily(new HColumnDescriptor("fam1"));
char c = 'A';
byte[][] split1 = new byte[12][];
for (int i = 0; i < 12; i++) {
byte[] b = { (byte) c };
split1[i] = b;
c++;
}
admin.setBalancerRunning(false, false);
admin.createTable(htd1, split1);
admin.disableTable(tableName);
admin.enableTable(tableName);
admin.setBalancerRunning(true, false);
admin.balancer();
boolean isRegionsColocated =
checkForColocation(master, tableName.getNameAsString(), indexTableName
.getNameAsString());
assertTrue("User regions and index regions should colocate.", isRegionsColocated);
}
public static void stopMasterAndAssignMeta(HBaseTestingUtility HTU)
throws IOException, InterruptedException {
// Stop master
HMaster master = HTU.getHBaseCluster().getMaster();
Thread masterThread = HTU.getHBaseCluster().getMasterThread();
ServerName masterAddr = master.getServerName();
master.stopMaster();
LOG.info("Waiting until master thread exits");
while (masterThread != null && masterThread.isAlive()) {
Threads.sleep(100);
}
HRegionServer.TEST_SKIP_REPORTING_TRANSITION = true;
// Master is down, so is the meta. We need to assign it somewhere
// so that regions can be assigned during the mocking phase.
HRegionServer hrs = HTU.getHBaseCluster()
.getLiveRegionServerThreads().get(0).getRegionServer();
ZKWatcher zkw = hrs.getZooKeeper();
ServerName sn = MetaTableLocator.getMetaRegionLocation(zkw);
if (sn != null && !masterAddr.equals(sn)) {
return;
}
ProtobufUtil.openRegion(null, hrs.getRSRpcServices(),
hrs.getServerName(), RegionInfoBuilder.FIRST_META_REGIONINFO);
while (true) {
sn = MetaTableLocator.getMetaRegionLocation(zkw);
if (sn != null && sn.equals(hrs.getServerName())
&& hrs.getOnlineRegions().containsKey(
RegionInfoBuilder.FIRST_META_REGIONINFO.getEncodedName())) {
break;
}
Thread.sleep(100);
}
}
@Test
public void testConcurrentSCPForSameServer() throws Exception {
final TableName tableName = TableName.valueOf("testConcurrentSCPForSameServer");
try (Table t = createTable(tableName)) {
// Load the table with a bit of data so some logs to split and some edits in each region.
this.util.loadTable(t, HBaseTestingUtility.COLUMNS[0]);
final int count = HBaseTestingUtility.countRows(t);
assertTrue("expected some rows", count > 0);
// find the first server that match the request and executes the test
ServerName rsToKill = null;
for (RegionInfo hri : util.getAdmin().getRegions(tableName)) {
final ServerName serverName = AssignmentTestingUtil.getServerHoldingRegion(util, hri);
if (AssignmentTestingUtil.isServerHoldingMeta(util, serverName) == true) {
rsToKill = serverName;
break;
}
}
HMaster master = util.getHBaseCluster().getMaster();
final ProcedureExecutor<MasterProcedureEnv> pExecutor = master.getMasterProcedureExecutor();
ServerCrashProcedure procB =
new ServerCrashProcedure(pExecutor.getEnvironment(), rsToKill, false, false);
AssignmentTestingUtil.killRs(util, rsToKill);
long procId = getSCPProcId(pExecutor);
Procedure<?> procA = pExecutor.getProcedure(procId);
LOG.info("submit SCP procedureA");
util.waitFor(5000, () -> procA.hasLock());
LOG.info("procedureA acquired the lock");
assertEquals(Procedure.LockState.LOCK_EVENT_WAIT,
procB.acquireLock(pExecutor.getEnvironment()));
LOG.info("procedureB should not be able to get the lock");
util.waitFor(60000,
() -> procB.acquireLock(pExecutor.getEnvironment()) == Procedure.LockState.LOCK_ACQUIRED);
LOG.info("when procedure B get the lock, procedure A should be finished");
assertTrue(procA.isFinished());
}
}
protected static MasterServices getMockedMaster() throws IOException {
TableDescriptors tds = Mockito.mock(TableDescriptors.class);
Mockito.when(tds.get(tables[0])).thenReturn(tableDescs.get(tables[0]));
Mockito.when(tds.get(tables[1])).thenReturn(tableDescs.get(tables[1]));
Mockito.when(tds.get(tables[2])).thenReturn(tableDescs.get(tables[2]));
Mockito.when(tds.get(tables[3])).thenReturn(tableDescs.get(tables[3]));
MasterServices services = Mockito.mock(HMaster.class);
Mockito.when(services.getTableDescriptors()).thenReturn(tds);
AssignmentManager am = Mockito.mock(AssignmentManager.class);
Mockito.when(services.getAssignmentManager()).thenReturn(am);
return services;
}
@Test
public void testPostModifyNamespace() throws IOException {
final Admin admin = UTIL.getAdmin();
final String ns = "postmodifyns";
NamespaceDescriptor nsDesc = NamespaceDescriptor.create(ns).build();
admin.createNamespace(nsDesc);
HMaster master = UTIL.getMiniHBaseCluster().getMaster();
MasterObserverForTest observer = master.getMasterCoprocessorHost().findCoprocessor(
MasterObserverForTest.class);
int preCount = observer.postHookCalls.get();
try {
admin.modifyNamespace(NamespaceDescriptor.create("nonexistent").build());
fail("Modifying a missing namespace should fail");
} catch (IOException e) {
// Pass
}
int postCount = observer.postHookCalls.get();
assertEquals("Expected no invocations of postModifyNamespace when the operation fails",
preCount, postCount);
// Validate that the postDeletNS hook is invoked
preCount = observer.postHookCalls.get();
admin.modifyNamespace(
NamespaceDescriptor.create(nsDesc).addConfiguration("foo", "bar").build());
postCount = observer.postHookCalls.get();
assertEquals("Expected 1 invocation of postModifyNamespace", preCount + 1, postCount);
}
@Test
public void testPostDeleteNamespace() throws IOException {
final Admin admin = UTIL.getAdmin();
final String ns = "postdeletens";
final TableName tn1 = TableName.valueOf(ns, "table1");
admin.createNamespace(NamespaceDescriptor.create(ns).build());
admin.createTable(TableDescriptorBuilder.newBuilder(tn1)
.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("f1")).build())
.build());
HMaster master = UTIL.getMiniHBaseCluster().getMaster();
MasterObserverForTest observer = master.getMasterCoprocessorHost().findCoprocessor(
MasterObserverForTest.class);
int preCount = observer.postHookCalls.get();
try {
admin.deleteNamespace(ns);
fail("Deleting a non-empty namespace should be disallowed");
} catch (IOException e) {
// Pass
}
int postCount = observer.postHookCalls.get();
assertEquals("Expected no invocations of postDeleteNamespace when the operation fails",
preCount, postCount);
// Disable and delete the table so that we can delete the NS.
admin.disableTable(tn1);
admin.deleteTable(tn1);
// Validate that the postDeletNS hook is invoked
preCount = observer.postHookCalls.get();
admin.deleteNamespace(ns);
postCount = observer.postHookCalls.get();
assertEquals("Expected 1 invocation of postDeleteNamespace", preCount + 1, postCount);
}
/**
* Gets the current active master, if available. If no active master, returns
* null.
* @return the HMaster for the active master
*/
public HMaster getActiveMaster() {
for (JVMClusterUtil.MasterThread mt : masterThreads) {
// Ensure that the current active master is not stopped.
// We don't want to return a stopping master as an active master.
if (mt.getMaster().isActiveMaster() && !mt.getMaster().isStopped()) {
return mt.getMaster();
}
}
return null;
}
public MobFileCompactionChore(HMaster master) {
super(master.getServerName() + "-MobFileCompactionChore", master,
master.getConfiguration().getInt(MobConstants.MOB_COMPACTION_CHORE_PERIOD,
MobConstants.DEFAULT_MOB_COMPACTION_CHORE_PERIOD),
master.getConfiguration().getInt(MobConstants.MOB_COMPACTION_CHORE_PERIOD,
MobConstants.DEFAULT_MOB_COMPACTION_CHORE_PERIOD),
TimeUnit.SECONDS);
this.master = master;
this.regionBatchSize =
master.getConfiguration().getInt(MobConstants.MOB_MAJOR_COMPACTION_REGION_BATCH_SIZE,
MobConstants.DEFAULT_MOB_MAJOR_COMPACTION_REGION_BATCH_SIZE);
}
private Table createTableAndLoadData(HMaster master, TableName tablename,
int numRegions, int replication) throws Exception {
assertTrue("ROWSIZE must > numregions:" + numRegions, ROWSIZE > numRegions);
byte[][] splitRows = new byte[numRegions - 1][];
for (int i = 0; i < splitRows.length; i++) {
splitRows[i] = ROWS[(i + 1) * ROWSIZE / numRegions];
}
Table table = TEST_UTIL.createTable(tablename, FAMILYNAME, splitRows);
LOG.info("Created " + table.getName());
if (replication > 1) {
HBaseTestingUtility.setReplicas(ADMIN, tablename, replication);
LOG.info("Set replication of " + replication + " on " + table.getName());
}
loadData(table);
LOG.info("Loaded " + table.getName());
verifyRowCount(table, ROWSIZE);
LOG.info("Verified " + table.getName());
List<Pair<RegionInfo, ServerName>> tableRegions;
TEST_UTIL.waitUntilAllRegionsAssigned(tablename);
LOG.info("All regions assigned for table - " + table.getName());
tableRegions = MetaTableAccessor.getTableRegionsAndLocations(
TEST_UTIL.getConnection(), tablename);
assertEquals("Wrong number of regions in table " + tablename,
numRegions * replication, tableRegions.size());
LOG.info(tableRegions.size() + "Regions after load: " + Joiner.on(',').join(tableRegions));
assertEquals(numRegions * replication, tableRegions.size());
return table;
}