PHOENIX-3334 ConnectionQueryServicesImpl should close HConnection if init fails
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/58596bbc Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/58596bbc Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/58596bbc Branch: refs/heads/calcite Commit: 58596bbc416ce577d3407910f1a1000027150d8c Parents: c6e703d Author: Samarth <[email protected]> Authored: Tue Sep 27 17:55:54 2016 -0700 Committer: Samarth <[email protected]> Committed: Tue Sep 27 17:55:54 2016 -0700 ---------------------------------------------------------------------- .../exception/RetriableUpgradeException.java | 31 ++++++++++++++++++++ .../exception/UpgradeInProgressException.java | 3 +- .../exception/UpgradeNotRequiredException.java | 3 +- .../exception/UpgradeRequiredException.java | 3 +- .../query/ConnectionQueryServicesImpl.java | 24 ++++++++++++++- 5 files changed, 57 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/58596bbc/phoenix-core/src/main/java/org/apache/phoenix/exception/RetriableUpgradeException.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/exception/RetriableUpgradeException.java b/phoenix-core/src/main/java/org/apache/phoenix/exception/RetriableUpgradeException.java new file mode 100644 index 0000000..b0f747d --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/exception/RetriableUpgradeException.java @@ -0,0 +1,31 @@ +/* + * 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.phoenix.exception; + +import java.sql.SQLException; + +/** + * + * Super class for upgrade related exceptions whose occurrence shouldn't prevent the + * client from retrying or reestablishing connection. + */ +public abstract class RetriableUpgradeException extends SQLException { + public RetriableUpgradeException(String message, String sqlState, int sqlExceptionCode) { + super(message, sqlState, sqlExceptionCode); + } +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/58596bbc/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeInProgressException.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeInProgressException.java b/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeInProgressException.java index 5b15216..08ae304 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeInProgressException.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeInProgressException.java @@ -17,9 +17,8 @@ */ package org.apache.phoenix.exception; -import java.sql.SQLException; -public class UpgradeInProgressException extends SQLException { +public class UpgradeInProgressException extends RetriableUpgradeException { public UpgradeInProgressException(String upgradeFrom, String upgradeTo) { super("Cluster is being concurrently upgraded from " + upgradeFrom + " to " + upgradeTo + ". Please retry establishing connection.", SQLExceptionCode.CONCURRENT_UPGRADE_IN_PROGRESS http://git-wip-us.apache.org/repos/asf/phoenix/blob/58596bbc/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeNotRequiredException.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeNotRequiredException.java b/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeNotRequiredException.java index 0490319..7e94977 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeNotRequiredException.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeNotRequiredException.java @@ -17,9 +17,8 @@ */ package org.apache.phoenix.exception; -import java.sql.SQLException; -public class UpgradeNotRequiredException extends SQLException { +public class UpgradeNotRequiredException extends RetriableUpgradeException { public UpgradeNotRequiredException() { super("Operation not allowed since cluster has already been upgraded. ", SQLExceptionCode.UPGRADE_NOT_REQUIRED .getSQLState(), SQLExceptionCode.UPGRADE_NOT_REQUIRED.getErrorCode()); http://git-wip-us.apache.org/repos/asf/phoenix/blob/58596bbc/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeRequiredException.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeRequiredException.java b/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeRequiredException.java index 005a1bd..9352a50 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeRequiredException.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/exception/UpgradeRequiredException.java @@ -17,9 +17,8 @@ */ package org.apache.phoenix.exception; -import java.sql.SQLException; -public class UpgradeRequiredException extends SQLException { +public class UpgradeRequiredException extends RetriableUpgradeException { public UpgradeRequiredException() { super("Operation not allowed since cluster hasn't been upgraded. Call EXECUTE UPGRADE. ", http://git-wip-us.apache.org/repos/asf/phoenix/blob/58596bbc/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java index dc07220..f5f85e4 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java @@ -130,6 +130,7 @@ import org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse; import org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataService; import org.apache.phoenix.coprocessor.generated.MetaDataProtos.UpdateIndexStateRequest; import org.apache.phoenix.exception.PhoenixIOException; +import org.apache.phoenix.exception.RetriableUpgradeException; import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.exception.SQLExceptionInfo; import org.apache.phoenix.exception.UpgradeInProgressException; @@ -2316,8 +2317,11 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement return null; } checkClosed(); + boolean hConnectionEstablished = false; + boolean success = false; try { openConnection(); + hConnectionEstablished = true; boolean isDoNotUpgradePropSet = UpgradeUtil.isNoUpgradeSet(props); try (HBaseAdmin admin = getAdmin()) { boolean mappedSystemCatalogExists = admin @@ -2364,6 +2368,11 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement } } scheduleRenewLeaseTasks(); + success = true; + } catch (RetriableUpgradeException e) { + // Don't set it as initializationException because otherwise the clien't won't be able + // to retry establishing connection. + throw e; } catch (Exception e) { if (e instanceof SQLException) { initializationException = (SQLException)e; @@ -2372,7 +2381,20 @@ public class ConnectionQueryServicesImpl extends DelegateQueryServices implement initializationException = new SQLException(e); } } finally { - initialized = true; + try { + if (!success && hConnectionEstablished) { + connection.close(); + } + } catch (IOException e) { + SQLException ex = new SQLException(e); + if (initializationException != null) { + initializationException.setNextException(ex); + } else { + initializationException = ex; + } + } finally { + initialized = true; + } } } return null;
