@Override
public void preMasterInitialization(ObserverContext<MasterCoprocessorEnvironment> ctx)
throws IOException {
LOG.info("Entering into preMasterInitialization.");
MasterServices master = ctx.getEnvironment().getMasterServices();
AssignmentManager am = master.getAssignmentManager();
ZKTable zkTable = am.getZKTable();
long timeout =
master.getConfiguration().getLong("hbase.bulk.assignment.waiton.empty.rit", 5 * 60 * 1000);
try {
am.waitUntilNoRegionsInTransition(timeout);
} catch (InterruptedException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Interrupted while waiting for the regions in transition to complete.", e);
}
}
TableDescriptors tableDescriptors = master.getTableDescriptors();
Map<String, HTableDescriptor> descMap = tableDescriptors.getAll();
Collection<HTableDescriptor> htds = descMap.values();
IndexedHTableDescriptor iHtd = null;
Configuration conf = master.getConfiguration();
FileSystem fs = FSUtils.getCurrentFileSystem(conf);
Path rootPath = FSUtils.getRootDir(conf);
for (HTableDescriptor htd : htds) {
if (false == htd.getNameAsString().endsWith(Constants.INDEX_TABLE_SUFFIX)) {
FSDataInputStream fsDataInputStream = null;
try {
Path path = FSUtils.getTablePath(rootPath, htd.getName());
FileStatus status = getTableInfoPath(fs, path);
if (null == status) {
return;
}
fsDataInputStream = fs.open(status.getPath());
iHtd = new IndexedHTableDescriptor();
iHtd.readFields(fsDataInputStream);
} catch (EOFException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(iHtd.getNameAsString() + " is normal table and not an indexed table.", e);
}
} catch (IOException i) {
throw i;
} finally {
if (null != fsDataInputStream) {
fsDataInputStream.close();
}
}
if (false == iHtd.getIndices().isEmpty()) {
String tableName = iHtd.getNameAsString();
String indexTableName = IndexUtils.getIndexTableName(tableName);
boolean tableExists = MetaReader.tableExists(master.getCatalogTracker(), tableName);
boolean indexTableExists =
MetaReader.tableExists(master.getCatalogTracker(), indexTableName);
if ((true == tableExists) && (false == indexTableExists)) {
LOG.info("Table has index specification details but " + "no corresponding index table.");
List<HRegionInfo> regions =
MetaReader.getTableRegions(master.getCatalogTracker(), iHtd.getName());
HRegionInfo[] regionsArray = new HRegionInfo[regions.size()];
byte[][] splitKeys = IndexUtils.getSplitKeys(regions.toArray(regionsArray));
createSecondaryIndexTable(iHtd, splitKeys, master, false);
} else if (true == tableExists && true == indexTableExists) {
// If both tables are present both should be in same state in zookeeper. If tables are
// partially enabled or disabled they will be processed as part of recovery
// enabling/disabling tables.
// if user table is in ENABLED state and index table is in DISABLED state means master
// restarted as soon as user table enabled. So here we need to enable index table.
if (zkTable.isEnabledTable(tableName) && zkTable.isDisabledTable(indexTableName)) {
new EnableTableHandler(master, Bytes.toBytes(indexTableName),
master.getCatalogTracker(), am, false).process();
} else if (zkTable.isDisabledTable(tableName) && zkTable.isEnabledTable(indexTableName)) {
// If user table is in DISABLED state and index table is in ENABLED state means master
// restarted as soon as user table disabled. So here we need to disable index table.
new DisableTableHandler(master, Bytes.toBytes(indexTableName),
master.getCatalogTracker(), am, false).process();
// clear index table region plans in secondary index load balancer.
clearRegionPlans((HMaster) master, indexTableName);
}
}
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Balancing after master initialization.");
}
try {
master.getAssignmentManager().waitUntilNoRegionsInTransition(timeout);
} catch (InterruptedException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Interrupted while waiting for the regions in transition to complete.", e);
}
}