Skip to content
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

Merged
merged 7 commits into from
Oct 24, 2016
Merged

Conversation

coocood
Copy link
Member

@coocood coocood commented Oct 22, 2016

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.

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.
@ngaut
Copy link
Member

ngaut commented Oct 22, 2016

PTAL @zimulala @tiancaiamao @disksing

}

func (s *testPrivilegeSuite) SetUpTest(c *C) {
fmt.Println("SETUPTEST")
Copy link
Member

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)
Copy link
Member

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove debug messages.

@coocood coocood force-pushed the coocood/schema-diff branch from 61ea4b0 to 2d98f70 Compare October 22, 2016 15:33
return []byte(fmt.Sprintf("%s:%d", mSchemaDiffPrefix, schemaVersion))
}

// GetSchemaDiff gets the modification information on a given schema version.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/modification/modified

Copy link
Member Author

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) {
Copy link
Contributor

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)
Copy link
Contributor

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 {
Copy link
Member

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) {
Copy link
Member

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
Copy link
Member

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.

Copy link
Member Author

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]
Copy link
Member

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define 0 as constant.

Copy link
Member

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.

Copy link
Member Author

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var diff model.SchemaDiff

Copy link
Member Author

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 {
Copy link
Member

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 {
Copy link
Member

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.

@coocood
Copy link
Member Author

coocood commented Oct 24, 2016

@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 {
Copy link
Member

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)
Copy link
Member

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().

@hanfei1991
Copy link
Member

LGTM

}
}

func (b *Builder) applyCreateTable(m *meta.Meta, di *model.DBInfo, tableID int64, alloc autoid.Allocator) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/di/roDBInfo ?

@ngaut
Copy link
Member

ngaut commented Oct 24, 2016

LGTM

ngaut and others added 3 commits October 24, 2016 14:43
Conflicts:
	ddl/column.go
	ddl/foreign_key.go
	ddl/index.go
	ddl/schema.go
	ddl/table.go
@coocood coocood merged commit d04dea2 into master Oct 24, 2016
@coocood coocood deleted the coocood/schema-diff branch October 24, 2016 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants