-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathDBSQLClient.ts
More file actions
681 lines (583 loc) · 22.3 KB
/
DBSQLClient.ts
File metadata and controls
681 lines (583 loc) · 22.3 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
import thrift from 'thrift';
import Int64 from 'node-int64';
import os from 'os';
import { EventEmitter } from 'events';
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';
import FeatureFlagCache from './telemetry/FeatureFlagCache';
import TelemetryClientProvider from './telemetry/TelemetryClientProvider';
import TelemetryEventEmitter from './telemetry/TelemetryEventEmitter';
import MetricsAggregator from './telemetry/MetricsAggregator';
import DatabricksTelemetryExporter from './telemetry/DatabricksTelemetryExporter';
import { CircuitBreakerRegistry } from './telemetry/CircuitBreaker';
import { DriverConfiguration, DRIVER_NAME } from './telemetry/types';
import driverVersion from './version';
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>();
// Telemetry components (instance-based, NOT singletons)
private host?: string;
private httpPath?: string;
private authType?: string;
private featureFlagCache?: FeatureFlagCache;
private telemetryClientProvider?: TelemetryClientProvider;
private telemetryEmitter?: TelemetryEventEmitter;
private telemetryAggregator?: MetricsAggregator;
private circuitBreakerRegistry?: CircuitBreakerRegistry;
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,
// Telemetry defaults
telemetryEnabled: true, // Enabled by default, gated by feature flag
telemetryBatchSize: 100,
telemetryFlushIntervalMs: 5000,
telemetryMaxRetries: 3,
telemetryAuthenticatedExport: true,
telemetryCircuitBreakerThreshold: 5,
telemetryCircuitBreakerTimeout: 60000, // 1 minute
};
}
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);
}
/**
* Extract workspace ID from hostname.
* @param host - The host string (e.g., "workspace-id.cloud.databricks.com")
* @returns Workspace ID or host if extraction fails
*/
private extractWorkspaceId(host: string): string {
// Extract workspace ID from hostname (first segment before first dot)
const parts = host.split('.');
return parts.length > 0 ? parts[0] : host;
}
/**
* Build driver configuration for telemetry reporting.
* @returns DriverConfiguration object with current driver settings
*/
private buildDriverConfiguration(): DriverConfiguration {
return {
driverVersion,
driverName: DRIVER_NAME,
nodeVersion: process.version,
platform: process.platform,
osVersion: os.release(),
osArch: os.arch(),
runtimeVendor: 'Node.js Foundation',
localeName: this.getLocaleName(),
charSetEncoding: 'UTF-8',
processName: this.getProcessName(),
authType: this.authType || 'pat',
// Feature flags
cloudFetchEnabled: this.config.useCloudFetch ?? false,
lz4Enabled: this.config.useLZ4Compression ?? false,
arrowEnabled: this.config.arrowEnabled ?? false,
directResultsEnabled: true, // Direct results always enabled
// Configuration values
socketTimeout: this.config.socketTimeout ?? 0,
retryMaxAttempts: this.config.retryMaxAttempts ?? 0,
cloudFetchConcurrentDownloads: this.config.cloudFetchConcurrentDownloads ?? 0,
// Connection parameters
httpPath: this.httpPath,
enableMetricViewMetadata: this.config.enableMetricViewMetadata,
};
}
/**
* Map Node.js auth type to telemetry auth enum string.
* Distinguishes between U2M and M2M OAuth flows.
*/
private mapAuthType(options: ConnectionOptions): string {
if (options.authType === 'databricks-oauth') {
// Check if M2M (has client secret) or U2M (no client secret)
return options.oauthClientSecret === undefined
? 'external-browser' // U2M OAuth (User-to-Machine)
: 'oauth-m2m'; // M2M OAuth (Machine-to-Machine)
}
if (options.authType === 'custom') {
return 'custom'; // Custom auth provider
}
// 'access-token' or undefined
return 'pat'; // Personal Access Token
}
/**
* Get locale name in format language_country (e.g., en_US).
* Matches JDBC format: user.language + '_' + user.country
*/
private getLocaleName(): string {
try {
// Try to get from environment variables
const lang = process.env.LANG || process.env.LC_ALL || process.env.LC_MESSAGES || '';
if (lang) {
// LANG format is typically "en_US.UTF-8", extract "en_US"
const match = lang.match(/^([a-z]{2}_[A-Z]{2})/);
if (match) {
return match[1];
}
}
// Fallback to en_US
return 'en_US';
} catch {
return 'en_US';
}
}
/**
* Get process name, similar to JDBC's ProcessNameUtil.
* Returns the script name or process title.
*/
private getProcessName(): string {
try {
// Try process.title first (can be set by application)
if (process.title && process.title !== 'node') {
return process.title;
}
// Try to get the main script name from argv[1]
if (process.argv && process.argv.length > 1) {
const scriptPath = process.argv[1];
// Extract filename without path
const filename = scriptPath.split('/').pop()?.split('\\').pop() || '';
// Remove extension
const nameWithoutExt = filename.replace(/\.[^.]*$/, '');
if (nameWithoutExt) {
return nameWithoutExt;
}
}
return 'node';
} catch {
return 'node';
}
}
/**
* Initialize telemetry components if enabled.
* CRITICAL: All errors swallowed and logged at LogLevel.debug ONLY.
* Driver NEVER throws exceptions due to telemetry.
*/
private async initializeTelemetry(): Promise<void> {
if (!this.host) {
return;
}
try {
// Create circuit breaker registry (shared by feature flags and telemetry)
this.circuitBreakerRegistry = new CircuitBreakerRegistry(this);
// Create feature flag cache instance with circuit breaker protection
this.featureFlagCache = new FeatureFlagCache(this, this.circuitBreakerRegistry);
this.featureFlagCache.getOrCreateContext(this.host);
// Check if telemetry enabled via feature flag
const enabled = await this.featureFlagCache.isTelemetryEnabled(this.host);
if (!enabled) {
this.logger.log(LogLevel.debug, 'Telemetry: disabled');
return;
}
// Create telemetry components (all instance-based)
this.telemetryClientProvider = new TelemetryClientProvider(this);
this.telemetryEmitter = new TelemetryEventEmitter(this);
// Get or create telemetry client for this host (increments refCount)
this.telemetryClientProvider.getOrCreateClient(this.host);
// Create telemetry exporter with shared circuit breaker registry
const exporter = new DatabricksTelemetryExporter(this, this.host, this.circuitBreakerRegistry);
this.telemetryAggregator = new MetricsAggregator(this, exporter);
// Wire up event listeners
this.telemetryEmitter.on('connection.open', (event) => {
try {
this.telemetryAggregator?.processEvent(event);
} catch (error: any) {
this.logger.log(LogLevel.debug, `Error processing connection.open event: ${error.message}`);
}
});
this.telemetryEmitter.on('connection.close', (event) => {
try {
this.telemetryAggregator?.processEvent(event);
} catch (error: any) {
this.logger.log(LogLevel.debug, `Error processing connection.close event: ${error.message}`);
}
});
this.telemetryEmitter.on('statement.start', (event) => {
try {
this.telemetryAggregator?.processEvent(event);
} catch (error: any) {
this.logger.log(LogLevel.debug, `Error processing statement.start event: ${error.message}`);
}
});
this.telemetryEmitter.on('statement.complete', (event) => {
try {
this.telemetryAggregator?.processEvent(event);
} catch (error: any) {
this.logger.log(LogLevel.debug, `Error processing statement.complete event: ${error.message}`);
}
});
this.telemetryEmitter.on('cloudfetch.chunk', (event) => {
try {
this.telemetryAggregator?.processEvent(event);
} catch (error: any) {
this.logger.log(LogLevel.debug, `Error processing cloudfetch.chunk event: ${error.message}`);
}
});
this.telemetryEmitter.on('error', (event) => {
try {
this.telemetryAggregator?.processEvent(event);
} catch (error: any) {
this.logger.log(LogLevel.debug, `Error processing error event: ${error.message}`);
}
});
this.logger.log(LogLevel.debug, 'Telemetry: enabled');
} catch (error: any) {
// Swallow all telemetry initialization errors
this.logger.log(LogLevel.debug, `Telemetry initialization error: ${error.message}`);
}
}
/**
* 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 connection params for telemetry
this.host = options.host;
this.httpPath = options.path;
this.authType = this.mapAuthType(options);
// Store enableMetricViewMetadata configuration
if (options.enableMetricViewMetadata !== undefined) {
this.config.enableMetricViewMetadata = options.enableMetricViewMetadata;
}
// Override telemetry config if provided in options
if (options.telemetryEnabled !== undefined) {
this.config.telemetryEnabled = options.telemetryEnabled;
}
if (options.telemetryBatchSize !== undefined) {
this.config.telemetryBatchSize = options.telemetryBatchSize;
}
if (options.telemetryAuthenticatedExport !== undefined) {
this.config.telemetryAuthenticatedExport = options.telemetryAuthenticatedExport;
}
// Persist userAgentEntry so telemetry and feature-flag call sites reuse
// the same value as the primary Thrift connection's User-Agent.
if (options.userAgentEntry !== undefined) {
this.config.userAgentEntry = options.userAgentEntry;
}
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');
});
// Initialize telemetry if enabled
if (this.config.telemetryEnabled) {
await this.initializeTelemetry();
}
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> {
// Track connection open latency
const startTime = Date.now();
// 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);
// Emit connection.open telemetry event
if (this.telemetryEmitter && this.host) {
try {
const latencyMs = Date.now() - startTime;
const workspaceId = this.extractWorkspaceId(this.host);
const driverConfig = this.buildDriverConfiguration();
this.telemetryEmitter.emitConnectionOpen({
sessionId: session.id,
workspaceId,
driverConfig,
latencyMs,
});
} catch (error: any) {
// CRITICAL: All telemetry exceptions swallowed
this.logger.log(LogLevel.debug, `Error emitting connection.open event: ${error.message}`);
}
}
return session;
}
public async close(): Promise<void> {
await this.sessions.closeAll();
// Cleanup telemetry
if (this.host) {
try {
// Step 1: Close aggregator (stops timer, completes statements, final flush)
if (this.telemetryAggregator) {
this.telemetryAggregator.close();
}
// Step 2: Release telemetry client (decrements ref count, closes if last)
if (this.telemetryClientProvider) {
await this.telemetryClientProvider.releaseClient(this.host);
}
// Step 3: Release feature flag context (decrements ref count)
if (this.featureFlagCache) {
this.featureFlagCache.releaseContext(this.host);
}
} catch (error: any) {
// Swallow all telemetry cleanup errors
this.logger.log(LogLevel.debug, `Telemetry cleanup error: ${error.message}`);
}
}
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;
}
/**
* Returns the authentication provider associated with this client, if any.
* Intended for internal telemetry/feature-flag call sites that need to
* obtain auth headers directly without routing through `IClientContext`.
*
* @internal Not part of the public API. May change without notice.
*/
public getAuthProvider(): IAuthentication | undefined {
return this.authProvider;
}
}