示例#1
0
func NewRaft(id int, path string) *Raft {
	//Intialising Object Properties
	RaftObj := new(raftState)
	RaftObj.isDebugOn = false
	cofigpath, _ := filepath.Abs(path)
	allDetails := getALLCOnfig(cofigpath)
	RaftObj.commitThis = make(chan *cluster.LogValue, 20)
	RaftObj.inputRequest = make(chan *[]byte, 20)
	//Getting Election Time Onut From COnfig Object
	for index := range allDetails.Timeouts {
		if allDetails.Timeouts[index].ID == id {
			RaftObj.electionTimeout = allDetails.Timeouts[index].ElectionTimeout
		}
		if allDetails.LogFile[index].ID == id {
			RaftObj.logFilePath = allDetails.LogFile[index].Filename
			RaftObj.logFilePath = allDetails.LogFile[index].Filename
			RaftObj.logFilePath = allDetails.LogFile[index].Filename
		}
		if allDetails.TermFile[index].ID == id {
			//	log.Println(allDetails.TermFile[index].Filename)
			RaftObj.termFilePath = allDetails.TermFile[index].Filename
		}

	}
	//log.Println("sdfhkjasdhflkjshadljkfhlkjsadhflkj")
	RaftObj.serverEntity = cluster.New(id, path)
	RaftObj.quorum = (len(RaftObj.serverEntity.Peers_o)+1)/2 + 1
	RaftObj.logWrite.LogState = make([]int, len(RaftObj.serverEntity.Peers_o)+2)
	RaftObj.logWrite.StartLogger(RaftObj.logFilePath, RaftObj.myID)
	getit := RaftObj.readTermFromFile()
	RaftObj.updateTerm(getit.Term)
	RaftObj.logWrite.CommittedIndex = getit.Commitedindex
	RaftObj.logWrite.Deleteafter(RaftObj.logWrite.CommittedIndex)
	for i := 1; i <= len(RaftObj.serverEntity.Peers_o)+1; i++ {
		RaftObj.logWrite.Setindex(i, RaftObj.logWrite.CommittedIndex+1)
		RaftObj.logWrite.LogState[i] = 0
	}
	RaftObj.heartBeatTime = 100
	RaftObj.updateState(Follower)
	RaftObj.writeTerm()
	RaftObj.myID = id
	RaftObj.votedFor = 0
	RaftObj.updateStarted(false)
	RaftObj.votedFor = 0
	if RaftObj.isDebugOn {
		log.Println(strconv.Itoa(RaftObj.myID) + " - " + strconv.Itoa(RaftObj.currentTerm) + " Start -> Follower")
	}
	RaftObj.Start()
	var x Raft
	x = RaftObj
	return &x
}
示例#2
0
func NewRaft(myid int, size_of_in_chan int, size_of_out_chan int, delay_before_conn time.Duration, configfile string, parent_dir string) *Raft_Implementer {

	host_id, host_ip, host_port, host_dir, host_timeout, list_serv, host_own_index := get_serverinfo(myid, configfile)

	new_chan_of_follower := make(chan *Raft_Msg_Envelope)
	new_chan_of_leader := make(chan *Raft_Msg_Envelope)
	new_chan_of_candidate := make(chan *Raft_Msg_Envelope)
	new_chan_exit_follower := make(chan *bool)
	new_chan_exit_leader := make(chan *bool)
	new_chan_exit_candidate := make(chan *bool)
	new_chan_exit_raft_controller := make(chan *bool)
	new_chan_exit_inbox := make(chan *bool)

	fnm := parent_dir + "/cluster/serverlist" + strconv.Itoa(host_id) + ".xml"

	my_server_main := cluster.New(host_id, size_of_in_chan, size_of_out_chan, fnm, delay_before_conn)
	raft = Raft_Implementer{S: my_server_main, pid: host_id, ip: host_ip, port: host_port, dir: host_dir, timeout: host_timeout, serv: list_serv, own_index: host_own_index, curr_term: 0, heart_beat_frequency: 3000 * time.Millisecond, chan_of_follower: new_chan_of_follower, chan_of_leader: new_chan_of_leader, chan_of_candidate: new_chan_of_candidate, chan_exit_follower: new_chan_exit_follower, chan_exit_leader: new_chan_exit_leader, chan_exit_candidate: new_chan_exit_candidate, chan_exit_raft_controller: new_chan_exit_raft_controller, chan_exit_inbox: new_chan_exit_inbox, phase: 0}

	return &raft

}