Exemple #1
0
func find(path string) {
	c := dial()

	if *rrev == -1 {
		var err error
		*rrev, err = c.Rev()
		if err != nil {
			bail(err)
		}
	}

	v := make(vis)
	errs := make(chan error)
	go func() {
		doozer.Walk(c, *rrev, path, v, errs)
		close(v)
	}()

	for {
		select {
		case path, ok := <-v:
			if !ok {
				return
			}
			fmt.Println(path)
		case err := <-errs:
			fmt.Fprintln(os.Stderr, err)
		}
	}
}
Exemple #2
0
// Del is a wrapper around (*doozer.Conn).Del which also supports
// deleting directories.
// TODO: Concurrent implementation
// TODO: Better error messages on NOENT
func (c *conn) Del(path string, rev int64) (err error) {
	path = c.prefixPath(path)

	err = doozer.Walk(c.conn, rev, path, func(path string, f *doozer.FileInfo, e error) error {
		if e != nil {
			return e
		}

		if !f.IsDir {
			e = c.conn.Del(path, rev)
			if e == doozer.ErrNoEnt || (e != nil && e.Error() == "NOENT") {
				return NewError(ErrNoEnt, fmt.Sprintf(`path "%s" not found @ %d`, path, rev))
			}
			if e != nil {
				return e
			}
		}

		return nil
	})
	if err == doozer.ErrNoEnt || (err != nil && err.Error() == "NOENT") {
		return NewError(ErrNoEnt, fmt.Sprintf(`path "%s" not found @ %d`, path, rev))
	}

	return
}
func runReplicate(cmd *Command) error {
	if len(cmd.Args) != 1 {
		cmd.Usage()
	}

	var (
		dst    = cmd.Conn
		root   = cmd.Globals.Root
		errc   = make(chan error)
		entryc = make(chan *journal.Entry)
		srcURI = cmd.Args[0]
	)

	// connect to source
	src, err := doozer.DialUri(srcURI, "")
	if err != nil {
		return fmt.Errorf("unable to connect to source '%s': %s", srcURI, err)
	}

	// get src rev for snapshot and streaming updates
	rev, err := src.Rev()
	if err != nil {
		return fmt.Errorf("unable to get rev from source '%s': %s", srcURI, err)
	}

	// check for lock existance
	_, prev, err := dst.Stat(lockPath, &cmd.Rev)
	if err != nil {
		return fmt.Errorf("unable to check for lock '%s': %s", lockPath, err)
	}
	if prev != 0 {
		return fmt.Errorf("lock exists")
	}

	// aquire lock
	_, err = dst.Set(lockPath, cmd.Rev, []byte(time.Now().String()))
	if err != nil {
		return fmt.Errorf("unable to set lock '%s': %s", lockPath, err)
	}

	// start writer
	go writer(dst, root, entryc, errc)

	// snapshot by walking src tree
	err = doozer.Walk(src, rev, root, walk(src, rev, entryc))
	if err != nil {
		return fmt.Errorf("unable to walk source '%s': %s", srcURI, err)
	}

	// stream updates
	go wait(src, rev, root, entryc, errc)

	return <-errc
}
Exemple #4
0
func find(path string) {
	c := dial()

	if *rrev == -1 {
		var err error
		*rrev, err = c.Rev()
		if err != nil {
			bail(err)
		}
	}

	v := func(path string, f *doozer.FileInfo, e error) (err error) {
		if e != nil {
			fmt.Fprintln(os.Stderr, err)
			return
		}

		fmt.Println(path)

		return nil
	}
	doozer.Walk(c, *rrev, path, v)
}
Exemple #5
0
// Main has doc
func Main(
	clusterName, self, buri, rwsk, rosk string,
	cl *doozer.Conn,
	udpConn *net.UDPConn,
	listener, webListener net.Listener,
	pulseInterval, fillDelay, kickTimeout, hi int64,
	st *store.Store,
) {
	listenAddr := listener.Addr().String()

	canWrite := make(chan bool, 1)
	in := make(chan consensus.Packet, 1024)

	pr := &proposer{
		seqns: make(chan int64, alpha),
		props: make(chan *consensus.Prop),
		st:    st,
	}

	calSrv := func(start int64) {
		go gc.Pulse(self, st.Seqns, pr, pulseInterval)
		go gc.Clean(st, hi, time.Tick(1e9))
		var m consensus.Manager
		m.Self = self
		m.DefRev = start
		m.Alpha = alpha
		m.In = in
		m.Out = in
		m.Ops = st.Ops
		m.PSeqn = pr.seqns
		m.Props = pr.props
		m.TFill = fillDelay
		m.Store = st
		m.Ticker = time.Tick(10e6)
		go m.Run()
	}

	hostname, err := os.Hostname()
	if err != nil {
		hostname = "unknown"
	}

	if cl == nil { // we are the only node in a new cluster
		set(st, "/ctl/name", clusterName, store.Missing)
		set(st, "/ctl/node/"+self+"/addr", listenAddr, store.Missing)
		set(st, "/ctl/node/"+self+"/hostname", hostname, store.Missing)
		set(st, "/ctl/node/"+self+"/version", Version, store.Missing)
		set(st, "/ctl/cal/0", self, store.Missing)
		if buri == "" {
			set(st, "/ctl/ns/"+clusterName+"/"+self, listenAddr, store.Missing)
		}
		calSrv(<-st.Seqns)
		// Skip ahead alpha steps so that the registrar can provide a
		// meaningful cluster.
		for i := 0; i < alpha; i++ {
			st.Ops <- store.Op{1 + <-st.Seqns, store.Nop}
		}
		canWrite <- true
		go setReady(pr, self)
	} else {
		setC(cl, "/ctl/node/"+self+"/addr", listenAddr, store.Clobber)
		setC(cl, "/ctl/node/"+self+"/hostname", hostname, store.Clobber)
		setC(cl, "/ctl/node/"+self+"/version", Version, store.Clobber)

		rev, err := cl.Rev()
		if err != nil {
			panic(err)
		}

		stop := make(chan bool, 1)

		go follow(st, cl, rev+1, stop)

		err = doozer.Walk(cl, rev, "/", func(path string, f *doozer.FileInfo, e error) (err error) {
			if f.IsDir {
				return
			}
			if e != nil {
				return e
			}
			// store.Clobber is okay here because the event
			// has already passed through another store
			body, _, err := cl.Get(path, &f.Rev)
			if err != nil {
				return
			}
			mut := store.MustEncodeSet(path, string(body), store.Clobber)
			st.Ops <- store.Op{f.Rev, mut}
			return
		})
		if err != nil {
			panic(err)
		}
		st.Flush()

		ch, err := st.Wait(store.Any, rev+1)
		if err == nil {
			<-ch
		}

		go func() {
			n := activate(st, self, cl)
			calSrv(n)
			advanceUntil(cl, st.Seqns, n+alpha)
			stop <- true
			canWrite <- true
			go setReady(pr, self)
			if buri != "" {
				b, err := doozer.DialUri(buri, "")
				if err != nil {
					panic(err)
				}
				setC(
					b,
					"/ctl/ns/"+clusterName+"/"+self,
					listenAddr,
					store.Missing,
				)
			}
		}()
	}

	shun := make(chan string, 3) // sufficient for a cluster of 7
	go member.Clean(shun, st, pr)
	go server.ListenAndServe(listener, canWrite, st, pr, rwsk, rosk, self)

	if rwsk == "" && rosk == "" && webListener != nil {
		web.Store = st
		web.ClusterName = clusterName
		go web.Serve(webListener)
	}

	select {}
}