Example #1
0
func main() {

	myid := flag.Int("id", 1, "a int")
	flag.Parse()
	port = 12345
	//var input string

	port = port + *myid
	Cal := new(RaftTest)

	rpc.Register(Cal)
	listener, e := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port))
	if e != nil {
		log.Fatal("listen error:", e)
	}
	go func() {
		for {
			if conn, err := listener.Accept(); err != nil {
				log.Fatal("accept error: " + err.Error())
			} else {
				log.Printf("new connection established\n")
				go rpc.ServeConn(conn)
			}
		}
	}()

	logfile := os.Getenv("GOPATH") + "/src/github.com/nilangshah/Raft/Raftlog" + strconv.Itoa(*myid)

	path := GetPath() + "/src/github.com/nilangshah/Raft/cluster/config.xml"
	server1 = cluster.New(*myid, path)
	replicator = Raft.New(server1, logfile)
	replicator.Start()
	fmt.Println(*myid)

	//fmt.Scanln(&input)

	select {
	case <-time.After(100 * time.Second):
		fmt.Println(replicator.IsRunning(), replicator.Term())

	}
}
Example #2
0
func main() {
	fmt.Println("hello")
	out := make(chan int)
	path := GetPath() + "/src/github.com/nilangshah/Raft/cluster/config.json"
	replicator := make([]Raft.Replicator, 5)
	server := make([]cluster.Server, 5)

	for i := 0; i < 5; i++ {
		fmt.Println("server", i, "started")
		logfile := os.Getenv("GOPATH") + "/src/github.com/nilangshah/Raft/Raftlog" + strconv.Itoa(i+1)

		server[i] = cluster.New(i+1, path)
		replicator[i] = Raft.New(server[i], logfile)
		replicator[i].Start()
	}
	leader_id := 0
	select {
	case <-time.After(5 * time.Second):
		for i := 0; i < 5; i++ {
			leader_id = int(replicator[i].GetLeader())
			fmt.Println("leader found")
		}

	}
	for i := 0; i < 10; i++ {
		response := make(chan bool)
		command := Raft.CommandTuple{Command: []byte("This is " + strconv.Itoa(i) + "th message"), CommandResponse: response}
		replicator[leader_id-1].Outbox() <- command
		fmt.Println("command send")
		select {
		case t := <-response:
			fmt.Println(t, "comited received")

		}
	}
	<-out
}
Example #3
0
// create new xceldb
func main() {

	var Cluster cluster.Server
	var Replicator Raft.Replicator
	Id := flag.Int("id", 1, "a int")
	flag.Parse()
	configFname := GetPath() + "/src/github.com/nilangshah/XcelDB/config.xml"
	Confname := GetPath() + "/src/github.com/nilangshah/XcelDB/c_config.xml"
	LogPath := GetPath() + "/src/github.com/nilangshah/XcelDB/Raftlog" + strconv.Itoa(*Id)

	//intialize state machine
	xcelSM := NewStateMachine()

	//intialize replicator and cluster
	Cluster = cluster.New(*Id, Confname)
	Replicator = Raft.New(Cluster, LogPath)

	// listen inbox for past commited commands
	go ListenInBox(Replicator)

	//initialize db
	xcelDB = &XcelDB{
		xcelId:         uint64(*Id),
		xcelSM:         xcelSM,
		xcelReplicator: Replicator,
	}

	//start replicator
	Replicator.Start()

	//intialize xceldb

	//read config file
	var Jsontype Jsonobject
	file, e := ioutil.ReadFile(configFname)
	if e != nil {
		panic("File error: " + e.Error())
	}

	xml.Unmarshal(file, &Jsontype)
	//fmt.Println(xml.Marshal(&Jsontype))
	// store configuration
	count := 0
	xcelDB.xcelPeers = make([]uint64, len(Jsontype.Object.Servers))
	xcelDB.xcelPeermap = make(map[uint64]string, len(Jsontype.Object.Servers))
	for i := range Jsontype.Object.Servers {
		if Jsontype.Object.Servers[i].Id == uint64(*Id) {

		} else {
			xcelDB.xcelPeers[count] = Jsontype.Object.Servers[i].Id
			count++
		}
		xcelDB.xcelPeermap[Jsontype.Object.Servers[i].Id] = Jsontype.Object.Servers[i].Host

	}

	//start http server
	http.HandleFunc("/", kvHandler)
	select {
	case <-time.After(2 * time.Second):
		fmt.Println(xcelDB.xcelReplicator.IsRunning())

	}
	http.ListenAndServe(xcelDB.xcelPeermap[uint64(*Id)], nil)

}