Commit 917043b4 authored by Irene Y Zhang's avatar Irene Y Zhang

running gofmt over everything

parent f9758b35
......@@ -4,16 +4,15 @@ import "net/rpc"
import "fmt"
type Clerk struct {
servers []string
// You will have to modify this struct.
servers []string
// You will have to modify this struct.
}
func MakeClerk(servers []string) *Clerk {
ck := new(Clerk)
ck.servers = servers
// You'll have to add code here.
return ck
ck := new(Clerk)
ck.servers = servers
// You'll have to add code here.
return ck
}
//
......@@ -33,20 +32,20 @@ func MakeClerk(servers []string) *Clerk {
// please don't change this function.
//
func call(srv string, rpcname string,
args interface{}, reply interface{}) bool {
c, errx := rpc.Dial("unix", srv)
if errx != nil {
return false
}
defer c.Close()
err := c.Call(rpcname, args, reply)
if err == nil {
return true
}
args interface{}, reply interface{}) bool {
c, errx := rpc.Dial("unix", srv)
if errx != nil {
return false
}
defer c.Close()
err := c.Call(rpcname, args, reply)
if err == nil {
return true
}
fmt.Println(err)
return false
fmt.Println(err)
return false
}
//
......@@ -55,8 +54,8 @@ func call(srv string, rpcname string,
// keeps trying forever in the face of all other errors.
//
func (ck *Clerk) Get(key string) string {
// You will have to modify this function.
return ""
// You will have to modify this function.
return ""
}
//
......@@ -64,14 +63,14 @@ func (ck *Clerk) Get(key string) string {
// keeps trying until it succeeds.
//
func (ck *Clerk) PutExt(key string, value string, dohash bool) string {
// You will have to modify this function.
return ""
// You will have to modify this function.
return ""
}
func (ck *Clerk) Put(key string, value string) {
ck.PutExt(key, value, false)
ck.PutExt(key, value, false)
}
func (ck *Clerk) PutHash(key string, value string) string {
v := ck.PutExt(key, value, true)
return v
v := ck.PutExt(key, value, true)
return v
}
......@@ -3,38 +3,39 @@ package kvpaxos
import "hash/fnv"
const (
OK = "OK"
ErrNoKey = "ErrNoKey"
OK = "OK"
ErrNoKey = "ErrNoKey"
)
type Err string
type PutArgs struct {
// You'll have to add definitions here.
Key string
Value string
DoHash bool // For PutHash
// You'll have to add definitions here.
// Field names must start with capital letters,
// otherwise RPC will break.
// You'll have to add definitions here.
Key string
Value string
DoHash bool // For PutHash
// You'll have to add definitions here.
// Field names must start with capital letters,
// otherwise RPC will break.
}
type PutReply struct {
Err Err
PreviousValue string // For PutHash
Err Err
PreviousValue string // For PutHash
}
type GetArgs struct {
Key string
// You'll have to add definitions here.
Key string
// You'll have to add definitions here.
}
type GetReply struct {
Err Err
Value string
Err Err
Value string
}
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
......@@ -11,52 +11,50 @@ import "syscall"
import "encoding/gob"
import "math/rand"
const Debug=0
const Debug = 0
func DPrintf(format string, a ...interface{}) (n int, err error) {
if Debug > 0 {
log.Printf(format, a...)
}
return
if Debug > 0 {
log.Printf(format, a...)
}
return
}
type Op struct {
// Your definitions here.
// Field names must start with capital letters,
// otherwise RPC will break.
// Your definitions here.
// Field names must start with capital letters,
// otherwise RPC will break.
}
type KVPaxos struct {
mu sync.Mutex
l net.Listener
me int
dead bool // for testing
unreliable bool // for testing
px *paxos.Paxos
// Your definitions here.
mu sync.Mutex
l net.Listener
me int
dead bool // for testing
unreliable bool // for testing
px *paxos.Paxos
// Your definitions here.
}
func (kv *KVPaxos) Get(args *GetArgs, reply *GetReply) error {
// Your code here.
return nil
// Your code here.
return nil
}
func (kv *KVPaxos) Put(args *PutArgs, reply *PutReply) error {
// Your code here.
// Your code here.
return nil
return nil
}
// tell the server to shut itself down.
// please do not change this function.
func (kv *KVPaxos) kill() {
DPrintf("Kill(%d): die\n", kv.me)
kv.dead = true
kv.l.Close()
kv.px.Kill()
DPrintf("Kill(%d): die\n", kv.me)
kv.dead = true
kv.l.Close()
kv.px.Kill()
}
//
......@@ -64,62 +62,60 @@ func (kv *KVPaxos) kill() {
// servers that will cooperate via Paxos to
// form the fault-tolerant key/value service.
// me is the index of the current server in servers[].
//
//
func StartServer(servers []string, me int) *KVPaxos {
// call gob.Register on structures you want
// Go's RPC library to marshall/unmarshall.
gob.Register(Op{})
kv := new(KVPaxos)
kv.me = me
// Your initialization code here.
rpcs := rpc.NewServer()
rpcs.Register(kv)
kv.px = paxos.Make(servers, me, rpcs)
os.Remove(servers[me])
l, e := net.Listen("unix", servers[me]);
if e != nil {
log.Fatal("listen error: ", e);
}
kv.l = l
// please do not change any of the following code,
// or do anything to subvert it.
go func() {
for kv.dead == false {
conn, err := kv.l.Accept()
if err == nil && kv.dead == false {
if kv.unreliable && (rand.Int63() % 1000) < 100 {
// discard the request.
conn.Close()
} else if kv.unreliable && (rand.Int63() % 1000) < 200 {
// process the request but force discard of reply.
c1 := conn.(*net.UnixConn)
f, _ := c1.File()
err := syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR)
if err != nil {
fmt.Printf("shutdown: %v\n", err)
}
go rpcs.ServeConn(conn)
} else {
go rpcs.ServeConn(conn)
}
} else if err == nil {
conn.Close()
}
if err != nil && kv.dead == false {
fmt.Printf("KVPaxos(%v) accept: %v\n", me, err.Error())
kv.kill()
}
}
}()
return kv
// call gob.Register on structures you want
// Go's RPC library to marshall/unmarshall.
gob.Register(Op{})
kv := new(KVPaxos)
kv.me = me
// Your initialization code here.
rpcs := rpc.NewServer()
rpcs.Register(kv)
kv.px = paxos.Make(servers, me, rpcs)
os.Remove(servers[me])
l, e := net.Listen("unix", servers[me])
if e != nil {
log.Fatal("listen error: ", e)
}
kv.l = l
// please do not change any of the following code,
// or do anything to subvert it.
go func() {
for kv.dead == false {
conn, err := kv.l.Accept()
if err == nil && kv.dead == false {
if kv.unreliable && (rand.Int63()%1000) < 100 {
// discard the request.
conn.Close()
} else if kv.unreliable && (rand.Int63()%1000) < 200 {
// process the request but force discard of reply.
c1 := conn.(*net.UnixConn)
f, _ := c1.File()
err := syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR)
if err != nil {
fmt.Printf("shutdown: %v\n", err)
}
go rpcs.ServeConn(conn)
} else {
go rpcs.ServeConn(conn)
}
} else if err == nil {
conn.Close()
}
if err != nil && kv.dead == false {
fmt.Printf("KVPaxos(%v) accept: %v\n", me, err.Error())
kv.kill()
}
}
}()
return kv
}
......@@ -9,647 +9,647 @@ import "fmt"
import "math/rand"
func check(t *testing.T, ck *Clerk, key string, value string) {
v := ck.Get(key)
if v != value {
t.Fatalf("Get(%v) -> %v, expected %v", key, v, value)
}
v := ck.Get(key)
if v != value {
t.Fatalf("Get(%v) -> %v, expected %v", key, v, value)
}
}
func port(tag string, host int) string {
s := "/var/tmp/824-"
s += strconv.Itoa(os.Getuid()) + "/"
os.Mkdir(s, 0777)
s += "kv-"
s += strconv.Itoa(os.Getpid()) + "-"
s += tag + "-"
s += strconv.Itoa(host)
return s
s := "/var/tmp/824-"
s += strconv.Itoa(os.Getuid()) + "/"
os.Mkdir(s, 0777)
s += "kv-"
s += strconv.Itoa(os.Getpid()) + "-"
s += tag + "-"
s += strconv.Itoa(host)
return s
}
func cleanup(kva []*KVPaxos) {
for i := 0; i < len(kva); i++ {
if kva[i] != nil {
kva[i].kill()
}
}
for i := 0; i < len(kva); i++ {
if kva[i] != nil {
kva[i].kill()
}
}
}
func NextValue(hprev string, val string) string {
h := hash(hprev + val)
return strconv.Itoa(int(h))
h := hash(hprev + val)
return strconv.Itoa(int(h))
}
func TestBasic(t *testing.T) {
runtime.GOMAXPROCS(4)
const nservers = 3
var kva []*KVPaxos = make([]*KVPaxos, nservers)
var kvh []string = make([]string, nservers)
defer cleanup(kva)
for i := 0; i < nservers; i++ {
kvh[i] = port("basic", i)
}
for i := 0; i < nservers; i++ {
kva[i] = StartServer(kvh, i)
}
ck := MakeClerk(kvh)
var cka [nservers]*Clerk
for i := 0; i < nservers; i++ {
cka[i] = MakeClerk([]string{kvh[i]})
}
fmt.Printf("Test: Basic put/puthash/get ...\n")
pv := ck.PutHash("a", "x")
ov := ""
if ov != pv {
t.Fatalf("wrong value; expected %s got %s", ov, pv)
}
ck.Put("a", "aa")
check(t, ck, "a", "aa")
cka[1].Put("a", "aaa")
check(t, cka[2], "a", "aaa")
check(t, cka[1], "a", "aaa")
check(t, ck, "a", "aaa")
fmt.Printf(" ... Passed\n")
fmt.Printf("Test: Concurrent clients ...\n")
for iters := 0; iters < 20; iters++ {
const npara = 15
var ca [npara]chan bool
for nth := 0; nth < npara; nth++ {
ca[nth] = make(chan bool)
go func(me int) {
defer func() { ca[me] <- true }()
ci := (rand.Int() % nservers)
myck := MakeClerk([]string{kvh[ci]})
if (rand.Int() % 1000) < 500 {
myck.Put("b", strconv.Itoa(rand.Int()))
} else {
myck.Get("b")
}
}(nth)
}
for nth := 0; nth < npara; nth++ {
<- ca[nth]
}
var va [nservers]string
for i := 0; i < nservers; i++ {
va[i] = cka[i].Get("b")
if va[i] != va[0] {
t.Fatalf("mismatch")
}
}
}
fmt.Printf(" ... Passed\n")
time.Sleep(1 * time.Second)
runtime.GOMAXPROCS(4)
const nservers = 3
var kva []*KVPaxos = make([]*KVPaxos, nservers)
var kvh []string = make([]string, nservers)
defer cleanup(kva)
for i := 0; i < nservers; i++ {
kvh[i] = port("basic", i)
}
for i := 0; i < nservers; i++ {
kva[i] = StartServer(kvh, i)
}
ck := MakeClerk(kvh)
var cka [nservers]*Clerk
for i := 0; i < nservers; i++ {
cka[i] = MakeClerk([]string{kvh[i]})
}
fmt.Printf("Test: Basic put/puthash/get ...\n")
pv := ck.PutHash("a", "x")
ov := ""
if ov != pv {
t.Fatalf("wrong value; expected %s got %s", ov, pv)
}
ck.Put("a", "aa")
check(t, ck, "a", "aa")
cka[1].Put("a", "aaa")
check(t, cka[2], "a", "aaa")
check(t, cka[1], "a", "aaa")
check(t, ck, "a", "aaa")
fmt.Printf(" ... Passed\n")
fmt.Printf("Test: Concurrent clients ...\n")
for iters := 0; iters < 20; iters++ {
const npara = 15
var ca [npara]chan bool
for nth := 0; nth < npara; nth++ {
ca[nth] = make(chan bool)
go func(me int) {
defer func() { ca[me] <- true }()
ci := (rand.Int() % nservers)
myck := MakeClerk([]string{kvh[ci]})
if (rand.Int() % 1000) < 500 {
myck.Put("b", strconv.Itoa(rand.Int()))
} else {
myck.Get("b")
}
}(nth)
}
for nth := 0; nth < npara; nth++ {
<-ca[nth]
}
var va [nservers]string
for i := 0; i < nservers; i++ {
va[i] = cka[i].Get("b")
if va[i] != va[0] {
t.Fatalf("mismatch")
}
}
}
fmt.Printf(" ... Passed\n")
time.Sleep(1 * time.Second)
}
func TestDone(t *testing.T) {
runtime.GOMAXPROCS(4)
const nservers = 3
var kva []*KVPaxos = make([]*KVPaxos, nservers)
var kvh []string = make([]string, nservers)
defer cleanup(kva)
for i := 0; i < nservers; i++ {
kvh[i] = port("done", i)
}
for i := 0; i < nservers; i++ {
kva[i] = StartServer(kvh, i)
}
ck := MakeClerk(kvh)
var cka [nservers]*Clerk
for pi := 0; pi < nservers; pi++ {
cka[pi] = MakeClerk([]string{kvh[pi]})
}
fmt.Printf("Test: server frees Paxos log memory...\n")
ck.Put("a", "aa")
check(t, ck, "a", "aa")
runtime.GC()
var m0 runtime.MemStats
runtime.ReadMemStats(&m0)
// rtm's m0.Alloc is 2 MB
sz := 1000000
items := 10
for iters := 0; iters < 2; iters++ {
for i := 0; i < items; i++ {
key := strconv.Itoa(i)
value := make([]byte, sz)
for j := 0; j < len(value); j++ {
value[j] = byte((rand.Int() % 100) + 1)
}
ck.Put(key, string(value))
check(t, cka[i % nservers], key, string(value))
}
}
// Put and Get to each of the replicas, in case
// the Done information is piggybacked on
// the Paxos proposer messages.
for iters := 0; iters < 2; iters++ {
for pi := 0; pi < nservers; pi++ {
cka[pi].Put("a", "aa")
check(t, cka[pi], "a", "aa")
}
}
time.Sleep(1 * time.Second)
runtime.GC()
var m1 runtime.MemStats
runtime.ReadMemStats(&m1)
// rtm's m1.Alloc is 45 MB
// fmt.Printf(" Memory: before %v, after %v\n", m0.Alloc, m1.Alloc)