From dd3ca2285446a767ee50ef9061535ad49d3cbf03 Mon Sep 17 00:00:00 2001 From: aliiohs Date: Thu, 21 Mar 2019 06:04:08 -0500 Subject: [PATCH] planner, executor: support Change and ChangeExec (#9789) * support update pump or drainer status --- executor/builder.go | 8 +++++ executor/change.go | 60 ++++++++++++++++++++++++++++++++++++ executor/compiler.go | 2 ++ go.mod | 4 +-- go.sum | 9 +++--- planner/core/common_plans.go | 6 ++++ planner/core/planbuilder.go | 9 ++++++ 7 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 executor/change.go diff --git a/executor/builder.go b/executor/builder.go index 24cf169cfad41..6d8791255aec9 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -78,6 +78,8 @@ func (b *executorBuilder) build(p plannercore.Plan) Executor { switch v := p.(type) { case nil: return nil + case *plannercore.Change: + return b.buildChange(v) case *plannercore.CheckTable: return b.buildCheckTable(v) case *plannercore.CheckIndex: @@ -207,6 +209,12 @@ func (b *executorBuilder) buildCancelDDLJobs(v *plannercore.CancelDDLJobs) Execu return e } +func (b *executorBuilder) buildChange(v *plannercore.Change) Executor { + return &ChangeExec{ + ChangeStmt: v.ChangeStmt, + } +} + func (b *executorBuilder) buildShowNextRowID(v *plannercore.ShowNextRowID) Executor { e := &ShowNextRowIDExec{ baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ExplainID()), diff --git a/executor/change.go b/executor/change.go new file mode 100644 index 0000000000000..dbf73b0c9b9e0 --- /dev/null +++ b/executor/change.go @@ -0,0 +1,60 @@ +// Copyright 2019 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package executor + +import ( + "context" + "strings" + + "github.com/pingcap/errors" + "github.com/pingcap/parser/ast" + "github.com/pingcap/tidb-tools/tidb-binlog/node" + "github.com/pingcap/tidb/config" + "github.com/pingcap/tidb/util/chunk" +) + +// ChangeExec represents a change executor. +type ChangeExec struct { + baseExecutor + *ast.ChangeStmt +} + +// Next implements the Executor Next interface. +func (e *ChangeExec) Next(ctx context.Context, req *chunk.RecordBatch) error { + kind := strings.ToLower(e.NodeType) + urls := config.GetGlobalConfig().Path + registry, err := createRegistry(urls) + if err != nil { + return err + } + nodes, _, err := registry.Nodes(ctx, node.NodePrefix[kind]) + if err != nil { + return err + } + state := e.State + nodeID := e.NodeID + for _, n := range nodes { + if n.NodeID != nodeID { + continue + } + switch state { + case node.Online, node.Pausing, node.Paused, node.Closing, node.Offline: + n.State = state + return registry.UpdateNode(ctx, node.NodePrefix[kind], n) + default: + return errors.Errorf("state %s is illegal", state) + } + } + return errors.NotFoundf("node %s, id %s from etcd %s", kind, nodeID, urls) +} diff --git a/executor/compiler.go b/executor/compiler.go index b9bf046ae2d4d..840b2a9d2d0b5 100644 --- a/executor/compiler.go +++ b/executor/compiler.go @@ -242,6 +242,8 @@ func GetStmtLabel(stmtNode ast.StmtNode) string { return "AnalyzeTable" case *ast.BeginStmt: return "Begin" + case *ast.ChangeStmt: + return "Change" case *ast.CommitStmt: return "Commit" case *ast.CreateDatabaseStmt: diff --git a/go.mod b/go.mod index fcd39ba6f7103..360f37df49dcb 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,7 @@ require ( github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562 github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 - github.com/pingcap/parser v0.0.0-20190320053247-fe243e3280cf + github.com/pingcap/parser v0.0.0-20190321052000-f9a452f8f24e github.com/pingcap/pd v2.1.0-rc.4+incompatible github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible github.com/pingcap/tipb v0.0.0-20190107072121-abbec73437b7 @@ -82,7 +82,7 @@ require ( golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb // indirect golang.org/x/text v0.3.0 - golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect + golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect golang.org/x/tools v0.0.0-20190130214255-bb1329dc71a0 google.golang.org/genproto v0.0.0-20190108161440-ae2f86662275 // indirect google.golang.org/grpc v1.17.0 diff --git a/go.sum b/go.sum index f77fa64d84138..c0d55b7ffa1f3 100644 --- a/go.sum +++ b/go.sum @@ -107,7 +107,6 @@ github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ= github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8 h1:USx2/E1bX46VG32FIw034Au6seQ2fY9NEILmNh/UlQg= github.com/pingcap/check v0.0.0-20190102082844-67f458068fc8/go.mod h1:B1+S9LNcuMyLH/4HMTViQOJevkGiik3wW2AN9zb2fNQ= -github.com/pingcap/errors v0.11.0 h1:DCJQB8jrHbQ1VVlMFIrbj2ApScNNotVmkSNplu2yUt4= github.com/pingcap/errors v0.11.0/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pingcap/errors v0.11.1 h1:BXFZ6MdDd2U1uJUa2sRAWTmm+nieEzuyYM0R4aUTcC8= github.com/pingcap/errors v0.11.1/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= @@ -119,8 +118,8 @@ github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562 h1:32oF1/8lVnBR2JV github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562/go.mod h1:QMdbTAXCHzzygQzqcG9uVUgU2fKeSN1GmfMiykdSzzY= github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ= github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw= -github.com/pingcap/parser v0.0.0-20190320053247-fe243e3280cf h1:yxK78TmeSK3BIm8Z8SwdZLVzRpY80HZe1VMlA2dL648= -github.com/pingcap/parser v0.0.0-20190320053247-fe243e3280cf/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= +github.com/pingcap/parser v0.0.0-20190321052000-f9a452f8f24e h1:Evw2H5BmAGqHTKbbcrGXBuOq9I02w3iVn/e7yHR+zvg= +github.com/pingcap/parser v0.0.0-20190321052000-f9a452f8f24e/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= github.com/pingcap/pd v2.1.0-rc.4+incompatible h1:/buwGk04aHO5odk/+O8ZOXGs4qkUjYTJ2UpCJXna8NE= github.com/pingcap/pd v2.1.0-rc.4+incompatible/go.mod h1:nD3+EoYes4+aNNODO99ES59V83MZSI+dFbhyr667a0E= github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible h1:e9Gi/LP9181HT3gBfSOeSBA+5JfemuE4aEAhqNgoE4k= @@ -209,8 +208,8 @@ golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb h1:1w588/yEchbPNpa9sEvOcMZYb golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg= -golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 h1:SvFZT6jyqRaOeXpc5h/JSfZenJ2O330aBsf7JfSUXmQ= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190130214255-bb1329dc71a0 h1:iRpjPej1fPzmfoBhMFkp3HdqzF+ytPmAwiQhJGV0zGw= golang.org/x/tools v0.0.0-20190130214255-bb1329dc71a0/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/planner/core/common_plans.go b/planner/core/common_plans.go index f976ce42cc392..912103f0f98b9 100644 --- a/planner/core/common_plans.go +++ b/planner/core/common_plans.go @@ -133,6 +133,12 @@ type CancelDDLJobs struct { JobIDs []int64 } +// Change represents a change plan. +type Change struct { + baseSchemaProducer + *ast.ChangeStmt +} + // Prepare represents prepare plan. type Prepare struct { baseSchemaProducer diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index ddc35c3ea43db..f3081373691bf 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -217,10 +217,19 @@ func (b *PlanBuilder) Build(node ast.Node) (Plan, error) { return b.buildSimple(node.(ast.StmtNode)) case ast.DDLNode: return b.buildDDL(x) + case *ast.ChangeStmt: + return b.buildChange(x) } return nil, ErrUnsupportedType.GenWithStack("Unsupported type %T", node) } +func (b *PlanBuilder) buildChange(v *ast.ChangeStmt) (Plan, error) { + exe := &Change{ + ChangeStmt: v, + } + return exe, nil +} + func (b *PlanBuilder) buildExecute(v *ast.ExecuteStmt) (Plan, error) { vars := make([]expression.Expression, 0, len(v.UsingVars)) for _, expr := range v.UsingVars {