func newRegionLeader(lockNode string) *regionLeader { rl := ®ionLeader{ active: true, lockNode: lockNode, rescinded: make(chan struct{}), } // establish a watch to cleanup go func() { _, _, watch, err := zookeeper.GetW(rl.lockNode) if err != nil { rl.Rescind() return } <-watch log.Debugf("[Sync:RegionLeader] Watch triggered on '%v', will rescind leadership", rl.lockNode) inst.Counter(1.0, "sync.regionleader.remotely-rescinded") rl.Rescind() return }() // Register region leader mu.Lock() rls = append(rls, rl) mu.Unlock() return rl }
func waitForWinner(path, ourNode string) error { seq, err := parseSeq(ourNode) if err != nil { return err } for { lowestSeq, prevSeqPath, err := findLowestSequenceNode(path, seq) if err != nil { return err } if seq == lowestSeq { // we are now the leader! break } // wait on the node next in line for the lock _, _, ch, err := zookeeper.GetW(path + "/" + prevSeqPath) if err != nil && err != gozk.ErrNoNode { return err } else if err != nil && err == gozk.ErrNoNode { // try again continue } ev := <-ch if ev.Err != nil { return ev.Err } } return nil }