Skip to content

Commit

Permalink
CLI: add a cli tool can delete all keys created by CDC in PD (pingcap…
Browse files Browse the repository at this point in the history
  • Loading branch information
leoppro authored Dec 13, 2019
1 parent 7f27685 commit 80ee381
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cdc/kv/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func GetEtcdKeyCaptureList() string {
return EtcdKeyBase + "/capture/info"
}

// ClearAllCDCInfo delete all keys created by CDC
func ClearAllCDCInfo(ctx context.Context, cli *clientv3.Client) error {
_, err := cli.Delete(ctx, EtcdKeyBase, clientv3.WithPrefix())
return errors.Trace(err)
}

// GetChangeFeeds returns kv revision and a map mapping from changefeedID to changefeed detail mvccpb.KeyValue
func GetChangeFeeds(ctx context.Context, cli *clientv3.Client, opts ...clientv3.OpOption) (int64, map[string]*mvccpb.KeyValue, error) {
key := GetEtcdKeyChangeFeedList()
Expand Down
16 changes: 16 additions & 0 deletions cdc/kv/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/embed"
"github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/ticdc/cdc/model"
"github.com/pingcap/ticdc/pkg/etcd"
"github.com/pingcap/ticdc/pkg/util"
Expand Down Expand Up @@ -84,6 +85,16 @@ func (s *etcdSuite) TestGetChangeFeeds(c *check.C) {
c.Assert(string(rawKv.Value), check.Equals, tc.details[i])
}
}
_, result, err := GetChangeFeeds(context.Background(), s.client)
c.Assert(err, check.IsNil)
c.Assert(len(result), check.Equals, 3)

err = ClearAllCDCInfo(context.Background(), s.client)
c.Assert(err, check.IsNil)

_, result, err = GetChangeFeeds(context.Background(), s.client)
c.Assert(err, check.IsNil)
c.Assert(len(result), check.Equals, 0)
}

func (s *etcdSuite) TestGetPutSubchangeFeed(c *check.C) {
Expand All @@ -105,4 +116,9 @@ func (s *etcdSuite) TestGetPutSubchangeFeed(c *check.C) {
_, getInfo, err := GetSubChangeFeedInfo(ctx, s.client, feedID, captureID)
c.Assert(err, check.IsNil)
c.Assert(getInfo, check.DeepEquals, info)

err = ClearAllCDCInfo(context.Background(), s.client)
c.Assert(err, check.IsNil)
_, _, err = GetSubChangeFeedInfo(ctx, s.client, feedID, captureID)
c.Assert(errors.Cause(err), check.Equals, model.ErrSubChangeFeedInfoNotExists)
}
4 changes: 4 additions & 0 deletions cmd/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const (
CtrlQueryCaptures = "query-capture-list"
// query subchangefeed replication status
CtrlQuerySubCf = "query-sub-cf"
// clear all key-values created by CDC
CtrlClearAll = "clear-all"
)

func init() {
Expand Down Expand Up @@ -119,6 +121,8 @@ var ctrlCmd = &cobra.Command{
return err
}
return jsonPrint(info)
case CtrlClearAll:
return kv.ClearAllCDCInfo(context.Background(), cli)
default:
fmt.Printf("unknown controller command: %s\n", ctrlCommand)
}
Expand Down

0 comments on commit 80ee381

Please sign in to comment.