|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.arrow.driver.jdbc; |
| 19 | + |
| 20 | +import static org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty.replaceSemiColons; |
| 21 | + |
| 22 | +import java.sql.SQLException; |
| 23 | +import java.util.Properties; |
| 24 | +import java.util.concurrent.ExecutorService; |
| 25 | +import java.util.concurrent.Executors; |
| 26 | + |
| 27 | +import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler; |
| 28 | +import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl; |
| 29 | +import org.apache.arrow.flight.FlightClient; |
| 30 | +import org.apache.arrow.memory.BufferAllocator; |
| 31 | +import org.apache.arrow.util.AutoCloseables; |
| 32 | +import org.apache.arrow.util.Preconditions; |
| 33 | +import org.apache.calcite.avatica.AvaticaConnection; |
| 34 | +import org.apache.calcite.avatica.AvaticaFactory; |
| 35 | + |
| 36 | +import io.netty.util.concurrent.DefaultThreadFactory; |
| 37 | + |
| 38 | +/** |
| 39 | + * Connection to the Arrow Flight server. |
| 40 | + */ |
| 41 | +public final class ArrowFlightConnection extends AvaticaConnection { |
| 42 | + |
| 43 | + private final BufferAllocator allocator; |
| 44 | + private final ArrowFlightSqlClientHandler clientHandler; |
| 45 | + private final ArrowFlightConnectionConfigImpl config; |
| 46 | + private ExecutorService executorService; |
| 47 | + |
| 48 | + /** |
| 49 | + * Creates a new {@link ArrowFlightConnection}. |
| 50 | + * |
| 51 | + * @param driver the {@link ArrowFlightJdbcDriver} to use. |
| 52 | + * @param factory the {@link AvaticaFactory} to use. |
| 53 | + * @param url the URL to use. |
| 54 | + * @param properties the {@link Properties} to use. |
| 55 | + * @param config the {@link ArrowFlightConnectionConfigImpl} to use. |
| 56 | + * @param allocator the {@link BufferAllocator} to use. |
| 57 | + * @param clientHandler the {@link ArrowFlightSqlClientHandler} to use. |
| 58 | + */ |
| 59 | + private ArrowFlightConnection(final ArrowFlightJdbcDriver driver, final AvaticaFactory factory, |
| 60 | + final String url, final Properties properties, |
| 61 | + final ArrowFlightConnectionConfigImpl config, |
| 62 | + final BufferAllocator allocator, |
| 63 | + final ArrowFlightSqlClientHandler clientHandler) { |
| 64 | + super(driver, factory, url, properties); |
| 65 | + this.config = Preconditions.checkNotNull(config, "Config cannot be null."); |
| 66 | + this.allocator = Preconditions.checkNotNull(allocator, "Allocator cannot be null."); |
| 67 | + this.clientHandler = Preconditions.checkNotNull(clientHandler, "Handler cannot be null."); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates a new {@link ArrowFlightConnection} to a {@link FlightClient}. |
| 72 | + * |
| 73 | + * @param driver the {@link ArrowFlightJdbcDriver} to use. |
| 74 | + * @param factory the {@link AvaticaFactory} to use. |
| 75 | + * @param url the URL to establish the connection to. |
| 76 | + * @param properties the {@link Properties} to use for this session. |
| 77 | + * @param allocator the {@link BufferAllocator} to use. |
| 78 | + * @return a new {@link ArrowFlightConnection}. |
| 79 | + * @throws SQLException on error. |
| 80 | + */ |
| 81 | + static ArrowFlightConnection createNewConnection(final ArrowFlightJdbcDriver driver, |
| 82 | + final AvaticaFactory factory, |
| 83 | + String url, final Properties properties, |
| 84 | + final BufferAllocator allocator) |
| 85 | + throws SQLException { |
| 86 | + url = replaceSemiColons(url); |
| 87 | + final ArrowFlightConnectionConfigImpl config = new ArrowFlightConnectionConfigImpl(properties); |
| 88 | + final ArrowFlightSqlClientHandler clientHandler = createNewClientHandler(config, allocator); |
| 89 | + return new ArrowFlightConnection(driver, factory, url, properties, config, allocator, clientHandler); |
| 90 | + } |
| 91 | + |
| 92 | + private static ArrowFlightSqlClientHandler createNewClientHandler( |
| 93 | + final ArrowFlightConnectionConfigImpl config, |
| 94 | + final BufferAllocator allocator) throws SQLException { |
| 95 | + try { |
| 96 | + return new ArrowFlightSqlClientHandler.Builder() |
| 97 | + .withHost(config.getHost()) |
| 98 | + .withPort(config.getPort()) |
| 99 | + .withUsername(config.getUser()) |
| 100 | + .withPassword(config.getPassword()) |
| 101 | + .withTrustStorePath(config.getTrustStorePath()) |
| 102 | + .withTrustStorePassword(config.getTrustStorePassword()) |
| 103 | + .withSystemTrustStore(config.useSystemTrustStore()) |
| 104 | + .withBufferAllocator(allocator) |
| 105 | + .withEncryption(config.useEncryption()) |
| 106 | + .withDisableCertificateVerification(config.getDisableCertificateVerification()) |
| 107 | + .withToken(config.getToken()) |
| 108 | + .withCallOptions(config.toCallOption()) |
| 109 | + .build(); |
| 110 | + } catch (final SQLException e) { |
| 111 | + try { |
| 112 | + allocator.close(); |
| 113 | + } catch (final Exception allocatorCloseEx) { |
| 114 | + e.addSuppressed(allocatorCloseEx); |
| 115 | + } |
| 116 | + throw e; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + void reset() throws SQLException { |
| 121 | + // Clean up any open Statements |
| 122 | + try { |
| 123 | + AutoCloseables.close(statementMap.values()); |
| 124 | + } catch (final Exception e) { |
| 125 | + throw AvaticaConnection.HELPER.createException(e.getMessage(), e); |
| 126 | + } |
| 127 | + |
| 128 | + statementMap.clear(); |
| 129 | + |
| 130 | + // Reset Holdability |
| 131 | + this.setHoldability(this.metaData.getResultSetHoldability()); |
| 132 | + |
| 133 | + // Reset Meta |
| 134 | + ((ArrowFlightMetaImpl) this.meta).setDefaultConnectionProperties(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Gets the client {@link #clientHandler} backing this connection. |
| 139 | + * |
| 140 | + * @return the handler. |
| 141 | + */ |
| 142 | + ArrowFlightSqlClientHandler getClientHandler() throws SQLException { |
| 143 | + return clientHandler; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Gets the {@link ExecutorService} of this connection. |
| 148 | + * |
| 149 | + * @return the {@link #executorService}. |
| 150 | + */ |
| 151 | + synchronized ExecutorService getExecutorService() { |
| 152 | + return executorService = executorService == null ? |
| 153 | + Executors.newFixedThreadPool(config.threadPoolSize(), |
| 154 | + new DefaultThreadFactory(getClass().getSimpleName())) : |
| 155 | + executorService; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public Properties getClientInfo() { |
| 160 | + final Properties copy = new Properties(); |
| 161 | + copy.putAll(info); |
| 162 | + return copy; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void close() throws SQLException { |
| 167 | + if (executorService != null) { |
| 168 | + executorService.shutdown(); |
| 169 | + } |
| 170 | + |
| 171 | + try { |
| 172 | + AutoCloseables.close(clientHandler); |
| 173 | + allocator.getChildAllocators().forEach(AutoCloseables::closeNoChecked); |
| 174 | + AutoCloseables.close(allocator); |
| 175 | + |
| 176 | + super.close(); |
| 177 | + } catch (final Exception e) { |
| 178 | + throw AvaticaConnection.HELPER.createException(e.getMessage(), e); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + BufferAllocator getBufferAllocator() { |
| 183 | + return allocator; |
| 184 | + } |
| 185 | + |
| 186 | + public ArrowFlightMetaImpl getMeta() { |
| 187 | + return (ArrowFlightMetaImpl) this.meta; |
| 188 | + } |
| 189 | +} |
0 commit comments