Commit 9493496f authored by Sushant Mahajan's avatar Sushant Mahajan

successful run of redirect test

parent 58cf8744
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/gob" "encoding/gob"
"log"
"net" "net"
"raft" "raft"
"strconv" "strconv"
...@@ -116,7 +117,7 @@ func Write(conn net.Conn, msg string) { ...@@ -116,7 +117,7 @@ func Write(conn net.Conn, msg string) {
conn.Write(buf) conn.Write(buf)
} }
func HandleClient(conn net.Conn, rft *raft.Raft) { func HandleClient(conn net.Conn, rft *raft.Raft, logger *log.Logger) {
defer conn.Close() defer conn.Close()
//channel for every connection for every client //channel for every connection for every client
ch := make(chan []byte) ch := make(chan []byte)
...@@ -125,6 +126,8 @@ func HandleClient(conn net.Conn, rft *raft.Raft) { ...@@ -125,6 +126,8 @@ func HandleClient(conn net.Conn, rft *raft.Raft) {
for { for {
command := new(utils.Command) command := new(utils.Command)
msg := <-ch msg := <-ch
logger.Println("got:", msg)
if len(msg) == 0 { if len(msg) == 0 {
continue continue
} }
...@@ -132,17 +135,19 @@ func HandleClient(conn net.Conn, rft *raft.Raft) { ...@@ -132,17 +135,19 @@ func HandleClient(conn net.Conn, rft *raft.Raft) {
flag := false flag := false
nr := uint64(0) nr := uint64(0)
tokens := strings.Fields(string(msg)) tokens := strings.Fields(string(msg))
if tokens[0] == "CAS" { if tokens[0] == "cas" {
n, _ := strconv.ParseUint(tokens[3], 10, 64) n, _ := strconv.ParseUint(tokens[4], 10, 64)
nr = n nr = n
flag = true flag = true
} else if tokens[0] == "SET" { } else if tokens[0] == "set" {
n, _ := strconv.ParseUint(tokens[2], 10, 64) n, _ := strconv.ParseUint(tokens[3], 10, 64)
nr = n nr = n
flag = true flag = true
} }
if flag { if flag {
logger.Println("numbytes", nr)
if v, err := readValue(ch, nr); err { if v, err := readValue(ch, nr); err {
logger.Println("error reading value")
Write(conn, "ERR_CMD_ERR") Write(conn, "ERR_CMD_ERR")
continue continue
} else { } else {
...@@ -159,6 +164,10 @@ func HandleClient(conn net.Conn, rft *raft.Raft) { ...@@ -159,6 +164,10 @@ func HandleClient(conn net.Conn, rft *raft.Raft) {
//log.Fatal("encode error:", err) //log.Fatal("encode error:", err)
} }
rft.Append(buffer.Bytes(), conn) if _, err := rft.Append(buffer.Bytes(), conn); err != nil {
Write(conn, "ERR_REDIRECT 127.0.0.1 "+strconv.Itoa(raft.CLIENT_PORT+1))
conn.Close()
break
}
} }
} }
...@@ -131,10 +131,6 @@ func (entry *LogEntryData) Committed() bool { ...@@ -131,10 +131,6 @@ func (entry *LogEntryData) Committed() bool {
return entry.committed return entry.committed
} }
func Test() {
}
//make raft implement the append function //make raft implement the append function
func (rft *Raft) Append(data []byte, conn net.Conn) (LogEntry, error) { func (rft *Raft) Append(data []byte, conn net.Conn) (LogEntry, error) {
if rft.id != 1 { if rft.id != 1 {
......
...@@ -74,7 +74,7 @@ func initClientCommunication(server *raft.ServerConfig, rft *raft.Raft, ch chan ...@@ -74,7 +74,7 @@ func initClientCommunication(server *raft.ServerConfig, rft *raft.Raft, ch chan
Info.Fatal("client accept error: " + err.Error()) Info.Fatal("client accept error: " + err.Error())
} else { } else {
Info.Printf("client new connection established\n") Info.Printf("client new connection established\n")
go connhandler.HandleClient(conn, rft) go connhandler.HandleClient(conn, rft, Info)
} }
} }
ch <- true ch <- true
......
...@@ -4,6 +4,7 @@ package main ...@@ -4,6 +4,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"net" "net"
"os" "os"
"os/exec" "os/exec"
...@@ -34,6 +35,9 @@ func TestAll(t *testing.T) { ...@@ -34,6 +35,9 @@ func TestAll(t *testing.T) {
//run client that tries connecting to the followers //run client that tries connecting to the followers
testConnectFollower(t) testConnectFollower(t)
//test no reply
testNoReply(t)
} }
//run servers //run servers
...@@ -52,7 +56,7 @@ func testServersCommunic(i int, t *testing.T) { ...@@ -52,7 +56,7 @@ func testServersCommunic(i int, t *testing.T) {
//on trying to connect with the followers the client should get redirect err from the followers //on trying to connect with the followers the client should get redirect err from the followers
func testConnectFollower(t *testing.T) { func testConnectFollower(t *testing.T) {
for i := 2; i < NUM_SERVERS; i++ { //the followers start at second position for i := 2; i <= NUM_SERVERS; i++ { //the followers start at second position
server_port := raft.CLIENT_PORT + i server_port := raft.CLIENT_PORT + i
conn, err := net.Dial("tcp", ":"+strconv.Itoa(server_port)) conn, err := net.Dial("tcp", ":"+strconv.Itoa(server_port))
if err != nil { if err != nil {
...@@ -60,7 +64,9 @@ func testConnectFollower(t *testing.T) { ...@@ -60,7 +64,9 @@ func testConnectFollower(t *testing.T) {
} else { } else {
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
sending := []byte("set mykey1 100 3\r\nlul\r\n") sending := []byte("set mykey1 100 3\r\nlul\r\n")
expecting := []byte("ERR_REDIRECT 127.0.0.1 " + strconv.Itoa(raft.CLIENT_PORT+1) + "\r\n" + "ERR_REDIRECT 127.0.0.1 " + strconv.Itoa(raft.CLIENT_PORT+1) + "\r\n") port := strconv.Itoa(raft.CLIENT_PORT + 1)
expecting := []byte("ERR_REDIRECT 127.0.0.1 " + port + "\r\n")
conn.Write(sending) conn.Write(sending)
buffer := make([]byte, 1024) buffer := make([]byte, 1024)
conn.Read(buffer) conn.Read(buffer)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment