File
Blob: chord/local_tasks.go
| 1 | package chord |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "fmt" |
| 6 | "time" |
| 7 | |
| 8 | "go.miragespace.co/specter/spec/chord" |
| 9 | "go.miragespace.co/specter/util" |
| 10 | |
| 11 | "github.com/zeebo/xxh3" |
| 12 | "go.uber.org/zap" |
| 13 | ) |
| 14 | |
| 15 | func v2d(n []chord.VNode) []uint64 { |
| 16 | x := make([]uint64, 0) |
| 17 | for _, xx := range n { |
| 18 | if xx == nil { |
| 19 | continue |
| 20 | } |
| 21 | x = append(x, xx.ID()) |
| 22 | } |
| 23 | return x |
| 24 | } |
| 25 | |
| 26 | // it is not safe to use xor under any ciscumstances as long as |
| 27 | // we have the possbility of cyclical ring that will have ourself |
| 28 | // in the successor list |
| 29 | func (n *LocalNode) hash(nodes []chord.VNode) uint64 { |
| 30 | hasher := xxh3.New() |
| 31 | buf := make([]byte, 8) |
| 32 | for _, node := range nodes { |
| 33 | if node == nil { |
| 34 | continue |
| 35 | } |
| 36 | binary.BigEndian.PutUint64(buf, node.ID()) |
| 37 | hasher.Write(buf) |
| 38 | } |
| 39 | return hasher.Sum64() |
| 40 | } |
| 41 | |
| 42 | // routine based on pseudo code from the paper "How to Make Chord Correct" |
| 43 | func (n *LocalNode) stabilize() (err error) { |
| 44 | defer func() { |
| 45 | if err != nil { |
| 46 | n.logger.Error("Stabilize task", zap.Error(err)) |
| 47 | } |
| 48 | }() |
| 49 | |
| 50 | succList := n.getSuccessors() |
| 51 | modified := false |
| 52 | |
| 53 | for len(succList) > 0 { |
| 54 | head := succList[0] |
| 55 | if head == nil { |
| 56 | return chord.ErrNodeNoSuccessor |
| 57 | } |
| 58 | newSucc, spErr := head.GetPredecessor() |
| 59 | newSuccList, nsErr := head.GetSuccessors() |
| 60 | if spErr == nil && nsErr == nil { |
| 61 | succList = chord.MakeSuccListByID(head, newSuccList, chord.ExtendedSuccessorEntries) |
| 62 | modified = true |
| 63 | |
| 64 | if newSucc != nil && chord.Between(n.ID(), newSucc.ID(), head.ID(), false) { |
| 65 | newSuccList, nsErr = newSucc.GetSuccessors() |
| 66 | if nsErr == nil { |
| 67 | succList = chord.MakeSuccListByID(newSucc, newSuccList, chord.ExtendedSuccessorEntries) |
| 68 | modified = true |
| 69 | } |
| 70 | } |
| 71 | break |
| 72 | } |
| 73 | n.logger.Debug("Skipping over successor", zap.Object("head", head.Identity()), zap.Uint64s("succ", v2d(succList))) |
| 74 | succList = succList[1:] |
| 75 | } |
| 76 | |
| 77 | if !modified { |
| 78 | return chord.ErrNodeNoSuccessor |
| 79 | } |
| 80 | |
| 81 | listHash := n.hash(succList) |
| 82 | if n.succListHash.Load() != listHash { |
| 83 | n.successorsMu.Lock() |
| 84 | n.updateSuccessorsList(listHash, succList) |
| 85 | n.successorsMu.Unlock() |
| 86 | } |
| 87 | |
| 88 | if n.checkNodeState(true) == nil { // don't re-notify our successor when we are leaving |
| 89 | succ := succList[0] |
| 90 | if err := succ.Notify(n); err != nil { |
| 91 | return fmt.Errorf("notifying successor %d: %w", succ.ID(), err) |
| 92 | } |
| 93 | n.lastStabilized.Store(time.Now()) |
| 94 | } |
| 95 | |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | func (n *LocalNode) updateSuccessorsList(listHash uint64, succList []chord.VNode) { |
| 100 | n.succListHash.Store(listHash) |
| 101 | n.successors = succList |
| 102 | |
| 103 | n.logger.Info("Discovered new successors via Stabilize", |
| 104 | zap.Uint64s("successors", v2d(succList)), |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | func (n *LocalNode) fixK(k int) (updated bool, err error) { |
| 109 | var f chord.VNode |
| 110 | next := chord.ModuloSum(n.ID(), 1<<(k-1)) |
| 111 | f, err = n.FindSuccessor(next) |
| 112 | if err != nil { |
| 113 | return |
| 114 | } |
| 115 | if f == nil { |
| 116 | err = fmt.Errorf("no successor found for k = %d", k) |
| 117 | return |
| 118 | } |
| 119 | n.fingers[k].computeUpdate(func(entry *fingerEntry) { |
| 120 | if entry.node == nil || entry.node.ID() != f.ID() { |
| 121 | entry.node = f |
| 122 | updated = true |
| 123 | } |
| 124 | }) |
| 125 | return |
| 126 | } |
| 127 | |
| 128 | func (n *LocalNode) fixFinger() error { |
| 129 | fixed := make([]int, 0) |
| 130 | for k := 1; k <= chord.MaxFingerEntries; k++ { |
| 131 | changed, err := n.fixK(k) |
| 132 | if err != nil { |
| 133 | continue |
| 134 | } |
| 135 | if changed { |
| 136 | fixed = append(fixed, k) |
| 137 | } |
| 138 | } |
| 139 | if len(fixed) > 0 { |
| 140 | n.logger.Info("FingerTable entries updated", zap.Ints("fixed", fixed)) |
| 141 | } |
| 142 | return nil |
| 143 | } |
| 144 | |
| 145 | func (n *LocalNode) checkPredecessor() error { |
| 146 | n.predecessorMu.RLock() |
| 147 | pre := n.predecessor |
| 148 | n.predecessorMu.RUnlock() |
| 149 | if pre == nil || pre.ID() == n.ID() { |
| 150 | return nil |
| 151 | } |
| 152 | |
| 153 | err := pre.Ping() |
| 154 | if err != nil { |
| 155 | n.predecessorMu.Lock() |
| 156 | if n.predecessor == pre { |
| 157 | n.predecessor = nil |
| 158 | n.logger.Info("Discovered dead predecessor", |
| 159 | zap.Object("old", pre.Identity()), |
| 160 | zap.String("new", "nil"), |
| 161 | ) |
| 162 | } |
| 163 | n.predecessorMu.Unlock() |
| 164 | } |
| 165 | return err |
| 166 | } |
| 167 | |
| 168 | func (n *LocalNode) periodicStabilize() { |
| 169 | defer n.stopWg.Done() |
| 170 | |
| 171 | time.Sleep(n.StabilizeInterval) |
| 172 | for { |
| 173 | select { |
| 174 | case <-n.stopCh: |
| 175 | n.logger.Debug("Stopping Stabilize task") |
| 176 | return |
| 177 | default: |
| 178 | n.stabilize() |
| 179 | time.Sleep(util.RandomTimeRange(n.StabilizeInterval)) |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func (n *LocalNode) periodicPredecessorCheck() { |
| 185 | defer n.stopWg.Done() |
| 186 | |
| 187 | for { |
| 188 | select { |
| 189 | case <-n.stopCh: |
| 190 | n.logger.Debug("Stopping predecessor checking task") |
| 191 | return |
| 192 | default: |
| 193 | n.checkPredecessor() |
| 194 | time.Sleep(util.RandomTimeRange(n.PredecessorCheckInterval)) |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func (n *LocalNode) periodicFixFingers() { |
| 200 | defer n.stopWg.Done() |
| 201 | |
| 202 | time.Sleep(n.FixFingerInterval) |
| 203 | for { |
| 204 | select { |
| 205 | case <-n.stopCh: |
| 206 | n.logger.Debug("Stopping FixFinger task") |
| 207 | return |
| 208 | default: |
| 209 | n.fixFinger() |
| 210 | time.Sleep(util.RandomTimeRange(n.FixFingerInterval)) |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | func (n *LocalNode) startTasks() { |
| 216 | // run once |
| 217 | n.stabilize() |
| 218 | n.fixFinger() |
| 219 | n.stopWg.Add(3) |
| 220 | // then run periodically |
| 221 | go n.periodicStabilize() |
| 222 | go n.periodicPredecessorCheck() |
| 223 | go n.periodicFixFingers() |
| 224 | } |