
    &`i"                         d dl Z d dlZd dlZd dlZddlmZ ddlmZ  e j        e	          Z
dZdZdZdZd	 Zd
 Zd Zd Zd ZdZdZdZ G d de          ZdS )    N   )RayOnSparkStartHook)get_spark_sessionDATABRICKS_HOSTDATABRICKS_TOKENDATABRICKS_CLIENT_IDDATABRICKS_CLIENT_SECRETc                      t           t          j        v rt          t          j        v p8t           t          j        v o%t          t          j        v ot
          t          j        v S N)r   osenvironr   r   r	        r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/spark/databricks_hook.pyverify_databricks_auth_envr      sK    rz)L.>"*.L 2:% 	3 BJ.	3$
2r   c                 z    dd l }|                                }|t          d          |j        d         |          S )Nr   zNo IPython environment.user_global)IPythonget_ipythonRuntimeErrorns_table)	func_namer   ip_shells      r   get_databricks_functionr      sD    NNN""$$H4555]+I66r   c                       t          d          S )NdisplayHTML)r   r   r   r   $get_databricks_display_html_functionr   "   s    "=111r   c                  ^    ddl m}  |                                 }|                                S )zo
    Return databricks entry_point instance, it is for calling some
    internal API in databricks runtime
    r   )UserNamespaceInitializer)	dbruntimer   getOrCreateget_spark_entry_point)r   user_namespace_initializers     r   get_db_entry_pointr$   &   s:    
 322222!9!E!E!G!G%;;===r   c                    | j         j        j        j        j        j        j        }|                                                                	                                
                    d          }|
                    d          }|
                    d          }d| d| d| d}d| d| }t          d           t          d	|             t                      d
| d| d           dS )aE  
    This helper function create a proxy URL for databricks driver webapp forwarding.
    In databricks runtime, user does not have permission to directly access web
    service binding on driver machine port, but user can visit it by a proxy URL with
    following format: "/driver-proxy/o/{orgId}/{clusterId}/{port}/".
    tagsorgId	clusterIdz/driver-proxy/o//zhttps://dbc-dp-z.cloud.databricks.comz@To monitor and debug Ray from Databricks, view the dashboard at  zM
      <div style="margin-top: 16px;margin-bottom: 16px">
          <a href="z">
              Open z. in a new tab
          </a>
      </div>
    N)_jvmcom
databricksbackenddaemondriverDriverLocalcommandContextgettoStringMapapplyprintr   )	spark_contextporttitledriverLocalcommandContextTagsr'   r(   
proxy_link	proxy_urls	            r   #display_databricks_driver_proxy_urlr>   1   s      $(3;BIUK$335599;;GGIIOOPVWW$$W--E"((55I?E??I?????JJ%JJjJJI	
LMMM	/i//*(**		 	 	 	 	    r      ,DATABRICKS_RAY_ON_SPARK_AUTOSHUTDOWN_MINUTESz/local_disk0/tmpc                   6     e Zd Zd Zd Zd Zd Z fdZ xZS )$DefaultDatabricksRayOnSparkStartHookc                     t           S r   ) _DATABRICKS_DEFAULT_TMP_ROOT_DIR)selfs    r   get_default_temp_root_dirz>DefaultDatabricksRayOnSparkStartHook.get_default_temp_root_dirX   s    //r   c                 J    t          t                      j        |d           d S )NzRay Cluster Dashboard)r>   r   sparkContext)rE   r8   s     r   on_ray_dashboard_createdz=DefaultDatabricksRayOnSparkStartHook.on_ray_dashboard_created[   s/    +,d4K	
 	
 	
 	
 	
r   c                 L   t                      | j        rdn2t          t          j                            t          d                    dk    rt                              d           d S dk     rt          dt           d          	 
                                 n+# t          $ r t                              d           Y d S w xY wt                              d dt           d	           fd
}t          j        |d                                           d S )Nr   30zThe Ray cluster will keep running until you manually detach the Databricks notebook or call `ray.util.spark.shutdown_ray_cluster()`.zYou must set 'z' to a value >= 0.a4  Failed to retrieve idle time since last notebook execution, so that we cannot automatically shut down Ray cluster when Databricks notebook is inactive for the specified minutes. You need to manually detach Databricks notebook or call `ray.util.spark.shutdown_ray_cluster()` to shut down Ray cluster on spark.ziThe Ray cluster will be shut down automatically if you don't run commands on the Databricks notebook for z? minutes. You can change the auto-shutdown minutes by setting 'z' environment variable, setting it to 0 means that the Ray cluster keeps running until you manually call `ray.util.spark.shutdown_ray_cluster()` or detach Databricks notebook.c                     dz  dz  } 	 j         rd S                                 }|| k    rDddlm} |j        5  |j        u r|                                 d d d            n# 1 swxY w Y   d S t          j        t                     )N<   i  Tr   )cluster_init)
is_shutdown+getIdleTimeMillisSinceLastNotebookExecutionray.util.sparkrN   _active_ray_cluster_rwlock_active_ray_clustershutdown_ray_clustertimesleep.DATABRICKS_AUTO_SHUTDOWN_POLL_INTERVAL_SECONDS)auto_shutdown_millis	idle_timerN   auto_shutdown_minutesdb_api_entryray_cluster_handlers      r   auto_shutdown_watcherzVDefaultDatabricksRayOnSparkStartHook.on_cluster_created.<locals>.auto_shutdown_watcher   s   #82#=#D K&2 F(TTVV	333;;;;;;%@ @ @.,2RRR(==???@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ F
IJJJKs   A%%A),A)T)targetr/   )r$   	is_globalfloatr   r   r3   r@   _loggerinfo
ValueErrorrP   	Exceptionwarning	threadingThreadstart)rE   r\   r]   rZ   r[   s    ` @@r   on_cluster_createdz7DefaultDatabricksRayOnSparkStartHook.on_cluster_created`   s   )++> 	 %&!!$)
KTRR% %! !A%%LL;  
 F 1$$#@# # #  	DDFFFF 		 		 		OO(   FF		 	*$* * =	* * *		
 		
 		
	K 	K 	K 	K 	K 	K 	K& 	 5dCCCIIKKKKKs   B$ $$CCc                 L    t                      }|                    |           d S r   )r$   registerBackgroundSparkJobGroup)rE   job_group_idr[   s      r   on_spark_job_createdz9DefaultDatabricksRayOnSparkStartHook.on_spark_job_created   s'    )++44\BBBBBr   c                    i t                                                      ddd}t                      rt          j        t
                   |t
          <   t          t          j        v r t          j        t                   |t          <   n\t          j        t                   |t          <   t          j        t                   |t          <   nd} t                      d| d           |S )Neth0TRUE)GLOO_SOCKET_IFNAMEDISABLE_MLFLOW_INTEGRATIONa~  MLflow support is not correctly configured within Ray tasks.To enable MLflow integration, you need to set environmental variables DATABRICKS_HOST + DATABRICKS_TOKEN, or set environmental variables DATABRICKS_HOST + DATABRICKS_CLIENT_ID + DATABRICKS_CLIENT_SECRET before calling `ray.util.spark.setup_ray_cluster`, these variables are used to set up authentication with Databricks MLflow service. For details, you can refer to Databricks documentation at <a href='https://docs.databricks.com/en/dev-tools/auth/pat.html'>Databricks PAT auth</a> or <a href='https://docs.databricks.com/en/dev-tools/auth/oauth-m2m.html'>Databricks OAuth</a>.z<b style='color:red;'>z<br></b>)
supercustom_environment_variablesr   r   r   r   r   r   r	   r   )rE   confwarn_msg	__class__s      r   rt   zADefaultDatabricksRayOnSparkStartHook.custom_environment_variables   s    
gg2244
 #)
 +1
 
 
 &'' 	$&J$?D!2:--)+4D)E%&& .0Z8L-M)*13<T1U-..8  3022;;;;   r   )	__name__
__module____qualname__rF   rI   ri   rm   rt   __classcell__)rw   s   @r   rB   rB   W   s~        0 0 0
 
 

LL LL LL\C C C+ + + + + + + + +r   rB   )loggingr   rf   rU   start_hook_baser   utilsr   	getLoggerrx   ra   r   r   r   r	   r   r   r   r$   r>   rW   r@   rD   rB   r   r   r   <module>r      s    				      0 0 0 0 0 0 $ $ $ $ $ $
'
H
%
%#% - 5   7 7 72 2 2> > >  : 23 .2 -
 $6  F F F F F+> F F F F Fr   