Github user jacques-n commented on a diff in the pull request: https://github.com/apache/drill/pull/443#discussion_r65015084 --- Diff: contrib/storage-hbase/src/main/java/org/apache/drill/exec/store/hbase/HBaseConnectionManager.java --- @@ -0,0 +1,92 @@ +/** + * 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.drill.exec.store.hbase; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import org.apache.drill.common.exceptions.UserException; +import org.apache.drill.exec.store.hbase.HBaseStoragePlugin.HBaseConnectionKey; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.ConnectionFactory; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; +import com.google.common.util.concurrent.UncheckedExecutionException; + +/** + * <p>A singleton class which manages the lifecycle of HBase connections.</p> + * <p>One connection per storage plugin instance is maintained.</p> + */ +public final class HBaseConnectionManager + extends CacheLoader<HBaseConnectionKey, Connection> implements RemovalListener<HBaseConnectionKey, Connection> { + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HBaseConnectionManager.class); + + public static final HBaseConnectionManager INSTANCE = new HBaseConnectionManager(); + + private final LoadingCache<HBaseConnectionKey, Connection> connectionCache; + + private HBaseConnectionManager() { + this.connectionCache = CacheBuilder.newBuilder() + .expireAfterAccess(24, TimeUnit.HOURS) + .removalListener(this) + .build(this); + } + + @Override + public Connection load(HBaseConnectionKey key) throws Exception { + Connection connection = ConnectionFactory.createConnection(key.getHBaseConf()); + logger.debug("HBase connection '{}' created.", connection); + return connection; + } + + @Override + public void onRemoval(RemovalNotification<HBaseConnectionKey, Connection> notification) { + try { + Connection conn = notification.getValue(); + if (conn != null) { + conn.close(); + } + logger.debug("HBase connection '{}' closed.", notification.getValue()); + } catch (Throwable t) { + logger.warn("Error while closing HBase connection.", t); + } + } + + public Connection getConnection(HBaseConnectionKey key) { + try { + Connection conn = connectionCache.get(key); + if (conn.isAborted() + || conn.isClosed()) { + connectionCache.invalidate(key); --- End diff -- It seems like there is a possible race condition here. If two separate threads both realize a key is bad, the first one may invalidate the key and insert a new one, then return that key. Meanwhile, the second thread will invalidate the new connection from the first thread (and thus close the connection) causing the first threads connection to be closed while in use. I think there is a way to check a value in Guava's model to ensure that it is valid before return. If not, you probably need to do a double-checked locking inside the invalidation (or similar).
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---