-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathDBSQLClient.ts
More file actions
368 lines (314 loc) · 11.5 KB
/
DBSQLClient.ts
File metadata and controls
368 lines (314 loc) · 11.5 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import thrift from 'thrift';
import Int64 from 'node-int64';
import { EventEmitter } from 'events';
import { HeadersInit } from 'node-fetch';
import TCLIService from '../thrift/TCLIService';
import { TProtocolVersion } from '../thrift/TCLIService_types';
import IDBSQLClient, { ClientOptions, ConnectionOptions, OpenSessionRequest } from './contracts/IDBSQLClient';
import IDriver from './contracts/IDriver';
import IClientContext, { ClientConfig } from './contracts/IClientContext';
import IThriftClient from './contracts/IThriftClient';
import HiveDriver from './hive/HiveDriver';
import DBSQLSession from './DBSQLSession';
import IDBSQLSession from './contracts/IDBSQLSession';
import IAuthentication from './connection/contracts/IAuthentication';
import HttpConnection from './connection/connections/HttpConnection';
import IConnectionOptions from './connection/contracts/IConnectionOptions';
import Status from './dto/Status';
import HiveDriverError from './errors/HiveDriverError';
import { buildUserAgentString, definedOrError } from './utils';
import PlainHttpAuthentication from './connection/auth/PlainHttpAuthentication';
import DatabricksOAuth, { OAuthFlow } from './connection/auth/DatabricksOAuth';
import {
TokenProviderAuthenticator,
StaticTokenProvider,
ExternalTokenProvider,
CachedTokenProvider,
FederationProvider,
ITokenProvider,
} from './connection/auth/tokenProvider';
import IDBSQLLogger, { LogLevel } from './contracts/IDBSQLLogger';
import DBSQLLogger from './DBSQLLogger';
import CloseableCollection from './utils/CloseableCollection';
import IConnectionProvider from './connection/contracts/IConnectionProvider';
function prependSlash(str: string): string {
if (str.length > 0 && str.charAt(0) !== '/') {
return `/${str}`;
}
return str;
}
function getInitialNamespaceOptions(catalogName?: string, schemaName?: string) {
if (!catalogName && !schemaName) {
return {};
}
return {
initialNamespace: {
catalogName,
schemaName,
},
};
}
export type ThriftLibrary = Pick<typeof thrift, 'createClient'>;
export default class DBSQLClient extends EventEmitter implements IDBSQLClient, IClientContext {
private static defaultLogger?: IDBSQLLogger;
private readonly config: ClientConfig;
private connectionProvider?: IConnectionProvider;
private authProvider?: IAuthentication;
private client?: IThriftClient;
private readonly driver = new HiveDriver({
context: this,
});
private readonly logger: IDBSQLLogger;
private thrift: ThriftLibrary = thrift;
private readonly sessions = new CloseableCollection<DBSQLSession>();
private static getDefaultLogger(): IDBSQLLogger {
if (!this.defaultLogger) {
this.defaultLogger = new DBSQLLogger();
}
return this.defaultLogger;
}
private static getDefaultConfig(): ClientConfig {
return {
directResultsDefaultMaxRows: 100000,
fetchChunkDefaultMaxRows: 100000,
arrowEnabled: true,
useArrowNativeTypes: true,
socketTimeout: 15 * 60 * 1000, // 15 minutes
retryMaxAttempts: 5,
retriesTimeout: 15 * 60 * 1000, // 15 minutes
retryDelayMin: 1 * 1000, // 1 second
retryDelayMax: 60 * 1000, // 60 seconds (1 minute)
useCloudFetch: true, // enabling cloud fetch by default.
cloudFetchConcurrentDownloads: 10,
cloudFetchSpeedThresholdMBps: 0.1,
useLZ4Compression: true,
};
}
constructor(options?: ClientOptions) {
super();
this.config = DBSQLClient.getDefaultConfig();
this.logger = options?.logger ?? DBSQLClient.getDefaultLogger();
this.logger.log(LogLevel.info, 'Created DBSQLClient');
}
private getConnectionOptions(options: ConnectionOptions): IConnectionOptions {
return {
host: options.host,
port: options.port || 443,
path: prependSlash(options.path),
https: true,
socketTimeout: options.socketTimeout,
proxy: options.proxy,
headers: {
'User-Agent': buildUserAgentString(options.userAgentEntry),
},
};
}
private createAuthProvider(options: ConnectionOptions, authProvider?: IAuthentication): IAuthentication {
if (authProvider) {
return authProvider;
}
switch (options.authType) {
case undefined:
case 'access-token':
return new PlainHttpAuthentication({
username: 'token',
password: options.token,
context: this,
});
case 'databricks-oauth':
return new DatabricksOAuth({
flow: options.oauthClientSecret === undefined ? OAuthFlow.U2M : OAuthFlow.M2M,
host: options.host,
persistence: options.persistence,
azureTenantId: options.azureTenantId,
clientId: options.oauthClientId,
clientSecret: options.oauthClientSecret,
useDatabricksOAuthInAzure: options.useDatabricksOAuthInAzure,
context: this,
});
case 'custom':
return options.provider;
case 'token-provider':
return new TokenProviderAuthenticator(
this.wrapTokenProvider(
options.tokenProvider,
options.host,
options.enableTokenFederation,
options.federationClientId,
),
this,
);
case 'external-token':
return new TokenProviderAuthenticator(
this.wrapTokenProvider(
new ExternalTokenProvider(options.getToken),
options.host,
options.enableTokenFederation,
options.federationClientId,
),
this,
);
case 'static-token':
return new TokenProviderAuthenticator(
this.wrapTokenProvider(
StaticTokenProvider.fromJWT(options.staticToken),
options.host,
options.enableTokenFederation,
options.federationClientId,
),
this,
);
// no default
}
}
/**
* Wraps a token provider with caching and optional federation.
* Caching is always enabled by default. Federation is opt-in.
*/
private wrapTokenProvider(
provider: ITokenProvider,
host: string,
enableFederation?: boolean,
federationClientId?: string,
): ITokenProvider {
// Always wrap with caching first
let wrapped: ITokenProvider = new CachedTokenProvider(provider);
// Optionally wrap with federation
if (enableFederation) {
wrapped = new FederationProvider(wrapped, host, {
clientId: federationClientId,
});
}
return wrapped;
}
private createConnectionProvider(options: ConnectionOptions): IConnectionProvider {
return new HttpConnection(this.getConnectionOptions(options), this);
}
/**
* Connects DBSQLClient to endpoint
* @public
* @param options - host, path, and token are required
* @param authProvider - [DEPRECATED - use `authType: 'custom'] Optional custom authentication provider
* @returns Session object that can be used to execute statements
* @example
* const session = client.connect({host, path, token});
*/
public async connect(options: ConnectionOptions, authProvider?: IAuthentication): Promise<IDBSQLClient> {
const deprecatedClientId = (options as any).clientId;
if (deprecatedClientId !== undefined) {
this.logger.log(
LogLevel.warn,
'Warning: The "clientId" option is deprecated. Please use "userAgentEntry" instead.',
);
if (!options.userAgentEntry) {
options.userAgentEntry = deprecatedClientId;
}
}
// Store enableMetricViewMetadata configuration
if (options.enableMetricViewMetadata !== undefined) {
this.config.enableMetricViewMetadata = options.enableMetricViewMetadata;
}
this.authProvider = this.createAuthProvider(options, authProvider);
this.connectionProvider = this.createConnectionProvider(options);
const thriftConnection = await this.connectionProvider.getThriftConnection();
thriftConnection.on('error', (error: Error) => {
// Error.stack already contains error type and message, so log stack if available,
// otherwise fall back to just error type + message
this.logger.log(LogLevel.error, error.stack || `${error.name}: ${error.message}`);
try {
this.emit('error', error);
} catch (e) {
// EventEmitter will throw unhandled error when emitting 'error' event.
// Since we already logged it few lines above, just suppress this behaviour
}
});
thriftConnection.on('reconnecting', (params: { delay: number; attempt: number }) => {
this.logger.log(LogLevel.debug, `Reconnecting, params: ${JSON.stringify(params)}`);
this.emit('reconnecting', params);
});
thriftConnection.on('close', () => {
this.logger.log(LogLevel.debug, 'Closing connection.');
this.emit('close');
});
thriftConnection.on('timeout', () => {
this.logger.log(LogLevel.debug, 'Connection timed out.');
this.emit('timeout');
});
return this;
}
/**
* Starts new session
* @public
* @param request - Can be instantiated with initialSchema, empty by default
* @returns Session object that can be used to execute statements
* @throws {StatusError}
* @example
* const session = await client.openSession();
*/
public async openSession(request: OpenSessionRequest = {}): Promise<IDBSQLSession> {
// Prepare session configuration
const configuration = request.configuration ? { ...request.configuration } : {};
// Add metric view metadata config if enabled
if (this.config.enableMetricViewMetadata) {
configuration['spark.sql.thriftserver.metadata.metricview.enabled'] = 'true';
}
const response = await this.driver.openSession({
client_protocol_i64: new Int64(TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8),
...getInitialNamespaceOptions(request.initialCatalog, request.initialSchema),
configuration,
canUseMultipleCatalogs: true,
});
Status.assert(response.status);
const session = new DBSQLSession({
handle: definedOrError(response.sessionHandle),
context: this,
serverProtocolVersion: response.serverProtocolVersion,
});
this.sessions.add(session);
return session;
}
public async close(): Promise<void> {
await this.sessions.closeAll();
this.client = undefined;
this.connectionProvider = undefined;
this.authProvider = undefined;
}
public getConfig(): ClientConfig {
return this.config;
}
public getLogger(): IDBSQLLogger {
return this.logger;
}
public async getConnectionProvider(): Promise<IConnectionProvider> {
if (!this.connectionProvider) {
throw new HiveDriverError('DBSQLClient: not connected');
}
return this.connectionProvider;
}
public async getClient(): Promise<IThriftClient> {
const connectionProvider = await this.getConnectionProvider();
if (!this.client) {
this.logger.log(LogLevel.info, 'DBSQLClient: initializing thrift client');
this.client = this.thrift.createClient(TCLIService, await connectionProvider.getThriftConnection());
}
if (this.authProvider) {
const authHeaders = await this.authProvider.authenticate();
connectionProvider.setHeaders(authHeaders);
}
return this.client;
}
public async getDriver(): Promise<IDriver> {
return this.driver;
}
public async getAuthHeaders(): Promise<HeadersInit> {
if (this.authProvider) {
try {
return await this.authProvider.authenticate();
} catch (error) {
this.logger.log(LogLevel.debug, `Error getting auth headers: ${error}`);
return {};
}
}
return {};
}
}