Exemple #1
0
func livetestlibSetupEasyTimeoutCluster(clusterSize int) []*replica.Replica {
	nodes := make([]*replica.Replica, clusterSize)

	for i := range nodes {
		param := &replica.Param{
			ExecuteInterval: time.Second * 50,      // disable execution
			TimeoutInterval: time.Millisecond * 20, // disable timeout
			ReplicaId:       uint8(i),
			Size:            uint8(clusterSize),
			StateMachine:    new(test.DummySM),
			Transporter:     transporter.NewDummyTR(uint8(i), clusterSize),
			PersistentPath:  tempfile(),
		}
		nodes[i], _ = replica.New(param)
	}

	chs := make([]chan message.Message, clusterSize)
	for i := range nodes {
		chs[i] = nodes[i].MessageChan
	}

	for i := range nodes {
		nodes[i].Transporter.(*transporter.DummyTransporter).RegisterChannels(chs)
		nodes[i].Start()
	}

	return nodes
}
Exemple #2
0
func main() {
	var id int
	var restore bool

	flag.IntVar(&id, "id", -1, "id of the server")
	flag.BoolVar(&restore, "restore", false, "if recover")

	flag.Parse()

	if id < 0 {
		fmt.Println("id is required!")
		flag.PrintDefaults()
		return
	}

	addrs := []string{
		":9000", ":9001", ":9002",
		//":9003", ":9004",
	}

	tr, err := transporter.NewUDPTransporter(addrs, uint8(id), len(addrs))
	if err != nil {
		panic(err)
	}
	param := &replica.Param{
		Addrs:            addrs,
		ReplicaId:        uint8(id),
		Size:             uint8(len(addrs)),
		StateMachine:     new(Voter),
		Transporter:      tr,
		EnablePersistent: true,
		Restore:          restore,
		TimeoutInterval:  time.Second,
		//ExecuteInterval:  time.Second,
	}
	if restore {
		fmt.Fprintln(os.Stderr, "===restore===")
	}

	fmt.Println("====== Spawn new replica ======")
	r, err := replica.New(param)
	if err != nil {
		glog.Fatal(err)
	}

	fmt.Println("Done!")
	fmt.Printf("Wait %d seconds to start\n", prepareInterval)
	time.Sleep(prepareInterval * time.Second)
	err = r.Start()
	if err != nil {
		glog.Fatal(err)
	}
	fmt.Println("====== start ======")

	rand.Seed(time.Now().UTC().UnixNano())
	counter := 1
	for {
		time.Sleep(time.Millisecond * 500)
		c := "From: " + strconv.Itoa(id) + ", Command: " + strconv.Itoa(id) + ":" + strconv.Itoa(counter) + ", " + time.Now().String()
		counter++

		cmds := make([]message.Command, 0)
		cmds = append(cmds, message.Command(c))
		r.Propose(cmds...)
	}
}