class CatalogSyncService
Materialises the “lightweight” sync path: list namespaces and tables via the catalog REST API, read TableMetadata (last snapshot, snapshot count) and refresh the local health signals. Never touches Trino.
Public Class Methods
Source
# File app/services/catalog_sync_service.rb, line 53 def self.enrich_from_trino!(table) catalog_name = table.catalog.trino_catalog_name client = TrinoClient.new(catalog_name: catalog_name) files_sql = %(SELECT SUM(file_size_in_bytes) AS total FROM #{table.trino_metadata_table("files")}) total_size = client.query_scalar(files_sql, "total") table.update_column(:total_size_bytes, total_size.to_i) if total_size manifests_sql = %(SELECT COUNT(*) AS n FROM #{table.trino_metadata_table("manifests")}) manifest_count = client.query_scalar(manifests_sql, "n") table.update_column(:manifest_count, manifest_count.to_i) if manifest_count # Re-evaluate health with the newly available Trino data. extractor = TableMetadataExtractor.from_persisted(table) health = HealthEvaluator.evaluate(extractor, plan: table.maintenance_plan, manifest_count: manifest_count&.to_i) attributes = table.health_attributes(health) table.update_columns(attributes) if attributes.any? rescue StandardError => e Rails.logger.warn("enrich_from_trino! failed for #{table.fully_qualified_name}: #{e.message}") end
Enriches a table with metrics that require a Trino query (engine must be up). Called on the post-maintenance resync path when the engine is already running. Never forces an engine start.
Queries $files for total size and $manifests for manifest count, updating the table’s persisted columns so health evaluation can use them.
@param table [IcebergTable] the table to enrich @return [void]
# File app/services/catalog_sync_service.rb, line 10 def initialize(catalog, client: nil, now: Time.current) @catalog = catalog @client = client || CatalogClientFactory.for(catalog) @now = now end
Creates the sync service for a catalog, defaulting the client and clock.
@param catalog [Catalog] the catalog to sync @param client [CatalogClient, nil] an injectable catalog client @param now [Time] the reference clock for health evaluation
Source
# File app/services/catalog_sync_service.rb, line 39 def self.sync_table(table_id) table = IcebergTable.includes(:catalog).find(table_id) new(table.catalog).sync_table(table) end
Convenience: refresh a single table by its id. @param table_id [Integer] @return [Hash]
Public Instance Methods
Source
# File app/services/catalog_sync_service.rb, line 80 def sync TrinoSecretMaterializer.new.materialize!(Catalog.includes(:catalog_credential)) errors = [] seen = [] # Only namespaces whose listing succeeded AND whose every table synced can # authorise a deactivation. A namespace that errored tells us nothing about # what it contains, so its tables must be left alone. complete_namespaces = [] @client.namespaces.each do |namespace| begin namespace_failed = false @client.tables_in(namespace).each do |table_name| begin seen << upsert_table(namespace, table_name) rescue StandardError => e namespace_failed = true ErrorEvent.record(catalog: @catalog, schema: namespace, table: table_name, operation: "sync-table", source_system: "catalog", error_class: e.class.name, message: e.message) errors << "#{namespace}.#{table_name}: #{e.message}" Rails.logger.warn("Sync failed for #{namespace}.#{table_name}: #{e.message}") end end complete_namespaces << namespace unless namespace_failed rescue StandardError => e ErrorEvent.record(catalog: @catalog, schema: namespace, operation: "sync-namespace", source_system: "catalog", error_class: e.class.name, message: e.message) errors << "namespace #{namespace}: #{e.message}" Rails.logger.warn("Sync failed for namespace #{namespace}: #{e.message}") end end deactivate_unseen(seen, complete_namespaces) if errors.any? { |e| e.match?(/secret|file|resolve/i) } ErrorEvent.record( catalog: @catalog, schema: "security", operation: "secret-resolution", source_system: "app", severity: "warning", error_class: "SecretResolutionFailure", message: "Catalog #{@catalog.name}: secret file not yet available — check trino-catalog-secrets and secret-file-base-dir" ) end { errors: errors } end
Syncs every namespace/table of the catalog and refreshes local health signals. Tables that no longer exist in the catalog are soft-deleted so their history is kept.
@return [Hash] the list of collected errors under the errors key
Source
# File app/services/catalog_sync_service.rb, line 24 def sync_table(table) TrinoSecretMaterializer.new.materialize!(Catalog.includes(:catalog_credential)) upsert_table(table.namespace, table.name) { ok: true } rescue StandardError => e Rails.logger.warn("Per-table sync failed for #{table.fully_qualified_name}: #{e.message}") ErrorEvent.record(catalog: table.catalog, schema: table.namespace, table: table.name, operation: "sync-table", source_system: "catalog", error_class: e.class.name, message: e.message) { ok: false, error: e.message } end
Refreshes a single table’s metadata without re-syncing the whole catalog.
The Trino catalog Secret is materialized for ALL catalogs: it is a single shared Secret built as a full replacement, so passing only this table’s catalog would delete every other catalog’s S3/OAuth2 credentials.
@param table [IcebergTable] the table to refresh @return [Hash] { ok: true } or { ok: false, error: message }