HBASE-4087 HBaseAdmin should perform validation of connection it holds

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1147352 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Zhihong Yu 2011-07-16 01:18:36 +00:00
parent 6f476bfd0e
commit 6b55b8dbe6
3 changed files with 50 additions and 15 deletions

View File

@ -160,6 +160,7 @@ Release 0.91.0 - Unreleased
HRegion.delete (Adam Warrington via Ted Yu)
HBASE-3893 HRegion.internalObtainRowLock shouldn't wait forever
HBASE-4075 A bug in TestZKBasedOpenCloseRegion (Jieshan Bean via Ted Yu)
HBASE-4087 HBaseAdmin should perform validation of connection it holds
IMPROVEMENTS
HBASE-3290 Max Compaction Size (Nicolas Spiegelberg via Stack)

View File

@ -1,5 +1,5 @@
/**
* Copyright 2010 The Apache Software Foundation
* Copyright 2011 The Apache Software Foundation
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@ -22,12 +22,11 @@ package org.apache.hadoop.hbase.client;
import java.io.Closeable;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.SocketTimeoutException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
@ -76,7 +75,7 @@ import org.apache.hadoop.util.StringUtils;
public class HBaseAdmin implements Abortable, Closeable {
private final Log LOG = LogFactory.getLog(this.getClass().getName());
// private final HConnection connection;
private final HConnection connection;
private HConnection connection;
private volatile Configuration conf;
private final long pause;
private final int numRetries;
@ -84,7 +83,7 @@ public class HBaseAdmin implements Abortable, Closeable {
// numRetries is for 'normal' stuff... Mutliply by this factor when
// want to wait a long time.
private final int retryLongerMultiplier;
/**
* Constructor
*
@ -95,11 +94,30 @@ public class HBaseAdmin implements Abortable, Closeable {
public HBaseAdmin(Configuration c)
throws MasterNotRunningException, ZooKeeperConnectionException {
this.conf = HBaseConfiguration.create(c);
this.connection = HConnectionManager.getConnection(this.conf);
this.connection = HConnectionManager.getConnection(this.conf);
this.pause = this.conf.getLong("hbase.client.pause", 1000);
this.numRetries = this.conf.getInt("hbase.client.retries.number", 10);
this.retryLongerMultiplier = this.conf.getInt("hbase.client.retries.longer.multiplier", 10);
this.connection.getMaster();
this.retryLongerMultiplier = this.conf.getInt(
"hbase.client.retries.longer.multiplier", 10);
int tries = 0;
for (; tries < numRetries; ++tries) {
try {
this.connection.getMaster();
break;
} catch (UndeclaredThrowableException ute) {
HConnectionManager.deleteStaleConnection(this.connection);
this.connection = HConnectionManager.getConnection(this.conf);
}
try { // Sleep
Thread.sleep(getPauseTime(tries));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MasterNotRunningException("Interrupted");
}
}
if (tries >= numRetries) {
throw new MasterNotRunningException("Retried " + numRetries + " times");
}
}
/**

View File

@ -199,7 +199,21 @@ public class HConnectionManager {
* .
*/
public static void deleteConnection(Configuration conf, boolean stopProxy) {
deleteConnection(new HConnectionKey(conf), stopProxy);
deleteConnection(new HConnectionKey(conf), stopProxy, false);
}
/**
* Delete stale connection information for the instance specified by configuration.
* This will then close connection to
* the zookeeper ensemble and let go of all resources.
*
* @param conf
* configuration whose identity is used to find {@link HConnection}
* instance.
* .
*/
public static void deleteStaleConnection(HConnection connection) {
deleteConnection(connection, true, true);
}
/**
@ -212,31 +226,33 @@ public class HConnectionManager {
Set<HConnectionKey> connectionKeys = new HashSet<HConnectionKey>();
connectionKeys.addAll(HBASE_INSTANCES.keySet());
for (HConnectionKey connectionKey : connectionKeys) {
deleteConnection(connectionKey, stopProxy);
deleteConnection(connectionKey, stopProxy, false);
}
HBASE_INSTANCES.clear();
}
}
private static void deleteConnection(HConnection connection, boolean stopProxy) {
private static void deleteConnection(HConnection connection, boolean stopProxy,
boolean staleConnection) {
synchronized (HBASE_INSTANCES) {
for (Entry<HConnectionKey, HConnectionImplementation> connectionEntry : HBASE_INSTANCES
.entrySet()) {
if (connectionEntry.getValue() == connection) {
deleteConnection(connectionEntry.getKey(), stopProxy);
deleteConnection(connectionEntry.getKey(), stopProxy, staleConnection);
break;
}
}
}
}
private static void deleteConnection(HConnectionKey connectionKey, boolean stopProxy) {
private static void deleteConnection(HConnectionKey connectionKey,
boolean stopProxy, boolean staleConnection) {
synchronized (HBASE_INSTANCES) {
HConnectionImplementation connection = HBASE_INSTANCES
.get(connectionKey);
if (connection != null) {
connection.decCount();
if (connection.isZeroReference()) {
if (connection.isZeroReference() || staleConnection) {
HBASE_INSTANCES.remove(connectionKey);
connection.close(stopProxy);
} else if (stopProxy) {
@ -1699,7 +1715,7 @@ public class HConnectionManager {
}
public void close() {
HConnectionManager.deleteConnection(this, stopProxy);
HConnectionManager.deleteConnection((HConnection)this, stopProxy, false);
LOG.debug("The connection to " + this.zooKeeper + " has been closed.");
}