github.com/matrixorigin/matrixone@v0.7.0/pkg/hakeeper/operator/steps.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 // Portions of this file are additionally subject to the following 15 // copyright. 16 // 17 // Copyright (C) 2021 Matrix Origin. 18 // 19 // Modified the behavior and the interface of the step. 20 21 package operator 22 23 import ( 24 "fmt" 25 26 pb "github.com/matrixorigin/matrixone/pkg/pb/logservice" 27 ) 28 29 type OpStep interface { 30 fmt.Stringer 31 32 IsFinish(state pb.LogState, dnState pb.DNState, cnState pb.CNState) bool 33 } 34 35 type Replica struct { 36 UUID string 37 ShardID uint64 38 ReplicaID uint64 39 Epoch uint64 40 } 41 42 type AddLogService struct { 43 Target string 44 Replica 45 } 46 47 func (a AddLogService) String() string { 48 return fmt.Sprintf("adding %v:%v(at epoch %v) to %s", a.ShardID, a.ReplicaID, a.Epoch, a.UUID) 49 } 50 51 func (a AddLogService) IsFinish(state pb.LogState, _ pb.DNState, _ pb.CNState) bool { 52 if _, ok := state.Shards[a.ShardID]; !ok { 53 return true 54 } 55 if _, ok := state.Shards[a.ShardID].Replicas[a.ReplicaID]; ok { 56 return true 57 } 58 59 return false 60 } 61 62 type RemoveLogService struct { 63 Target string 64 Replica 65 } 66 67 func (a RemoveLogService) String() string { 68 return fmt.Sprintf("removing %v:%v(at epoch %v) on log store %s", a.ShardID, a.ReplicaID, a.Epoch, a.UUID) 69 } 70 71 func (a RemoveLogService) IsFinish(state pb.LogState, _ pb.DNState, _ pb.CNState) bool { 72 if shard, ok := state.Shards[a.ShardID]; ok { 73 if _, ok := shard.Replicas[a.ReplicaID]; ok { 74 return false 75 } 76 } 77 78 return true 79 } 80 81 type StartLogService struct { 82 Replica 83 } 84 85 func (a StartLogService) String() string { 86 return fmt.Sprintf("starting %v:%v on %s", a.ShardID, a.ReplicaID, a.UUID) 87 } 88 89 func (a StartLogService) IsFinish(state pb.LogState, _ pb.DNState, _ pb.CNState) bool { 90 if _, ok := state.Stores[a.UUID]; !ok { 91 return true 92 } 93 for _, replicaInfo := range state.Stores[a.UUID].Replicas { 94 if replicaInfo.ShardID == a.ShardID { 95 return true 96 } 97 } 98 99 return false 100 } 101 102 type StopLogService struct { 103 Replica 104 } 105 106 func (a StopLogService) String() string { 107 return fmt.Sprintf("stopping %v on %s", a.ShardID, a.UUID) 108 } 109 110 func (a StopLogService) IsFinish(state pb.LogState, _ pb.DNState, _ pb.CNState) bool { 111 if store, ok := state.Stores[a.UUID]; ok { 112 for _, replicaInfo := range store.Replicas { 113 if replicaInfo.ShardID == a.ShardID { 114 return false 115 } 116 } 117 } 118 119 return true 120 } 121 122 type KillLogZombie struct { 123 Replica 124 } 125 126 func (a KillLogZombie) String() string { 127 return fmt.Sprintf("killing zombie on %s", a.UUID) 128 } 129 130 func (a KillLogZombie) IsFinish(state pb.LogState, _ pb.DNState, _ pb.CNState) bool { 131 if store, ok := state.Stores[a.UUID]; ok { 132 for _, replicaInfo := range store.Replicas { 133 if replicaInfo.ShardID == a.ShardID { 134 return false 135 } 136 } 137 } 138 139 return true 140 } 141 142 type AddDnReplica struct { 143 StoreID string 144 ShardID, ReplicaID uint64 145 LogShardID uint64 146 } 147 148 func (a AddDnReplica) String() string { 149 return fmt.Sprintf("adding %v:%v to dn store %s (log shard %d)", 150 a.ShardID, a.ReplicaID, a.StoreID, a.LogShardID, 151 ) 152 } 153 154 func (a AddDnReplica) IsFinish(_ pb.LogState, state pb.DNState, _ pb.CNState) bool { 155 for _, info := range state.Stores[a.StoreID].Shards { 156 if a.ShardID == info.GetShardID() && a.ReplicaID == info.GetReplicaID() { 157 return true 158 } 159 } 160 return false 161 } 162 163 type RemoveDnReplica struct { 164 StoreID string 165 ShardID, ReplicaID uint64 166 LogShardID uint64 167 } 168 169 func (a RemoveDnReplica) String() string { 170 return fmt.Sprintf("removing %v:%v to dn store %s (log shard %d)", 171 a.ShardID, a.ReplicaID, a.StoreID, a.LogShardID, 172 ) 173 } 174 175 func (a RemoveDnReplica) IsFinish(_ pb.LogState, state pb.DNState, _ pb.CNState) bool { 176 for _, info := range state.Stores[a.StoreID].Shards { 177 if a.ShardID == info.GetShardID() && a.ReplicaID == info.GetReplicaID() { 178 return false 179 } 180 } 181 return true 182 } 183 184 // StopDnStore corresponds to dn store shutdown command. 185 type StopDnStore struct { 186 StoreID string 187 } 188 189 func (a StopDnStore) String() string { 190 return fmt.Sprintf("stopping dn store %s", a.StoreID) 191 } 192 193 func (a StopDnStore) IsFinish(_ pb.LogState, state pb.DNState, _ pb.CNState) bool { 194 if _, ok := state.Stores[a.StoreID]; ok { 195 return false 196 } 197 return true 198 } 199 200 // StopLogStore corresponds to log store shutdown command. 201 type StopLogStore struct { 202 StoreID string 203 } 204 205 func (a StopLogStore) String() string { 206 return fmt.Sprintf("stopping log store %s", a.StoreID) 207 } 208 209 func (a StopLogStore) IsFinish(state pb.LogState, _ pb.DNState, _ pb.CNState) bool { 210 if _, ok := state.Stores[a.StoreID]; ok { 211 return false 212 } 213 return true 214 } 215 216 type CreateTaskService struct { 217 StoreID string 218 StoreType pb.ServiceType 219 TaskUser pb.TaskTableUser 220 } 221 222 func (a CreateTaskService) String() string { 223 return fmt.Sprintf("create task service on %s(%s)", a.StoreID, a.StoreType) 224 } 225 226 func (a CreateTaskService) IsFinish(logState pb.LogState, dnState pb.DNState, cnState pb.CNState) bool { 227 if state, ok := logState.Stores[a.StoreID]; ok { 228 return state.GetTaskServiceCreated() 229 } 230 231 if state, ok := dnState.Stores[a.StoreID]; ok { 232 return state.GetTaskServiceCreated() 233 } 234 235 if state, ok := cnState.Stores[a.StoreID]; ok { 236 return state.GetTaskServiceCreated() 237 } 238 239 return true 240 } 241 242 type DeleteCNStore struct { 243 StoreID string 244 } 245 246 func (a DeleteCNStore) String() string { 247 return fmt.Sprintf("deleting cn store %s", a.StoreID) 248 } 249 250 func (a DeleteCNStore) IsFinish(_ pb.LogState, _ pb.DNState, state pb.CNState) bool { 251 if _, ok := state.Stores[a.StoreID]; ok { 252 return false 253 } 254 return true 255 }