github.com/matrixorigin/matrixone@v1.2.0/pkg/tests/service/utility.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 service
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/hakeeper"
    19  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    20  )
    21  
    22  const (
    23  	// The expected number of tn replicas.
    24  	TNShardExpectedSize = 1
    25  )
    26  
    27  // ParseExpectedTNShardCount returns the expected count of tn shards.
    28  func ParseExpectedTNShardCount(cluster pb.ClusterInfo) int {
    29  	set := make(map[uint64]struct{})
    30  	for _, record := range cluster.TNShards {
    31  		set[record.ShardID] = struct{}{}
    32  
    33  	}
    34  	return len(set)
    35  }
    36  
    37  // ParseExpectedLogShardCount returns the expected count of log shards.
    38  func ParseExpectedLogShardCount(cluster pb.ClusterInfo) int {
    39  	set := make(map[uint64]struct{})
    40  	for _, record := range cluster.LogShards {
    41  		set[record.ShardID] = struct{}{}
    42  
    43  	}
    44  	return len(set)
    45  }
    46  
    47  // ParseReportedTNShardCount returns the reported count of tn shards.
    48  func ParseReportedTNShardCount(
    49  	state pb.TNState, hkcfg hakeeper.Config, currTick uint64,
    50  ) int {
    51  	set := make(map[uint64]struct{})
    52  	for _, storeInfo := range state.Stores {
    53  		// ignore expired tn stores
    54  		if hkcfg.TNStoreExpired(storeInfo.Tick, currTick) {
    55  			continue
    56  		}
    57  
    58  		// record tn shard
    59  		for _, shardInfo := range storeInfo.Shards {
    60  			set[shardInfo.ShardID] = struct{}{}
    61  		}
    62  	}
    63  	return len(set)
    64  }
    65  
    66  // ParseReportedLogShardCount returns the reported count of log shards.
    67  func ParseReportedLogShardCount(
    68  	state pb.LogState, hkcfg hakeeper.Config, currTick uint64,
    69  ) int {
    70  	set := make(map[uint64]struct{})
    71  	for _, storeInfo := range state.Stores {
    72  		// ignore expired log stores
    73  		if hkcfg.LogStoreExpired(storeInfo.Tick, currTick) {
    74  			continue
    75  		}
    76  
    77  		for _, replicaInfo := range storeInfo.Replicas {
    78  			set[replicaInfo.ShardID] = struct{}{}
    79  		}
    80  	}
    81  	return len(set)
    82  }
    83  
    84  // ParseLogShardExpectedSize returns the expected count of log replicas.
    85  func ParseLogShardExpectedSize(shardID uint64, cluster pb.ClusterInfo) int {
    86  	for _, record := range cluster.LogShards {
    87  		if record.ShardID == shardID {
    88  			return int(record.NumberOfReplicas)
    89  		}
    90  	}
    91  	return 0
    92  }
    93  
    94  // ParseLogShardReportedSize returns the reported count of log replicas.
    95  func ParseLogShardReportedSize(
    96  	shardID uint64, state pb.LogState, hkcfg hakeeper.Config, currTick uint64,
    97  ) int {
    98  	set := make(map[uint64]struct{})
    99  	for _, storeInfo := range state.Stores {
   100  		// ignore expired log stores
   101  		if hkcfg.LogStoreExpired(storeInfo.Tick, currTick) {
   102  			continue
   103  		}
   104  
   105  		for _, replicaInfo := range storeInfo.Replicas {
   106  			if replicaInfo.ShardID == shardID {
   107  				set[replicaInfo.ReplicaID] = struct{}{}
   108  			}
   109  		}
   110  	}
   111  	return len(set)
   112  }
   113  
   114  // ParseTNShardReportedSize returns the reported count of tn replicas.
   115  func ParseTNShardReportedSize(
   116  	shardID uint64, state pb.TNState, hkcfg hakeeper.Config, currTick uint64,
   117  ) int {
   118  	set := make(map[uint64]struct{})
   119  	for _, storeInfo := range state.Stores {
   120  		// ignore expired tn stores
   121  		if hkcfg.TNStoreExpired(storeInfo.Tick, currTick) {
   122  			continue
   123  		}
   124  
   125  		for _, shardInfo := range storeInfo.Shards {
   126  			if shardInfo.ShardID == shardID {
   127  				set[shardInfo.ReplicaID] = struct{}{}
   128  			}
   129  		}
   130  	}
   131  	return len(set)
   132  }