-
Notifications
You must be signed in to change notification settings - Fork 72
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
schema fingerprint verifier #292
Open
Manan007224
wants to merge
9
commits into
main
Choose a base branch
from
schema-fingerprinting-inlineverifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2d0fab2
continously check databasse schema from inline_verifier
Manan007224 df0c644
move scheme_fingerprint_verification out of inline_verifier
Manan007224 d83ec42
add integration tests
Manan007224 4e34389
adding some docs
Manan007224 fbb08e4
structural changes
Manan007224 9bf1bcc
add cpu and offcpu profile
Manan007224 031bd4c
changing conf.json back
Manan007224 0f58419
modify schema_fingerprint_verifier to check target as well
Manan007224 2afb0c3
deleted profiles
Manan007224 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 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 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 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 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,134 @@ | ||
package ghostferry | ||
|
||
import ( | ||
"context" | ||
"crypto/md5" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
sql "github.com/Shopify/ghostferry/sqlwrapper" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type SchemaFingerPrintVerifier struct { | ||
SourceDB *sql.DB | ||
TargetDB *sql.DB | ||
DatabaseRewrites map[string]string | ||
TableSchemaCache TableSchemaCache | ||
ErrorHandler ErrorHandler | ||
PeriodicallyVerifyInterval time.Duration | ||
|
||
SourceSchemaFingerprint string | ||
TargetSchemaFingerprint string | ||
|
||
logger *logrus.Entry | ||
} | ||
|
||
func (sf *SchemaFingerPrintVerifier) PeriodicallyVerifySchemaFingerprints(ctx context.Context) { | ||
sf.logger.Info("starting periodic schema fingerprint verification") | ||
ticker := time.NewTicker(sf.PeriodicallyVerifyInterval) | ||
|
||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
err := sf.VerifySchemaFingerprint() | ||
if err != nil { | ||
sf.ErrorHandler.Fatal("schema_fingerprint_verifier", err) | ||
} | ||
case <-ctx.Done(): | ||
sf.logger.Info("shutdown schema_fingerprint_verifier") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (sf *SchemaFingerPrintVerifier) VerifySchemaFingerprint() error { | ||
err := sf.verifySourceSchemaFingerprint() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = sf.verifyTargetSchemaFingerprint() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sf *SchemaFingerPrintVerifier) verifySourceSchemaFingerprint() error { | ||
newSchemaSourceFingerPrint, err := sf.getSchemaFingerPrint(sf.SourceDB, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(sf.SourceSchemaFingerprint) != 0 && newSchemaSourceFingerPrint != sf.SourceSchemaFingerprint { | ||
return fmt.Errorf("failed to verifiy schema fingerprint on source") | ||
} else { | ||
sf.SourceSchemaFingerprint = newSchemaSourceFingerPrint | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sf *SchemaFingerPrintVerifier) verifyTargetSchemaFingerprint() error { | ||
newSchemaTargetFingerPrint, err := sf.getSchemaFingerPrint(sf.TargetDB, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(sf.TargetSchemaFingerprint) != 0 && newSchemaTargetFingerPrint != sf.TargetSchemaFingerprint { | ||
return fmt.Errorf("failed to verifiy schema fingerprint on target") | ||
} else { | ||
sf.TargetSchemaFingerprint = newSchemaTargetFingerPrint | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sf *SchemaFingerPrintVerifier) getSchemaFingerPrint(db *sql.DB, isTargetDB bool) (string, error) { | ||
dbSet := map[string]struct{}{} | ||
schemaData := [][]interface{}{} | ||
|
||
for _, table := range sf.TableSchemaCache { | ||
if _, found := dbSet[table.Schema]; found { | ||
continue | ||
} | ||
dbSet[table.Schema] = struct{}{} | ||
|
||
dbname := table.Schema | ||
if isTargetDB { | ||
if targetDbName, exists := sf.DatabaseRewrites[dbname]; exists { | ||
dbname = targetDbName | ||
} | ||
} | ||
|
||
query := fmt.Sprintf("SELECT * FROM information_schema.columns WHERE table_schema = '%s' ORDER BY table_name, column_name", dbname) | ||
rows, err := db.Query(query) | ||
if err != nil { | ||
fmt.Println(err) | ||
return "", err | ||
} | ||
|
||
for rows.Next() { | ||
// `information_schema.columns` table has 21 columns. | ||
rowData, err := ScanGenericRow(rows, 21) | ||
if err != nil { | ||
return "", err | ||
} | ||
schemaData = append(schemaData, rowData) | ||
} | ||
} | ||
|
||
schemaDataInBytes, err := json.Marshal(schemaData) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
hash := md5.Sum([]byte(schemaDataInBytes)) | ||
return hex.EncodeToString(hash[:]), nil | ||
} |
This file contains 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 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 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,76 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Shopify/ghostferry" | ||
sql "github.com/Shopify/ghostferry/sqlwrapper" | ||
"github.com/Shopify/ghostferry/testhelpers" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type SchemaFingerPrintVerifierTestSuite struct { | ||
*testhelpers.GhostferryUnitTestSuite | ||
tablename string | ||
sf *ghostferry.SchemaFingerPrintVerifier | ||
} | ||
|
||
func alterTestTableSchema(db *sql.DB, this *SchemaFingerPrintVerifierTestSuite) { | ||
query := fmt.Sprintf("ALTER TABLE IF EXISTS %s.%s ADD COLUMN extracol VARCHAR(15)", testhelpers.TestSchemaName, this.tablename) | ||
this.Ferry.SourceDB.Query(query) | ||
} | ||
|
||
func resetTestTableSchema(db *sql.DB, this *SchemaFingerPrintVerifierTestSuite) { | ||
query := fmt.Sprintf("ALTER TABLE IF EXISTS %s.%s DROP COLUMN extracol", testhelpers.TestSchemaName, this.tablename) | ||
db.Query(query) | ||
} | ||
|
||
func (this *SchemaFingerPrintVerifierTestSuite) SetupTest() { | ||
this.GhostferryUnitTestSuite.SetupTest() | ||
|
||
this.tablename = "test_table_1" | ||
testhelpers.SeedInitialData(this.Ferry.SourceDB, testhelpers.TestSchemaName, this.tablename, 0) | ||
|
||
tableFilter := &testhelpers.TestTableFilter{ | ||
DbsFunc: testhelpers.DbApplicabilityFilter([]string{testhelpers.TestSchemaName}), | ||
TablesFunc: nil, | ||
} | ||
tableSchema, err := ghostferry.LoadTables(this.Ferry.SourceDB, tableFilter, nil, nil, nil, nil) | ||
this.Require().Nil(err) | ||
|
||
periodicallyVerifyInterval, _ := time.ParseDuration(this.Ferry.Config.PeriodicallyVerifySchemaFingerPrintInterval) | ||
|
||
this.sf = &ghostferry.SchemaFingerPrintVerifier{ | ||
SourceDB: this.Ferry.SourceDB, | ||
TargetDB: this.Ferry.TargetDB, | ||
ErrorHandler: this.Ferry.ErrorHandler, | ||
DatabaseRewrites: map[string]string{}, | ||
TableSchemaCache: tableSchema, | ||
PeriodicallyVerifyInterval: periodicallyVerifyInterval, | ||
} | ||
} | ||
|
||
func (this *SchemaFingerPrintVerifierTestSuite) TestVerifySchemaFingerprint() { | ||
err := this.sf.VerifySchemaFingerprint() | ||
this.Require().Nil(err) | ||
|
||
alterTestTableSchema(this.sf.SourceDB, this) | ||
err = this.sf.VerifySchemaFingerprint() | ||
this.Require().Error(fmt.Errorf("failed to verifiy schema fingerprint on source")) | ||
|
||
resetTestTableSchema(this.sf.SourceDB, this) | ||
this.Require().Nil(err) | ||
|
||
alterTestTableSchema(this.sf.TargetDB, this) | ||
err = this.sf.VerifySchemaFingerprint() | ||
this.Require().Error(fmt.Errorf("failed to verifiy schema fingerprint on target")) | ||
} | ||
|
||
func TestSchemaFingerPrintVerifier(t *testing.T) { | ||
testhelpers.SetupTest() | ||
suite.Run(t, &SchemaFingerPrintVerifierTestSuite{ | ||
GhostferryUnitTestSuite: &testhelpers.GhostferryUnitTestSuite{}, | ||
}) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can you write a comment about this magic 21? I think this is the number of columns in
information_schema.columns
, but if you have a comment here guiding the reader, then the reader doesn't have to go look on their own.