Skip to content

Commit

Permalink
cmd: support delete gc ttl via cli (#652)
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Shen <[email protected]>
  • Loading branch information
overvenus authored Jun 15, 2020
1 parent 913c9ab commit 4590d79
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
5 changes: 3 additions & 2 deletions cdc/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ type Owner struct {
gcTTL int64
}

const cdcServiceSafePointID = "ticdc"
// CDCServiceSafePointID is the ID of CDC service in pd.UpdateServiceGCSafePoint.
const CDCServiceSafePointID = "ticdc"

// NewOwner creates a new Owner instance
func NewOwner(pdClient pd.Client, sess *concurrency.Session, gcTTL int64) (*Owner, error) {
Expand Down Expand Up @@ -417,7 +418,7 @@ func (o *Owner) flushChangeFeedInfos(ctx context.Context) error {
if err != nil {
return errors.Trace(err)
}
_, err = o.pdClient.UpdateServiceGCSafePoint(ctx, cdcServiceSafePointID, o.gcTTL, minCheckpointTs)
_, err = o.pdClient.UpdateServiceGCSafePoint(ctx, CDCServiceSafePointID, o.gcTTL, minCheckpointTs)
if err != nil {
log.Info("failed to update service safe point", zap.Error(err))
return errors.Trace(err)
Expand Down
53 changes: 51 additions & 2 deletions cmd/client_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
package cmd

import (
"fmt"
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/ticdc/cdc"
"github.com/spf13/cobra"
)

Expand All @@ -24,7 +29,9 @@ func newMetadataCommand() *cobra.Command {
}
command.AddCommand(
newDeleteMetaCommand(),
newDeleteGCTTLCommand(),
)
command.PersistentFlags().BoolVar(&noConfirm, "no-confirm", false, "Don't ask user whether to confirm executing meta command")
return command
}

Expand All @@ -33,13 +40,55 @@ func newDeleteMetaCommand() *cobra.Command {
Use: "delete",
Short: "Delete all meta data in etcd, confirm that you know what this command will do and use it at your own risk",
RunE: func(cmd *cobra.Command, args []string) error {
if err := confirmMetaDelete(cmd); err != nil {
return err
}
ctx := defaultContext
err := cdcEtcdCli.ClearAllCDCInfo(ctx)
if err != nil {
return errors.Trace(err)
}
_, err = pdCli.UpdateServiceGCSafePoint(ctx, cdc.CDCServiceSafePointID, 0, 0)
if err == nil {
cmd.Println("already truncate all meta in etcd!")
cmd.Println("all metadata truncated in PD!")
}
return err
return errors.Trace(err)
},
}
return command
}

func newDeleteGCTTLCommand() *cobra.Command {
command := &cobra.Command{
Use: "delete-gc-ttl",
Short: "Delete CDC GC TTL in PD, confirm that you know what this command will do and use it at your own risk",
RunE: func(cmd *cobra.Command, args []string) error {
if err := confirmMetaDelete(cmd); err != nil {
return err
}
ctx := defaultContext
_, err := pdCli.UpdateServiceGCSafePoint(ctx, cdc.CDCServiceSafePointID, 0, 0)
if err == nil {
cmd.Println("CDC GC TTL truncated in PD!")
}
return errors.Trace(err)
},
}
return command
}

func confirmMetaDelete(cmd *cobra.Command) error {
if noConfirm {
return nil
}
cmd.Printf("Confirm that you know what this command will do and use it at your own risk [Y/N]\n")
var yOrN string
_, err := fmt.Scan(&yOrN)
if err != nil {
return err
}
if strings.ToLower(strings.TrimSpace(yOrN)) != "y" {
return errors.NewNoStackError("abort meta command")
}
return nil
}
4 changes: 3 additions & 1 deletion tests/_utils/run_cdc_cli
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
set -eux

cdc.test -test.coverprofile="$OUT_DIR/cov.$TEST_NAME.cli.$$.out" cli "$@"

set +x
4 changes: 4 additions & 0 deletions tests/cli/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function run() {
exit 1
fi

# Smoke test meta delete-gc-ttl and delete
echo "y" | run_cdc_cli meta delete-gc-ttl
run_cdc_cli meta delete --no-confirm

cleanup_process $CDC_BINARY
}

Expand Down

0 comments on commit 4590d79

Please sign in to comment.