示例#1
0
文件: node.go 项目: postfix/gobby
func main() {
	fmt.Printf("node %d starts\n", nid)
	node, err := paxos.NewPaxosNode(nid, numNodes, fakecallback)
	if err != nil {
		fmt.Println("Cannot start node.\n")
		fmt.Println(err)
		return
	}
	listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Nodes[nid].Port))
	if err != nil {
		fmt.Printf("node %d cannot listen to port:%s\n", err)
		return
	}
	node.SetListener(&listener)
	rpc.HandleHTTP()
	go http.Serve(listener, nil)

	time.Sleep(5 * time.Second)

	for i := 0; i < 2; i++ {
		c := command.Command{strconv.Itoa(nid), strconv.Itoa(i), command.Put, i, ""}
		node.Replicate(&c)
	}

	for res := 0; res < 4; res++ {
		_, ok := <-done
		if !ok {
			break
		}
	}
	node.DumpLog()
	fmt.Printf("node %d closes\n", nid)
}
示例#2
0
func NewGobbyServer(nodeID int, numNodes int) (Gobbyserver, error) {
	server := new(gobbyserver)
	server.addrport = config.Nodes[nodeID].Address + ":" + strconv.Itoa(config.Nodes[nodeID].Port)
	server.store = make(map[string]*kstate)
	server.commandLog = make([]*command.Command, 0)
	server.nextIndex = 0
	server.replyChs = make(map[int]chan *gobbyrpc.GobbyReply)
	server.pending = NewQueue()
	if pnode, err := paxos.NewPaxosNode(nodeID, numNodes, server.getCommand); err != nil {
		return nil, err
	} else {
		server.paxosnode = pnode
	}
	if lnode, err := lease.NewLeaseNode(nodeID, numNodes); err != nil {
		return nil, err
	} else {
		server.leasenode = lnode
	}

	if err := rpc.RegisterName("gobbyServer", gobbyrpc.Wrap(server)); err != nil {
		return nil, err
	} else {
		go ReplicateRoutine(server.pending, server.paxosnode)
		return server, nil
	}

	//TODO:Now the listener is in the lease node, and we should let gobbyserver contain listener
}
示例#3
0
文件: nodep.go 项目: postfix/gobby
func main() {
	n3, err := paxos.NewPaxosNode(config.Nodes[nid].Address,
		config.Nodes[nid].Port,
		config.Nodes[nid].NodeID,
		fakecallback)
	if err != nil {
		fmt.Println("Cannot start node.\n")
		fmt.Println(err)
		return
	}
	for i := 0; i < 2; i++ {
		err = n3.Pause()
		if err != nil {
			fmt.Println("Cannot Pause node.\n")
			fmt.Println(err)
			return
		}
		time.Sleep(1 * time.Second)
		err = n3.Resume()
		if err != nil {
			fmt.Println("Cannot Resume node.\n")
			fmt.Println(err)
			return
		}
	}

}
示例#4
0
文件: node.go 项目: postfix/gobby
func main() {
	node, err := paxos.NewPaxosNode(nid,
		numNodes,
		fakecallback)
	if err != nil {
		fmt.Println("Cannot start node.\n")
		fmt.Println(err)
		return
	}
	listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Nodes[nid].Port))
	if err != nil {
		fmt.Printf("node %d cannot listen to port:%s\n", err)
		return
	}
	node.SetListener(&listener)
	rpc.HandleHTTP()
	go http.Serve(listener, nil)
	fmt.Println("Pause node.\n")
	err = node.Pause()
	if err != nil {
		fmt.Println("Cannot Pause node.\n")
		fmt.Println(err)
		return
	}

	time.Sleep(5 * time.Second)
	go func() {
		time.Sleep(10 * time.Second)
		fmt.Println("Resume node.\n")
		err = node.Resume()
		if err != nil {
			fmt.Println("Cannot Resume node.\n")
			fmt.Println(err)
			return
		}
	}()

	res := 0
	for res < 6 {
		_, ok := <-done
		if ok {
			res++
			if res == 5 {
				go func() {
					c := command.Command{strconv.Itoa(nid), strconv.Itoa(0), command.Put, 0, ""}
					node.Replicate(&c)
				}()
			}
		} else {
			break
		}
	}

	if res == 6 {
		fmt.Printf("\n%d receive all commands\n", nid)
	} else {
		fmt.Printf("%d Just break!!!!!\n", res)
	}
	time.Sleep(5 * time.Second)
}
示例#5
0
文件: node.go 项目: postfix/gobby
func main() {
	node, err := paxos.NewPaxosNode(nid, numNodes, fakecallback)
	if err != nil {
		fmt.Println("Cannot start node.\n")
		fmt.Println(err)
		return
	}
	listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Nodes[nid].Port))
	if err != nil {
		fmt.Printf("node %d cannot listen to port:%s\n", err)
		return
	}
	node.SetListener(&listener)
	rpc.HandleHTTP()
	go http.Serve(listener, nil)

	fmt.Println("Pause node.\n")
	err = node.Pause()
	if err != nil {
		fmt.Println("Cannot Pause node.\n")
		fmt.Println(err)
		return
	}
	time.Sleep(5 * time.Second)
	go func() {
		time.Sleep(5 * time.Second)
		fmt.Println("Resume node.\n")
		err = node.Resume()
		if err != nil {
			fmt.Println("Cannot Resume node.\n")
			fmt.Println(err)
			return
		}
	}()

	res := 0
	for res < 22 {
		_, ok := <-done
		if ok {
			res++
		} else {
			break
		}
	}

	if res == 22 {
		fmt.Printf("\n%d receive all commands\n", nid)
	} else {
		fmt.Printf("%d Just break %d!!!!!\n", nid, res)
	}
	node.DumpLog()
}
示例#6
0
文件: node.go 项目: postfix/gobby
func main() {
	node, err := paxos.NewPaxosNode(nid, numNodes, fakecallback)
	if err != nil {
		fmt.Println("Cannot start node.\n")
		fmt.Println(err)
		return
	}
	listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Nodes[nid].Port))
	if err != nil {
		fmt.Printf("node %d cannot listen to port:%s\n", err)
		return
	}
	node.SetListener(&listener)
	rpc.HandleHTTP()
	go http.Serve(listener, nil)

	time.Sleep(5 * time.Second)
	go func() {
		for i := 0; i < 5; i++ {
			c := command.Command{strconv.Itoa(nid), strconv.Itoa(i), command.Put, i, ""}
			node.Replicate(&c)
		}
		time.Sleep(10 * time.Second)
		c := command.Command{strconv.Itoa(nid), strconv.Itoa(100), command.Put, 100, ""}
		node.Replicate(&c)
		c = command.Command{strconv.Itoa(nid), strconv.Itoa(101), command.Put, 101, ""}
		node.Replicate(&c)
	}()

	res := 0
	for res < 22 {
		_, ok := <-done
		if ok {
			res++
		} else {
			break
		}
	}

	if res == 22 {
		fmt.Printf("\n%d receive all commands\n", nid)
	} else {
		fmt.Printf("%d Just break %d!!!!!\n", nid, res)
	}
	node.DumpLog()
	time.Sleep(15 * time.Second)
}