|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +// Package faulttolerance provides visibility into the cluster's ability |
| 7 | +// to tolerate failures across different failure domains. |
| 8 | +package faulttolerance |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "iter" |
| 13 | + "maps" |
| 14 | + "slices" |
| 15 | + "strings" |
| 16 | + |
| 17 | + "github.com/cockroachdb/cockroach/pkg/gossip" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/kv/kvserver" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/roachpb" |
| 21 | + "github.com/cockroachdb/cockroach/pkg/util/protoutil" |
| 22 | + "github.com/cockroachdb/errors" |
| 23 | +) |
| 24 | + |
| 25 | +// LocalityMapsFromGossip constructs a map from each NodeID to its |
| 26 | +// Locality, by deserializing the node descriptors from Gossip. |
| 27 | +func LocalityMapsFromGossip(g *gossip.Gossip) (map[roachpb.NodeID]roachpb.Locality, error) { |
| 28 | + nodeLocality := make(map[roachpb.NodeID]roachpb.Locality) |
| 29 | + if err := g.IterateInfos(gossip.KeyNodeDescPrefix, func(key string, info gossip.Info) error { |
| 30 | + bs, err := info.Value.GetBytes() |
| 31 | + if err != nil { |
| 32 | + return errors.NewAssertionErrorWithWrappedErrf(err, |
| 33 | + "failed to extract bytes for key %q", key) |
| 34 | + } |
| 35 | + |
| 36 | + var d roachpb.NodeDescriptor |
| 37 | + if err := protoutil.Unmarshal(bs, &d); err != nil { |
| 38 | + return errors.NewAssertionErrorWithWrappedErrf(err, |
| 39 | + "failed to parse value for key %q", key) |
| 40 | + } |
| 41 | + |
| 42 | + // Don't use node descriptors with NodeID 0, because that's meant to |
| 43 | + // indicate that the node has been removed from the cluster. |
| 44 | + if d.NodeID != 0 { |
| 45 | + nodeLocality[d.NodeID] = d.Locality |
| 46 | + } |
| 47 | + return nil |
| 48 | + }); err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return nodeLocality, nil |
| 52 | +} |
| 53 | + |
| 54 | +// ComputeFaultTolerance computes the fault tolerance margin for each |
| 55 | +// failure domain. A failure domain is a set of nodes that map to a |
| 56 | +// single locality in the nodeLocality map, or a merged set of nodes |
| 57 | +// that share a locality prefix. The fault tolerance margin is the |
| 58 | +// number of additional nodes (beyond the nodes in the failure domain) |
| 59 | +// that can fail before any range experiences unavailability. |
| 60 | +// |
| 61 | +// Margins can be negative (indicating that the failure domain is |
| 62 | +// critical; its failure will cause unavailability), 0 (the failure |
| 63 | +// domain is not critical, but no additional node failures can be |
| 64 | +// tolerated), or positive. |
| 65 | +// |
| 66 | +// The keys in the returned map are locality strings, as returned by |
| 67 | +// `roachpb.Locality.String()`. They include "parent localities" as |
| 68 | +// well as the actual leaf localities to which nodes are assigned. |
| 69 | +// |
| 70 | +// The fault tolerance computation occurs in the context of the |
| 71 | +// livenessMap (to determine any existing node failures), and is |
| 72 | +// evaluated for only the replicas that appear in the replicas Seq. |
| 73 | +// |
| 74 | +// The evaluation for each replica is independent: it is valid to merge |
| 75 | +// the results of this computation for different sets of replicas by |
| 76 | +// taking the `min` of the margin values. |
| 77 | +func ComputeFaultTolerance( |
| 78 | + ctx context.Context, |
| 79 | + livenessMap livenesspb.NodeVitalityMap, |
| 80 | + nodeLocality map[roachpb.NodeID]roachpb.Locality, |
| 81 | + replicas iter.Seq[*kvserver.Replica], |
| 82 | +) (map[string]int, error) { |
| 83 | + return computeFaultToleranceImpl(ctx, livenessMap, nodeLocality, replicas) |
| 84 | +} |
| 85 | + |
| 86 | +type replicaDescriber interface { |
| 87 | + Desc() *roachpb.RangeDescriptor |
| 88 | +} |
| 89 | + |
| 90 | +func computeFaultToleranceImpl[RD replicaDescriber]( |
| 91 | + ctx context.Context, |
| 92 | + livenessMap livenesspb.NodeVitalityMap, |
| 93 | + nodeLocality map[roachpb.NodeID]roachpb.Locality, |
| 94 | + replicas iter.Seq[RD], |
| 95 | +) (map[string]int, error) { |
| 96 | + for n, l := range nodeLocality { |
| 97 | + if len(l.Tiers) == 0 { |
| 98 | + return nil, errors.AssertionFailedf("node %d missing locality", n) |
| 99 | + } |
| 100 | + } |
| 101 | + failureDomainMap := makeFailureDomains(nodeLocality) |
| 102 | + |
| 103 | + domainMargins := make(map[string]int) |
| 104 | + for replica := range replicas { |
| 105 | + for domainKey, fd := range failureDomainMap { |
| 106 | + if err := ctx.Err(); err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + margin := replica.Desc().Replicas().ReplicationMargin(func(rd roachpb.ReplicaDescriptor) bool { |
| 111 | + if _, ok := fd.nodes[rd.NodeID]; ok { |
| 112 | + return false |
| 113 | + } |
| 114 | + return livenessMap[rd.NodeID].IsLive(livenesspb.SpanConfigConformance) |
| 115 | + }) |
| 116 | + |
| 117 | + if oldMargin, ok := domainMargins[domainKey]; ok { |
| 118 | + domainMargins[domainKey] = min(oldMargin, margin) |
| 119 | + } else { |
| 120 | + domainMargins[domainKey] = margin |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return domainMargins, nil |
| 125 | +} |
| 126 | + |
| 127 | +func makeFailureDomains( |
| 128 | + nodeLocality map[roachpb.NodeID]roachpb.Locality, |
| 129 | +) map[string]*failureDomain { |
| 130 | + domainMap := make(map[string]*failureDomain) |
| 131 | + var unresolved []string |
| 132 | + // First, construct the leaf domains. |
| 133 | + for _, l := range nodeLocality { |
| 134 | + k := l.String() |
| 135 | + if _, ok := domainMap[k]; !ok { |
| 136 | + domainMap[k] = newFailureDomain(l, nodeLocality) |
| 137 | + } |
| 138 | + unresolved = append(unresolved, k) |
| 139 | + } |
| 140 | + |
| 141 | + // Sort the domains by descending length. In case the depth of the |
| 142 | + // locality tree varies, we want to handle the taller trees first. |
| 143 | + slices.SortStableFunc(unresolved, func(a, b string) int { |
| 144 | + aComma := strings.Count(a, ",") |
| 145 | + bComma := strings.Count(b, ",") |
| 146 | + return bComma - aComma |
| 147 | + }) |
| 148 | + |
| 149 | + // Merge existing domains into parent domains. |
| 150 | + for len(unresolved) > 0 { |
| 151 | + fd := domainMap[unresolved[0]] |
| 152 | + unresolved = unresolved[1:] |
| 153 | + |
| 154 | + pdKey := fd.parentKey() |
| 155 | + if pdKey == "" { |
| 156 | + continue |
| 157 | + } |
| 158 | + if parentFailureDomain, ok := domainMap[pdKey]; !ok { |
| 159 | + // new unresolved parent domain |
| 160 | + pd := fd.parent() |
| 161 | + domainMap[pdKey] = pd |
| 162 | + unresolved = append(unresolved, pdKey) |
| 163 | + } else { |
| 164 | + // merge child into parent domain |
| 165 | + parentFailureDomain.merge(fd) |
| 166 | + } |
| 167 | + } |
| 168 | + return domainMap |
| 169 | +} |
| 170 | + |
| 171 | +type failureDomain struct { |
| 172 | + domain roachpb.Locality |
| 173 | + nodes map[roachpb.NodeID]struct{} |
| 174 | +} |
| 175 | + |
| 176 | +func (fd *failureDomain) merge(rhs *failureDomain) { |
| 177 | + if match, _ := rhs.domain.Matches(fd.domain); !match { |
| 178 | + panic("cannot merge failure domain") |
| 179 | + } |
| 180 | + |
| 181 | + for n := range rhs.nodes { |
| 182 | + fd.nodes[n] = struct{}{} |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func (fd *failureDomain) parentKey() string { |
| 187 | + return roachpb.Locality{Tiers: fd.domain.Tiers[0 : len(fd.domain.Tiers)-1]}.String() |
| 188 | +} |
| 189 | + |
| 190 | +func newFailureDomain( |
| 191 | + domain roachpb.Locality, nodeLocality map[roachpb.NodeID]roachpb.Locality, |
| 192 | +) *failureDomain { |
| 193 | + faultScenario := make(map[roachpb.NodeID]struct{}) |
| 194 | + for node := range nodeLocality { |
| 195 | + if match, _ := nodeLocality[node].Matches(domain); match { |
| 196 | + faultScenario[node] = struct{}{} |
| 197 | + } |
| 198 | + } |
| 199 | + return &failureDomain{ |
| 200 | + domain: domain, |
| 201 | + nodes: faultScenario, |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func (fd *failureDomain) parent() *failureDomain { |
| 206 | + pd := roachpb.Locality{Tiers: fd.domain.Tiers[0 : len(fd.domain.Tiers)-1]} |
| 207 | + nodes := make(map[roachpb.NodeID]struct{}, len(fd.nodes)) |
| 208 | + maps.Copy(nodes, fd.nodes) |
| 209 | + return &failureDomain{ |
| 210 | + domain: pd, |
| 211 | + nodes: nodes, |
| 212 | + } |
| 213 | +} |
0 commit comments