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

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
}
This diff is collapsed.
......@@ -8,17 +8,16 @@ import "fmt"
// and maintains a little state.
//
type Clerk struct {
servers [2]string // primary port, backup port
// Your definitions here.
servers [2]string // primary port, backup port
// Your definitions here.
}
func MakeClerk(primary string, backup string) *Clerk {
ck := new(Clerk)
ck.servers[0] = primary
ck.servers[1] = backup
// Your initialization code here.
return ck
ck := new(Clerk)
ck.servers[0] = primary
ck.servers[1] = backup
// Your initialization code here.
return ck
}
//
......@@ -38,20 +37,20 @@ func MakeClerk(primary string, backup 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
}
//
......@@ -62,20 +61,19 @@ func call(srv string, rpcname string,
// you will have to modify this function.
//
func (ck *Clerk) Lock(lockname string) bool {
// prepare the arguments.
args := &LockArgs{}
args.Lockname = lockname
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
}
// prepare the arguments.
args := &LockArgs{}
args.Lockname = lockname
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
}
//
// ask the lock service to unlock a lock.
......@@ -85,7 +83,7 @@ func (ck *Clerk) Lock(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
//
// Lock(lockname) returns OK=true if the lock is not held.
// If it is held, it returns OK=false immediately.
//
//
type LockArgs struct {
// Go's net/rpc requires that these field
// names start with upper case letters!
Lockname string // lock name
// Go's net/rpc requires that these field
// names start with upper case letters!
Lockname string // lock name
}
type LockReply struct {
OK bool
OK bool
}
//
......@@ -25,9 +25,9 @@ type LockReply struct {
// It returns OK=false if the lock was not held.
//
type UnlockArgs struct {
Lockname string
Lockname string
}
type UnlockReply struct {
OK bool
OK bool
}
......@@ -10,39 +10,37 @@ import "io"
import "time"
type LockServer struct {
mu sync.Mutex
l net.Listener
dead bool // for test_test.go
dying bool // for test_test.go
mu sync.Mutex
l net.Listener
dead bool // for test_test.go
dying bool // for test_test.go
am_primary bool // am I the primary?
backup string // backup's port
am_primary bool // am I the primary?
backup string // backup's port
// for each lock name, is it locked?
locks map[string]bool
// for each lock name, is it locked?
locks map[string]bool
}
//
// server Lock RPC handler.
//
// you will have to modify this function
//
func (ls *LockServer) Lock(args *LockArgs, reply *LockReply) error {
ls.mu.Lock()
defer ls.mu.Unlock()
ls.mu.Lock()
defer ls.mu.Unlock()
locked, _ := ls.locks[args.Lockname]
locked, _ := ls.locks[args.Lockname]
if locked {
reply.OK = false
} else {
reply.OK = true
ls.locks[args.Lockname] = true
}
if locked {
reply.OK = false
} else {
reply.OK = true
ls.locks[args.Lockname] = true
}
return nil
return nil
}
//
......@@ -50,9 +48,9 @@ func (ls *LockServer) Lock(args *LockArgs, reply *LockReply) error {
//
func (ls *LockServer) Unlock(args *UnlockArgs, reply *UnlockReply) error {
// Your code here.
// Your code here.
return nil
return nil
}
//
......@@ -61,8 +59,8 @@ func (ls *LockServer) Unlock(args *UnlockArgs, reply *UnlockReply) error {
// please don't change this.
//
func (ls *LockServer) kill() {
ls.dead = true
ls.l.Close()
ls.dead = true
ls.l.Close()
}
//
......@@ -73,86 +71,86 @@ func (ls *LockServer) kill() {
// please don't change anything to do with DeafConn.
//
type DeafConn struct {
c io.ReadWriteCloser
c io.ReadWriteCloser
}
func (dc DeafConn) Write(p []byte) (n int, err error) {
return len(p), nil
return len(p), nil
}
func (dc DeafConn) Close() error {
return dc.c.Close()
return dc.c.Close()
}
func (dc DeafConn) Read(p []byte) (n int, err error) {
return dc.c.Read(p)
return dc.c.Read(p)
}
func StartServer(primary string, backup string, am_primary bool) *LockServer {
ls := new(LockServer)
ls.backup = backup
ls.am_primary = am_primary
ls.locks = map[string]bool{}
// Your initialization code here.
me := ""
if am_primary {
me = primary
} else {
me = backup
}
// tell net/rpc about our RPC server and handlers.
rpcs := rpc.NewServer()
rpcs.Register(ls)
// prepare to receive connections from clients.
// change "unix" to "tcp" to use over a network.
os.Remove(me) // only needed for "unix"
l, e := net.Listen("unix", me);
if e != nil {
log.Fatal("listen error: ", e);
}
ls.l = l
// please don't change any of the following code,
// or do anything to subvert it.
// create a thread to accept RPC connections from clients.
go func() {
for ls.dead == false {
conn, err := ls.l.Accept()
if err == nil && ls.dead == false {
if ls.dying {
// process the request but force discard of reply.
// without this the connection is never closed,
// b/c ServeConn() is waiting for more requests.
// test_test.go depends on this two seconds.
go func() {
time.Sleep(2 * time.Second)
conn.Close()
}()
ls.l.Close()