github.com/matrixorigin/matrixone@v1.2.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 ClusterState struct {
    30  	LogState   pb.LogState
    31  	TNState    pb.TNState
    32  	CNState    pb.CNState
    33  	ProxyState pb.ProxyState
    34  }
    35  
    36  type OpStep interface {
    37  	fmt.Stringer
    38  
    39  	IsFinish(state ClusterState) bool
    40  }
    41  
    42  type Replica struct {
    43  	UUID      string
    44  	ShardID   uint64
    45  	ReplicaID uint64
    46  	Epoch     uint64
    47  }
    48  
    49  type AddLogService struct {
    50  	Target string
    51  	Replica
    52  }
    53  
    54  func (a AddLogService) String() string {
    55  	return fmt.Sprintf("adding %v:%v(at epoch %v) to %s", a.ShardID, a.ReplicaID, a.Epoch, a.UUID)
    56  }
    57  
    58  func (a AddLogService) IsFinish(state ClusterState) bool {
    59  	if _, ok := state.LogState.Shards[a.ShardID]; !ok {
    60  		return true
    61  	}
    62  	if _, ok := state.LogState.Shards[a.ShardID].Replicas[a.ReplicaID]; ok {
    63  		return true
    64  	}
    65  
    66  	return false
    67  }
    68  
    69  type RemoveLogService struct {
    70  	Target string
    71  	Replica
    72  }
    73  
    74  func (a RemoveLogService) String() string {
    75  	return fmt.Sprintf("removing %v:%v(at epoch %v) on log store %s", a.ShardID, a.ReplicaID, a.Epoch, a.UUID)
    76  }
    77  
    78  func (a RemoveLogService) IsFinish(state ClusterState) bool {
    79  	if shard, ok := state.LogState.Shards[a.ShardID]; ok {
    80  		if _, ok := shard.Replicas[a.ReplicaID]; ok {
    81  			return false
    82  		}
    83  	}
    84  
    85  	return true
    86  }
    87  
    88  type StartLogService struct {
    89  	Replica
    90  }
    91  
    92  func (a StartLogService) String() string {
    93  	return fmt.Sprintf("starting %v:%v on %s", a.ShardID, a.ReplicaID, a.UUID)
    94  }
    95  
    96  func (a StartLogService) IsFinish(state ClusterState) bool {
    97  	if _, ok := state.LogState.Stores[a.UUID]; !ok {
    98  		return true
    99  	}
   100  	for _, replicaInfo := range state.LogState.Stores[a.UUID].Replicas {
   101  		if replicaInfo.ShardID == a.ShardID {
   102  			return true
   103  		}
   104  	}
   105  
   106  	return false
   107  }
   108  
   109  type StopLogService struct {
   110  	Replica
   111  }
   112  
   113  func (a StopLogService) String() string {
   114  	return fmt.Sprintf("stopping %v on %s", a.ShardID, a.UUID)
   115  }
   116  
   117  func (a StopLogService) IsFinish(state ClusterState) bool {
   118  	if store, ok := state.LogState.Stores[a.UUID]; ok {
   119  		for _, replicaInfo := range store.Replicas {
   120  			if replicaInfo.ShardID == a.ShardID {
   121  				return false
   122  			}
   123  		}
   124  	}
   125  
   126  	return true
   127  }
   128  
   129  type KillLogZombie struct {
   130  	Replica
   131  }
   132  
   133  func (a KillLogZombie) String() string {
   134  	return fmt.Sprintf("killing zombie on %s", a.UUID)
   135  }
   136  
   137  func (a KillLogZombie) IsFinish(state ClusterState) bool {
   138  	if store, ok := state.LogState.Stores[a.UUID]; ok {
   139  		for _, replicaInfo := range store.Replicas {
   140  			if replicaInfo.ShardID == a.ShardID {
   141  				return false
   142  			}
   143  		}
   144  	}
   145  
   146  	return true
   147  }
   148  
   149  type AddTnReplica struct {
   150  	StoreID            string
   151  	ShardID, ReplicaID uint64
   152  	LogShardID         uint64
   153  }
   154  
   155  func (a AddTnReplica) String() string {
   156  	return fmt.Sprintf("adding %v:%v to tn store %s (log shard %d)",
   157  		a.ShardID, a.ReplicaID, a.StoreID, a.LogShardID,
   158  	)
   159  }
   160  
   161  func (a AddTnReplica) IsFinish(state ClusterState) bool {
   162  	for _, info := range state.TNState.Stores[a.StoreID].Shards {
   163  		if a.ShardID == info.GetShardID() && a.ReplicaID == info.GetReplicaID() {
   164  			return true
   165  		}
   166  	}
   167  	return false
   168  }
   169  
   170  type RemoveTnReplica struct {
   171  	StoreID            string
   172  	ShardID, ReplicaID uint64
   173  	LogShardID         uint64
   174  }
   175  
   176  func (a RemoveTnReplica) String() string {
   177  	return fmt.Sprintf("removing %v:%v to tn store %s (log shard %d)",
   178  		a.ShardID, a.ReplicaID, a.StoreID, a.LogShardID,
   179  	)
   180  }
   181  
   182  func (a RemoveTnReplica) IsFinish(state ClusterState) bool {
   183  	for _, info := range state.TNState.Stores[a.StoreID].Shards {
   184  		if a.ShardID == info.GetShardID() && a.ReplicaID == info.GetReplicaID() {
   185  			return false
   186  		}
   187  	}
   188  	return true
   189  }
   190  
   191  // StopTnStore corresponds to tn store shutdown command.
   192  type StopTnStore struct {
   193  	StoreID string
   194  }
   195  
   196  func (a StopTnStore) String() string {
   197  	return fmt.Sprintf("stopping tn store %s", a.StoreID)
   198  }
   199  
   200  func (a StopTnStore) IsFinish(state ClusterState) bool {
   201  	if _, ok := state.TNState.Stores[a.StoreID]; ok {
   202  		return false
   203  	}
   204  	return true
   205  }
   206  
   207  // StopLogStore corresponds to log store shutdown command.
   208  type StopLogStore struct {
   209  	StoreID string
   210  }
   211  
   212  func (a StopLogStore) String() string {
   213  	return fmt.Sprintf("stopping log store %s", a.StoreID)
   214  }
   215  
   216  func (a StopLogStore) IsFinish(state ClusterState) bool {
   217  	if _, ok := state.LogState.Stores[a.StoreID]; ok {
   218  		return false
   219  	}
   220  	return true
   221  }
   222  
   223  type CreateTaskService struct {
   224  	StoreID   string
   225  	StoreType pb.ServiceType
   226  	TaskUser  pb.TaskTableUser
   227  }
   228  
   229  func (a CreateTaskService) String() string {
   230  	return fmt.Sprintf("create task service on %s(%s)", a.StoreID, a.StoreType)
   231  }
   232  
   233  func (a CreateTaskService) IsFinish(state ClusterState) bool {
   234  	if state, ok := state.LogState.Stores[a.StoreID]; ok {
   235  		return state.GetTaskServiceCreated()
   236  	}
   237  
   238  	if state, ok := state.TNState.Stores[a.StoreID]; ok {
   239  		return state.GetTaskServiceCreated()
   240  	}
   241  
   242  	if state, ok := state.CNState.Stores[a.StoreID]; ok {
   243  		return state.GetTaskServiceCreated()
   244  	}
   245  
   246  	return true
   247  }
   248  
   249  type DeleteCNStore struct {
   250  	StoreID string
   251  }
   252  
   253  func (a DeleteCNStore) String() string {
   254  	return fmt.Sprintf("deleting cn store %s", a.StoreID)
   255  }
   256  
   257  func (a DeleteCNStore) IsFinish(state ClusterState) bool {
   258  	if _, ok := state.CNState.Stores[a.StoreID]; ok {
   259  		return false
   260  	}
   261  	return true
   262  }
   263  
   264  type JoinGossipCluster struct {
   265  	StoreID  string
   266  	Existing []string
   267  }
   268  
   269  func (a JoinGossipCluster) String() string {
   270  	return fmt.Sprintf("join gossip cluster for %s", a.StoreID)
   271  }
   272  
   273  func (a JoinGossipCluster) IsFinish(state ClusterState) bool {
   274  	if state, ok := state.CNState.Stores[a.StoreID]; ok {
   275  		return state.GetGossipJoined()
   276  	}
   277  	return true
   278  }
   279  
   280  type DeleteProxyStore struct {
   281  	StoreID string
   282  }
   283  
   284  func (a DeleteProxyStore) String() string {
   285  	return fmt.Sprintf("deleting proxy store %s", a.StoreID)
   286  }
   287  
   288  func (a DeleteProxyStore) IsFinish(state ClusterState) bool {
   289  	if _, ok := state.ProxyState.Stores[a.StoreID]; ok {
   290  		return false
   291  	}
   292  	return true
   293  }