|
| 1 | +from abc import ABC |
| 2 | +from typing import Optional |
| 3 | +from typing import Tuple |
| 4 | + |
| 5 | +from databricks.sql.client import Connection |
| 6 | +from dbt.adapters.databricks.events.base import SQLErrorEvent |
| 7 | +from dbt.contracts.graph.nodes import ResultNode |
| 8 | + |
| 9 | + |
| 10 | +class ConnectionEvent(ABC): |
| 11 | + def __init__(self, connection: Connection, message: str): |
| 12 | + self.message = message |
| 13 | + self.session_id = "Unknown" |
| 14 | + if connection: |
| 15 | + self.session_id = connection.get_session_id_hex() or "Unknown" |
| 16 | + |
| 17 | + def __str__(self) -> str: |
| 18 | + return f"Connection(session-id={self.session_id}) - {self.message}" |
| 19 | + |
| 20 | + |
| 21 | +class ConnectionCancel(ConnectionEvent): |
| 22 | + def __init__(self, connection: Connection): |
| 23 | + super().__init__(connection, "Cancelling connection") |
| 24 | + |
| 25 | + |
| 26 | +class ConnectionClose(ConnectionEvent): |
| 27 | + def __init__(self, connection: Connection): |
| 28 | + super().__init__(connection, "Closing connection") |
| 29 | + |
| 30 | + |
| 31 | +class ConnectionCancelError(ConnectionEvent): |
| 32 | + def __init__(self, connection: Connection, exception: Exception): |
| 33 | + super().__init__( |
| 34 | + connection, str(SQLErrorEvent(exception, "Exception while trying to cancel connection")) |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +class ConnectionCloseError(ConnectionEvent): |
| 39 | + def __init__(self, connection: Connection, exception: Exception): |
| 40 | + super().__init__( |
| 41 | + connection, str(SQLErrorEvent(exception, "Exception while trying to close connection")) |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +class ConnectionCreateError(ConnectionEvent): |
| 46 | + def __init__(self, connection: Connection, exception: Exception): |
| 47 | + super().__init__( |
| 48 | + connection, str(SQLErrorEvent(exception, "Exception while trying to create connection")) |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class ConnectionWrapperEvent(ABC): |
| 53 | + def __init__(self, description: str, message: str): |
| 54 | + self.message = message |
| 55 | + self.description = description |
| 56 | + |
| 57 | + def __str__(self) -> str: |
| 58 | + return f"{self.description} - {self.message}" |
| 59 | + |
| 60 | + |
| 61 | +class ConnectionAcquire(ConnectionWrapperEvent): |
| 62 | + def __init__( |
| 63 | + self, |
| 64 | + description: str, |
| 65 | + model: Optional[ResultNode], |
| 66 | + compute_name: Optional[str], |
| 67 | + thread_identifier: Tuple[int, int], |
| 68 | + ): |
| 69 | + message = f"Acquired connection on thread {thread_identifier}, using " |
| 70 | + if not compute_name: |
| 71 | + message += "default compute resource" |
| 72 | + else: |
| 73 | + message += f"compute resource '{compute_name}'" |
| 74 | + |
| 75 | + if model: |
| 76 | + # ResultNode *should* have relation_name attr, but we work around a core |
| 77 | + # issue by checking. |
| 78 | + relation_name = getattr(model, "relation_name", "[Unknown]") |
| 79 | + message += f" for model '{relation_name}'" |
| 80 | + |
| 81 | + super().__init__(description, message) |
| 82 | + |
| 83 | + |
| 84 | +class ConnectionRelease(ConnectionWrapperEvent): |
| 85 | + def __init__(self, description: str): |
| 86 | + super().__init__(description, "Released connection") |
| 87 | + |
| 88 | + |
| 89 | +class ConnectionReset(ConnectionWrapperEvent): |
| 90 | + def __init__(self, description: str): |
| 91 | + super().__init__(description, "Reset connection handle") |
| 92 | + |
| 93 | + |
| 94 | +class ConnectionReuse(ConnectionWrapperEvent): |
| 95 | + def __init__(self, description: str, prior_name: str): |
| 96 | + super().__init__(description, f"Reusing connection previously named {prior_name}") |
| 97 | + |
| 98 | + |
| 99 | +class ConnectionCreate(ConnectionWrapperEvent): |
| 100 | + def __init__(self, description: str): |
| 101 | + super().__init__(description, "Creating connection") |
| 102 | + |
| 103 | + |
| 104 | +class ConnectionIdleCheck(ConnectionWrapperEvent): |
| 105 | + def __init__(self, description: str): |
| 106 | + super().__init__(description, "Checking idleness") |
| 107 | + |
| 108 | + |
| 109 | +class ConnectionIdleClose(ConnectionWrapperEvent): |
| 110 | + def __init__(self, description: str): |
| 111 | + super().__init__(description, "Closing for idleness") |
| 112 | + |
| 113 | + |
| 114 | +class ConnectionRetrieve(ConnectionWrapperEvent): |
| 115 | + def __init__(self, description: str): |
| 116 | + super().__init__(description, "Retrieving connection") |
| 117 | + |
| 118 | + |
| 119 | +class ConnectionCreated(ConnectionWrapperEvent): |
| 120 | + def __init__(self, description: str): |
| 121 | + super().__init__(description, "Connection created") |
0 commit comments