blob: f7457fb7223503064d03b4152538b552627e3cbd [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.metron.pcapservice;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.log4j.Logger;
import org.mortbay.log.Log;
/**
* Utility class which creates HConnection instance when the first request is
* received and registers a shut down hook which closes the connection when the
* JVM exits. Creates new connection to the cluster only if the existing
* connection is closed for unknown reasons. Also creates Configuration with
* HBase resources using configuration properties.
*
* @author Sayi
*
*/
public class HBaseConfigurationUtil {
/** The Constant LOGGER. */
private static final Logger LOGGER = Logger
.getLogger(HBaseConfigurationUtil.class);
/** Configuration which holds all HBase properties. */
private static Configuration config;
/**
* A cluster connection which knows how to find master node and locate regions
* on the cluster.
*/
private static HConnection clusterConnection = null;
/**
* Creates HConnection instance when the first request is received and returns
* the same instance for all subsequent requests if the connection is still
* open.
*
* @return HConnection instance
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public static HConnection getConnection() throws IOException {
if (!connectionAvailable()) {
synchronized (HBaseConfigurationUtil.class) {
createClusterConncetion();
}
}
return clusterConnection;
}
/**
* Creates the cluster conncetion.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private static void createClusterConncetion() throws IOException {
try {
if (connectionAvailable()) {
return;
}
clusterConnection = HConnectionManager.createConnection(read());
addShutdownHook();
System.out.println("Created HConnection and added shutDownHook");
} catch (IOException e) {
LOGGER
.error(
"Exception occurred while creating HConnection using HConnectionManager",
e);
throw e;
}
}
/**
* Connection available.
*
* @return true, if successful
*/
private static boolean connectionAvailable() {
if (clusterConnection == null) {
System.out.println("clusterConnection=" + clusterConnection);
return false;
}
System.out.println("clusterConnection.isClosed()="
+ clusterConnection.isClosed());
return clusterConnection != null && !clusterConnection.isClosed();
}
/**
* Adds the shutdown hook.
*/
private static void addShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
System.out
.println("Executing ShutdownHook HBaseConfigurationUtil : Closing HConnection");
try {
clusterConnection.close();
} catch (IOException e) {
Log.debug("Caught ignorable exception ", e);
}
}
}, "HBaseConfigurationUtilShutDown"));
}
/**
* Closes the underlying connection to cluster; ignores if any exception is
* thrown.
*/
public static void closeConnection() {
if (clusterConnection != null) {
try {
clusterConnection.close();
} catch (IOException e) {
Log.debug("Caught ignorable exception ", e);
}
}
}
/**
* This method creates Configuration with HBase resources using configuration
* properties. The same Configuration object will be used to communicate with
* all HBase tables;
*
* @return Configuration object
*/
public static Configuration read() {
if (config == null) {
synchronized (HBaseConfigurationUtil.class) {
if (config == null) {
config = HBaseConfiguration.create();
config.set(
HBaseConfigConstants.HBASE_ZOOKEEPER_QUORUM,
ConfigurationUtil.getConfiguration().getString(
"hbase.zookeeper.quorum"));
config.set(
HBaseConfigConstants.HBASE_ZOOKEEPER_CLIENT_PORT,
ConfigurationUtil.getConfiguration().getString(
"hbase.zookeeper.clientPort"));
config.set(
HBaseConfigConstants.HBASE_CLIENT_RETRIES_NUMBER,
ConfigurationUtil.getConfiguration().getString(
"hbase.client.retries.number"));
config.set(
HBaseConfigConstants.HBASE_ZOOKEEPER_SESSION_TIMEOUT,
ConfigurationUtil.getConfiguration().getString(
"zookeeper.session.timeout"));
config.set(
HBaseConfigConstants.HBASE_ZOOKEEPER_RECOVERY_RETRY,
ConfigurationUtil.getConfiguration().getString(
"zookeeper.recovery.retry"));
}
}
}
return config;
}
}