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" ...@@ -4,16 +4,15 @@ import "net/rpc"
import "fmt" import "fmt"
type Clerk struct { type Clerk struct {
servers []string servers []string
// You will have to modify this struct. // You will have to modify this struct.
} }
func MakeClerk(servers []string) *Clerk { func MakeClerk(servers []string) *Clerk {
ck := new(Clerk) ck := new(Clerk)
ck.servers = servers ck.servers = servers
// You'll have to add code here. // You'll have to add code here.
return ck return ck
} }
// //
...@@ -33,20 +32,20 @@ func MakeClerk(servers []string) *Clerk { ...@@ -33,20 +32,20 @@ func MakeClerk(servers []string) *Clerk {
// please don't change this function. // please don't change this function.
// //
func call(srv string, rpcname string, func call(srv string, rpcname string,
args interface{}, reply interface{}) bool { args interface{}, reply interface{}) bool {
c, errx := rpc.Dial("unix", srv) c, errx := rpc.Dial("unix", srv)
if errx != nil { if errx != nil {
return false return false
} }
defer c.Close() defer c.Close()
err := c.Call(rpcname, args, reply) err := c.Call(rpcname, args, reply)
if err == nil { if err == nil {
return true return true
} }
fmt.Println(err) fmt.Println(err)
return false return false
} }
// //
...@@ -55,8 +54,8 @@ func call(srv string, rpcname string, ...@@ -55,8 +54,8 @@ func call(srv string, rpcname string,
// keeps trying forever in the face of all other errors. // keeps trying forever in the face of all other errors.
// //
func (ck *Clerk) Get(key string) string { func (ck *Clerk) Get(key string) string {
// You will have to modify this function. // You will have to modify this function.
return "" return ""
} }
// //
...@@ -64,14 +63,14 @@ func (ck *Clerk) Get(key string) string { ...@@ -64,14 +63,14 @@ func (ck *Clerk) Get(key string) string {
// keeps trying until it succeeds. // keeps trying until it succeeds.
// //
func (ck *Clerk) PutExt(key string, value string, dohash bool) string { func (ck *Clerk) PutExt(key string, value string, dohash bool) string {
// You will have to modify this function. // You will have to modify this function.
return "" return ""
} }
func (ck *Clerk) Put(key string, value string) { 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 { func (ck *Clerk) PutHash(key string, value string) string {
v := ck.PutExt(key, value, true) v := ck.PutExt(key, value, true)
return v return v
} }
...@@ -3,38 +3,39 @@ package kvpaxos ...@@ -3,38 +3,39 @@ package kvpaxos
import "hash/fnv" import "hash/fnv"
const ( const (
OK = "OK" OK = "OK"
ErrNoKey = "ErrNoKey" ErrNoKey = "ErrNoKey"
) )
type Err string type Err string
type PutArgs struct { type PutArgs struct {
// You'll have to add definitions here. // You'll have to add definitions here.
Key string Key string
Value string Value string
DoHash bool // For PutHash DoHash bool // For PutHash
// You'll have to add definitions here. // You'll have to add definitions here.
// Field names must start with capital letters, // Field names must start with capital letters,
// otherwise RPC will break. // otherwise RPC will break.
} }
type PutReply struct { type PutReply struct {
Err Err Err Err
PreviousValue string // For PutHash PreviousValue string // For PutHash
} }
type GetArgs struct { type GetArgs struct {
Key string Key string
// You'll have to add definitions here. // You'll have to add definitions here.
} }
type GetReply struct { type GetReply struct {
Err Err Err Err
Value string Value string
} }
func hash(s string) uint32 { func hash(s string) uint32 {
h := fnv.New32a() h := fnv.New32a()
h.Write([]byte(s)) h.Write([]byte(s))
return h.Sum32() return h.Sum32()
} }
...@@ -11,52 +11,50 @@ import "syscall" ...@@ -11,52 +11,50 @@ import "syscall"
import "encoding/gob" import "encoding/gob"
import "math/rand" import "math/rand"
const Debug=0 const Debug = 0
func DPrintf(format string, a ...interface{}) (n int, err error) { func DPrintf(format string, a ...interface{}) (n int, err error) {
if Debug > 0 { if Debug > 0 {
log.Printf(format, a...) log.Printf(format, a...)
} }
return return
} }
type Op struct { type Op struct {
// Your definitions here. // Your definitions here.
// Field names must start with capital letters, // Field names must start with capital letters,
// otherwise RPC will break. // otherwise RPC will break.
} }
type KVPaxos struct { type KVPaxos struct {
mu sync.Mutex mu sync.Mutex
l net.Listener l net.Listener
me int me int
dead bool // for testing dead bool // for testing
unreliable bool // for testing unreliable bool // for testing
px *paxos.Paxos px *paxos.Paxos
// Your definitions here. // Your definitions here.
} }
func (kv *KVPaxos) Get(args *GetArgs, reply *GetReply) error { func (kv *KVPaxos) Get(args *GetArgs, reply *GetReply) error {
// Your code here. // Your code here.
return nil return nil
} }
func (kv *KVPaxos) Put(args *PutArgs, reply *PutReply) error { 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. // tell the server to shut itself down.
// please do not change this function. // please do not change this function.
func (kv *KVPaxos) kill() { func (kv *KVPaxos) kill() {
DPrintf("Kill(%d): die\n", kv.me) DPrintf("Kill(%d): die\n", kv.me)
kv.dead = true kv.dead = true
kv.l.Close() kv.l.Close()
kv.px.Kill() kv.px.Kill()
} }
// //
...@@ -64,62 +62,60 @@ func (kv *KVPaxos) kill() { ...@@ -64,62 +62,60 @@ func (kv *KVPaxos) kill() {
// servers that will cooperate via Paxos to // servers that will cooperate via Paxos to
// form the fault-tolerant key/value service. // form the fault-tolerant key/value service.
// me is the index of the current server in servers[]. // me is the index of the current server in servers[].
// //
func StartServer(servers []string, me int) *KVPaxos { func StartServer(servers []string, me int) *KVPaxos {
// call gob.Register on structures you want // call gob.Register on structures you want
// Go's RPC library to marshall/unmarshall. // Go's RPC library to marshall/unmarshall.
gob.Register(Op{}) gob.Register(Op{})
kv := new(KVPaxos) kv := new(KVPaxos)
kv.me = me kv.me = me
// Your initialization code here. // Your initialization code here.
rpcs := rpc.NewServer() rpcs := rpc.NewServer()
rpcs.Register(kv) rpcs.Register(kv)
kv.px = paxos.Make(servers, me, rpcs) kv.px = paxos.Make(servers, me, rpcs)
os.Remove(servers[me]) os.Remove(servers[me])
l, e := net.Listen("unix", servers[me]); l, e := net.Listen("unix", servers[me])
if e != nil { if e != nil {
log.Fatal("listen error: ", e); log.Fatal("listen error: ", e)
} }
kv.l = l kv.l = l
// please do not change any of the following code,
// please do not change any of the following code, // or do anything to subvert it.
// or do anything to subvert it.
go func() {
go func() { for kv.dead == false {
for kv.dead == false { conn, err := kv.l.Accept()
conn, err := kv.l.Accept() if err == nil && kv.dead == false {
if err == nil && kv.dead == false { if kv.unreliable && (rand.Int63()%1000) < 100 {
if kv.unreliable && (rand.Int63() % 1000) < 100 { // discard the request.
// discard the request. conn.Close()
conn.Close() } else if kv.unreliable && (rand.Int63()%1000) < 200 {
} else if kv.unreliable && (rand.Int63() % 1000) < 200 { // process the request but force discard of reply.
// process the request but force discard of reply. c1 := conn.(*net.UnixConn)
c1 := conn.(*net.UnixConn) f, _ := c1.File()
f, _ := c1.File() err := syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR)
err := syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR) if err != nil {
if err != nil { fmt.Printf("shutdown: %v\n", err)
fmt.Printf("shutdown: %v\n", err) }
} go rpcs.ServeConn(conn)
go rpcs.ServeConn(conn) } else {
} else { go rpcs.ServeConn(conn)
go rpcs.ServeConn(conn) }
} } else if err == nil {
} else if err == nil { conn.Close()
conn.Close() }
} if err != nil && kv.dead == false {
if err != nil && kv.dead == false { fmt.Printf("KVPaxos(%v) accept: %v\n", me, err.Error())
fmt.Printf("KVPaxos(%v) accept: %v\n", me, err.Error()) kv.kill()
kv.kill() }
} }
} }()
}()
return kv
return kv
} }
This diff is collapsed.
...@@ -8,17 +8,16 @@ import "fmt" ...@@ -8,17 +8,16 @@ import "fmt"
// and maintains a little state. // and maintains a little state.
// //
type Clerk struct { type Clerk struct {
servers [2]string // primary port, backup port servers [2]string // primary port, backup port
// Your definitions here. // Your definitions here.
} }
func MakeClerk(primary string, backup string) *Clerk { func MakeClerk(primary string, backup string) *Clerk {
ck := new(Clerk) ck := new(Clerk)
ck.servers[0] = primary ck.servers[0] = primary
ck.servers[1] = backup ck.servers[1] = backup
// Your initialization code here. // Your initialization code here.
return ck return ck
} }
// //
...@@ -38,20 +37,20 @@ func MakeClerk(primary string, backup string) *Clerk { ...@@ -38,20 +37,20 @@ func MakeClerk(primary string, backup string) *Clerk {
// please don't change this function. // please don't change this function.
// //
func call(srv string, rpcname string, func call(srv string, rpcname string,
args interface{}, reply interface{}) bool { args interface{}, reply interface{}) bool {
c, errx := rpc.Dial("unix", srv) c, errx := rpc.Dial("unix", srv)
if errx != nil { if errx != nil {
return false return false
} }
defer c.Close() defer c.Close()
err := c.Call(rpcname, args, reply) err := c.Call(rpcname, args, reply)
if err == nil { if err == nil {
return true return true
} }
fmt.Println(err) fmt.Println(err)
return false return false
} }
// //
...@@ -62,20 +61,19 @@ func call(srv string, rpcname string, ...@@ -62,20 +61,19 @@ func call(srv string, rpcname string,
// you will have to modify this function. // you will have to modify this function.
// //
func (ck *Clerk) Lock(lockname string) bool { func (ck *Clerk) Lock(lockname string) bool {
// prepare the arguments. // prepare the arguments.
args := &LockArgs{} args := &LockArgs{}
args.Lockname = lockname args.Lockname = lockname
var reply LockReply var reply LockReply
// send an RPC request, wait for the reply.
ok := call(ck.servers[0], "LockServer.Lock", args, &reply)
if ok == false {
return false
}
return reply.OK
}
// send an RPC request, wait for the reply.
ok := call(ck.servers[0], "LockServer.Lock", args, &reply)
if ok == false {
return false
}
return reply.OK
}
// //
// ask the lock service to unlock a lock. // ask the lock service to unlock a lock.
...@@ -85,7 +83,7 @@ func (ck *Clerk) Lock(lockname string) bool { ...@@ -85,7 +83,7 @@ func (ck *Clerk) Lock(lockname string) bool {
func (ck *Clerk) Unlock(lockname string) bool { func (ck *Clerk) Unlock(lockname string) bool {
// Your code here. // Your code here.
return false return false
} }
...@@ -9,15 +9,15 @@ package lockservice ...@@ -9,15 +9,15 @@ package lockservice
// //
// Lock(lockname) returns OK=true if the lock is not held. // Lock(lockname) returns OK=true if the lock is not held.
// If it is held, it returns OK=false immediately. // If it is held, it returns OK=false immediately.
// //
type LockArgs struct { type LockArgs struct {
// Go's net/rpc requires that these field // Go's net/rpc requires that these field
// names start with upper case letters! // names start with upper case letters!
Lockname string // lock name Lockname string // lock name
} }
type LockReply struct { type LockReply struct {
OK bool OK bool
} }
// //
...@@ -25,9 +25,9 @@ type LockReply struct { ...@@ -25,9 +25,9 @@ type LockReply struct {
// It returns OK=false if the lock was not held. // It returns OK=false if the lock was not held.
// //
type UnlockArgs struct { type UnlockArgs struct {
Lockname string Lockname string
} }
type UnlockReply struct { type UnlockReply struct {
OK bool OK bool
} }
...@@ -10,39 +10,37 @@ import "io" ...@@ -10,39 +10,37 @@ import "io"
import "time" import "time"
type LockServer struct { type LockServer struct {
mu sync.Mutex mu sync.Mutex
l net.Listener l net.Listener
dead bool // for test_test.go dead bool // for test_test.go
dying bool // for test_test.go dying bool // for test_test.go
am_primary bool // am I the primary? am_primary bool // am I the primary?
backup string // backup's port backup string // backup's port
// for each lock name, is it locked? // for each lock name, is it locked?
locks map[string]bool locks map[string]bool
} }
// //
// server Lock RPC handler. // server Lock RPC handler.
// //
// you will have to modify this function // you will have to modify this function
// //
func (ls *LockServer) Lock(args *LockArgs, reply *LockReply) error { func (ls *LockServer) Lock(args *LockArgs, reply *LockReply) error {
ls.mu.Lock() ls.mu.Lock()
defer ls.mu.Unlock() defer ls.mu.Unlock()
locked, _ := ls.locks[args.Lockname] locked, _ := ls.locks[args.Lockname]
if locked { if locked {
reply.OK = false reply.OK = false
} else { } else {
reply.OK = true reply.OK = true
ls.locks[args.Lockname] = true ls.locks[args.Lockname] = true
} }
return nil return nil
} }
// //
...@@ -50,9 +48,9 @@ func (ls *LockServer) Lock(args *LockArgs, reply *LockReply) error { ...@@ -50,9 +48,9 @@ func (ls *LockServer) Lock(args *LockArgs, reply *LockReply) error {
//