(path: str, ns_config: Dict)
| 37 | |
| 38 | @tracer.capture_method |
| 39 | def sync_table(path: str, ns_config: Dict) -> str: |
| 40 | if not isinstance(ns_config, dict): |
| 41 | return '' |
| 42 | |
| 43 | metadata_config = ns_config.get('inferenceItemMetadata') |
| 44 | if not metadata_config: |
| 45 | return '' |
| 46 | |
| 47 | if metadata_config.get('type') != 'dynamodb': |
| 48 | logger.info('Inference metadata type is not dynamodb so skipping resource sync/provisioning') |
| 49 | return '' |
| 50 | |
| 51 | table_name = table_name_prefix + path |
| 52 | table_status = None |
| 53 | |
| 54 | if metadata_config.get('autoProvision') is not None and metadata_config.get('autoProvision') == False: |
| 55 | logger.info('Auto provisioning inference item metadata resources for namespace "%s" is disabled; skipping') |
| 56 | return table_name |
| 57 | |
| 58 | billing_mode = metadata_config.get('billingMode', 'PAY_PER_REQUEST') |
| 59 | if not billing_mode in ['PAY_PER_REQUEST','PROVISIONED']: |
| 60 | raise Exception(f'"inferenceItemMetadata.billingMode" ({billing_mode}) for namespace ("{path}") is invalid; must be PAY_PER_REQUEST or PROVISIONED') |
| 61 | |
| 62 | provisioned_throughput = metadata_config.get('provisionedThroughput') |
| 63 | if billing_mode == 'PROVISIONED' and not provisioned_throughput: |
| 64 | raise Exception(f'"inferenceItemMetadata.provisionedThroughput" is required for billingMode PROVISIONED but is not specfied for namespace ("{path}") in application configuration') |
| 65 | |
| 66 | try: |
| 67 | response = dynamodb.meta.client.describe_table(TableName = table_name) |
| 68 | |
| 69 | table_status = response['Table']['TableStatus'] |
| 70 | |
| 71 | if response['Table']['BillingModeSummary']['BillingMode'] != billing_mode: |
| 72 | logger.info('Updating BillingMode for table %s from %s to %s', table_name, response['Table']['BillingModeSummary']['BillingMode'], billing_mode) |
| 73 | if billing_mode == 'PAY_PER_REQUEST': |
| 74 | dynamodb.meta.client.update_table( |
| 75 | TableName = table_name, |
| 76 | BillingMode = billing_mode |
| 77 | ) |
| 78 | else: |
| 79 | dynamodb.meta.client.update_table( |
| 80 | TableName = table_name, |
| 81 | BillingMode = billing_mode, |
| 82 | ProvisionedThroughput = { |
| 83 | 'ReadCapacityUnits': provisioned_throughput['readCapacityUnits'], |
| 84 | 'WriteCapacityUnits': provisioned_throughput['writeCapacityUnits'] |
| 85 | } |
| 86 | ) |
| 87 | elif (billing_mode == 'PROVISIONED' and |
| 88 | (response['Table']['ProvisionedThroughput']['ReadCapacityUnits'] != provisioned_throughput['readCapacityUnits'] or |
| 89 | response['Table']['ProvisionedThroughput']['WriteCapacityUnits'] != provisioned_throughput['writeCapacityUnits'])): |
| 90 | logger.info('Provisioned read and/or write capacity units to not match configuration for table %s; updating table', table_name) |
| 91 | dynamodb.meta.client.update_table( |
| 92 | TableName = table_name, |
| 93 | ProvisionedThroughput = { |
| 94 | 'ReadCapacityUnits': provisioned_throughput['readCapacityUnits'], |
| 95 | 'WriteCapacityUnits': provisioned_throughput['writeCapacityUnits'] |
| 96 | } |
no test coverage detected