func connectWith(addr string) uint64 { if len(addr) == 0 { return 0 } pool := conn.NewPool(addr, 5) query := new(conn.Query) var network bytes.Buffer enc := gob.NewEncoder(&network) err := enc.Encode(helloRPC{cur_node.id, *workerPort}) if err != nil { glog.Fatalf("encode:", err) } query.Data = network.Bytes() reply := new(conn.Reply) if err := pool.Call("Worker.Hello", query, reply); err != nil { glog.WithField("call", "Worker.Hello").Fatal(err) } i, _ := strconv.Atoi(string(reply.Data)) glog.WithField("reply", i).WithField("addr", addr). Info("Got reply from server") pools[uint64(i)] = pool peers[uint64(i)] = pool.Addr return uint64(i) }
func sendOverNetwork(ctx context.Context, message raftpb.Message) { pool, ok := pools[message.To] if !ok { glog.WithField("From", cur_node.id).WithField("To", message.To). Error("Error in making connetions") return } addr := pool.Addr fmt.Println(addr) query := new(conn.Query) var network bytes.Buffer gob.Register(ctx) enc := gob.NewEncoder(&network) err := enc.Encode(raftRPC{ctx, message}) if err != nil { glog.Fatalf("encode:", err) } query.Data = network.Bytes() reply := new(conn.Reply) if err := pool.Call("Worker.ReceiveOverNetwork", query, reply); err != nil { glog.WithField("call", "Worker.ReceiveOverNetwork").Error(err) //cur_node.raft.ReportUnreachable(message.To) // Report to raft cluster that the node is unreachable RemoveNodeFromCluster(message.To) return } glog.WithField("reply_len", len(reply.Data)).WithField("addr", addr). Info("Got reply from server") }
func removeFromPeerList(id string, idRem uint64) { pool := pools[idRem] query := new(conn.Query) query.Data = []byte(id) reply := new(conn.Reply) if err := pool.Call("Worker.RemovePeer", query, reply); err != nil { glog.WithField("call", "Worker.RemovePeer").Fatal(err) } }
func getMasterIp(ip string) string { if len(ip) == 0 { return "" } pool := conn.NewPool(ip, 5) query := new(conn.Query) var network bytes.Buffer enc := gob.NewEncoder(&network) err := enc.Encode(helloRPC{cur_node.id, *workerPort}) if err != nil { glog.Fatalf("encode:", err) } query.Data = network.Bytes() reply := new(conn.Reply) if err := pool.Call("Worker.GetMasterIP", query, reply); err != nil { glog.WithField("call", "Worker.GetMasterIP").Fatal(err) } masterIP := string(reply.Data) glog.WithField("reply", masterIP).WithField("addr", ip). Info("Got reply from server") return masterIP }
func proposeJoin(id uint64) { pool := pools[id] addr := pool.Addr fmt.Println(addr) query := new(conn.Query) query.Data = []byte(strconv.Itoa(int(cur_node.id))) reply := new(conn.Reply) co := 0 for cur_node.raft.Status().Lead != id && co < 3330 { glog.Info("Trying to connect with master") if err := pool.Call("Worker.JoinCluster", query, reply); err != nil { glog.WithField("call", "Worker.JoinCluster").Fatal(err) } glog.WithField("reply_len", len(reply.Data)).WithField("addr", addr). Info("Got reply from server") time.Sleep(1000 * time.Millisecond) // sleep for a second and rety joining the cluster co++ } if cur_node.raft.Status().Lead != id { glog.Fatalf("Unable to joing the cluster") } }