@@ -342,7 +342,12 @@ def generate_cloud_function_code(self, def_, dir, package_requirements=None):
342342 return entry_point
343343
344344 def create_cloud_function (
345- self , def_ , cf_name , package_requirements = None , cloud_function_timeout = 600
345+ self ,
346+ def_ ,
347+ cf_name ,
348+ package_requirements = None ,
349+ timeout_seconds = 600 ,
350+ max_instance_count = None ,
346351 ):
347352 """Create a cloud function from the given user defined function."""
348353
@@ -411,14 +416,16 @@ def create_cloud_function(
411416 )
412417 function .service_config = functions_v2 .ServiceConfig ()
413418 function .service_config .available_memory = "1024M"
414- if cloud_function_timeout is not None :
415- if cloud_function_timeout > 1200 :
419+ if timeout_seconds is not None :
420+ if timeout_seconds > 1200 :
416421 raise ValueError (
417422 "BigQuery remote function can wait only up to 20 minutes"
418423 ", see for more details "
419424 "https://cloud.google.com/bigquery/quotas#remote_function_limits."
420425 )
421- function .service_config .timeout_seconds = cloud_function_timeout
426+ function .service_config .timeout_seconds = timeout_seconds
427+ if max_instance_count is not None :
428+ function .service_config .max_instance_count = max_instance_count
422429 function .service_config .service_account_email = (
423430 self ._cloud_function_service_account
424431 )
@@ -466,6 +473,7 @@ def provision_bq_remote_function(
466473 package_requirements ,
467474 max_batching_rows ,
468475 cloud_function_timeout ,
476+ cloud_function_max_instance_count ,
469477 ):
470478 """Provision a BigQuery remote function."""
471479 # If reuse of any existing function with the same name (indicated by the
@@ -487,7 +495,11 @@ def provision_bq_remote_function(
487495 # Create the cloud function if it does not exist
488496 if not cf_endpoint :
489497 cf_endpoint = self .create_cloud_function (
490- def_ , cloud_function_name , package_requirements , cloud_function_timeout
498+ def_ ,
499+ cloud_function_name ,
500+ package_requirements ,
501+ cloud_function_timeout ,
502+ cloud_function_max_instance_count ,
491503 )
492504 else :
493505 logger .info (f"Cloud function { cloud_function_name } already exists." )
@@ -642,6 +654,7 @@ def remote_function(
642654 cloud_function_docker_repository : Optional [str ] = None ,
643655 max_batching_rows : Optional [int ] = 1000 ,
644656 cloud_function_timeout : Optional [int ] = 600 ,
657+ cloud_function_max_instances : Optional [int ] = None ,
645658):
646659 """Decorator to turn a user defined function into a BigQuery remote function.
647660
@@ -778,6 +791,14 @@ def remote_function(
778791 https://cloud.google.com/bigquery/quotas#remote_function_limits.
779792 By default BigQuery DataFrames uses a 10 minute timeout. `None`
780793 can be passed to let the cloud functions default timeout take effect.
794+ cloud_function_max_instances (int, Optional):
795+ The maximumm instance count for the cloud function created. This
796+ can be used to control how many cloud function instances can be
797+ active at max at any given point of time. Lower setting can help
798+ control the spike in the billing. Higher setting can help
799+ support processing larger scale data. When not specified, cloud
800+ function's default setting applies. For more details see
801+ https://cloud.google.com/functions/docs/configuring/max-instances
781802 """
782803 if isinstance (input_types , type ):
783804 input_types = [input_types ]
@@ -906,6 +927,7 @@ def wrapper(f):
906927 packages ,
907928 max_batching_rows ,
908929 cloud_function_timeout ,
930+ cloud_function_max_instances ,
909931 )
910932
911933 # TODO: Move ibis logic to compiler step
0 commit comments