-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathDBSQLClient.ts
231 lines (193 loc) · 7.4 KB
/
DBSQLClient.ts
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
import thrift from 'thrift';
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 from './contracts/IClientContext';
import HiveDriver from './hive/HiveDriver';
import { Int64 } from './hive/Types';
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 from './connection/auth/DatabricksOAuth';
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 default class DBSQLClient extends EventEmitter implements IDBSQLClient, IClientContext {
private static defaultLogger?: IDBSQLLogger;
private authProvider?: IAuthentication;
private client?: TCLIService.Client;
private readonly driver = new HiveDriver({
context: this,
});
private readonly logger: IDBSQLLogger;
private connectionProvider?: IConnectionProvider;
private ConnectionProviderConstructor: new(o: IConnectionOptions) => IConnectionProvider;
private readonly thrift = thrift;
private sessions = new CloseableCollection<DBSQLSession>();
private static getDefaultLogger(): IDBSQLLogger {
if (!this.defaultLogger) {
this.defaultLogger = new DBSQLLogger();
}
return this.defaultLogger;
}
constructor(options?: ClientOptions) {
super();
this.logger = options?.logger ?? DBSQLClient.getDefaultLogger();
this.logger.log(LogLevel.info, 'Created DBSQLClient');
this.ConnectionProviderConstructor = options?.connectionProvider || HttpConnection;
}
private getConnectionOptions(options: ConnectionOptions): IConnectionOptions {
return {
...options,
port: options.port || 443,
path: prependSlash(options.path || ''),
https: true,
headers: {
'User-Agent': buildUserAgentString(options.clientId),
...(options.headers || {})
},
};
}
private initAuthProvider(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({
host: options.host,
persistence: options.persistence,
azureTenantId: options.azureTenantId,
clientId: options.oauthClientId,
clientSecret: options.oauthClientSecret,
context: this,
});
case 'custom':
return options.provider;
// no default
}
}
/**
* 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> {
this.authProvider = this.initAuthProvider(options, authProvider);
this.connectionProvider = new this.ConnectionProviderConstructor(this.getConnectionOptions(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> {
const response = await this.driver.openSession({
client_protocol_i64: new Int64(TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8),
...getInitialNamespaceOptions(request.initialCatalog, request.initialSchema),
});
Status.assert(response.status);
const session = new DBSQLSession({
handle: definedOrError(response.sessionHandle),
context: this,
});
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 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<TCLIService.Client> {
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;
}
}