// Elect chooses a seed node, and returns a connection to a cal. // If this process is the seed, returns nil. func elect(name, id, laddr string, b *doozer.Client) *doozer.Client { // advertise our presence, since we might become a cal nspath := "/ctl/ns/" + name + "/" + id r, err := b.Set(nspath, 0, []byte(laddr)) if err != nil { panic(err) } // fight to be the seed _, err = b.Set("/ctl/boot/"+name, 0, []byte(id)) switch err { case doozer.ErrOldRev: // we lost, lookup addresses again cl := lookupAndAttach(b, name) if cl == nil { panic("failed to attach after losing election") } // also delete our entry, since we're not officially a cal yet. // it gets set again in peer.Main when we become a cal. err := b.Del(nspath, r) if err != nil { panic(err) } return cl case nil: return nil // we are the seed node -- don't attach } panic(err) }
func activate(st *store.Store, self string, c *doozer.Client) int64 { w := store.NewWatch(st, calGlob) for _, base := range store.Getdir(st, calDir) { p := calDir + "/" + base v, rev := st.Get(p) if rev != store.Dir && v[0] == "" { seqn, err := c.Set(p, rev, []byte(self)) if err != nil { log.Println(err) continue } w.Stop() return seqn } } for ev := range w.C { // TODO ev.IsEmpty() if ev.IsSet() && ev.Body == "" { seqn, err := c.Set(ev.Path, ev.Rev, []byte(self)) if err != nil { log.Println(err) continue } w.Stop() return seqn } } return 0 }
func setC(cl *doozer.Client, path, body string, rev int64) { _, err := cl.Set(path, rev, []byte(body)) if err != nil { panic(err) } }