-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
*: use schema diff to reduce memory allocation #1858
Conversation
When schema version has changed, we don't need to reload all schemas, only modified table need to be reloaded, this will reduce the pressure on Go garbage collector. Also removed used map in *inforSchema.
} | ||
|
||
func (s *testPrivilegeSuite) SetUpTest(c *C) { | ||
fmt.Println("SETUPTEST") |
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.
Remove debug messages.
@@ -71,7 +73,9 @@ func (s *testPrivilegeSuite) SetUpTest(c *C) { | |||
mustExec(c, se, s.createDBSQL) | |||
mustExec(c, se, s.createDB1SQL) // create database test1 | |||
mustExec(c, se, s.useDBSQL) | |||
mustExec(c, se, s.createTableSQL) | |||
_, err := se.Execute(s.createTableSQL) | |||
log.Error(err) |
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.
Remove debug messages.
@@ -87,9 +91,11 @@ func (s *testPrivilegeSuite) SetUpTest(c *C) { | |||
} | |||
|
|||
func (s *testPrivilegeSuite) TearDownTest(c *C) { | |||
fmt.Println("TEARDOWNTEST") |
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.
Remove debug messages.
61ea4b0
to
2d98f70
Compare
return []byte(fmt.Sprintf("%s:%d", mSchemaDiffPrefix, schemaVersion)) | ||
} | ||
|
||
// GetSchemaDiff gets the modification information on a given schema version. |
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.
s/modification/modified
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.
I think modification is fine.
} | ||
|
||
// GetSchemaDiff gets the modification information on a given schema version. | ||
func (m *Meta) GetSchemaDiff(schemaVersion int64) (*model.SchemaDiff, error) { |
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.
It need tests.
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
log.Infof("[ddl] loadInfoSchema %d", latestSchemaVersion) |
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.
Update the log and add some logs at line68.
// If there isn't any used version, or used version is too old, we do full load. | ||
return false, nil | ||
} | ||
if usedVersion > newVersion { |
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.
Is is possible that usedVersion larger than newVersion ?
} | ||
|
||
// tryLoadSchemaDiffs tries to only load latest schema changes. | ||
func (do *Domain) tryLoadSchemaDiffs(m *meta.Meta, usedVersion, newVersion int64) (bool, error) { |
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.
Please add comments to explain bool return value.
} | ||
di, ok := b.is.schemas[diff.SchemaID] | ||
if !ok { | ||
return ErrDatabaseNotExists |
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.
There must be some serious bug if we go into the branch. Maybe we should log fatal message.
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.
If that happens, we can fall back to full load, we don't have to log.Fatal
.
b.applyDropSchema(diff.SchemaID) | ||
return nil | ||
} | ||
di, ok := b.is.schemas[diff.SchemaID] |
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.
s/di/roDBInfo/
newDbInfo := new(model.DBInfo) | ||
*newDbInfo = *di | ||
newDbInfo.Tables = make([]*model.TableInfo, 0, len(di.Tables)) | ||
if newTableID != 0 { |
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.
Define 0 as constant.
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.
when the new TableID is equal to 0 ? add comments here.
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.
@ngaut define '0' as 'zeroValue‘?
if len(data) == 0 { | ||
return nil, nil | ||
} | ||
diff := new(model.SchemaDiff) |
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.
var diff model.SchemaDiff
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.
We need to return a pointer type.
|
||
// tryLoadSchemaDiffs tries to only load latest schema changes. | ||
func (do *Domain) tryLoadSchemaDiffs(m *meta.Meta, usedVersion, newVersion int64) (bool, error) { | ||
if usedVersion == 0 || newVersion-usedVersion > 100 { |
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.
Can we set 100 as a const ?
newDbInfo := new(model.DBInfo) | ||
*newDbInfo = *di | ||
newDbInfo.Tables = make([]*model.TableInfo, 0, len(di.Tables)) | ||
if newTableID != 0 { |
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.
when the new TableID is equal to 0 ? add comments here.
@zimulala @ngaut @hanfei1991 PTAL |
// Returns true if the schema is loaded successfully. | ||
// Returns false if the schema can not be loaded by schema diff, then we need to do full load. | ||
func (do *Domain) tryLoadSchemaDiffs(m *meta.Meta, usedVersion, newVersion int64) (bool, error) { | ||
if usedVersion == 0 || newVersion-usedVersion > maxNumberOfDiffsToLoad { |
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.
define 0 as constant.
} | ||
// The old DBInfo still holds a reference to old table info, we need to update it. | ||
// As old DBInfo should be read only, we clone a new DBInfo. | ||
newDbInfo := new(model.DBInfo) |
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.
Extract to a new function, cloneDBInfo().
LGTM |
} | ||
} | ||
|
||
func (b *Builder) applyCreateTable(m *meta.Meta, di *model.DBInfo, tableID int64, alloc autoid.Allocator) error { |
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.
s/di/roDBInfo ?
LGTM |
Conflicts: ddl/column.go ddl/foreign_key.go ddl/index.go ddl/schema.go ddl/table.go
When schema version has changed, we don't need to reload all schemas, only
modified table need to be reloaded, this will reduce the pressure on Go garbage collector.
Also removed used map in *inforSchema.