Milan Bariya fadefff3e1
Fix databricks timeout issue (#10613)
* Fix databricks timeout issue

* Change based on comments

* Change timeOut word to connectionTimeout
2023-03-16 16:13:49 +01:00

97 lines
2.9 KiB
Python

# Copyright 2021 Collate
# Licensed 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.
"""
Source connection handler
"""
from functools import partial
from sqlalchemy.engine import Engine
from sqlalchemy.inspection import inspect
from metadata.generated.schema.entity.services.connections.database.databricksConnection import (
DatabricksConnection,
)
from metadata.ingestion.connections.builders import (
create_generic_db_connection,
get_connection_args_common,
init_empty_connection_arguments,
)
from metadata.ingestion.connections.test_connections import (
TestConnectionResult,
TestConnectionStep,
test_connection_db_common,
)
def get_connection_url(connection: DatabricksConnection) -> str:
url = f"{connection.scheme.value}://token:{connection.token.get_secret_value()}@{connection.hostPort}"
return url
def get_connection(connection: DatabricksConnection) -> Engine:
"""
Create connection
"""
if connection.httpPath:
if not connection.connectionArguments:
connection.connectionArguments = init_empty_connection_arguments()
connection.connectionArguments.__root__["http_path"] = connection.httpPath
return create_generic_db_connection(
connection=connection,
get_connection_url_fn=get_connection_url,
get_connection_args_fn=get_connection_args_common,
)
def test_connection(engine: Engine, service_connection) -> TestConnectionResult:
"""
Test connection
"""
def custom_executor(engine, statement):
cursor = engine.execute(statement)
return [item[0] for item in list(cursor.all())]
inspector = inspect(engine)
steps = [
TestConnectionStep(
function=partial(
custom_executor,
statement="SHOW CATALOGS",
engine=engine,
),
name="Get Catalogs",
),
TestConnectionStep(
function=partial(
custom_executor,
statement="SHOW SCHEMAS",
engine=engine,
),
name="Get Schemas",
),
TestConnectionStep(
function=inspector.get_table_names,
name="Get Tables",
),
TestConnectionStep(
function=inspector.get_view_names,
name="Get Views",
mandatory=False,
),
]
timeout_seconds = service_connection.connectionTimeout
return test_connection_db_common(engine, steps, timeout_seconds)