-
Notifications
You must be signed in to change notification settings - Fork 75
Adding performance schema collector for query assessments #1168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+752
−34
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
92a0d27
Adding performance collector for query assessments
shreyakhajanchi 0f36d42
addressing comments
shreyakhajanchi 7f37e05
addressing comments
shreyakhajanchi e226ab1
Merge branch 'master' into query-assessment-2-pr
shreyakhajanchi e31f0e2
Merge branch 'master' into query-assessment-2-pr
shreyakhajanchi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License.*/ | ||
|
|
||
| package common | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "fmt" | ||
|
|
||
| "github.com/GoogleCloudPlatform/spanner-migration-tool/conversion" | ||
| "github.com/GoogleCloudPlatform/spanner-migration-tool/profiles" | ||
| ) | ||
|
|
||
| // ConnectionConfigProvider interface for getting database connection configuration | ||
| type ConnectionConfigProvider interface { | ||
| GetConnectionConfig(sourceProfile profiles.SourceProfile) (interface{}, error) | ||
| } | ||
|
|
||
| // DefaultConnectionConfigProvider provides default connection configuration | ||
| type DefaultConnectionConfigProvider struct{} | ||
|
|
||
| // GetConnectionConfig returns the connection configuration for the given source profile | ||
| func (d DefaultConnectionConfigProvider) GetConnectionConfig(sourceProfile profiles.SourceProfile) (interface{}, error) { | ||
| return conversion.ConnectionConfig(sourceProfile) | ||
| } | ||
|
|
||
| // DBConnector interface for establishing database connections | ||
| type DBConnector interface { | ||
| Connect(driver string, connectionConfig interface{}) (*sql.DB, error) | ||
| } | ||
|
|
||
| // SQLDBConnector provides default SQL database connection functionality | ||
| type SQLDBConnector struct{} | ||
|
|
||
| // Connect establishes a database connection using the provided configuration | ||
| func (d SQLDBConnector) Connect(driver string, connectionConfig interface{}) (*sql.DB, error) { | ||
| connectionStr, ok := connectionConfig.(string) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid connection configuration type") | ||
| } | ||
|
|
||
| db, err := sql.Open(driver, connectionStr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to open database connection: %w", err) | ||
| } | ||
|
|
||
| // Test the connection | ||
| if err := db.Ping(); err != nil { | ||
| db.Close() | ||
| return nil, fmt.Errorf("failed to ping database: %w", err) | ||
| } | ||
|
|
||
| return db, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package assessment | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| _ "embed" | ||
| "fmt" | ||
|
|
||
| collectorCommon "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/collectors/common" | ||
| sourcesCommon "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/sources" | ||
| "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/sources/mysql" | ||
| "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/utils" | ||
| "github.com/GoogleCloudPlatform/spanner-migration-tool/common/constants" | ||
| "github.com/GoogleCloudPlatform/spanner-migration-tool/logger" | ||
| "github.com/GoogleCloudPlatform/spanner-migration-tool/profiles" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // PerformanceSchemaCollector collects performance schema data from source databases | ||
| type PerformanceSchemaCollector struct { | ||
| queries []utils.QueryAssessmentInfo | ||
| } | ||
|
|
||
| // IsEmpty checks if the collector has any data | ||
| func (c PerformanceSchemaCollector) IsEmpty() bool { | ||
| return len(c.queries) == 0 | ||
| } | ||
|
|
||
| // GetDefaultPerformanceSchemaCollector creates a new PerformanceSchemaCollector with default settings | ||
| func GetDefaultPerformanceSchemaCollector(sourceProfile profiles.SourceProfile) (PerformanceSchemaCollector, error) { | ||
| return GetPerformanceSchemaCollector(sourceProfile, collectorCommon.SQLDBConnector{}, collectorCommon.DefaultConnectionConfigProvider{}, getPerformanceSchema) | ||
| } | ||
|
|
||
| // GetPerformanceSchemaCollector creates a new PerformanceSchemaCollector with custom dependencies | ||
| func GetPerformanceSchemaCollector(sourceProfile profiles.SourceProfile, dbConnector collectorCommon.DBConnector, configProvider collectorCommon.ConnectionConfigProvider, performanceSchemaProvider func(*sql.DB, profiles.SourceProfile) (sourcesCommon.PerformanceSchema, error)) (PerformanceSchemaCollector, error) { | ||
| logger.Log.Info("initializing performance schema collector") | ||
|
|
||
| connectionConfig, err := configProvider.GetConnectionConfig(sourceProfile) | ||
| if err != nil { | ||
| return PerformanceSchemaCollector{}, fmt.Errorf("failed to get connection config: %w", err) | ||
| } | ||
|
|
||
| db, err := dbConnector.Connect(sourceProfile.Driver, connectionConfig) | ||
| if err != nil { | ||
| return PerformanceSchemaCollector{}, fmt.Errorf("failed to connect to database: %w", err) | ||
| } | ||
|
|
||
| performanceSchema, err := performanceSchemaProvider(db, sourceProfile) | ||
| if err != nil { | ||
| return PerformanceSchemaCollector{}, fmt.Errorf("failed to get performance schema: %w", err) | ||
| } | ||
|
|
||
| queries, err := performanceSchema.GetAllQueries() | ||
| if err != nil { | ||
| return PerformanceSchemaCollector{}, fmt.Errorf("failed to get all queries: %w", err) | ||
| } | ||
|
|
||
| logger.Log.Info("performance schema collector initialized successfully", | ||
| zap.Int("query_count", len(queries))) | ||
|
|
||
| return PerformanceSchemaCollector{ | ||
| queries: queries, | ||
| }, nil | ||
| } | ||
|
|
||
| // getPerformanceSchema creates a performance schema implementation based on the database driver | ||
| func getPerformanceSchema(db *sql.DB, sourceProfile profiles.SourceProfile) (sourcesCommon.PerformanceSchema, error) { | ||
| driver := sourceProfile.Driver | ||
| switch driver { | ||
| case constants.MYSQL: | ||
| return mysql.PerformanceSchemaImpl{ | ||
| Db: db, | ||
| DbName: sourceProfile.Conn.Mysql.Db, | ||
| }, nil | ||
| default: | ||
| return nil, fmt.Errorf("driver %s not supported for performance schema", driver) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.