forked from apache/arrow-adbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparkHttpConnection.cs
More file actions
286 lines (261 loc) · 16.2 KB
/
SparkHttpConnection.cs
File metadata and controls
286 lines (261 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* 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.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Apache.Arrow.Adbc.Drivers.Apache.Hive2;
using Apache.Arrow.Ipc;
using Apache.Hive.Service.Rpc.Thrift;
using Thrift;
using Thrift.Protocol;
using Thrift.Transport;
namespace Apache.Arrow.Adbc.Drivers.Apache.Spark
{
internal class SparkHttpConnection : SparkConnection
{
private const string BasicAuthenticationScheme = "Basic";
private const string BearerAuthenticationScheme = "Bearer";
public SparkHttpConnection(IReadOnlyDictionary<string, string> properties) : base(properties)
{
}
protected override void ValidateAuthentication()
{
// Validate authentication parameters
Properties.TryGetValue(SparkParameters.Token, out string? token);
Properties.TryGetValue(AdbcOptions.Username, out string? username);
Properties.TryGetValue(AdbcOptions.Password, out string? password);
Properties.TryGetValue(SparkParameters.AuthType, out string? authType);
Properties.TryGetValue(SparkParameters.AccessToken, out string? access_token);
if (!SparkAuthTypeParser.TryParse(authType, out SparkAuthType authTypeValue))
{
throw new ArgumentOutOfRangeException(SparkParameters.AuthType, authType, $"Unsupported {SparkParameters.AuthType} value.");
}
switch (authTypeValue)
{
case SparkAuthType.Token:
if (string.IsNullOrWhiteSpace(token))
throw new ArgumentException(
$"Parameter '{SparkParameters.AuthType}' is set to '{SparkAuthTypeConstants.Token}' but parameter '{SparkParameters.Token}' is not set. Please provide a value for '{SparkParameters.Token}'.",
nameof(Properties));
break;
case SparkAuthType.Basic:
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
throw new ArgumentException(
$"Parameter '{SparkParameters.AuthType}' is set to '{SparkAuthTypeConstants.Basic}' but parameters '{AdbcOptions.Username}' or '{AdbcOptions.Password}' are not set. Please provide a values for these parameters.",
nameof(Properties));
break;
case SparkAuthType.UsernameOnly:
if (string.IsNullOrWhiteSpace(username))
throw new ArgumentException(
$"Parameter '{SparkParameters.AuthType}' is set to '{SparkAuthTypeConstants.UsernameOnly}' but parameter '{AdbcOptions.Username}' is not set. Please provide a values for this parameter.",
nameof(Properties));
break;
case SparkAuthType.None:
break;
case SparkAuthType.Empty:
if (string.IsNullOrWhiteSpace(token) && (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)))
throw new ArgumentException(
$"Parameters must include valid authentiation settings. Please provide either '{SparkParameters.Token}'; or '{AdbcOptions.Username}' and '{AdbcOptions.Password}'.",
nameof(Properties));
break;
case SparkAuthType.OAuth:
if (string.IsNullOrWhiteSpace(access_token))
throw new ArgumentException(
$"Parameter '{SparkParameters.AuthType}' is set to '{SparkAuthTypeConstants.OAuth}' but parameter '{SparkParameters.AccessToken}' is not set. Please provide a value for '{SparkParameters.AccessToken}'.",
nameof(Properties));
break;
default:
throw new ArgumentOutOfRangeException(SparkParameters.AuthType, authType, $"Unsupported {SparkParameters.AuthType} value.");
}
}
protected override void ValidateConnection()
{
// HostName or Uri is required parameter
Properties.TryGetValue(AdbcOptions.Uri, out string? uri);
Properties.TryGetValue(SparkParameters.HostName, out string? hostName);
if ((Uri.CheckHostName(hostName) == UriHostNameType.Unknown)
&& (string.IsNullOrEmpty(uri) || !Uri.TryCreate(uri, UriKind.Absolute, out Uri? _)))
{
throw new ArgumentException(
$"Required parameter '{SparkParameters.HostName}' or '{AdbcOptions.Uri}' is missing or invalid. Please provide a valid hostname or URI for the data source.",
nameof(Properties));
}
// Validate port range
Properties.TryGetValue(SparkParameters.Port, out string? port);
if (int.TryParse(port, out int portNumber) && (portNumber <= IPEndPoint.MinPort || portNumber > IPEndPoint.MaxPort))
throw new ArgumentOutOfRangeException(
nameof(Properties),
port,
$"Parameter '{SparkParameters.Port}' value is not in the valid range of 1 .. {IPEndPoint.MaxPort}.");
// Ensure the parameters will produce a valid address
Properties.TryGetValue(SparkParameters.Path, out string? path);
_ = new HttpClient()
{
BaseAddress = GetBaseAddress(uri, hostName, path, port, SparkParameters.HostName, TlsOptions.IsTlsEnabled)
};
}
protected override void ValidateOptions()
{
Properties.TryGetValue(SparkParameters.DataTypeConv, out string? dataTypeConv);
DataTypeConversion = DataTypeConversionParser.Parse(dataTypeConv);
Properties.TryGetValue(SparkParameters.ConnectTimeoutMilliseconds, out string? connectTimeoutMs);
if (connectTimeoutMs != null)
{
ConnectTimeoutMilliseconds = int.TryParse(connectTimeoutMs, NumberStyles.Integer, CultureInfo.InvariantCulture, out int connectTimeoutMsValue) && (connectTimeoutMsValue >= 0)
? connectTimeoutMsValue
: throw new ArgumentOutOfRangeException(SparkParameters.ConnectTimeoutMilliseconds, connectTimeoutMs, $"must be a value of 0 (infinite) or between 1 .. {int.MaxValue}. default is 30000 milliseconds.");
}
TlsOptions = HiveServer2TlsImpl.GetHttpTlsOptions(Properties);
}
internal override IArrowArrayStream NewReader<T>(T statement, Schema schema, TGetResultSetMetadataResp? metadataResp = null) => new HiveServer2Reader(statement, schema, dataTypeConversion: statement.Connection.DataTypeConversion);
protected override TTransport CreateTransport()
{
// Assumption: parameters have already been validated.
Properties.TryGetValue(SparkParameters.HostName, out string? hostName);
Properties.TryGetValue(SparkParameters.Path, out string? path);
Properties.TryGetValue(SparkParameters.Port, out string? port);
Properties.TryGetValue(SparkParameters.AuthType, out string? authType);
if (!SparkAuthTypeParser.TryParse(authType, out SparkAuthType authTypeValue))
{
throw new ArgumentOutOfRangeException(SparkParameters.AuthType, authType, $"Unsupported {SparkParameters.AuthType} value.");
}
Properties.TryGetValue(SparkParameters.Token, out string? token);
Properties.TryGetValue(SparkParameters.AccessToken, out string? access_token);
Properties.TryGetValue(AdbcOptions.Username, out string? username);
Properties.TryGetValue(AdbcOptions.Password, out string? password);
Properties.TryGetValue(AdbcOptions.Uri, out string? uri);
Uri baseAddress = GetBaseAddress(uri, hostName, path, port, SparkParameters.HostName, TlsOptions.IsTlsEnabled);
AuthenticationHeaderValue? authenticationHeaderValue = GetAuthenticationHeaderValue(authTypeValue, token, username, password, access_token);
HttpClientHandler httpClientHandler = HiveServer2TlsImpl.NewHttpClientHandler(TlsOptions);
HttpClient httpClient = new(httpClientHandler);
httpClient.BaseAddress = baseAddress;
httpClient.DefaultRequestHeaders.Authorization = authenticationHeaderValue;
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(GetUserAgent());
httpClient.DefaultRequestHeaders.AcceptEncoding.Clear();
httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("identity"));
httpClient.DefaultRequestHeaders.ExpectContinue = false;
TConfiguration config = new();
ThriftHttpTransport transport = new(httpClient, config)
{
// This value can only be set before the first call/request. So if a new value for query timeout
// is set, we won't be able to update the value. Setting to ~infinite and relying on cancellation token
// to ensure cancelled correctly.
ConnectTimeout = int.MaxValue,
};
return transport;
}
private static AuthenticationHeaderValue? GetAuthenticationHeaderValue(SparkAuthType authType, string? token, string? username, string? password, string? access_token)
{
if (!string.IsNullOrEmpty(token) && (authType == SparkAuthType.Empty || authType == SparkAuthType.Token))
{
return new AuthenticationHeaderValue(BearerAuthenticationScheme, token);
}
else if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password) && (authType == SparkAuthType.Empty || authType == SparkAuthType.Basic))
{
return new AuthenticationHeaderValue(BasicAuthenticationScheme, Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")));
}
else if (!string.IsNullOrEmpty(username) && (authType == SparkAuthType.Empty || authType == SparkAuthType.UsernameOnly))
{
return new AuthenticationHeaderValue(BasicAuthenticationScheme, Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:")));
}
else if (!string.IsNullOrEmpty(access_token) && authType == SparkAuthType.OAuth)
{
return new AuthenticationHeaderValue(BearerAuthenticationScheme, access_token);
}
else if (authType == SparkAuthType.None)
{
return null;
}
else
{
throw new AdbcException("Missing connection properties. Must contain 'token' or 'username' and 'password'");
}
}
protected override async Task<TProtocol> CreateProtocolAsync(TTransport transport, CancellationToken cancellationToken = default)
{
if (!transport.IsOpen) await transport.OpenAsync(cancellationToken);
return new TBinaryProtocol(transport);
}
protected override TOpenSessionReq CreateSessionRequest()
{
var req = new TOpenSessionReq
{
Client_protocol = TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V10,
CanUseMultipleCatalogs = true,
};
return req;
}
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetSchemasResp response, CancellationToken cancellationToken = default) =>
GetResultSetMetadataAsync(response.OperationHandle, Client, cancellationToken);
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetCatalogsResp response, CancellationToken cancellationToken = default) =>
GetResultSetMetadataAsync(response.OperationHandle, Client, cancellationToken);
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetColumnsResp response, CancellationToken cancellationToken = default) =>
GetResultSetMetadataAsync(response.OperationHandle, Client, cancellationToken);
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetTablesResp response, CancellationToken cancellationToken = default) =>
GetResultSetMetadataAsync(response.OperationHandle, Client, cancellationToken);
protected internal override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetPrimaryKeysResp response, CancellationToken cancellationToken = default) =>
GetResultSetMetadataAsync(response.OperationHandle, Client, cancellationToken);
protected override Task<TRowSet> GetRowSetAsync(TGetTableTypesResp response, CancellationToken cancellationToken = default) =>
FetchResultsAsync(response.OperationHandle, cancellationToken: cancellationToken);
protected override Task<TRowSet> GetRowSetAsync(TGetColumnsResp response, CancellationToken cancellationToken = default) =>
FetchResultsAsync(response.OperationHandle, cancellationToken: cancellationToken);
protected override Task<TRowSet> GetRowSetAsync(TGetTablesResp response, CancellationToken cancellationToken = default) =>
FetchResultsAsync(response.OperationHandle, cancellationToken: cancellationToken);
protected override Task<TRowSet> GetRowSetAsync(TGetCatalogsResp response, CancellationToken cancellationToken = default) =>
FetchResultsAsync(response.OperationHandle, cancellationToken: cancellationToken);
protected override Task<TRowSet> GetRowSetAsync(TGetSchemasResp response, CancellationToken cancellationToken = default) =>
FetchResultsAsync(response.OperationHandle, cancellationToken: cancellationToken);
protected internal override Task<TRowSet> GetRowSetAsync(TGetPrimaryKeysResp response, CancellationToken cancellationToken = default) =>
FetchResultsAsync(response.OperationHandle, cancellationToken: cancellationToken);
internal override SchemaParser SchemaParser => new HiveServer2SchemaParser();
internal override SparkServerType ServerType => SparkServerType.Http;
protected override int ColumnMapIndexOffset => 1;
private string GetUserAgent()
{
// Build the base user agent string with Thrift version
string thriftVersion = GetThriftVersion();
string thriftComponent = string.IsNullOrEmpty(thriftVersion) ? "Thrift" : $"Thrift/{thriftVersion}";
string baseUserAgent = $"{DriverName.Replace(" ", "")}/{ProductVersionDefault} {thriftComponent}";
// Check if a client has provided a user-agent entry
if (Properties.TryGetValue(SparkParameters.UserAgentEntry, out string? userAgentEntry) && !string.IsNullOrWhiteSpace(userAgentEntry))
{
return $"{baseUserAgent} {userAgentEntry}";
}
return baseUserAgent;
}
private string GetThriftVersion()
{
try
{
var thriftAssembly = typeof(TProtocol).Assembly;
var version = thriftAssembly.GetName().Version;
return version != null ? $"{version.Major}.{version.Minor}.{version.Build}" : "";
}
catch
{
// Return empty string if there's any issue retrieving the assembly version
return "";
}
}
}
}