1212from databricks .labs .blueprint .paths import WorkspacePath
1313from databricks .sdk import WorkspaceClient
1414from databricks .sdk .errors import InvalidParameterValue , NotFound
15- from databricks .sdk .service .jobs import JobParameterDefinition , JobSettings , NotebookTask , Source , Task
15+ from databricks .sdk .service import compute
16+ from databricks .sdk .service .jobs import JobCluster , JobParameterDefinition , JobSettings , NotebookTask , Source , Task
1617
1718
1819logger = logging .getLogger (__name__ )
@@ -32,12 +33,12 @@ def __init__(
3233 self ._installation = installation
3334 self ._install_state = install_state
3435
35- def install (self ) -> None :
36+ def install (self , use_serverless : bool = True ) -> None :
3637 """Deploy Switch to workspace and configure resources."""
3738 logger .debug ("Deploying Switch resources to workspace..." )
3839 try :
3940 self ._deploy_resources_to_workspace ()
40- self ._setup_job ()
41+ self ._setup_job (use_serverless )
4142 logger .debug ("Switch deployment completed" )
4243 except (RuntimeError , ValueError , InvalidParameterValue ) as e :
4344 msg = f"Failed to setup required resources for Switch llm transpiler: { e } "
@@ -108,11 +109,11 @@ def _enumerate_resources(
108109
109110 yield from _enumerate_resources (root )
110111
111- def _setup_job (self ) -> None :
112+ def _setup_job (self , use_serverless : bool = True ) -> None :
112113 """Create or update Switch job."""
113114 existing_job_id = self ._get_existing_job_id ()
114115 logger .debug ("Setting up Switch job in workspace..." )
115- job_id = self ._create_or_update_switch_job (existing_job_id )
116+ job_id = self ._create_or_update_switch_job (existing_job_id , use_serverless )
116117 self ._install_state .jobs [self ._INSTALL_STATE_KEY ] = job_id
117118 self ._install_state .save ()
118119 job_url = f"{ self ._ws .config .host } /jobs/{ job_id } "
@@ -129,9 +130,9 @@ def _get_existing_job_id(self) -> str | None:
129130 except (InvalidParameterValue , NotFound , ValueError ):
130131 return None
131132
132- def _create_or_update_switch_job (self , job_id : str | None ) -> str :
133+ def _create_or_update_switch_job (self , job_id : str | None , use_serverless : bool = True ) -> str :
133134 """Create or update Switch job, returning job ID."""
134- job_settings = self ._get_switch_job_settings ()
135+ job_settings = self ._get_switch_job_settings (use_serverless )
135136
136137 # Try to update existing job
137138 if job_id :
@@ -149,7 +150,7 @@ def _create_or_update_switch_job(self, job_id: str | None) -> str:
149150 assert new_job_id is not None
150151 return new_job_id
151152
152- def _get_switch_job_settings (self ) -> dict [str , Any ]:
153+ def _get_switch_job_settings (self , use_serverless : bool = True ) -> dict [str , Any ]:
153154 """Build job settings for Switch transpiler."""
154155 job_name = "Lakebridge_Switch"
155156 notebook_path = self ._get_switch_workspace_path () / "notebooks" / "00_main"
@@ -160,14 +161,31 @@ def _get_switch_job_settings(self) -> dict[str, Any]:
160161 disable_auto_optimization = True , # To disable retries on failure
161162 )
162163
163- return {
164+ settings : dict [ str , Any ] = {
164165 "name" : job_name ,
165166 "tags" : {"created_by" : self ._ws .current_user .me ().user_name , "switch_version" : f"v{ switch_version } " },
166167 "tasks" : [task ],
167168 "parameters" : self ._generate_switch_job_parameters (),
168169 "max_concurrent_runs" : 100 , # Allow simultaneous transpilations
169170 }
170171
172+ if not use_serverless :
173+ job_cluster_key = "Switch_Cluster"
174+ settings ["job_clusters" ] = [
175+ JobCluster (
176+ job_cluster_key = job_cluster_key ,
177+ new_cluster = compute .ClusterSpec (
178+ spark_version = self ._ws .clusters .select_spark_version (latest = True , long_term_support = True ),
179+ node_type_id = self ._ws .clusters .select_node_type (local_disk = True , min_memory_gb = 16 ),
180+ num_workers = 1 ,
181+ data_security_mode = compute .DataSecurityMode .USER_ISOLATION ,
182+ ),
183+ )
184+ ]
185+ task .job_cluster_key = job_cluster_key
186+
187+ return settings
188+
171189 @staticmethod
172190 def _generate_switch_job_parameters () -> Sequence [JobParameterDefinition ]:
173191 # Add required runtime parameters, static for now.
0 commit comments