-
Notifications
You must be signed in to change notification settings - Fork 4
/
livysession.py
542 lines (447 loc) · 17.3 KB
/
livysession.py
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
from __future__ import annotations
import json
import time
import requests
from requests.models import Response
from urllib import response
import re
import datetime as dt
from types import TracebackType
from typing import Any
import dbt.exceptions
from dbt.events import AdapterLogger
from dbt.utils import DECIMALS
from azure.core.credentials import AccessToken
from azure.identity import AzureCliCredential, ClientSecretCredential
from dbt.adapters.fabricspark.fabric_spark_credentials import SparkCredentials
logger = AdapterLogger("Microsoft Fabric-Spark")
NUMBERS = DECIMALS + (int, float)
livysession_credentials: SparkCredentials
DEFAULT_POLL_WAIT = 45
DEFAULT_POLL_STATEMENT_WAIT = 5
AZURE_CREDENTIAL_SCOPE = "https://analysis.windows.net/powerbi/api/.default"
accessToken: AccessToken = None
def is_token_refresh_necessary(unixTimestamp: int) -> bool:
# Convert to datetime object
dt_object = dt.datetime.fromtimestamp(unixTimestamp)
# Convert to local time
local_time = time.localtime(time.time())
# Calculate difference
difference = dt_object - dt.datetime.fromtimestamp(time.mktime(local_time))
if int(difference.total_seconds() / 60) < 5:
logger.debug(f"Token Refresh necessary in {int(difference.total_seconds() / 60)}")
return True
else:
return False
def get_cli_access_token(credentials: SparkCredentials) -> AccessToken:
"""
Get an Azure access token using the CLI credentials
First login with:
```bash
az login
```
Parameters
----------
credentials: FabricConnectionManager
The credentials.
Returns
-------
out : AccessToken
Access token.
"""
_ = credentials
accessToken = AzureCliCredential().get_token(AZURE_CREDENTIAL_SCOPE)
logger.debug("CLI - Fetched Access Token")
return accessToken
def get_sp_access_token(credentials: SparkCredentials) -> AccessToken:
"""
Get an Azure access token using the SP credentials.
Parameters
----------
credentials : FabricCredentials
Credentials.
Returns
-------
out : AccessToken
The access token.
"""
accessToken = ClientSecretCredential(
str(credentials.tenant_id), str(credentials.client_id), str(credentials.client_secret)
).get_token(AZURE_CREDENTIAL_SCOPE)
logger.info("SPN - Fetched Access Token")
return accessToken
def get_headers(credentials: SparkCredentials, tokenPrint: bool = False) -> dict[str, str]:
global accessToken
if accessToken is None or is_token_refresh_necessary(accessToken.expires_on):
if credentials.authentication and credentials.authentication.lower() == "cli":
logger.debug("Using CLI auth")
accessToken = get_cli_access_token(credentials)
else:
logger.debug("Using SPN auth")
accessToken = get_sp_access_token(credentials)
headers = {"Content-Type": "application/json", "Authorization": f"Bearer {accessToken.token}"}
if tokenPrint:
logger.debug(accessToken.token)
return headers
class LivySession:
def __init__(self, credentials: SparkCredentials):
self.credential = credentials
self.connect_url = credentials.lakehouse_endpoint
self.session_id = None
self.is_new_session_required = True
def __enter__(self) -> LivySession:
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: Exception | None,
exc_tb: TracebackType | None,
) -> bool:
# self.delete_session()
return True
def create_session(self, data) -> str:
# Create sessions
response = None
try:
response = requests.post(
self.connect_url + "/sessions",
data=json.dumps(data),
headers=get_headers(self.credential, True),
)
if response.status_code == 200:
logger.debug("Initiated Livy Session...")
response.raise_for_status()
except requests.exceptions.ConnectionError as c_err:
print("Connection Error :", c_err)
except requests.exceptions.HTTPError as h_err:
print("Http Error: ", h_err)
except requests.exceptions.Timeout as t_err:
print("Timeout Error: ", t_err)
except requests.exceptions.RequestException as a_err:
print("Authorization Error: ", a_err)
if response is None:
raise Exception("Invalid response from livy server")
self.session_id = None
try:
self.session_id = str(response.json()["id"])
except requests.exceptions.JSONDecodeError as json_err:
raise Exception("Json decode error to get session_id") from json_err
# Wait for started state
while True:
res = requests.get(
self.connect_url + "/sessions/" + self.session_id,
headers=get_headers(self.credential, False),
).json()
if res["state"] == "starting" or res["state"] == "not_started":
# logger.debug("Polling Session creation status - ", self.connect_url + '/sessions/' + self.session_id )
time.sleep(DEFAULT_POLL_WAIT)
elif res["livyInfo"]["currentState"] == "idle":
logger.debug(f"New livy session id is: {self.session_id}, {res}")
self.is_new_session_required = False
break
elif res["livyInfo"]["currentState"] == "dead":
print("ERROR, cannot create a livy session")
raise dbt.exceptions.FailedToConnectException("failed to connect")
return
return self.session_id
def delete_session(self) -> None:
logger.debug(f"Closing the livy session: {self.session_id}")
try:
# delete the session_id
_ = requests.delete(
self.connect_url + "/sessions/" + self.session_id,
headers=get_headers(self.credential, False),
)
if _.status_code == 200:
logger.debug(f"Closed the livy session: {self.session_id}")
else:
response.raise_for_status()
except Exception as ex:
logger.error(f"Unable to close the livy session {self.session_id}, error: {ex}")
def is_valid_session(self) -> bool:
res = requests.get(
self.connect_url + "/sessions/" + self.session_id,
headers=get_headers(self.credential, False),
).json()
return res["livyInfo"]["currentState"] == "idle"
# cursor object - wrapped for livy API
class LivyCursor:
"""
Mock a pyodbc cursor.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor
"""
def __init__(self, credential, livy_session) -> None:
self._rows = None
self._schema = None
self.credential = credential
self.connect_url = credential.lakehouse_endpoint
self.session_id = livy_session.session_id
self.livy_session = livy_session
def __enter__(self) -> LivyCursor:
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: Exception | None,
exc_tb: TracebackType | None,
) -> bool:
self.close()
return True
@property
def description(
self,
) -> list[tuple[str, str, None, None, None, None, bool]]:
"""
Get the description.
Returns
-------
out : list[tuple[str, str, None, None, None, None, bool]]
The description.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#description
"""
if self._schema is None:
description = list()
else:
description = [
(
field["name"],
field["type"], # field['dataType'],
None,
None,
None,
None,
field["nullable"],
)
for field in self._schema
]
return description
def close(self) -> None:
"""
Close the connection.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#close
"""
self._rows = None
def _submitLivyCode(self, code) -> Response:
if self.livy_session.is_new_session_required:
LivySessionManager.connect(self.credential)
self.session_id = self.livy_session.session_id
# Submit code
data = {"code": code, "kind": "sql"}
logger.debug(
f"Submitted: {data} {self.connect_url + '/sessions/' + self.session_id + '/statements'}"
)
res = requests.post(
self.connect_url + "/sessions/" + self.session_id + "/statements",
data=json.dumps(data),
headers=get_headers(self.credential, False),
)
return res
def _getLivySQL(self, sql) -> str:
# Comment, what is going on?!
# The following code is actually injecting SQL to pyspark object for executing it via the Livy session - over an HTTP post request.
# Basically, it is like code inside a code. As a result the strings passed here in 'escapedSQL' variable are unescapted and interpreted on the server side.
# This may have repurcursions of code injection not only as SQL, but also arbritary Python code. An alternate way safer way to acheive this is still unknown.
# escapedSQL = sql.replace("\n", "\\n").replace('"', '\\\"')
# code = "val sprk_sql = spark.sql(\"" + escapedSQL + "\")\nval sprk_res=sprk_sql.collect\n%json sprk_res" # .format(escapedSQL)
# TODO: since the above code is not changed to sending direct SQL to the livy backend, client side string escaping is probably not needed
code = re.sub(r"\s*/\*(.|\n)*?\*/\s*", "\n", sql, re.DOTALL).strip()
return code
def _getLivyResult(self, res_obj) -> Response:
json_res = res_obj.json()
while True:
res = requests.get(
self.connect_url
+ "/sessions/"
+ self.session_id
+ "/statements/"
+ repr(json_res["id"]),
headers=get_headers(self.credential, False),
).json()
# print(res)
if res["state"] == "available":
return res
time.sleep(DEFAULT_POLL_STATEMENT_WAIT)
def execute(self, sql: str, *parameters: Any) -> None:
"""
Execute a sql statement.
Parameters
----------
sql : str
Execute a sql statement.
*parameters : Any
The parameters.
Raises
------
NotImplementedError
If there are parameters given. We do not format sql statements.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#executesql-parameters
"""
if len(parameters) > 0:
sql = sql % parameters
# TODO: handle parameterised sql
res = self._getLivyResult(self._submitLivyCode(self._getLivySQL(sql)))
logger.debug(res)
if res["output"]["status"] == "ok":
# values = res['output']['data']['application/json']
values = res["output"]["data"]["application/json"]
if len(values) >= 1:
self._rows = values["data"] # values[0]['values']
self._schema = values["schema"]["fields"] # values[0]['schema']
# print("rows", self._rows)
# print("schema", self._schema)
else:
self._rows = []
self._schema = []
else:
self._rows = None
self._schema = None
raise dbt.exceptions.DbtDatabaseError(
"Error while executing query: " + res["output"]["evalue"]
)
def fetchall(self):
"""
Fetch all data.
Returns
-------
out : list() | None
The rows.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#fetchall
"""
return self._rows
def fetchone(self):
"""
Fetch the first output.
Returns
-------
out : one row | None
The first row.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#fetchone
"""
if self._rows is not None and len(self._rows) > 0:
row = self._rows.pop(0)
else:
row = None
return row
class LivyConnection:
"""
Mock a pyodbc connection.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Connection
"""
def __init__(self, credentials, livy_session) -> None:
self.credential: SparkCredentials = credentials
self.connect_url = credentials.lakehouse_endpoint
self.session_id = livy_session.session_id
self.livy_session_parameters = credentials.livy_session_parameters
self._cursor = LivyCursor(self.credential, livy_session)
def get_session_id(self) -> str:
return self.session_id
def get_headers(self) -> dict[str, str]:
return get_headers(self.credential, False)
def get_connect_url(self) -> str:
return self.connect_url
def cursor(self) -> LivyCursor:
"""
Get a cursor.
Returns
-------
out : Cursor
The cursor.
"""
return self._cursor
def close(self) -> None:
"""
Close the connection.
Source
------
https://github.com/mkleehammer/pyodbc/wiki/Cursor#close
"""
logger.debug("Connection.close()")
self._cursor.close()
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: Exception | None,
exc_tb: TracebackType | None,
) -> bool:
self.close()
return True
# TODO: How to authenticate
class LivySessionManager:
livy_global_session = None
@staticmethod
def connect(credentials: SparkCredentials) -> LivyConnection:
# the following opens an spark / sql session
data = {"kind": "sql", "conf": credentials.livy_session_parameters} # 'spark'
if __class__.livy_global_session is None:
__class__.livy_global_session = LivySession(credentials)
__class__.livy_global_session.create_session(data)
__class__.livy_global_session.is_new_session_required = False
elif not __class__.livy_global_session.is_valid_session():
__class__.livy_global_session.delete_session()
__class__.livy_global_session.create_session(data)
__class__.livy_global_session.is_new_session_required = False
elif __class__.livy_global_session.is_new_session_required:
__class__.livy_global_session.create_session(data)
__class__.livy_global_session.is_new_session_required = False
else:
logger.debug(f"Reusing session: {__class__.livy_global_session.session_id}")
livyConnection = LivyConnection(credentials, __class__.livy_global_session)
return livyConnection
@staticmethod
def disconnect() -> None:
if __class__.livy_global_session.is_valid_session():
__class__.livy_global_session.delete_session()
__class__.livy_global_session.is_new_session_required = True
class LivySessionConnectionWrapper(object):
"""Connection wrapper for the livy sessoin connection method."""
def __init__(self, handle):
self.handle = handle
self._cursor = None
def cursor(self) -> LivySessionConnectionWrapper:
self._cursor = self.handle.cursor()
return self
def cancel(self) -> None:
logger.debug("NotImplemented: cancel")
def close(self) -> None:
self.handle.close()
def rollback(self, *args, **kwargs):
logger.debug("NotImplemented: rollback")
def fetchall(self):
return self._cursor.fetchall()
def execute(self, sql, bindings=None):
if sql.strip().endswith(";"):
sql = sql.strip()[:-1]
if bindings is None:
self._cursor.execute(sql)
else:
bindings = [self._fix_binding(binding) for binding in bindings]
self._cursor.execute(sql, *bindings)
@property
def description(self):
return self._cursor.description
@classmethod
def _fix_binding(cls, value):
"""Convert complex datatypes to primitives that can be loaded by
the Spark driver"""
if isinstance(value, NUMBERS):
return float(value)
elif isinstance(value, dt.datetime):
return f"'{value.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]}'"
elif value is None:
return "''"
else:
return f"'{value}'"