Ejemplo n.º 1
0
/******************************************************************************************
	Main() method , starts the proceedings by sending the Server in Follower phase
******************************************************************************************/
func main() {

	myServerId, _ := strconv.Atoi(os.Args[1])
	serverStateFile := os.Args[2]
	configFile := os.Args[3]
	minTimeOut, _ := strconv.Atoi(os.Args[4])
	maxTimeOut, _ := strconv.Atoi(os.Args[5])

	fmt.Println("Starting Main", myServerId)

	machine := NewMachine(myServerId, serverStateFile, configFile, minTimeOut, maxTimeOut)

	//***********For Tester Purpose Only***********
	testerPort := os.Args[6]

	var t Testerstruct

	t.TesterInit(&machine, testerPort)
	go t.TesterRecv(&machine)

	//*********Tester Methods Closed***************
	go cluster.ReceiveMessage(&machine.server)

	go cluster.SendMessage(&machine.server)

	fmt.Println("\nElection TimeOut", machine.Election_Timeout())

	Start(&machine)

}
Ejemplo n.º 2
0
func main() {

	gob.Register(cluster.AppendEntriesRequestStruct{})
	gob.Register(cluster.AppendEntriesResponseStruct{})
	gob.Register(cluster.RequestVoteRequestStruct{})
	gob.Register(cluster.RequestVoteResponseStruct{})

	myServerId, _ := strconv.Atoi(os.Args[1])
	//serverStateFile := os.Args[2]
	//configFile := os.Args[3]
	//minTimeOut, _ := strconv.Atoi(os.Args[4])
	//maxTimeOut, _ := strconv.Atoi(os.Args[5])

	serverStateFile := "./serverState.json"
	configFile := "./config.json"
	minTimeOut := 5000
	maxTimeOut := 6000

	fmt.Println("Starting Main", myServerId)

	machine := NewMachine(myServerId, serverStateFile, configFile, minTimeOut, maxTimeOut)

	machine.Log = make([]cluster.LogEntry, 0)
	machine.LastLogIndex = -1
	machine.CommitIndex = -1
	machine.LastApplied = -1
	machine.NextIndex = make(map[int]int64)
	machine.MatchIndex = make(map[int]int64)
	machine.FollowerTerm = make(map[int]int)

	for _, Sid := range machine.server.Peers() {

		machine.NextIndex[Sid] = int64(0)
		machine.MatchIndex[Sid] = int64(-1)
		machine.FollowerTerm[Sid] = 0
	}

	//***********For Tester Purpose Only***********
	/*testerPort := os.Args[6]

	var t Testerstruct

	t.TesterInit(&machine, testerPort)
	go t.TesterRecv(&machine)
	*/
	//*********Tester Methods Closed***************
	go cluster.ReceiveMessage(&machine.server)

	go cluster.SendMessage(&machine.server)

	go machine.StartRaftInbox()

	fmt.Println("\nElection TimeOut", machine.Election_Timeout())

	Start(&machine)

}
Ejemplo n.º 3
0
func TestBroadcastMsgCount(t *testing.T) {

	// Create 4 servers
	serverArray := make([]cluster.Server, 4)

	send_count := 0
	received_count := 0

	//	all_send_sockets := make([][]sendSocket,4,3)

	// Start the receiver first , so that we catch all the packets
	for tempServerId := 0; tempServerId <= 3; tempServerId++ {
		serverArray[tempServerId] = cluster.NewServer(tempServerId+1, "config.json")

		// fmt.Println("Press Any Enter to Start ...")
		// var line string
		// fmt.Scanln(&line)

		go cluster.SendMessage(serverArray[tempServerId])
		go cluster.ReceiveMessage(serverArray[tempServerId])

		go receive_always(serverArray[tempServerId], &received_count)
	}

	for tempServerId := 0; tempServerId <= 3; tempServerId++ {

		// Put cluster.Envelope after implementing Pacakges
		for i := 1; i <= 50; i++ {
			serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: cluster.BROADCAST, Msg: "hello there"}
			send_count += 3
		}

		fmt.Println("Sent all messages for Server ", serverArray[tempServerId].Pid())
	}

	time.Sleep(2 * time.Second)

	fmt.Printf("\n--------Going to Count---------")
	fmt.Printf("\nTotal msg sent " + strconv.Itoa(send_count))
	fmt.Printf("\nReceived msg   " + strconv.Itoa(received_count))

	if send_count != received_count {
		t.Errorf("send_count - %v , received_count - %v", send_count, received_count)
	}

}
Ejemplo n.º 4
0
/******************************************************************************************
	Main() method , starts the proceedings by sending the Server in Follower phase
******************************************************************************************/
func main() {

	gob.Register(cluster.AppendEntriesRequestStruct{})
	gob.Register(cluster.AppendEntriesResponseStruct{})
	gob.Register(cluster.RequestVoteRequestStruct{})
	gob.Register(cluster.RequestVoteResponseStruct{})

	myServerId, _ := strconv.Atoi(os.Args[1])
	serverStateFile := os.Args[2]
	configFile := os.Args[3]
	minTimeOut, _ := strconv.Atoi(os.Args[4])
	maxTimeOut, _ := strconv.Atoi(os.Args[5])

	fmt.Println("Starting Main", myServerId)

	machine := NewMachine(myServerId, serverStateFile, configFile, minTimeOut, maxTimeOut)

	machine.Log = make([]cluster.LogEntry, 0)
	machine.NextIndex = make(map[int]int64)
	machine.MatchIndex = make(map[int]int64)
	machine.FollowerTerm = make(map[int]int)
	machine.Client_channel = make(chan string)

	machine.LastLogIndex = -1
	machine.CommitIndex = -1
	machine.LastApplied = -1
	machine.LogKVStore, machine.DataKVStore = machine.loadDataFromLevelDB()

	if machine.LogKVStore == nil || machine.DataKVStore == nil {

		fmt.Println("Error in LevelDB files")
		return
	}

	for _, Sid := range machine.server.Peers() {

		machine.NextIndex[Sid] = int64(0)
		machine.MatchIndex[Sid] = int64(-1)
		machine.FollowerTerm[Sid] = 0
	}

	//***********For Tester Purpose Only***********
	testerPort := os.Args[6]

	var t Testerstruct

	t.TesterInit(&machine, testerPort)

	go t.TesterRecv(&machine)

	//*********Tester Methods Closed***************
	go cluster.ReceiveMessage(&machine.server)

	go cluster.SendMessage(&machine.server)

	//go machine.every5sec()

	//go machine.StartRaftInbox()

	fmt.Println("\nElection TimeOut", machine.Election_Timeout())

	Start(&machine)

}
Ejemplo n.º 5
0
func TestUnicastMsgCount(t *testing.T) {

	// Create 4 servers
	serverArray := make([]cluster.Server, 4)

	send_count := 0
	received_count := 0

	// Start the receiver first , so that we catch all the packets
	for tempServerId := 0; tempServerId <= 3; tempServerId++ {
		serverArray[tempServerId] = cluster.NewServer(tempServerId+1, "config.json")

		go cluster.SendMessage(serverArray[tempServerId])
		go cluster.ReceiveMessage(serverArray[tempServerId])

		// *********** Error Prone Code ********************

		// 1. Corrected code , which calls a function created below
		// Uncomment following line to work smoothly without exception (and comment option 2)
		// go receive_always(serverArray[tempServerId], &received_count)

		// 2. Errorneous code , which create a go-routine here itself and somehow throws exception
		// Comment following code to work smoothly and Uncomment option 1
		go func() {
			fmt.Println("Value of i :- ", tempServerId)
			_ = <-serverArray[tempServerId].Inbox()
			mutex1.Lock()
			received_count += 1
			mutex1.Unlock()
		}()

		// ********** Ends *********************************

	}

	for tempServerId := 0; tempServerId <= 3; tempServerId++ {

		count := rand.Intn(100)

		serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: 1, Msg: "hello there"}
		send_count += 1

		serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: 2, Msg: "hello there"}
		send_count += 1
		serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: 3, Msg: "hello there"}
		send_count += 1
		serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: 4, Msg: "hello there"}
		send_count += 1

		// Take few random values and send those many Envelopes
		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: cluster.BROADCAST, Msg: "hello there"}
			send_count += 3
		}

		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: 1, Msg: "hello there"}
			send_count += 1
		}

		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: 2, Msg: "hello there"}
			send_count += 1
		}

		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: 3, Msg: "hello there"}
			send_count += 1
		}

		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: 4, Msg: "hello there"}
			send_count += 1
		}

		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[tempServerId].Outbox() <- &cluster.Envelope{Pid: cluster.BROADCAST, Msg: "hello there"}
			send_count += 3
		}

		fmt.Println("Sent all messages for Server ", serverArray[tempServerId].Pid())

	}

	time.Sleep(2 * time.Second)

	fmt.Printf("\n--------Going to Count---------")
	fmt.Printf("\nTotal msg sent " + strconv.Itoa(send_count))
	fmt.Printf("\nReceived msg   " + strconv.Itoa(received_count))

	if send_count != received_count {
		t.Errorf("send_count - %v , received_count - %v", send_count, received_count)
	}

}
Ejemplo n.º 6
0
// Testing only for UNICAST messaging
func TestUnicastMsgCount(t *testing.T) {

	send_count = 0
	received_count = 0

	fmt.Printf("\n\nTesting UNICAST ...\n")

	// Create 4 servers
	// Start the receiver first , so that we catch all the packets
	for id := 0; id <= 3; id++ {
		serverArray[id] = cluster.NewServer(id+1, "config.json")

		go cluster.SendMessage(serverArray[id])
		go cluster.ReceiveMessage(serverArray[id])

		go receive_always(serverArray[id], &received_count)
	}

	for id := 0; id <= 3; id++ {

		count := rand.Intn(100)

		serverArray[id].Outbox() <- &cluster.Envelope{Pid: 1, Msg: "hello there"}
		send_count += 1

		serverArray[id].Outbox() <- &cluster.Envelope{Pid: 2, Msg: "hello there"}
		send_count += 1
		serverArray[id].Outbox() <- &cluster.Envelope{Pid: 3, Msg: "hello there"}
		send_count += 1
		serverArray[id].Outbox() <- &cluster.Envelope{Pid: 4, Msg: "hello there"}
		send_count += 1

		// Take few random values and send those many Envelopes
		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[id].Outbox() <- &cluster.Envelope{Pid: 1, Msg: "hello there"}
			send_count += 1
		}

		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[id].Outbox() <- &cluster.Envelope{Pid: 2, Msg: "hello there"}
			send_count += 1
		}

		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[id].Outbox() <- &cluster.Envelope{Pid: 3, Msg: "hello there"}
			send_count += 1
		}

		count = rand.Intn(1000)
		for i := 1; i <= count; i++ {
			serverArray[id].Outbox() <- &cluster.Envelope{Pid: 4, Msg: "hello there"}
			send_count += 1
		}

		//fmt.Println("Sent all messages for Server ", serverArray[id].Pid())

	}

	time.Sleep(2 * time.Second)

	fmt.Printf("\n--------Going to Count---------")
	fmt.Printf("\nTotal msg sent " + strconv.Itoa(send_count))
	fmt.Printf("\nReceived msg   " + strconv.Itoa(received_count))

	fmt.Printf("\nTesing UNICAST result")
	if send_count != received_count {
		t.Errorf("send_count - %v , received_count - %v", send_count, received_count)
	}
	fmt.Printf("\n\n\n")

}