|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package assessment |
| 16 | + |
| 17 | +import ( |
| 18 | + "database/sql" |
| 19 | + _ "embed" |
| 20 | + "fmt" |
| 21 | + |
| 22 | + collectorCommon "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/collectors/common" |
| 23 | + sourcesCommon "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/sources" |
| 24 | + "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/sources/mysql" |
| 25 | + "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/utils" |
| 26 | + "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" |
| 27 | + "github.com/GoogleCloudPlatform/spanner-migration-tool/logger" |
| 28 | + "github.com/GoogleCloudPlatform/spanner-migration-tool/profiles" |
| 29 | + "go.uber.org/zap" |
| 30 | +) |
| 31 | + |
| 32 | +// PerformanceSchemaCollector collects performance schema data from source databases |
| 33 | +type PerformanceSchemaCollector struct { |
| 34 | + queries []utils.QueryAssessmentInfo |
| 35 | +} |
| 36 | + |
| 37 | +// IsEmpty checks if the collector has any data |
| 38 | +func (c PerformanceSchemaCollector) IsEmpty() bool { |
| 39 | + return len(c.queries) == 0 |
| 40 | +} |
| 41 | + |
| 42 | +// GetDefaultPerformanceSchemaCollector creates a new PerformanceSchemaCollector with default settings |
| 43 | +func GetDefaultPerformanceSchemaCollector(sourceProfile profiles.SourceProfile) (PerformanceSchemaCollector, error) { |
| 44 | + return GetPerformanceSchemaCollector(sourceProfile, collectorCommon.SQLDBConnector{}, collectorCommon.DefaultConnectionConfigProvider{}, DefaultPerformanceSchemaProvider{}) |
| 45 | +} |
| 46 | + |
| 47 | +// GetPerformanceSchemaCollector creates a new PerformanceSchemaCollector with custom dependencies |
| 48 | +func GetPerformanceSchemaCollector(sourceProfile profiles.SourceProfile, dbConnector collectorCommon.DBConnector, configProvider collectorCommon.ConnectionConfigProvider, performanceSchemaProvider PerformanceSchemaProvider) (PerformanceSchemaCollector, error) { |
| 49 | + logger.Log.Info("initializing performance schema collector") |
| 50 | + |
| 51 | + connectionConfig, err := configProvider.GetConnectionConfig(sourceProfile) |
| 52 | + if err != nil { |
| 53 | + return PerformanceSchemaCollector{}, fmt.Errorf("failed to get connection config: %w", err) |
| 54 | + } |
| 55 | + |
| 56 | + db, err := dbConnector.Connect(sourceProfile.Driver, connectionConfig) |
| 57 | + if err != nil { |
| 58 | + return PerformanceSchemaCollector{}, fmt.Errorf("failed to connect to database: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + performanceSchema, err := performanceSchemaProvider.getPerformanceSchema(db, sourceProfile) |
| 62 | + if err != nil { |
| 63 | + return PerformanceSchemaCollector{}, fmt.Errorf("failed to get performance schema: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + queries, err := performanceSchema.GetAllQueryAssessments() |
| 67 | + if err != nil { |
| 68 | + return PerformanceSchemaCollector{}, fmt.Errorf("failed to get all queries: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + logger.Log.Info("performance schema collector initialized successfully", |
| 72 | + zap.Int("query_count", len(queries))) |
| 73 | + |
| 74 | + return PerformanceSchemaCollector{ |
| 75 | + queries: queries, |
| 76 | + }, nil |
| 77 | +} |
| 78 | + |
| 79 | +// DBConnector interface for establishing database connections |
| 80 | +type PerformanceSchemaProvider interface { |
| 81 | + getPerformanceSchema(db *sql.DB, sourceProfile profiles.SourceProfile) (sourcesCommon.PerformanceSchema, error) |
| 82 | +} |
| 83 | + |
| 84 | +// SQLDBConnector provides default SQL database connection functionality |
| 85 | +type DefaultPerformanceSchemaProvider struct{} |
| 86 | + |
| 87 | +// getPerformanceSchema creates a performance schema implementation based on the database driver |
| 88 | +func (d DefaultPerformanceSchemaProvider) getPerformanceSchema(db *sql.DB, sourceProfile profiles.SourceProfile) (sourcesCommon.PerformanceSchema, error) { |
| 89 | + driver := sourceProfile.Driver |
| 90 | + switch driver { |
| 91 | + case constants.MYSQL: |
| 92 | + return mysql.PerformanceSchemaImpl{ |
| 93 | + Db: db, |
| 94 | + DbName: sourceProfile.Conn.Mysql.Db, |
| 95 | + }, nil |
| 96 | + default: |
| 97 | + return nil, fmt.Errorf("driver %s not supported for performance schema", driver) |
| 98 | + } |
| 99 | +} |
0 commit comments