Ejemplo n.º 1
0
// Helper function to construct HTTP Peers
func mustNewHTTPPeer(u *url.URL) raft.Peer {
	p, err := raft.NewHTTPPeer(u)
	if err != nil {
		panic(err)
	}
	return p
}
Ejemplo n.º 2
0
// propagate listens for changes in the set of Progol-validated peers, and
// propagates those changes to the Raft server.
func propagate(substrate chan []progol.Peer, raftServer *raft.Server, minimum int, ready chan struct{}) {
	signaled := false
	for basePeers := range substrate {
		log.Printf("Propagate: got %d base peer(s)", len(basePeers))
		raftPeers := []raft.Peer{}
		for _, basePeer := range basePeers {
			raftPeer, err := raft.NewHTTPPeer(basePeer.URL)
			if err != nil {
				log.Printf("Propagate: %s (OK=%v): %s", basePeer.URL, basePeer.OK, err)
				continue
			}
			log.Printf("Propagate: %s is a valid Raft peer", basePeer.URL.String())
			raftPeers = append(raftPeers, raftPeer)
		}
		if n := len(raftPeers); n > 0 {
			if err := raftServer.SetConfiguration(raftPeers...); err != nil {
				log.Printf("Propagate: SetConfiguration (%d): %s", n, err)
			}
			log.Printf("Propagate: SetConfiguration (%d) successful", n)
			if n >= minimum && !signaled {
				close(ready)
				signaled = true
			}
		}
	}
}
Ejemplo n.º 3
0
func ExampleNewServer_hTTP() {
	// A no-op ApplyFunc
	a := func(uint64, []byte) []byte { return []byte{} }

	// Helper function to parse URLs
	mustParseURL := func(rawurl string) *url.URL {
		u, err := url.Parse(rawurl)
		if err != nil {
			panic(err)
		}
		u.Path = ""
		return u
	}

	// Helper function to construct HTTP Peers
	mustNewHTTPPeer := func(u *url.URL) raft.Peer {
		p, err := raft.NewHTTPPeer(u)
		if err != nil {
			panic(err)
		}
		return p
	}

	// Construct the server
	s := raft.NewServer(1, &bytes.Buffer{}, a)

	// Expose the server using a HTTP transport
	raft.HTTPTransport(http.DefaultServeMux, s)
	go http.ListenAndServe(":8080", nil)

	// Set the initial server configuration
	s.SetConfiguration(
		mustNewHTTPPeer(mustParseURL("http://127.0.0.1:8080")), // this server
		mustNewHTTPPeer(mustParseURL("http://10.1.1.11:8080")),
		mustNewHTTPPeer(mustParseURL("http://10.1.1.12:8080")),
		mustNewHTTPPeer(mustParseURL("http://10.1.1.13:8080")),
		mustNewHTTPPeer(mustParseURL("http://10.1.1.14:8080")),
	)

	// Start the server
	s.Start()
}