func DoozerConnect() { var err os.Error DC, err = doozer.Dial(*DoozerServer) if err != nil { log.Panic(err.String()) } }
func dial(addr string) *doozer.Conn { c, err := doozer.Dial(addr) if err != nil { panic(err) } return c }
func main() { flag.Parse() conn, err := doozer.Dial(*doozerUri) if err != nil { panic(err) } events := make(chan doozer.Event, 100) revs := make(chan int64, 100) curr, err := conn.Rev() if err != nil { panic(err) } list, err := conn.Walk("/*", curr, 0, -1) if err != nil { panic(err) } go backup(events) for i := range list { events <- list[i] } go watch(conn, events, revs) revs <- curr }
func main() { flag.Parse() // Setup the doozer connection. d, err := doozer.Dial(daddr) if err != nil { log.Fatalf("connecting to doozer: %v\n", err) } defer d.Close() // Setup the cache. pool = groupcache.NewHTTPPool("http://" + addr) dict = groupcache.NewGroup("dict", 64<<20, groupcache.GetterFunc( func(ctx groupcache.Context, key string, dest groupcache.Sink) error { def, err := query(key) if err != nil { err = fmt.Errorf("querying remote dictionary: %v", err) log.Println(err) return err } log.Println("retrieved remote definition for", key) dest.SetString(def) return nil })) // Start watching for changes and signals. go watch(d) // Add the handler for definition requests and then start the // server. http.Handle("/define/", http.HandlerFunc(handler)) log.Println(http.ListenAndServe(addr, nil)) }
// IsCal checks if addr is a CAL in the cluster named name. // Returns a client if so, nil if not. // 判断节点是不是存活可连接(?) // 建立链接到 /ctl/cal/XXX 下的主机连接 func isCal(name, addr string) (*doozer.Conn, error) { c, err := doozer.Dial(addr) if err != nil { return nil, err } err = c.Access(rwsk) if err != nil { return nil, err } v, _, _ := c.Get("/ctl/name", nil) // 怎么个意思? // 判断是不是属于同一个集群? if string(v) != name { return nil, nil } rev, err := c.Rev() if err != nil { return nil, err } var cals []string names, err := c.Getdir("/ctl/cal", rev, 0, -1) if err != nil { return nil, err } for _, name := range names { cals = append(cals, name) } for _, cal := range cals { // 在这个字段保存了主机ID body, _, err := c.Get("/ctl/cal/"+cal, nil) if err != nil || len(body) == 0 { continue } id := string(body) v, _, err := c.Get("/ctl/node/"+id+"/addr", nil) if err != nil { return nil, err } if string(v) == addr { return c, nil } } return nil, nil }
func dial() *doozer.Conn { c, err := doozer.Dial(*addr) if err != nil { bail(err) } if token := os.Getenv("DOOZER_TOKEN"); token != "" { err := c.Access(token) if err != nil { bail(err) } } return c }
// IsCal checks if addr is a CAL in the cluster named name. // Returns a client if so, nil if not. func isCal(name, addr string) *doozer.Conn { c, err := doozer.Dial(addr) if err != nil { panic(err) } err = c.Access(secret) if err != nil { panic(err) } v, _, _ := c.Get("/ctl/name", nil) if string(v) != name { return nil } rev, err := c.Rev() if err != nil { panic(err) } var cals []string names, err := c.Getdir("/ctl/cal", rev, 0, -1) if err != nil { panic(err) } for _, name := range names { cals = append(cals, name) } for _, cal := range cals { body, _, err := c.Get("/ctl/cal/"+cal, nil) if err != nil || len(body) == 0 { continue } id := string(body) v, _, err := c.Get("/ctl/node/"+id+"/addr", nil) if err != nil { panic(err) } if string(v) == addr { return c } } return nil }