dbwong commented on code in PR #1430: URL: https://github.com/apache/phoenix/pull/1430#discussion_r899652092
########## phoenix-core/src/main/java/org/apache/phoenix/jdbc/ParallelPhoenixConnection.java: ########## @@ -0,0 +1,653 @@ +/* + * 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.hadoop.hbase.util.PairOfSameType; +import org.apache.phoenix.exception.SQLExceptionInfo; +import org.apache.phoenix.jdbc.ParallelPhoenixUtil.FutureResult; +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.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.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Stream; + +import static org.apache.phoenix.exception.SQLExceptionCode.CLASS_NOT_UNWRAPPABLE; + +public class ParallelPhoenixConnection implements PhoenixMonitoredConnection { + + private static final Logger LOG = LoggerFactory.getLogger(ParallelPhoenixConnection.class); + + private final ParallelPhoenixContext context; + CompletableFuture<PhoenixConnection> futureConnection1; + CompletableFuture<PhoenixConnection> futureConnection2; + + public ParallelPhoenixConnection(ParallelPhoenixContext context) throws SQLException { + this.context = context; + LOG.debug("First Url: {} Second Url: {}", context.getHaGroup().getGroupInfo().getUrl1(), + context.getHaGroup().getGroupInfo().getUrl2()); + futureConnection1 = context.chainOnConn1(() -> getConnection(context.getHaGroup(), + context.getHaGroup().getGroupInfo().getUrl1(), + context.getProperties())); + futureConnection2 = context.chainOnConn2(() -> getConnection(context.getHaGroup(), + context.getHaGroup().getGroupInfo().getUrl2(), + context.getProperties())); + } + + @VisibleForTesting + ParallelPhoenixConnection(ParallelPhoenixContext context, CompletableFuture<PhoenixConnection> futureConnection1, CompletableFuture<PhoenixConnection> futureConnection2) { + this.context = context; + this.futureConnection1 = futureConnection1; + this.futureConnection2 = futureConnection2; + } + + private static PhoenixConnection getConnection(HighAvailabilityGroup haGroup, String url, Properties properties) { + try { + return haGroup.connectToOneCluster(url, properties); + } catch (SQLException exception) { + if (LOG.isTraceEnabled()) { + LOG.trace(String.format("Failed to get a connection for haGroup %s to %s", haGroup.toString(), url), exception); + } + throw new CompletionException(exception); + } + } + + public CompletableFuture<PhoenixConnection> getFutureConnection1() { + return futureConnection1; + } + + public CompletableFuture<PhoenixConnection> getFutureConnection2() { + return futureConnection2; + } + + @VisibleForTesting + ParallelPhoenixContext getContext() { + return this.context; + } + + Object runOnConnections(Function<PhoenixConnection, ?> function, boolean useMetrics) throws SQLException { + return ParallelPhoenixUtil.INSTANCE.runFutures(function, futureConnection1, futureConnection2, context, useMetrics); + } + + PairOfSameType<Object> runOnConnectionsGetAll(Function<PhoenixConnection, ?> function, boolean useMetrics) throws SQLException { + return ParallelPhoenixUtil.INSTANCE.runOnFuturesGetAll(function, futureConnection1, futureConnection2, context, useMetrics); + } + + @Override + public ParallelPhoenixStatement createStatement() throws SQLException { + context.checkOpen(); + + Function<PhoenixConnection, PhoenixMonitoredStatement> function = (T) -> { + try { + return (PhoenixStatement) T.createStatement(); + } catch (SQLException exception) { + throw new CompletionException(exception); + } + }; + + List<CompletableFuture<PhoenixMonitoredStatement>> futures = ParallelPhoenixUtil.INSTANCE.applyFunctionToFutures(function, futureConnection1, + futureConnection2, context, true); + + Preconditions.checkState(futures.size() == 2); + CompletableFuture<PhoenixMonitoredStatement> statement1 = futures.get(0); + CompletableFuture<PhoenixMonitoredStatement> statement2 = futures.get(1); + + //Ensure one statement is successful before returning + ParallelPhoenixUtil.INSTANCE.runFutures(futures, context, true); + + return new ParallelPhoenixStatement(context, statement1, statement2); + } + + @Override + public PreparedStatement prepareStatement(String sql) throws SQLException { + context.checkOpen(); + + Function<PhoenixConnection, PhoenixMonitoredPreparedStatement> function = (T) -> { + try { + return (PhoenixMonitoredPreparedStatement) T.prepareStatement(sql); + } catch (SQLException exception) { + throw new CompletionException(exception); + } + }; + + List<CompletableFuture<PhoenixMonitoredPreparedStatement>> futures = ParallelPhoenixUtil.INSTANCE.applyFunctionToFutures(function, futureConnection1, + futureConnection2, context, true); + + Preconditions.checkState(futures.size() == 2); + CompletableFuture<PhoenixMonitoredPreparedStatement> statement1 = futures.get(0); + CompletableFuture<PhoenixMonitoredPreparedStatement> statement2 = futures.get(1); + + //Ensure one statement is successful before returning + ParallelPhoenixUtil.INSTANCE.runFutures(futures, context, true); Review Comment: We would return a failure to the end user. Operations get queued until a failure occurs on a connection. -- 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]
