forked from efficient/epaxos
/
client.go
272 lines (229 loc) · 7.26 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"bufio"
"context"
"flag"
"fmt"
"genericsmrproto"
"golang.org/x/sync/semaphore"
"log"
"masterproto"
"math/rand"
"net"
"net/rpc"
"os"
"poisson"
"runtime"
"state"
"sync"
"time"
"zipfian"
)
var masterAddr *string = flag.String("maddr", "", "Master address. Defaults to localhost")
var masterPort *int = flag.Int("mport", 7087, "Master port.")
var procs *int = flag.Int("p", 2, "GOMAXPROCS.")
var conflicts *int = flag.Int("c", 0, "Percentage of conflicts. If -1, uses Zipfian distribution.")
var forceLeader = flag.Int("l", -1, "Force client to talk to a certain replica.")
var startRange = flag.Int("sr", 0, "Key range start")
var T = flag.Int("T", 10, "Number of threads (simulated clients).")
var outstandingReqs = flag.Int64("or", 1, "Number of outstanding requests a thread can have at any given time.")
var theta = flag.Float64("theta", 0.99, "Theta zipfian parameter")
var zKeys = flag.Uint64("z", 1e9, "Number of unique keys in zipfian distribution.")
var poissonAvg = flag.Int("poisson", -1, "The average number of microseconds between requests. -1 disables Poisson.")
var percentWrites = flag.Float64("writes", 1, "A float between 0 and 1 that corresponds to the percentage of requests that should be writes. The remainder will be reads.")
var blindWrites = flag.Bool("blindwrites", false, "True if writes don't need to execute before clients receive responses.")
// Information about the latency of an operation
type response struct {
receivedAt time.Time
rtt float64 // The operation latency, in ms
commitLatency float64 // The operation's commit latency, in ms
}
// Information pertaining to operations that have been issued but that have not
// yet received responses
type outstandingRequestInfo struct {
sync.Mutex
sema *semaphore.Weighted // Controls number of outstanding operations
startTimes map[int32]time.Time // The time at which operations were sent out
}
// An outstandingRequestInfo per client thread
var orInfos []*outstandingRequestInfo
func main() {
flag.Parse()
runtime.GOMAXPROCS(*procs)
if *conflicts > 100 {
log.Fatalf("Conflicts percentage must be between 0 and 100.\n")
}
orInfos = make([]*outstandingRequestInfo, *T)
var master *rpc.Client
var err error
for {
master, err = rpc.DialHTTP("tcp", fmt.Sprintf("%s:%d", *masterAddr, *masterPort))
if err != nil {
log.Println("Error connecting to master", err)
} else {
break
}
}
rlReply := new(masterproto.GetReplicaListReply)
for !rlReply.Ready {
err := master.Call("Master.GetReplicaList", new(masterproto.GetReplicaListArgs), rlReply)
if err != nil {
log.Println("Error making the GetReplicaList RPC", err)
}
}
leader := 0
if *forceLeader < 0 {
reply := new(masterproto.GetLeaderReply)
if err = master.Call("Master.GetLeader", new(masterproto.GetLeaderArgs), reply); err != nil {
log.Println("Error making the GetLeader RPC:", err)
}
leader = reply.LeaderId
} else {
leader = *forceLeader
}
log.Printf("The leader is replica %d\n", leader)
readings := make(chan *response, 100000)
for i := 0; i < *T; i++ {
server, err := net.Dial("tcp", rlReply.ReplicaList[leader])
if err != nil {
log.Fatalf("Error connecting to replica %d\n", leader)
}
reader := bufio.NewReader(server)
writer := bufio.NewWriter(server)
orInfo := &outstandingRequestInfo{
sync.Mutex{},
semaphore.NewWeighted(*outstandingReqs),
make(map[int32]time.Time, *outstandingReqs),
}
go simulatedClientWriter(writer, orInfo)
go simulatedClientReader(reader, orInfo, readings)
orInfos[i] = orInfo
}
printer(readings)
}
func simulatedClientWriter(writer *bufio.Writer, orInfo *outstandingRequestInfo) {
args := genericsmrproto.Propose{0 /* id */, state.Command{state.PUT, 0, 0}, 0 /* timestamp */}
conflictRand := rand.New(rand.NewSource(time.Now().UnixNano()))
zipf := zipfian.NewZipfianGenerator(*zKeys, *theta)
poissonGenerator := poisson.NewPoisson(*poissonAvg)
opRand := rand.New(rand.NewSource(time.Now().UnixNano()))
queuedReqs := 0 // The number of poisson departures that have been missed
for id := int32(0); ; id++ {
args.CommandId = id
// Determine key
if *conflicts >= 0 {
r := conflictRand.Intn(100)
if r < *conflicts {
args.Command.K = 42
} else {
args.Command.K = state.Key(*startRange + 43 + int(id))
}
} else {
args.Command.K = state.Key(zipf.NextNumber())
}
// Determine operation type
if *percentWrites > opRand.Float64() {
if !*blindWrites {
args.Command.Op = state.PUT // write operation
} else {
args.Command.Op = state.PUT_BLIND
}
} else {
args.Command.Op = state.GET // read operation
}
if *poissonAvg == -1 { // Poisson disabled
orInfo.sema.Acquire(context.Background(), 1)
} else {
for {
if orInfo.sema.TryAcquire(1) {
if queuedReqs == 0 {
time.Sleep(poissonGenerator.NextArrival())
} else {
queuedReqs -= 1
}
break
}
time.Sleep(poissonGenerator.NextArrival())
queuedReqs += 1
}
}
before := time.Now()
writer.WriteByte(genericsmrproto.PROPOSE)
args.Marshal(writer)
writer.Flush()
orInfo.Lock()
orInfo.startTimes[id] = before
orInfo.Unlock()
}
}
func simulatedClientReader(reader *bufio.Reader, orInfo *outstandingRequestInfo, readings chan *response) {
var reply genericsmrproto.ProposeReply
for {
if err := reply.Unmarshal(reader); err != nil || reply.OK == 0 {
log.Println("Error when reading:", err)
break
}
after := time.Now()
orInfo.sema.Release(1)
orInfo.Lock()
before := orInfo.startTimes[reply.CommandId]
delete(orInfo.startTimes, reply.CommandId)
orInfo.Unlock()
rtt := (after.Sub(before)).Seconds() * 1000
commitToExec := float64(reply.Timestamp) / 1e6
commitLatency := rtt - commitToExec
readings <- &response{
after,
rtt,
commitLatency,
}
}
}
func printer(readings chan *response) {
lattputFile, err := os.Create("lattput.txt")
if err != nil {
log.Println("Error creating lattput file", err)
return
}
lattputFile.WriteString("# time (ns), avg lat over the past second, tput since last line, total count, totalOrs, avg commit lat over the past second\n")
latFile, err := os.Create("latency.txt")
if err != nil {
log.Println("Error creating latency file", err)
return
}
latFile.WriteString("# time (ns), latency, commit latency\n")
startTime := time.Now()
for {
time.Sleep(time.Second)
count := len(readings)
var sum float64 = 0
var commitSum float64 = 0
endTime := time.Now() // Set to current time in case there are no readings
for i := 0; i < count; i++ {
resp := <-readings
// Log all to latency file
latFile.WriteString(fmt.Sprintf("%d %f %f\n", resp.receivedAt.UnixNano(), resp.rtt, resp.commitLatency))
sum += resp.rtt
commitSum += resp.commitLatency
endTime = resp.receivedAt
}
var avg float64
var avgCommit float64
var tput float64
if count > 0 {
avg = sum / float64(count)
avgCommit = commitSum / float64(count)
tput = float64(count) / endTime.Sub(startTime).Seconds()
}
totalOrs := 0
for i := 0; i < *T; i++ {
orInfos[i].Lock()
totalOrs += len(orInfos[i].startTimes)
orInfos[i].Unlock()
}
// Log summary to lattput file
lattputFile.WriteString(fmt.Sprintf("%d %f %f %d %d %f\n", endTime.UnixNano(),
avg, tput, count, totalOrs, avgCommit))
startTime = endTime
}
}