github.com/matrixorigin/matrixone@v0.7.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  				return nil
    47  			} else {
    48  				operators = append(operators, op)
    49  				toAdd--
    50  			}
    51  		}
    52  	}
    53  
    54  	for shardID, toRemove := range stats.toRemove {
    55  		for _, toRemoveReplica := range toRemove {
    56  			if contains(removing[shardID], toRemoveReplica.replicaID) {
    57  				continue
    58  			}
    59  			if op, err := operator.CreateRemoveReplica(toRemoveReplica.uuid,
    60  				infos.Shards[toRemoveReplica.shardID]); err != nil {
    61  				return nil
    62  			} else {
    63  				operators = append(operators, op)
    64  			}
    65  		}
    66  	}
    67  
    68  	for _, toStart := range stats.toStart {
    69  		if contains(starting[toStart.shardID], toStart.replicaID) {
    70  			continue
    71  		}
    72  
    73  		operators = append(operators, operator.CreateStartReplica("",
    74  			toStart.uuid, toStart.shardID, toStart.replicaID))
    75  	}
    76  
    77  	for _, zombie := range stats.zombies {
    78  		operators = append(operators, operator.CreateKillZombie("",
    79  			zombie.uuid, zombie.shardID, zombie.replicaID))
    80  	}
    81  
    82  	if user.Username != "" {
    83  		for _, store := range working {
    84  			if !infos.Stores[store].TaskServiceCreated {
    85  				operators = append(operators, operator.CreateTaskServiceOp("",
    86  					store, pb.LogService, user))
    87  			}
    88  		}
    89  	}
    90  
    91  	return operators
    92  }
    93  
    94  func contains[T comparable](slice []T, v T) bool {
    95  	for i := range slice {
    96  		if slice[i] == v {
    97  			return true
    98  		}
    99  	}
   100  	return false
   101  }