github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/ha/doc.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 ha 15 16 // Data need to be persisted for the HA scheduler. 17 // - configuration: 18 // - the upstream MySQL config (content of `SourceConfig`): 19 // - PUT when adding an upstream (`operate-mysql create`) by DM-master. 20 // - verify the validation before PUT it into etcd. 21 // - GET when scheduling the source to a DM-worker instance by DM-worker. 22 // - DELETE when removing an upstream (`operate-mysql stop`) by DM-master. 23 // - DELETE with `the expectant stage of the relay` in one txn. 24 // - DELETE with `the bound relationship between the DM-worker instance and the upstream MySQL source` in one txn. 25 // - TODO: UPDATE support with `the expectant stage of the relay`. 26 // - the data migration subtask config (content of `SubTaskConfig`): 27 // - PUT when starting a task (`start-task`) by DM-master. 28 // - verify the validation before PUT it into etcd. 29 // - PUT with `the expectant stage of the subtask` in one txn. 30 // - GET when starting a subtask by DM-worker. 31 // - DELETE when stopping a task (`stop-task`) by DM-master. 32 // - DELETE with `the expectant stage of the subtask` in one txn. 33 // - TODO: UPDATE support with `the expectant stage of the subtask`. 34 // 35 // - node information (name, address, etc.): 36 // - the DM-worker instance: 37 // - PUT when adding a DM-worker instance by DM-master. 38 // - GET only when restoring the in-memory information after the leader of DM-master changed by the new leader. 39 // - DELETE when removing a DM-worker instance by DM-master. 40 // - TODO: UPDATE support later. 41 // 42 // - the health status (or keep-alive) of component instances: 43 // - the DM-worker instance: 44 // - PUT (keep-alive) by DM-worker (when the node is healthy). 45 // - GET (through WATCH) by DM-master to know if another schedule needed. 46 // - DELETE (when the lease is timeout) by etcd (when the node is un-healthy). 47 // - no need to UPDATE it manually. 48 // 49 // - the running stage: 50 // - NOTE: persist the current stage of the relay and subtask if needed later. 51 // - the bound relationship between the DM-worker instance and the upstream MySQL source (including relevant relay and subtasks): 52 // - PUT when scheduling the source to a DM-worker instance by DM-master. 53 // - PUT with `the expectant stage of the relay` in one txn for the first time. 54 // - GET (through GET/WATCH) by DM-worker to know relevant relay/subtasks have to do. 55 // - DELETE when the bound DM-worker become offline. 56 // - DELETE when removing an upstream by DM-master. 57 // - DELETE with `the upstream MySQL config` in one txn. 58 // - DELETE with `the expectant stage of the relay` in one txn. 59 // - UPDATE when scheduling the source to another DM-worker instance by DM-master. 60 // - the expectant stage of the relay: 61 // - PUT when scheduling the source to a DM-worker instance by DM-master. 62 // - PUT with `the bound relationship between the DM-worker instance and the upstream MySQL source` in one txn for the first time. 63 // - GET (through GET/WATCH) by DM-worker to know how to update the current stage. 64 // - UPDATE when handling the user request (pause-relay/resume-relay) by DM-master. 65 // - DELETE when removing an upstream by DM-master. 66 // - DELETE with `the upstream MySQL config` in one txn. 67 // - DELETE with `the bound relationship between the DM-worker instance and the upstream MySQL source` in one txn. 68 // - the expectant stage of the subtask: 69 // - PUT/DELETE/UPDATE when handling the user request (start-task/stop-task/pause-task/resume-task) by DM-master. 70 // - GET (through GET/WATCH) by DM-worker to know how to update the current stage. 71 // 72 // The summary of the above: 73 // - only the DM-master WRITE schedule operations 74 // - NOTE: the DM-worker WRITE (PUT) its information and health status. 75 // - the DM-worker READ schedule operations and obey them. 76 // In other words, behaviors of the cluster are clear, that are decisions made by the DM-master. 77 // As long as the DM-worker can connect to the cluster, it must obey these decisions. 78 // If the DM-worker can't connect to the cluster, it must shutdown all operations. 79 // 80 // In this model, we use etcd as the command queue for communication between the DM-master and DM-worker instead of gRPC. 81 // 82 // One example of the workflow: 83 // 0. the user starts the DM-master cluster, and GET all history persisted data described above. 84 // - restore the in-memory status. 85 // 1. the user starts a DM-worker instance. 86 // - PUT DM-worker instance information into etcd. 87 // 2. DM-master GET the information of the DM-worker instance, and mark it as `free` status. 88 // 3. the user adds an upstream config. 89 // - PUT the config of the upstream into etcd. 90 // 4. DM-master schedules the upstream relevant operations to the free DM-worker. 91 // - PUT the bound relationship. 92 // - PUT the expectant stage of the relay if not exists. 93 // 5. DM-worker GET the bound relationship, the config of the upstream and the expectant stage of the relay. 94 // 6. DM-worker obey the expectant stage of the relay. 95 // - start relay (if error occurred, wait for the user to resolve it and do not re-schedule it to other DM-worker instances). 96 // 7. the user starts a data migration task. 97 // 8. DM-master PUT the data migration task config and the expectant stage of subtasks into etcd. 98 // 9. DM-worker GET the config of the subtask, the expectant stage of the subtask. 99 // 10. DM-worker obey the expectant stage of the subtask 100 // - start the subtask (if error occurred, wait for the user to resolve it). 101 // 11. the task keeps running for a period. 102 // 12. the user pauses the task. 103 // 13. DM-master PUT the expectant stage of the subtask. 104 // 14. DM-worker obey the expectant stage of the subtask. 105 // 15. the user resumes the task (DM-master and DM-worker handle it similar to pause the task). 106 // 16. the user stops the task. 107 // 17. DM-master DELETE the data migration task config and the expectant stage of subtasks in etcd. 108 // - DELETE the information before subtasks shutdown. 109 // 18. DM-worker stops the subtask. 110 // - NOTE: DM-worker should always stop the subtask if the expectant stage of the subtask is missing. 111 // 19. the relay of the DM-worker continues to run. 112 // 20. the user remove the upstream config. 113 // 21. DM-master DELETE the upstream MySQL config, the bound relationship and the expectant stage of the relay. 114 // 22. DM-worker shutdown. 115 // 23. the user marks the DM-worker as offline. 116 // - DELETE DM-worker instance information in etcd. 117 // 118 // when the DM-worker (with relay and subtasks) is down: 119 // 0. the status of the old DM-worker is un-health (keep-alive failed). 120 // 1. DM-master choose another DM-worker instance for failover. 121 // - DM-master can only schedule the source to another new DM-worker only after the old DM-worker shutdown, 122 // this may be achieved with some timeout/lease. 123 // 2. DM-master UPDATE the bound relationship to the new DM-worker. 124 // 3. the new DM-worker GET upstream config, the expectant stage of the relay and the expectant stage of the subtasks. 125 // 4. the new DM-worker obey the expectant stage. 126 // 127 // when the leader of the DM-master cluster changed: 128 // 0. the old DM-master shutdown its operation. 129 // 1. the new DM-master GET all history information to restore the in-memory status. 130 // 2. the new DM-master continue to handle user requests and scheduler for upstream sources. 131 // 132 // the operation for expectant stage (both for the relay and subtasks): 133 // - New: 134 // - not a valid expectant stage. 135 // - always mark the expectant stage as Running for the first create. 136 // - Running (schedule the source to the DM-worker, resume-relay or start-task, resume-task): 137 // - create and start if the relay/subtask instance not exists. 138 // - resume when in Paused currently. 139 // - invalid for other current stages, do nothing. 140 // - Paused (pause-relay or pause-task): 141 // - do nothing if the relay/subtask instance not exists. 142 // - pause when in Running currently. 143 // - invalid for other current stages, do nothing. 144 // - Stopped (stop-relay or stop-task): 145 // - never exists for expectant stage in etcd but DELETE the relevant information. 146 // - do nothing if the relay/subtask instance not exists. 147 // - stop if the relay/subtask instance exists. 148 // - Finished: 149 // - never exists for expectant stage in etcd.