-
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
*: implement auto analyze #4141
Changes from 15 commits
22d3419
db1d979
0e683d6
1d837b0
4e30ada
e7fc3ef
ce569f5
2216f2b
d89d2ee
64d9663
2688976
6410708
b176156
ff5513b
f2c51ab
20707cf
5bcf6ae
58b87a5
d400708
f3a6191
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import ( | |
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/meta" | ||
"github.com/pingcap/tidb/model" | ||
"github.com/pingcap/tidb/owner" | ||
"github.com/pingcap/tidb/perfschema" | ||
"github.com/pingcap/tidb/privilege/privileges" | ||
"github.com/pingcap/tidb/sessionctx/variable" | ||
|
@@ -527,8 +528,9 @@ func (do *Domain) CreateStatsHandle(ctx context.Context) { | |
do.statsHandle = statistics.NewHandle(ctx, do.statsLease) | ||
} | ||
|
||
// UpdateTableStatsLoop creates a goroutine loads stats info and updates stats info in a loop. It | ||
// should be called only once in BootstrapSession. | ||
// UpdateTableStatsLoop creates a goroutine loads stats info and updates stats info in a loop. | ||
// It will also start a goroutine to analyze tables automatically. | ||
// It should be called only once in BootstrapSession. | ||
func (do *Domain) UpdateTableStatsLoop(ctx context.Context) error { | ||
ctx.GetSessionVars().InRestrictedSQL = true | ||
do.statsHandle = statistics.NewHandle(ctx, do.statsLease) | ||
|
@@ -541,41 +543,68 @@ func (do *Domain) UpdateTableStatsLoop(ctx context.Context) error { | |
if lease <= 0 { | ||
return nil | ||
} | ||
go do.updateStatsWorker(ctx, lease) | ||
go do.autoAnalyzeWorker(lease) | ||
return nil | ||
} | ||
|
||
func (do *Domain) updateStatsWorker(ctx context.Context, lease time.Duration) { | ||
deltaUpdateDuration := lease * 5 | ||
go func(do *Domain) { | ||
loadTicker := time.NewTicker(lease) | ||
defer loadTicker.Stop() | ||
deltaUpdateTicker := time.NewTicker(deltaUpdateDuration) | ||
defer deltaUpdateTicker.Stop() | ||
loadTicker := time.NewTicker(lease) | ||
defer loadTicker.Stop() | ||
deltaUpdateTicker := time.NewTicker(deltaUpdateDuration) | ||
defer deltaUpdateTicker.Stop() | ||
|
||
for { | ||
select { | ||
case <-loadTicker.C: | ||
err = do.statsHandle.Update(do.InfoSchema()) | ||
if err != nil { | ||
log.Error(errors.ErrorStack(err)) | ||
} | ||
case <-do.exit: | ||
return | ||
for { | ||
select { | ||
case <-loadTicker.C: | ||
err := do.statsHandle.Update(do.InfoSchema()) | ||
if err != nil { | ||
log.Error("[stats] update stats info fail: ", errors.ErrorStack(err)) | ||
} | ||
case <-do.exit: | ||
return | ||
// This channel is sent only by ddl owner or the drop stats executor. | ||
case t := <-do.statsHandle.DDLEventCh(): | ||
err = do.statsHandle.HandleDDLEvent(t) | ||
case t := <-do.statsHandle.DDLEventCh(): | ||
err := do.statsHandle.HandleDDLEvent(t) | ||
if err != nil { | ||
log.Error("[stats] handle ddl event fail: ", errors.ErrorStack(err)) | ||
} | ||
case t := <-do.statsHandle.AnalyzeResultCh(): | ||
for _, hg := range t.Hist { | ||
err := hg.SaveToStorage(ctx, t.TableID, t.Count, t.IsIndex) | ||
if err != nil { | ||
log.Error(errors.ErrorStack(err)) | ||
log.Error("[stats] save histogram to storage fail: ", errors.ErrorStack(err)) | ||
} | ||
case t := <-do.statsHandle.AnalyzeResultCh(): | ||
for _, hg := range t.Hist { | ||
err = hg.SaveToStorage(t.Ctx, t.TableID, t.Count, t.IsIndex) | ||
if err != nil { | ||
log.Error(errors.ErrorStack(err)) | ||
} | ||
} | ||
case <-deltaUpdateTicker.C: | ||
do.statsHandle.DumpStatsDeltaToKV() | ||
} | ||
case <-deltaUpdateTicker.C: | ||
do.statsHandle.DumpStatsDeltaToKV() | ||
} | ||
}(do) | ||
return nil | ||
} | ||
} | ||
|
||
func (do *Domain) autoAnalyzeWorker(lease time.Duration) { | ||
id := do.ddl.OwnerManager().ID() | ||
cancelCtx, cancelFunc := goctx.WithCancel(goctx.Background()) | ||
var statsOwner owner.Manager | ||
if do.etcdClient == nil { | ||
statsOwner = owner.NewMockManager(id, cancelFunc) | ||
} else { | ||
statsOwner = owner.NewOwnerManager(do.etcdClient, statistics.StatsPrompt, id, statistics.StatsOwnerKey, cancelFunc) | ||
} | ||
err := statsOwner.CampaignOwner(cancelCtx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to do something when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like what? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The network between PD and TiDB is unstable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try until the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It‘s better to put it out of a goroutine. If it hard to handle, you can add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I add a TODO here. |
||
if err != nil { | ||
log.Warnf("[stats] campaign owner fail:", errors.ErrorStack(err)) | ||
} | ||
for { | ||
if statsOwner.IsOwner() { | ||
err := do.statsHandle.HandleAutoAnalyze(do.InfoSchema()) | ||
if err != nil { | ||
log.Error("[stats] auto analyze fail:", errors.ErrorStack(err)) | ||
} | ||
} | ||
time.Sleep(lease) | ||
} | ||
} | ||
|
||
const privilegeKey = "/tidb/privilege" | ||
|
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.
Add
ddl
as a constant.