コード例 #1
0
ファイル: main.go プロジェクト: patrickToca/progol
func main() {
	peers := []url.URL{}
	for _, rawurl := range []string{
		"http://localhost:8001",
		"http://localhost:8002",
	} {
		peer, err := url.Parse(rawurl)
		if err != nil {
			panic(err)
		}
		peers = append(peers, *peer)
	}

	go func() {
		http.HandleFunc(progol.ValidationPath, progol.ValidationHandler)
		log.Printf("listening on :%d", *port)
		log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
	}()

	d := progol.StaticDiscovery(peers)
	v := progol.NewValidator(d, 1*time.Second)
	c := make(chan []progol.Peer)
	go func() {
		for peers := range c {
			log.Printf("Peers: %v", peers)
		}
	}()
	v.Subscribe(c)

	select {}
}
コード例 #2
0
ファイル: main.go プロジェクト: patrickToca/progol
func main() {
	me, err := url.Parse(*myEndpoint)
	if err != nil {
		panic(err)
	}
	log.Printf("my address is: %s", me.String())

	multicast, err := url.Parse(*multicastEndpoint)
	if err != nil {
		panic(err)
	}
	log.Printf("joining multicast group on: %s", multicast.String())

	go func() {
		http.HandleFunc(progol.ValidationPath, progol.ValidationHandler)
		log.Printf("starting HTTP server on %s", me.Host)
		log.Fatal(http.ListenAndServe(me.Host, nil))
	}()

	d, err := progol.NewMulticastDiscovery(*myEndpoint, multicast.Host, 3*time.Second)
	if err != nil {
		log.Fatalf("creating Multicast Discovery: %s", err)
	}

	v := progol.NewValidator(d, 1*time.Second)
	c := make(chan []progol.Peer)
	go func() {
		for peers := range c {
			log.Printf("Peers: %v", peers)
		}
	}()
	v.Subscribe(c)

	select {}
}
コード例 #3
0
ファイル: main.go プロジェクト: patrickToca/progol
func main() {
	// Gather statistics about the data file.
	filesz, checksum, err := stat(*datafile)
	if err != nil {
		log.Fatal(err)
	}

	// Set up this process to be discovered. Choose a random port.
	discovery, port, err := newMulticastDiscovery(*host, *discoveryGroup, *discoveryLifetime)
	if err != nil {
		log.Fatal(err)
	}

	// Build a state machine arount the data file statistics. ID is the port.
	machine, err := newStateMachine(uint64(port), filesz, checksum, *maxmem)
	if err != nil {
		log.Fatal(err)
	}

	// Build a Raft server, applying into our state machine. ID is the port.
	raftServer := raft.NewServer(uint64(port), &bytes.Buffer{}, machine.Apply)

	// HTTP server, for Progol discovery/validation, and Raft communication.
	http.HandleFunc(progol.ValidationPath, okHandler)
	raft.HTTPTransport(http.DefaultServeMux, raftServer)
	go http.ListenAndServe(net.JoinHostPort(*host, fmt.Sprint(port)), nil)

	// When we discover new peers at the Progol level,
	// we'll want to propagate that topology to Raft.
	substrate := make(chan []progol.Peer)
	ready := make(chan struct{})
	go propagate(substrate, raftServer, *minimum, ready)

	// Start a validator, to publish Progol peers to our propagator.
	validator := progol.NewValidator(discovery, *validationInterval)
	validator.Subscribe(substrate)
	defer validator.Unsubscribe(substrate)

	// Wait for some minimum number of nodes.
	<-ready

	// Then, start the Raft server.
	log.SetOutput(&bytes.Buffer{})
	raftServer.Start()

	// After some random amount of time, make a claim
	time.Sleep(time.Duration(1000+rand.Intn(1000)) * time.Millisecond)
	raftServer.Command(machine.MakeClaim(), make(chan []byte, 1))

	// Wait until our state machine reports the offset we own.
	offset, length := machine.Segment() // blocking

	// Process that segment
	output := process(*datafile, offset, length)
	fmt.Printf("%s\n", output)

	<-signalHandler()
}