Danny Steffy
02/21/2023, 11:23 PMcompute_server_round_robin
resource that would get a compute server resource based on the modulus of the total number of compute servers and the index of the fanned out op. Is there a way to get a resource to use based on a value passed into a resource? Or do I need to rethink my strategy?Stefan Adelbert
02/22/2023, 12:30 AMZach
02/22/2023, 3:31 AM@resource
def sql_server_round_robin(context):
def factory(index):
# custom logic for generating server name from index
server = get_server_using_index(index)
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
f"Server={server};"
"Database=db_name;"
"Trusted_Connection=yes;")
return cnxn
return factory
@op(required_resource_keys={"sql_server_selector"})
def fanned_out_op(context: OpExecutionContext):
connection = context.resources.sql_server_selector(context.get_mapping_key())
Danny Steffy
02/22/2023, 4:49 AMrequired_resource_keys
defined in the resource for previously created connections that we create dynamically. However, on code load I'm getting an error that those resources don't exist. What am I missing here?for index, row in databases.iterrows():
server = str(row["ServerName"])
print(f"{str(row['DatabaseLabel']).replace('-','_')}_resource")
RESOURCES[
f"{str(row['DatabaseLabel']).replace('-','_')}_resource"
] = sr.sql_server_resource.configured(
{"server": server, "database": row["DatabaseName"]}
)
@resource(
required_resource_keys={
"sql_01_resource,sql_02_resource,sql_03_resource,sql_04_resource,sql_05_resource,sql_06_resource,sql_07_resource,sql_08_resource"
}
)
def sql_server_round_robin(context):
def factory(index):
# custom logic for generating server name from index
cnxn = context.resources.sql_01_resource
match (index % 2):
case 1:
cnxn = context.resources.sql_02_resource
case 2:
cnxn = context.resources.sql_03_resource
case 3:
cnxn = context.resources.sql_04_resource
case 4:
cnxn = context.resources.sql_05_resource
return cnxn
return factory
Resource with key 'sql_01_resource,sql_02_resource,sql_03_resource,sql_04_resource,sql_05_resource,sql_06_resource,sql_07_resource,sql_08_resource' required by resource with key 'sql_server_selector', but not provided.
Zach
02/22/2023, 8:00 PMsql_server_round_robin
and dynamically generate their server using that as a factory. this would require directly instantiating the sql server object within sql_server_round_robin
with the server / database, instead of using the .configured()
api on another resource.
more like
@resource()
def sql_server_round_robin(context):
RESOURCES = []
for index, row in databases.iterrows():
server = str(row["ServerName"])
print(f"{str(row['DatabaseLabel']).replace('-','_')}_resource")
RESOURCES.append(pyodbc.connect("Driver={SQL Server Native Client 11.0};"
f"Server={server};"
"Database={row['DatabaseName']};"
"Trusted_Connection=yes;"))
def factory(index):
# custom logic for generating server name from index
return RESOURCES[index%len(RESOURCES)]
return factory
but maybe I'm missing something. also, isn't index % 2
only ever going to match 0 or 1?Danny Steffy
02/22/2023, 8:05 PMZach
02/22/2023, 9:27 PM.configured()
API within a resource definition on one of its dependent resources - .configured()
is only available on ResourceDefinitions - when you access context.resources.resource_name
you're accessing an object that has been instantiated as a result of resolving a ResourceDefinition - in other words you're accessing whatever object you're returning from the resource definition mapped to context.resources.resource_name
, not the ResourceDefinition itself. you can use .configured()
on resources at a global level or within @graph
/ @job
definitions because the definition objects have not