dbwong commented on code in PR #1430: URL: https://github.com/apache/phoenix/pull/1430#discussion_r898505795
########## phoenix-core/src/main/java/org/apache/phoenix/jdbc/FailoverPhoenixConnection.java: ########## @@ -0,0 +1,646 @@ +/* + * 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.jdbc; + +import org.apache.phoenix.exception.FailoverSQLException; +import org.apache.phoenix.exception.SQLExceptionCode; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.monitoring.MetricType; +import org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting; +import org.apache.phoenix.thirdparty.com.google.common.base.Preconditions; +import org.apache.phoenix.util.EnvironmentEdgeManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executor; + +/** + * An implementation of JDBC connection which supports failover between two cluster in an HA group. + * <p> + * During its lifetime, a failover Phoenix connection could possibly connect to two HBase clusters + * in an HA group mutually exclusively. It wraps and delegates the logic to a PhoenixConnection + * object. At any given time, the wrapped connection should only talk to the ACTIVE HBase cluster + * in the HA group. + * <p> + * A failover connection will behave according to the given failover policy upon cluster role + * failover, especially when the current connected HBase cluster becomes STANDBY role from ACTIVE. + * The default behavior (aka default failover policy) will simply close the current connection and + * throw {@link org.apache.phoenix.exception.FailoverSQLException} exception to those clients who + * still use this connection after closing. + * <p> + * This class is not thread safe. + * + * @see HighAvailabilityGroup + * @see FailoverPolicy + */ +public class FailoverPhoenixConnection implements PhoenixMonitoredConnection { + /** + * Failover timeout interval after which failover operation will fail and clients can retry. + */ + public static final String FAILOVER_TIMEOUT_MS_ATTR = "phoenix.ha.failover.timeout.ms"; + public static final long FAILOVER_TIMEOUT_MS_DEFAULT = 10_000; + private static final Logger LOG = LoggerFactory.getLogger(FailoverPhoenixConnection.class); + /** + * Connection properties. + */ + private final Properties properties; + /** + * High availability group. + */ + private final HighAvailabilityGroup haGroup; + /** + * Failover policy, per connection. + */ + private final FailoverPolicy policy; + + /** + * True iff this connection has been closed by the client. + */ + private boolean isClosed; + /** + * The wrapped PhoenixConnection object which could be re-assigned upon failover operation. + */ + private PhoenixConnection connection; + + /** + * Mutation metrics before failover to current connection. + */ + private Map<String, Map<MetricType, Long>> previousMutationMetrics = new HashMap<>(); + /** + * Read metrics before failover to current connection. + */ + private Map<String, Map<MetricType, Long>> previousReadMetrics = new HashMap<>(); + + public FailoverPhoenixConnection(HighAvailabilityGroup haGroup, Properties properties) + throws SQLException { + this.properties = properties; + this.haGroup = haGroup; + this.policy = FailoverPolicy.get(properties); + this.isClosed = false; + this.connection = haGroup.connectActive(properties); + } + + /** + * This is used for explicit failover request made by client. + * <p> + * It fails over to the current ACTIVE HBase cluster; if failover happens in between, this could + * possibly target this same cluster again. + * <p> + * TODO: If there are two ACTIVE clusters, then switch to the other ACTIVE cluster. Review Comment: Yes not a important edge case sofar. I've removed the comment. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
