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

*: Reduce loading schema times #1984

Merged
merged 13 commits into from
Nov 22, 2016
Merged

*: Reduce loading schema times #1984

merged 13 commits into from
Nov 22, 2016

Conversation

zimulala
Copy link
Contributor

@zimulala zimulala commented Nov 9, 2016

@zimulala zimulala added the WIP label Nov 9, 2016
}
do.SchemaValidity.setLastFailedTS(ver.Ver)
log.Errorf("[ddl] load schema err %v, retry again", errors.ErrorStack(err))
if atomic.LoadInt32(exit) == 1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not Go-style, I suggest make exit a channel:

select {
   case <-timeout:
   case <-exit:
}

var defaultMinReloadTimeout = 20 * time.Second

// Reload reloads InfoSchema.
func (do *Domain) Reload() error {
func (do *Domain) reload() error {
// for test
if do.SchemaValidity.MockReloadFailed {
Copy link
Contributor

Choose a reason for hiding this comment

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

mixing the test code logic would hurt readability, can you find better way to do it?
such as extract to an interface and wrap the mocked code upon the real implementation

log.Errorf("[ddl] reload schema in loop err %v", errors.ErrorStack(err))
time.Sleep(mustReloadSleepTime)
Copy link
Contributor

Choose a reason for hiding this comment

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

after the time.Sleep, it would miss the do.leaseCh signal for a while, would this cause problem?

c.Assert(dd.GetLease(), Equals, 100*time.Millisecond)
dom.SetLease(0 * time.Millisecond)
c.Assert(dd.GetLease(), Equals, 100*time.Millisecond)
time.Sleep(100 * time.Millisecond)
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the purpose of this line?

@zimulala zimulala force-pushed the zimuxia/domain-reload branch 4 times, most recently from 0fb9dc0 to 5bd5a13 Compare November 15, 2016 10:17
@zimulala zimulala force-pushed the zimuxia/domain-reload branch from 5bd5a13 to e29e0e7 Compare November 16, 2016 01:40
@zimulala zimulala force-pushed the zimuxia/domain-reload branch from e29e0e7 to 897715d Compare November 17, 2016 03:01
@zimulala zimulala removed the WIP label Nov 17, 2016
}

return errors.Trace(err)
}

// MustReload reloads the infoschema.
// If reload error, it will hold whole program to guarantee data safe.
// It's public in order to do the test.
func (do *Domain) MustReload() error {
Copy link
Member

@coocood coocood Nov 17, 2016

Choose a reason for hiding this comment

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

MustReload does nothing more than reload. We can remove this function, use reload instead.

@zimulala zimulala force-pushed the zimuxia/domain-reload branch from 6e503b8 to 8af9fba Compare November 17, 2016 06:33
@zimulala
Copy link
Contributor Author

PTAL @coocood @tiancaiamao @shenli

return
log.Infof("[ddl] check validity in a loop, sub:%v, lease:%v, succ:%v, waitTime:%v",
sub, lease, lastSuccTS, waitTime)
timer.Reset(waitTime)
Copy link
Contributor

@tiancaiamao tiancaiamao Nov 18, 2016

Choose a reason for hiding this comment

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

This is not the ideal usage of timer.Reset

see https://godoc.org/time#Timer.Reset

To reuse an active timer, always call its Stop method first and—if it had expired—drain the value from its channel.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here doesn't need to add that judgment.

do.SchemaValidity.SetValidity(false)
return errors.Trace(err)
lease = newLease
timer.Reset(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think line 344~350 can just one line:
lease = newLease

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don‘t want to wait a old lease

ticker := time.NewTicker(lease)
defer ticker.Stop()
ticker := time.NewTicker(lease / 4)
defer func() { ticker.Stop() }()
Copy link
Contributor

Choose a reason for hiding this comment

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

defer ticker.Stop()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because ticker is reassigned in line 375 . It can prevent panic.

}

func (do *Domain) loadSchemaInLoop(lease time.Duration) {
ticker := time.NewTicker(lease)
Copy link
Contributor

Choose a reason for hiding this comment

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

the code is misleading, I suggest set lease / 4 outside...
let caller do it instead of callee


// Reload reloads InfoSchema.
// It's public in order to do the test.
func (do *Domain) Reload() error {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think sleep and retry in Reload is not a good idea.
There would be new ticker signal happen during this, and may cause Reload be called several times.

A better way is reload returns error and let loadSchemaInLoop handle the retry.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reload will return an error.

}

// SetValidity sets the schema validity value.
// It's public in order to do the test.
func (s *schemaValidityInfo) SetValidity(v bool) {
func (s *schemaValidityInfo) SetValidity(v bool, lastSuccTS uint64) {
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 remove the lastSuccTS parameter, and set s.firstValidTS = s.lastSuccTS directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Setting s.firstValidTS = s.lastSuccTS may be concurrent.

ticker := time.NewTicker(lease)
defer ticker.Stop()
ticker := time.NewTicker(minInterval(lease))
defer func() { ticker.Stop() }()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will deal with it in the next PR.

@zimulala
Copy link
Contributor Author

PTAL @tiancaiamao @coocood

@coocood
Copy link
Member

coocood commented Nov 21, 2016

LGTM

}()

wg.Wait()

// retry after 2 seconds
time.Sleep(2000 * time.Millisecond)
Copy link
Contributor

Choose a reason for hiding this comment

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

it seems server is busy just 60ms, is it necessary sleep so long here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If server is busy, transaction will retry after 2s.

@tiancaiamao
Copy link
Contributor

LGTM

@zimulala
Copy link
Contributor Author

PTAL @disksing

@zimulala zimulala changed the title *: Reduce loading schema times [DNM]*: Reduce loading schema times Nov 22, 2016
@zimulala zimulala force-pushed the zimuxia/domain-reload branch from 1432c16 to ab32b51 Compare November 22, 2016 09:49
@zimulala zimulala changed the title [DNM]*: Reduce loading schema times *: Reduce loading schema times Nov 22, 2016
@zimulala zimulala merged commit 1181acc into master Nov 22, 2016
@zimulala zimulala deleted the zimuxia/domain-reload branch November 22, 2016 10:34
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.

Make sure lastInvalidTS is a normal txn TS.
3 participants