-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
118 lines (102 loc) · 2.65 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package kvraft
import (
"../labrpc"
"sync/atomic"
)
import "crypto/rand"
import "math/big"
type Clerk struct {
servers []*labrpc.ClientEnd
// You will have to modify this struct.
leaderId int32
clientId int64
requestId int64
}
func nrand() int64 {
max := big.NewInt(int64(1) << 62)
bigx, _ := rand.Int(rand.Reader, max)
x := bigx.Int64()
return x
}
func MakeClerk(servers []*labrpc.ClientEnd) *Clerk {
ck := new(Clerk)
ck.servers = servers
// You'll have to add code here.
ck.leaderId = 0
ck.clientId = nrand()
ck.requestId = 0
return ck
}
//
// fetch the current value for a key.
// returns "" if the key does not exist.
// keeps trying forever in the face of all other errors.
//
// you can send an RPC with code like this:
// ok := ck.servers[i].Call("KVServer.Get", &args, &reply)
//
// the types of args and reply (including whether they are pointers)
// must match the declared types of the RPC handler function's
// arguments. and reply must be passed as a pointer.
//
func (ck *Clerk) Get(key string) string {
// You will have to modify this function.
reqId := atomic.AddInt64(&ck.requestId, 1)
leader := atomic.LoadInt32(&ck.leaderId)
args := GetArgs{
Key: key,
ClientId: ck.clientId,
RequestId: reqId,
}
value := ""
server := leader
for ; ; server = (server + 1) % int32(len(ck.servers)) {
reply := GetReply{}
ok := ck.servers[server].Call("KVServer.Get", &args, &reply)
if ok && reply.Err != ErrWrongLeader {
if reply.Err == OK {
value = reply.Value
}
break
}
}
atomic.StoreInt32(&ck.leaderId, server)
return value
}
//
// shared by Put and Append.
//
// you can send an RPC with code like this:
// ok := ck.servers[i].Call("KVServer.PutAppend", &args, &reply)
//
// the types of args and reply (including whether they are pointers)
// must match the declared types of the RPC handler function's
// arguments. and reply must be passed as a pointer.
//
func (ck *Clerk) PutAppend(key string, value string, op string) {
// You will have to modify this function.
reqId := atomic.AddInt64(&ck.requestId, 1)
leader := atomic.LoadInt32(&ck.leaderId)
args := PutAppendArgs{
Key: key,
Value: value,
Op: op,
ClientId: ck.clientId,
RequestId: reqId,
}
server := leader
for ; ; server = (server + 1) % int32(len(ck.servers)) {
reply := PutAppendReply{}
ok := ck.servers[server].Call("KVServer.PutAppend", &args, &reply)
if ok && reply.Err != ErrWrongLeader {
break
}
}
atomic.StoreInt32(&ck.leaderId, server)
}
func (ck *Clerk) Put(key string, value string) {
ck.PutAppend(key, value, "Put")
}
func (ck *Clerk) Append(key string, value string) {
ck.PutAppend(key, value, "Append")
}