Beispiel #1
0
func initcluster(c *cli.Context) {
	hosts := c.StringSlice("host")
	if c.IsSet("host") || c.IsSet("H") {
		hosts = hosts[1:]
	}

	lis, err := net.Listen("tcp", hosts[0])
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	server := grpc.NewServer()

	hostname := c.String("hostname")

	id := proton.GenID(hostname)

	if c.Bool("withRaftLogs") {
		raftLogger = &raft.DefaultLogger{Logger: log.New(ioutil.Discard, "", 0)}
	}

	cfg := proton.DefaultNodeConfig()
	cfg.Logger = raftLogger

	node, err := proton.NewNode(id, hosts[0], cfg, handler)
	if err != nil {
		log.Fatal("Can't initialize raft node")
	}

	node.Campaign(node.Ctx)
	go node.Start()

	log.Println("Starting raft transport layer..")
	proton.Register(server, node)

	go server.Serve(lis)

	ticker := time.NewTicker(time.Second * 10)
	go func() {
		i := 0
		for _ = range ticker.C {
			s := strconv.Itoa(i)
			pair, err := proton.EncodePair("key"+s, []byte("myvalue"+s))
			if err != nil {
				log.Fatal("Can't encode KV pair")
			}

			if node.IsLeader() {
				fmt.Println("---> Leading the raft")
			}

			node.Propose(node.Ctx, pair)
			i++
		}
	}()

	select {}
}
Beispiel #2
0
func join(c *cli.Context) {
	var (
		err error
	)

	hosts := c.StringSlice("host")
	if c.IsSet("host") || c.IsSet("H") {
		hosts = hosts[1:]
	}

	lis, err := net.Listen("tcp", hosts[0])
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}
	server := grpc.NewServer()

	joinAddr := c.String("join")
	hostname := c.String("hostname")

	if c.Bool("withRaftLogs") {
		raftLogger = &raft.DefaultLogger{Logger: log.New(ioutil.Discard, "", 0)}
	}

	id := proton.GenID(hostname)
	cfg := proton.DefaultNodeConfig()
	cfg.Logger = raftLogger

	node, err := proton.NewNode(id, hosts[0], cfg, handler)
	if err != nil {
		log.Fatal("Can't initialize raft node")
	}

	proton.Register(server, node)

	client, err := proton.GetRaftClient(joinAddr, 2*time.Second)
	if err != nil {
		log.Fatal("couldn't initialize client connection")
	}

	// Start raft
	go node.Start()
	go server.Serve(lis)

	info := &proton.NodeInfo{
		ID:   id,
		Addr: hosts[0],
	}

	resp, err := client.JoinRaft(context.Background(), info)
	if err != nil {
		log.Fatalf("could not join: %v", err)
	}

	err = node.RegisterNodes(resp.GetNodes())
	if err != nil {
		log.Fatal(err)
	}

	ticker := time.NewTicker(time.Second * 5)
	go func() {
		for _ = range ticker.C {
			fmt.Println("----- Leader is: ", node.Leader())
		}
	}()

	select {}
}