github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/shardddl/optimism/ops.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package optimism 15 16 import ( 17 "github.com/pingcap/tiflow/dm/common" 18 "github.com/pingcap/tiflow/dm/pkg/etcdutil" 19 clientv3 "go.etcd.io/etcd/client/v3" 20 ) 21 22 // DeleteInfosOperationsColumns deletes the shard DDL infos, operations, and dropped columns in etcd. 23 // This function should often be called by DM-master when removing the lock. 24 // Only delete when all info's version are greater or equal to etcd's version, otherwise it means new info was putted into etcd before. 25 func DeleteInfosOperationsColumns(cli *clientv3.Client, infos []Info, ops []Operation, lockID string) (int64, bool, error) { 26 opsDel := make([]clientv3.Op, 0, len(infos)+len(ops)) 27 cmps := make([]clientv3.Cmp, 0, len(infos)) 28 for _, info := range infos { 29 key := common.ShardDDLOptimismInfoKeyAdapter.Encode(info.Task, info.Source, info.UpSchema, info.UpTable) 30 cmps = append(cmps, clientv3.Compare(clientv3.Version(key), "<", info.Version+1)) 31 opsDel = append(opsDel, deleteInfoOp(info)) 32 } 33 for _, op := range ops { 34 opsDel = append(opsDel, deleteOperationOp(op)) 35 } 36 opsDel = append(opsDel, deleteDroppedColumnsByLockOp(lockID)) 37 resp, rev, err := etcdutil.DoTxnWithRepeatable(cli, etcdutil.FullOpFunc(cmps, opsDel, []clientv3.Op{})) 38 if err != nil { 39 return 0, false, err 40 } 41 return rev, resp.Succeeded, nil 42 } 43 44 // DeleteInfosOperationsTablesByTask deletes the shard DDL infos and operations in etcd. 45 // This function should often be called by DM-master when stop a task for all sources. 46 func DeleteInfosOperationsTablesByTask(cli *clientv3.Client, task string, lockIDSet map[string]struct{}) (int64, error) { 47 opsDel := make([]clientv3.Op, 0, 5) 48 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismInfoKeyAdapter.Encode(task), clientv3.WithPrefix())) 49 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismOperationKeyAdapter.Encode(task), clientv3.WithPrefix())) 50 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismSourceTablesKeyAdapter.Encode(task), clientv3.WithPrefix())) 51 for lockID := range lockIDSet { 52 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismDroppedColumnsKeyAdapter.Encode(lockID), clientv3.WithPrefix())) 53 } 54 _, rev, err := etcdutil.DoTxnWithRepeatable(cli, etcdutil.ThenOpFunc(opsDel...)) 55 return rev, err 56 } 57 58 // DeleteInfosOperationsTablesByTaskAndSource deletes the shard DDL infos and operations in etcd by task and source. 59 // This function should often be called by DM-master when stop a task for sources. 60 func DeleteInfosOperationsTablesByTaskAndSource(cli *clientv3.Client, task string, sources []string, dropColumns map[string][]string) (int64, error) { 61 opsDel := make([]clientv3.Op, 0, 5) 62 for _, source := range sources { 63 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismInfoKeyAdapter.Encode(task, source), clientv3.WithPrefix())) 64 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismOperationKeyAdapter.Encode(task, source), clientv3.WithPrefix())) 65 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismSourceTablesKeyAdapter.Encode(task, source), clientv3.WithPrefix())) 66 for lockID, cols := range dropColumns { 67 for _, col := range cols { 68 for _, source := range sources { 69 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismDroppedColumnsKeyAdapter.Encode(lockID, col, source), clientv3.WithPrefix())) 70 } 71 } 72 } 73 } 74 _, rev, err := etcdutil.DoTxnWithRepeatable(cli, etcdutil.ThenOpFunc(opsDel...)) 75 return rev, err 76 } 77 78 // DeleteInfosOperationsTablesByTable deletes the shard DDL infos and operations in etcd by table 79 // This function should often be called by DM-master when drop a table. 80 func DeleteInfosOperationsTablesByTable(cli *clientv3.Client, task, source, upSchema, upTable, lockID string, dropCols []string) (int64, error) { 81 opsDel := make([]clientv3.Op, 0, 5) 82 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismInfoKeyAdapter.Encode(task, source, upSchema, upTable))) 83 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismOperationKeyAdapter.Encode(task, source, upSchema, upTable))) 84 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismSourceTablesKeyAdapter.Encode(task, source, upSchema, upTable))) 85 for _, col := range dropCols { 86 opsDel = append(opsDel, clientv3.OpDelete(common.ShardDDLOptimismDroppedColumnsKeyAdapter.Encode(lockID, col, source, upSchema, upTable))) 87 } 88 _, rev, err := etcdutil.DoTxnWithRepeatable(cli, etcdutil.ThenOpFunc(opsDel...)) 89 return rev, err 90 }