99from urllib3 .util import make_headers
1010from urllib3 .exceptions import MaxRetryError
1111
12- from databricks .sql .auth .retry import DatabricksRetryPolicy
12+ from databricks .sql .auth .retry import DatabricksRetryPolicy , CommandType
1313from databricks .sql .exc import RequestError
1414
1515logger = logging .getLogger (__name__ )
@@ -33,6 +33,7 @@ def __init__(self, client_context):
3333 """
3434 self .config = client_context
3535 self ._pool_manager = None
36+ self ._retry_policy = None
3637 self ._setup_pool_manager ()
3738
3839 def _setup_pool_manager (self ):
@@ -69,20 +70,25 @@ def _setup_pool_manager(self):
6970 )
7071
7172 # Create retry policy
72- retry_policy = DatabricksRetryPolicy (
73+ self . _retry_policy = DatabricksRetryPolicy (
7374 delay_min = self .config .retry_delay_min ,
7475 delay_max = self .config .retry_delay_max ,
7576 stop_after_attempts_count = self .config .retry_stop_after_attempts_count ,
7677 stop_after_attempts_duration = self .config .retry_stop_after_attempts_duration ,
7778 delay_default = self .config .retry_delay_default ,
7879 force_dangerous_codes = self .config .retry_dangerous_codes ,
7980 )
81+
82+ # Initialize the required attributes that DatabricksRetryPolicy expects
83+ # but doesn't initialize in its constructor
84+ self ._retry_policy ._command_type = None
85+ self ._retry_policy ._retry_start_time = None
8086
8187 # Common pool manager kwargs
8288 pool_kwargs = {
8389 "num_pools" : self .config .pool_connections ,
8490 "maxsize" : self .config .pool_maxsize ,
85- "retries" : retry_policy ,
91+ "retries" : self . _retry_policy ,
8692 "timeout" : urllib3 .Timeout (
8793 connect = self .config .socket_timeout , read = self .config .socket_timeout
8894 )
@@ -119,6 +125,14 @@ def _prepare_headers(
119125
120126 return request_headers
121127
128+ def _prepare_retry_policy (self ):
129+ """Set up the retry policy for the current request."""
130+ if isinstance (self ._retry_policy , DatabricksRetryPolicy ):
131+ # Set command type for HTTP requests to OTHER (not database commands)
132+ self ._retry_policy .command_type = CommandType .OTHER
133+ # Start the retry timer for duration-based retry limits
134+ self ._retry_policy .start_retry_timer ()
135+
122136 @contextmanager
123137 def request_context (
124138 self , method : str , url : str , headers : Optional [Dict [str , str ]] = None , ** kwargs
@@ -138,6 +152,10 @@ def request_context(
138152 logger .debug ("Making %s request to %s" , method , url )
139153
140154 request_headers = self ._prepare_headers (headers )
155+
156+ # Prepare retry policy for this request
157+ self ._prepare_retry_policy ()
158+
141159 response = None
142160
143161 try :
0 commit comments