github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/checkers/logservice/check.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logservice
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/runtime"
    19  	"github.com/matrixorigin/matrixone/pkg/hakeeper"
    20  	"github.com/matrixorigin/matrixone/pkg/hakeeper/checkers/util"
    21  	"github.com/matrixorigin/matrixone/pkg/hakeeper/operator"
    22  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    23  	"go.uber.org/zap"
    24  )
    25  
    26  func Check(alloc util.IDAllocator, cfg hakeeper.Config, cluster pb.ClusterInfo, infos pb.LogState,
    27  	executing operator.ExecutingReplicas, user pb.TaskTableUser, currentTick uint64) (operators []*operator.Operator) {
    28  	working, expired := parseLogStores(cfg, infos, currentTick)
    29  	for _, node := range expired {
    30  		runtime.ProcessLevelRuntime().Logger().Info("node is expired", zap.String("uuid", node))
    31  	}
    32  	stats := parseLogShards(cluster, infos, expired)
    33  
    34  	removing := executing.Removing
    35  	adding := executing.Adding
    36  	starting := executing.Starting
    37  
    38  	for shardID, toAdd := range stats.toAdd {
    39  		for toAdd > uint32(len(adding[shardID])) {
    40  			bestStore := selectStore(infos.Shards[shardID], working)
    41  			newReplicaID, ok := alloc.Next()
    42  			if !ok {
    43  				return nil
    44  			}
    45  			if op, err := operator.CreateAddReplica(bestStore, infos.Shards[shardID], newReplicaID); err != nil {
    46  				runtime.ProcessLevelRuntime().Logger().Error("create add replica operator failed", zap.Error(err))
    47  				// may be no more stores, skip this shard
    48  				break
    49  			} else {
    50  				operators = append(operators, op)
    51  				toAdd--
    52  			}
    53  		}
    54  	}
    55  
    56  	for shardID, toRemove := range stats.toRemove {
    57  		for _, toRemoveReplica := range toRemove {
    58  			if contains(removing[shardID], toRemoveReplica.replicaID) {
    59  				continue
    60  			}
    61  			if op, err := operator.CreateRemoveReplica(toRemoveReplica.uuid,
    62  				infos.Shards[toRemoveReplica.shardID]); err != nil {
    63  				runtime.ProcessLevelRuntime().Logger().Error("create remove replica operator failed", zap.Error(err))
    64  				// skip this replica
    65  				continue
    66  			} else {
    67  				operators = append(operators, op)
    68  			}
    69  		}
    70  	}
    71  
    72  	for _, toStart := range stats.toStart {
    73  		if contains(starting[toStart.shardID], toStart.replicaID) {
    74  			continue
    75  		}
    76  
    77  		operators = append(operators, operator.CreateStartReplica("",
    78  			toStart.uuid, toStart.shardID, toStart.replicaID))
    79  	}
    80  
    81  	for _, zombie := range stats.zombies {
    82  		operators = append(operators, operator.CreateKillZombie("",
    83  			zombie.uuid, zombie.shardID, zombie.replicaID))
    84  	}
    85  
    86  	if user.Username != "" {
    87  		for _, store := range working {
    88  			if !infos.Stores[store].TaskServiceCreated {
    89  				operators = append(operators, operator.CreateTaskServiceOp("",
    90  					store, pb.LogService, user))
    91  			}
    92  		}
    93  	}
    94  
    95  	return operators
    96  }
    97  
    98  func contains[T comparable](slice []T, v T) bool {
    99  	for i := range slice {
   100  		if slice[i] == v {
   101  			return true
   102  		}
   103  	}
   104  	return false
   105  }