HBASE-12652 Allow unmanaged connections in MetaTableAccessor (Solomon Duskis); REAPPLY

This commit is contained in:
stack 2014-12-17 11:11:35 -08:00
parent 5b51218a56
commit 476bd40716
11 changed files with 108 additions and 91 deletions

View File

@ -22,8 +22,9 @@ import com.google.protobuf.ServiceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.client.ClusterConnection;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
@ -170,37 +171,31 @@ public class MetaTableAccessor {
}
/**
* Callers should call close on the returned {@link HTable} instance.
* @param connection connection we're using to access table
* @param tableName Table to get an {@link org.apache.hadoop.hbase.client.HTable} against.
* @return An {@link org.apache.hadoop.hbase.client.HTable} for <code>tableName</code>
* Callers should call close on the returned {@link Table} instance.
* @param connection connection we're using to access Meta
* @return An {@link Table} for <code>hbase:meta</code>
* @throws IOException
* @SuppressWarnings("deprecation")
*/
private static Table getHTable(final Connection connection, final TableName tableName)
static Table getMetaHTable(final Connection connection)
throws IOException {
// We used to pass whole CatalogTracker in here, now we just pass in Connection
if (connection == null || connection.isClosed()) {
throw new NullPointerException("No connection");
}
// If the passed in 'connection' is 'managed' -- i.e. every second test uses
// an HTable or an HBaseAdmin with managed connections -- then doing
// a Table or an HBaseAdmin with managed connections -- then doing
// connection.getTable will throw an exception saying you are NOT to use
// managed connections getting tables. Leaving this as it is for now. Will
// revisit when inclined to change all tests. User code probaby makes use of
// managed connections too so don't change it till post hbase 1.0.
return new HTable(tableName, connection);
}
/**
* Callers should call close on the returned {@link HTable} instance.
* @param connection connection we're using to access Meta
* @return An {@link HTable} for <code>hbase:meta</code>
* @throws IOException
*/
static Table getMetaHTable(final Connection connection)
throws IOException {
return getHTable(connection, TableName.META_TABLE_NAME);
//
// There should still be a way to use this method with an unmanaged connection.
if (connection instanceof ClusterConnection) {
if (((ClusterConnection) connection).isManaged()) {
return new HTable(TableName.META_TABLE_NAME, (ClusterConnection) connection);
}
}
return connection.getTable(TableName.META_TABLE_NAME);
}
/**
@ -808,7 +803,7 @@ public class MetaTableAccessor {
* @return a pair of HRegionInfo or PairOfSameType(null, null) if the region is not a split
* parent
*/
public static PairOfSameType<HRegionInfo> getDaughterRegions(Result data) throws IOException {
public static PairOfSameType<HRegionInfo> getDaughterRegions(Result data) {
HRegionInfo splitA = getHRegionInfo(data, HConstants.SPLITA_QUALIFIER);
HRegionInfo splitB = getHRegionInfo(data, HConstants.SPLITB_QUALIFIER);
@ -822,7 +817,7 @@ public class MetaTableAccessor {
* @return a pair of HRegionInfo or PairOfSameType(null, null) if the region is not a split
* parent
*/
public static PairOfSameType<HRegionInfo> getMergeRegions(Result data) throws IOException {
public static PairOfSameType<HRegionInfo> getMergeRegions(Result data) {
HRegionInfo mergeA = getHRegionInfo(data, HConstants.MERGEA_QUALIFIER);
HRegionInfo mergeB = getHRegionInfo(data, HConstants.MERGEB_QUALIFIER);
@ -1089,8 +1084,8 @@ public class MetaTableAccessor {
/**
* Adds a hbase:meta row for the specified new region to the given catalog table. The
* HTable is not flushed or closed.
* @param meta the HTable for META
* Table is not flushed or closed.
* @param meta the Table for META
* @param regionInfo region information
* @throws IOException if problem connecting or updating meta
*/
@ -1105,7 +1100,7 @@ public class MetaTableAccessor {
* {@link #splitRegion(org.apache.hadoop.hbase.client.Connection,
* HRegionInfo, HRegionInfo, HRegionInfo, ServerName)}
* if you want to do that.
* @param meta the HTable for META
* @param meta the Table for META
* @param regionInfo region information
* @param splitA first split daughter of the parent regionInfo
* @param splitB second split daughter of the parent regionInfo

View File

@ -282,5 +282,11 @@ public interface ClusterConnection extends HConnection {
* @return RpcRetryingCallerFactory
*/
RpcRetryingCallerFactory getNewRpcRetryingCallerFactory(Configuration conf);
/**
*
* @return true if this is a managed connection.
*/
boolean isManaged();
}

View File

@ -437,4 +437,9 @@ abstract class ConnectionAdapter implements ClusterConnection {
public RpcRetryingCallerFactory getNewRpcRetryingCallerFactory(Configuration conf) {
return wrappedConnection.getNewRpcRetryingCallerFactory(conf);
}
@Override
public boolean isManaged() {
return wrappedConnection.isManaged();
}
}

View File

@ -2476,6 +2476,11 @@ class ConnectionManager {
public RpcRetryingCallerFactory getNewRpcRetryingCallerFactory(Configuration conf) {
return RpcRetryingCallerFactory.instantiate(conf, this.interceptor);
}
@Override
public boolean isManaged() {
return managed;
}
}
/**

View File

@ -1757,7 +1757,7 @@ public class HBaseFsck extends Configured {
for (OnlineEntry rse : hi.deployedEntries) {
LOG.debug("Undeploy region " + rse.hri + " from " + rse.hsa);
try {
HBaseFsckRepair.closeRegionSilentlyAndWait(admin, rse.hsa, rse.hri);
HBaseFsckRepair.closeRegionSilentlyAndWait(connection, rse.hsa, rse.hri);
offline(rse.hri.getRegionName());
} catch (IOException ioe) {
LOG.warn("Got exception when attempting to offline region "
@ -1809,7 +1809,7 @@ public class HBaseFsck extends Configured {
}
// close the region -- close files and remove assignment
HBaseFsckRepair.closeRegionSilentlyAndWait(admin, serverName, hri);
HBaseFsckRepair.closeRegionSilentlyAndWait(connection, serverName, hri);
}
private void tryAssignmentRepair(HbckInfo hbi, String msg) throws IOException,
@ -1974,7 +1974,7 @@ public class HBaseFsck extends Configured {
if (shouldFixAssignments()) {
errors.print("Trying to close the region " + descriptiveName);
setShouldRerun();
HBaseFsckRepair.fixMultiAssignment(admin, hbi.metaEntry, hbi.deployedOn);
HBaseFsckRepair.fixMultiAssignment(connection, hbi.metaEntry, hbi.deployedOn);
}
} else if (inMeta && inHdfs && isMultiplyDeployed) {
errors.reportError(ERROR_CODE.MULTI_DEPLOYED, "Region " + descriptiveName
@ -1985,7 +1985,7 @@ public class HBaseFsck extends Configured {
if (shouldFixAssignments()) {
errors.print("Trying to fix assignment error...");
setShouldRerun();
HBaseFsckRepair.fixMultiAssignment(admin, hbi.metaEntry, hbi.deployedOn);
HBaseFsckRepair.fixMultiAssignment(connection, hbi.metaEntry, hbi.deployedOn);
}
} else if (inMeta && inHdfs && isDeployed && !deploymentMatchesMeta) {
errors.reportError(ERROR_CODE.SERVER_DOES_NOT_MATCH_META, "Region "
@ -1996,7 +1996,7 @@ public class HBaseFsck extends Configured {
if (shouldFixAssignments()) {
errors.print("Trying to fix assignment error...");
setShouldRerun();
HBaseFsckRepair.fixMultiAssignment(admin, hbi.metaEntry, hbi.deployedOn);
HBaseFsckRepair.fixMultiAssignment(connection, hbi.metaEntry, hbi.deployedOn);
HBaseFsckRepair.waitUntilAssigned(admin, hbi.getHdfsHRI());
}
} else {
@ -2901,7 +2901,7 @@ public class HBaseFsck extends Configured {
errors.print("Trying to fix a problem with hbase:meta..");
setShouldRerun();
// try fix it (treat is a dupe assignment)
HBaseFsckRepair.fixMultiAssignment(admin, metaHbckInfo.metaEntry, servers);
HBaseFsckRepair.fixMultiAssignment(connection, metaHbckInfo.metaEntry, servers);
}
}
// rerun hbck with hopefully fixed META

View File

@ -18,23 +18,20 @@
*/
package org.apache.hadoop.hbase.util;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MetaTableAccessor;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.MetaTableAccessor;
import org.apache.hadoop.hbase.classification.InterfaceAudience;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Table;
@ -44,6 +41,12 @@ import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.AdminService;
import org.apache.hadoop.hbase.regionserver.HRegion;
import org.apache.zookeeper.KeeperException;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* This class contains helper methods that repair parts of hbase's filesystem
* contents.
@ -57,22 +60,22 @@ public class HBaseFsckRepair {
* and then force ZK unassigned node to OFFLINE to trigger assignment by
* master.
*
* @param admin HBase admin used to undeploy
* @param connection HBase connection to the cluster
* @param region Region to undeploy
* @param servers list of Servers to undeploy from
*/
public static void fixMultiAssignment(HBaseAdmin admin, HRegionInfo region,
public static void fixMultiAssignment(HConnection connection, HRegionInfo region,
List<ServerName> servers)
throws IOException, KeeperException, InterruptedException {
HRegionInfo actualRegion = new HRegionInfo(region);
// Close region on the servers silently
for(ServerName server : servers) {
closeRegionSilentlyAndWait(admin, server, actualRegion);
closeRegionSilentlyAndWait(connection, server, actualRegion);
}
// Force ZK node to OFFLINE so master assigns
forceOfflineInZK(admin, actualRegion);
forceOfflineInZK(connection.getAdmin(), actualRegion);
}
/**
@ -146,16 +149,15 @@ public class HBaseFsckRepair {
* (default 120s) to close the region. This bypasses the active hmaster.
*/
@SuppressWarnings("deprecation")
public static void closeRegionSilentlyAndWait(HBaseAdmin admin,
public static void closeRegionSilentlyAndWait(HConnection connection,
ServerName server, HRegionInfo region) throws IOException, InterruptedException {
HConnection connection = admin.getConnection();
AdminService.BlockingInterface rs = connection.getAdmin(server);
try {
ProtobufUtil.closeRegion(rs, server, region.getRegionName(), false);
} catch (IOException e) {
LOG.warn("Exception when closing region: " + region.getRegionNameAsString(), e);
}
long timeout = admin.getConfiguration()
long timeout = connection.getConfiguration()
.getLong("hbase.hbck.close.timeout", 120000);
long expiration = timeout + System.currentTimeMillis();
while (System.currentTimeMillis() < expiration) {
@ -177,9 +179,11 @@ public class HBaseFsckRepair {
*/
public static void fixMetaHoleOnline(Configuration conf,
HRegionInfo hri) throws IOException {
Table meta = new HTable(conf, TableName.META_TABLE_NAME);
Connection conn = ConnectionFactory.createConnection(conf);
Table meta = conn.getTable(TableName.META_TABLE_NAME);
MetaTableAccessor.addRegionToMeta(meta, hri);
meta.close();
conn.close();
}
/**

View File

@ -32,6 +32,7 @@ import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
@ -62,10 +63,11 @@ public class TestMetaTableAccessor {
// responsive. 1 second is default as is ten retries.
c.setLong("hbase.client.pause", 1000);
c.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 10);
connection = HConnectionManager.getConnection(c);
connection = ConnectionFactory.createConnection(c);
}
@AfterClass public static void afterClass() throws Exception {
connection.close();
UTIL.shutdownMiniCluster();
}

View File

@ -21,14 +21,11 @@ package org.apache.hadoop.hbase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.RegionLocator;
@ -47,6 +44,12 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* Test whether region re-balancing works. (HBASE-71)
*/
@ -97,7 +100,8 @@ public class TestRegionRebalancing {
@SuppressWarnings("deprecation")
public void testRebalanceOnRegionServerNumberChange()
throws IOException, InterruptedException {
HBaseAdmin admin = new HBaseAdmin(UTIL.getConfiguration());
Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
Admin admin = connection.getAdmin();
admin.createTable(this.desc, Arrays.copyOfRange(HBaseTestingUtility.KEYS,
1, HBaseTestingUtility.KEYS.length));
this.table = new HTable(UTIL.getConfiguration(), this.desc.getTableName());

View File

@ -137,6 +137,8 @@ public class HConnectionTestingUtility {
RetryingCallerInterceptorFactory.NO_OP_INTERCEPTOR));
HTableInterface t = Mockito.mock(HTableInterface.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;
}

View File

@ -40,6 +40,8 @@ import org.apache.hadoop.hbase.testclassification.LargeTests;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.Tag;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
@ -72,6 +74,9 @@ public class TestReplicationWithTags {
private static ReplicationAdmin replicationAdmin;
private static Connection connection1;
private static Connection connection2;
private static Table htable1;
private static Table htable2;
@ -136,22 +141,13 @@ public class TestReplicationWithTags {
fam.setMaxVersions(3);
fam.setScope(HConstants.REPLICATION_SCOPE_GLOBAL);
table.addFamily(fam);
Admin admin = null;
try {
admin = new HBaseAdmin(conf1);
try (Connection conn = ConnectionFactory.createConnection(conf1);
Admin admin = conn.getAdmin()) {
admin.createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
} finally {
if (admin != null) {
admin.close();
}
}
try {
admin = new HBaseAdmin(conf2);
try (Connection conn = ConnectionFactory.createConnection(conf2);
Admin admin = conn.getAdmin()) {
admin.createTable(table, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
} finally {
if(admin != null){
admin.close();
}
}
htable1 = new HTable(conf1, TABLE_NAME);
htable1.setWriteBufferSize(1024);

View File

@ -265,12 +265,12 @@ public class TestHBaseFsck {
* This method is used to undeploy a region -- close it and attempt to
* remove its state from the Master.
*/
private void undeployRegion(HBaseAdmin admin, ServerName sn,
private void undeployRegion(HConnection conn, ServerName sn,
HRegionInfo hri) throws IOException, InterruptedException {
try {
HBaseFsckRepair.closeRegionSilentlyAndWait(admin, sn, hri);
HBaseFsckRepair.closeRegionSilentlyAndWait(conn, sn, hri);
if (!hri.isMetaTable()) {
admin.offline(hri.getRegionName());
conn.getAdmin().offline(hri.getRegionName());
}
} catch (IOException ioe) {
LOG.warn("Got exception when attempting to offline region "
@ -303,6 +303,7 @@ public class TestHBaseFsck {
dumpMeta(htd.getTableName());
Map<HRegionInfo, ServerName> hris = tbl.getRegionLocations();
HConnection conn = (HConnection) ConnectionFactory.createConnection(conf);
for (Entry<HRegionInfo, ServerName> e: hris.entrySet()) {
HRegionInfo hri = e.getKey();
ServerName hsa = e.getValue();
@ -314,7 +315,7 @@ public class TestHBaseFsck {
if (unassign) {
LOG.info("Undeploying region " + hri + " from server " + hsa);
undeployRegion(new HBaseAdmin(conf), hsa, hri);
undeployRegion(conn, hsa, hri);
}
if (regionInfoOnly) {
@ -352,6 +353,7 @@ public class TestHBaseFsck {
TEST_UTIL.getMetaTableRows(htd.getTableName());
LOG.info("*** After delete:");
dumpMeta(htd.getTableName());
conn.close();
}
/**
@ -410,8 +412,9 @@ public class TestHBaseFsck {
* @throws IOException
*/
void deleteTable(TableName tablename) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conf);
admin.getConnection().clearRegionCache();
HConnection conn = (HConnection) ConnectionFactory.createConnection(conf);
Admin admin = conn.getAdmin();
conn.clearRegionCache();
if (admin.isTableEnabled(tablename)) {
admin.disableTableAsync(tablename);
}
@ -431,6 +434,8 @@ public class TestHBaseFsck {
}
}
admin.deleteTable(tablename);
admin.close();
conn.close();
}
/**
@ -808,7 +813,7 @@ public class TestHBaseFsck {
public void testSidelineOverlapRegion() throws Exception {
TableName table =
TableName.valueOf("testSidelineOverlapRegion");
try {
try (HConnection conn = (HConnection) ConnectionFactory.createConnection(conf)){
setupTable(table);
assertEquals(ROWKEYS.length, countRows());
@ -850,7 +855,7 @@ public class TestHBaseFsck {
}
HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
HBaseFsckRepair.closeRegionSilentlyAndWait(admin,
HBaseFsckRepair.closeRegionSilentlyAndWait(conn,
cluster.getRegionServer(k).getServerName(), hbi.getHdfsHRI());
admin.offline(regionName);
break;
@ -859,7 +864,7 @@ public class TestHBaseFsck {
assertNotNull(regionName);
assertNotNull(serverName);
Table meta = new HTable(conf, TableName.META_TABLE_NAME, executorService);
Table meta = conn.getTable(TableName.META_TABLE_NAME, executorService);
Put put = new Put(regionName);
put.add(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
Bytes.toBytes(serverName.getHostAndPort()));
@ -1471,8 +1476,8 @@ public class TestHBaseFsck {
public void testSplitDaughtersNotInMeta() throws Exception {
TableName table =
TableName.valueOf("testSplitdaughtersNotInMeta");
Table meta = null;
try {
try (HConnection conn = (HConnection) ConnectionFactory.createConnection(conf);
Table meta = conn.getTable(TableName.META_TABLE_NAME)){
setupTable(table);
assertEquals(ROWKEYS.length, countRows());
@ -1480,13 +1485,11 @@ public class TestHBaseFsck {
TEST_UTIL.getHBaseAdmin().flush(table);
HRegionLocation location = tbl.getRegionLocation("B");
meta = new HTable(conf, TableName.META_TABLE_NAME);
HRegionInfo hri = location.getRegionInfo();
// do a regular split
HBaseAdmin admin = TEST_UTIL.getHBaseAdmin();
byte[] regionName = location.getRegionInfo().getRegionName();
admin.splitRegion(location.getRegionInfo().getRegionName(), Bytes.toBytes("BM"));
conn.getAdmin().splitRegion(location.getRegionInfo().getRegionName(), Bytes.toBytes("BM"));
TestEndToEndSplitTransaction.blockUntilRegionSplit(
TEST_UTIL.getConfiguration(), 60000, regionName, true);
@ -1494,8 +1497,8 @@ public class TestHBaseFsck {
// Delete daughter regions from meta, but not hdfs, unassign it.
Map<HRegionInfo, ServerName> hris = tbl.getRegionLocations();
undeployRegion(admin, hris.get(daughters.getFirst()), daughters.getFirst());
undeployRegion(admin, hris.get(daughters.getSecond()), daughters.getSecond());
undeployRegion(conn, hris.get(daughters.getFirst()), daughters.getFirst());
undeployRegion(conn, hris.get(daughters.getSecond()), daughters.getSecond());
meta.delete(new Delete(daughters.getFirst().getRegionName()));
meta.delete(new Delete(daughters.getSecond().getRegionName()));
@ -1529,7 +1532,6 @@ public class TestHBaseFsck {
assertNoErrors(doFsck(conf, false)); //should be fixed by now
} finally {
deleteTable(table);
IOUtils.closeQuietly(meta);
}
}
@ -2254,13 +2256,9 @@ public class TestHBaseFsck {
HRegionInfo hri = metaLocation.getRegionInfo();
if (unassign) {
LOG.info("Undeploying meta region " + hri + " from server " + hsa);
Connection unmanagedConnection = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) unmanagedConnection.getAdmin();
try {
undeployRegion(admin, hsa, hri);
} finally {
admin.close();
unmanagedConnection.close();
try (HConnection unmanagedConnection =
(HConnection) ConnectionFactory.createConnection(conf)) {
undeployRegion(unmanagedConnection, hsa, hri);
}
}