github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/checkers/util/store.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 util
    16  
    17  // IDAllocator is used to fetch new replica ID.
    18  type IDAllocator interface {
    19  	// Next returns `false` when IDAllocator was exhausted temporarily.
    20  	Next() (uint64, bool)
    21  }
    22  
    23  type TestIDAllocator struct {
    24  	id uint64
    25  }
    26  
    27  func NewTestIDAllocator(startFrom uint64) *TestIDAllocator {
    28  	return &TestIDAllocator{id: startFrom}
    29  }
    30  
    31  func (a *TestIDAllocator) Next() (uint64, bool) {
    32  	a.id += 1
    33  	return a.id, true
    34  }
    35  
    36  // Store records metadata for tn store.
    37  type Store struct {
    38  	ID       string
    39  	Length   int
    40  	Capacity int
    41  }
    42  
    43  func NewStore(storeID string, length int, capacity int) *Store {
    44  	return &Store{
    45  		ID:       storeID,
    46  		Length:   length,
    47  		Capacity: capacity,
    48  	}
    49  }
    50  
    51  type StoreSlice []*Store
    52  
    53  func (ss StoreSlice) Contains(storeID string) bool {
    54  	for _, s := range ss {
    55  		if storeID == s.ID {
    56  			return true
    57  		}
    58  	}
    59  	return false
    60  }
    61  
    62  // ClusterStores collects stores by their status.
    63  type ClusterStores struct {
    64  	Working StoreSlice
    65  	Expired StoreSlice
    66  }
    67  
    68  func NewClusterStores() *ClusterStores {
    69  	return &ClusterStores{}
    70  }
    71  
    72  // RegisterWorking collects working stores.
    73  func (cs *ClusterStores) RegisterWorking(store *Store) {
    74  	cs.Working = append(cs.Working, store)
    75  }
    76  
    77  // RegisterExpired collects expired stores.
    78  func (cs *ClusterStores) RegisterExpired(store *Store) {
    79  	cs.Expired = append(cs.Expired, store)
    80  }
    81  
    82  // WorkingStores returns all recorded working stores.
    83  // NB: the returned order isn't deterministic.
    84  func (cs *ClusterStores) WorkingStores() StoreSlice {
    85  	return cs.Working
    86  }
    87  
    88  // ExpiredStores returns all recorded expired stores.
    89  // NB: the returned order isn't deterministic.
    90  func (cs *ClusterStores) ExpiredStores() StoreSlice {
    91  	return cs.Expired
    92  }