Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.ArrayList;
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.concurrent.ExecutorService;
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 futureConnection1;
CompletableFuture futureConnection2;
public ParallelPhoenixConnection(ParallelPhoenixContext context) throws SQLException {
this.context = context;
LOG.trace("First Url: {} Second Url: {}", context.getHaGroup().getGroupInfo().getJDBCUrl1(),
context.getHaGroup().getGroupInfo().getJDBCUrl2());
futureConnection1 = context.chainOnConn1(() -> getConnection(context.getHaGroup(),
context.getHaGroup().getGroupInfo().getJDBCUrl1(),
context.getProperties()));
futureConnection2 = context.chainOnConn2(() -> getConnection(context.getHaGroup(),
context.getHaGroup().getGroupInfo().getJDBCUrl2(),
context.getProperties()));
// Ensure one connection is successful before returning
ParallelPhoenixUtil.INSTANCE.runFutures(Arrays.asList(futureConnection1, futureConnection2), context, false);
}
@VisibleForTesting
ParallelPhoenixConnection(ParallelPhoenixContext context, CompletableFuture futureConnection1, CompletableFuture futureConnection2) throws SQLException {
this.context = context;
this.futureConnection1 = futureConnection1;
this.futureConnection2 = futureConnection2;
// Ensure one connection is successful before returning
ParallelPhoenixUtil.INSTANCE.runFutures(Arrays.asList(futureConnection1, futureConnection2), context, false);
}
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 getFutureConnection1() {
return futureConnection1;
}
public CompletableFuture getFutureConnection2() {
return futureConnection2;
}
@VisibleForTesting
ParallelPhoenixContext getContext() {
return this.context;
}
Object runOnConnections(Function function, boolean useMetrics) throws SQLException {
return ParallelPhoenixUtil.INSTANCE.runFutures(function, futureConnection1, futureConnection2, context, useMetrics);
}
PairOfSameType