vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/durability.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package inst
    18  
    19  import (
    20  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    21  	"vitess.io/vitess/go/vt/vtctl/reparentutil"
    22  	"vitess.io/vitess/go/vt/vtctl/reparentutil/promotionrule"
    23  )
    24  
    25  // IsReplicaSemiSync returns the replica semi-sync setting for the instance.
    26  func IsReplicaSemiSync[V InstanceKey | *topodatapb.Tablet](durabilityPolicy reparentutil.Durabler, primaryInstance V, replicaInstance V) bool {
    27  	primary, err := getTablet(primaryInstance)
    28  	if err != nil {
    29  		return false
    30  	}
    31  	replica, err := getTablet(replicaInstance)
    32  	if err != nil {
    33  		return false
    34  	}
    35  	return reparentutil.IsReplicaSemiSync(durabilityPolicy, primary, replica)
    36  }
    37  
    38  // SemiSyncAckers returns the primary semi-sync setting for the instance.
    39  // 0 means none. Non-zero specifies the number of required ackers.
    40  func SemiSyncAckers[V InstanceKey | *topodatapb.Tablet](durabilityPolicy reparentutil.Durabler, instance V) int {
    41  	primary, err := getTablet(instance)
    42  	if err != nil {
    43  		return 0
    44  	}
    45  	return reparentutil.SemiSyncAckers(durabilityPolicy, primary)
    46  }
    47  
    48  // PromotionRule returns the promotion rule for the instance.
    49  func PromotionRule[V InstanceKey | *topodatapb.Tablet](durabilityPolicy reparentutil.Durabler, instance V) promotionrule.CandidatePromotionRule {
    50  	tablet, err := getTablet(instance)
    51  	if err != nil {
    52  		return promotionrule.MustNot
    53  	}
    54  	return reparentutil.PromotionRule(durabilityPolicy, tablet)
    55  }
    56  
    57  func getTablet[V InstanceKey | *topodatapb.Tablet](instance V) (*topodatapb.Tablet, error) {
    58  	var instanceTablet *topodatapb.Tablet
    59  	var err error
    60  	switch node := any(instance).(type) {
    61  	case InstanceKey:
    62  		instanceTablet, err = ReadTablet(node)
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  	case *topodatapb.Tablet:
    67  		instanceTablet = node
    68  	}
    69  	return instanceTablet, nil
    70  }
    71  
    72  // GetDurabilityPolicy gets the durability policy for the keyspace of the given instance
    73  func GetDurabilityPolicy[V InstanceKey | *topodatapb.Tablet](instance V) (reparentutil.Durabler, error) {
    74  	tablet, err := getTablet(instance)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	ki, err := ReadKeyspace(tablet.Keyspace)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	return reparentutil.GetDurabilityPolicy(ki.DurabilityPolicy)
    83  }