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

successful run of redirect test

parent 58cf8744
......@@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"encoding/gob"
"log"
"net"
"raft"
"strconv"
......@@ -116,7 +117,7 @@ func Write(conn net.Conn, msg string) {
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()
//channel for every connection for every client
ch := make(chan []byte)
......@@ -125,6 +126,8 @@ func HandleClient(conn net.Conn, rft *raft.Raft) {
for {
command := new(utils.Command)
msg := <-ch
logger.Println("got:", msg)
if len(msg) == 0 {
continue
}
......@@ -132,17 +135,19 @@ func HandleClient(conn net.Conn, rft *raft.Raft) {
flag := false
nr := uint64(0)
tokens := strings.Fields(string(msg))
if tokens[0] == "CAS" {
n, _ := strconv.ParseUint(tokens[3], 10, 64)
if tokens[0] == "cas" {
n, _ := strconv.ParseUint(tokens[4], 10, 64)
nr = n
flag = true
} else if tokens[0] == "SET" {
n, _ := strconv.ParseUint(tokens[2], 10, 64)
} else if tokens[0] == "set" {
n, _ := strconv.ParseUint(tokens[3], 10, 64)
nr = n
flag = true
}
if flag {
logger.Println("numbytes", nr)
if v, err := readValue(ch, nr); err {
logger.Println("error reading value")
Write(conn, "ERR_CMD_ERR")
continue
} else {
......@@ -159,6 +164,10 @@ func HandleClient(conn net.Conn, rft *raft.Raft) {
//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 {
return entry.committed
}
func Test() {
}
//make raft implement the append function
func (rft *Raft) Append(data []byte, conn net.Conn) (LogEntry, error) {
if rft.id != 1 {
......
......@@ -74,7 +74,7 @@ func initClientCommunication(server *raft.ServerConfig, rft *raft.Raft, ch chan
Info.Fatal("client accept error: " + err.Error())
} else {
Info.Printf("client new connection established\n")
go connhandler.HandleClient(conn, rft)
go connhandler.HandleClient(conn, rft, Info)
}
}
ch <- true
......
......@@ -4,6 +4,7 @@ package main
import (
"bytes"
"fmt"
"net"
"os"
"os/exec"
......@@ -34,6 +35,9 @@ func TestAll(t *testing.T) {
//run client that tries connecting to the followers
testConnectFollower(t)
//test no reply
testNoReply(t)
}
//run servers
......@@ -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
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
conn, err := net.Dial("tcp", ":"+strconv.Itoa(server_port))
if err != nil {
......@@ -60,7 +64,9 @@ func testConnectFollower(t *testing.T) {
} else {
time.Sleep(time.Millisecond)
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)
buffer := make([]byte, 1024)
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