-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
249 lines (202 loc) · 4.81 KB
/
utils.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
package spaxos
import (
"fmt"
"github.com/op/go-logging"
"math/rand"
"reflect"
"runtime"
"time"
// pb "spaxos/spaxospb"
pb "github.com/dengoswei/spaxos/spaxospb"
)
var log = logging.MustGetLogger("spaxos")
var rd *rand.Rand
func assert(cond bool) {
hassert(cond, "assert failed")
}
func hassert(cond bool, format string, args ...interface{}) {
if !cond {
panic(fmt.Sprintf(format, args...))
}
}
func LogDebug(format string, args ...interface{}) {
log.Debug(format, args...)
}
func LogErr(format string, args ...interface{}) {
log.Error(format, args...)
}
func MaxUint64(a, b uint64) uint64 {
if a >= b {
return a
}
return b
}
func MinUint64(a, b uint64) uint64 {
if a <= b {
return a
}
return b
}
func RandUint64() uint64 {
return uint64(rd.Int63())
}
func RandBool() bool {
return 0 == rd.Int()%2
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandString(n int) string {
b := make([]rune, n)
rlen := len(letters)
for i := range b {
idx := rd.Intn(rlen)
b[i] = letters[idx]
}
return string(b)
}
func RandByte(n int) []byte {
return []byte(RandString(n))
}
func randPropItem() *pb.ProposeItem {
pitem := &pb.ProposeItem{
Reqid: RandUint64(),
Values: [][]byte{RandByte(10)}}
return pitem
}
func randHardState() pb.HardState {
acceptedNum := RandUint64()
promisedNum := acceptedNum + RandUint64()
hs := pb.HardState{
Chosen: false,
Index: RandUint64(),
MaxProposedNum: RandUint64(),
MaxPromisedNum: promisedNum,
MaxAcceptedNum: acceptedNum,
// TODO: test function stall on RandByte(100)!! ? why
AcceptedValue: randPropItem(),
}
return hs
}
func randSpaxosInstance() *spaxosInstance {
index := RandUint64()
ins := newSpaxosInstance(index)
ins.chosen = RandBool()
ins.maxProposedNum = RandUint64()
ins.promisedNum = RandUint64()
ins.acceptedNum = MinUint64(ins.promisedNum, RandUint64())
ins.acceptedValue = randPropItem()
return ins
}
func randSpaxos() *spaxos {
c := NewDefaultConfig()
assert(nil != c)
db := NewFakeStorage()
assert(nil != db)
sp, err := newSpaxos(c, db)
assert(nil == err)
assert(nil != sp)
sp.outMsgs = nil
return sp
}
func randRspVotes(falseCnt, trueCnt uint64) map[uint64]bool {
cnt := falseCnt + trueCnt
rspVotes := make(map[uint64]bool, cnt)
for id := uint64(1); id <= falseCnt; id += 1 {
rspVotes[id] = false
}
for id := falseCnt + 1; id <= cnt; id += 1 {
rspVotes[id] = true
}
return rspVotes
}
func randId(sp *spaxos, exclude bool) uint64 {
for {
newid := rd.Intn(len(sp.groups)) + 1
if !exclude ||
(exclude && uint64(newid) != sp.id) {
return uint64(newid)
}
}
}
func randPropRsp(sp *spaxos, ins *spaxosInstance) pb.Message {
randid := sp.id
for id, _ := range sp.groups {
if _, ok := ins.rspVotes[id]; !ok {
randid = id
break
}
}
msg := pb.Message{
Type: pb.MsgPropResp, Index: ins.index, Reject: false,
From: randid, To: sp.id,
Entry: pb.PaxosEntry{PropNum: ins.maxProposedNum}}
return msg
}
func randAccptRsp(sp *spaxos, ins *spaxosInstance) pb.Message {
randid := sp.id
for id, _ := range sp.groups {
if _, ok := ins.rspVotes[id]; !ok {
randid = id
break
}
}
msg := pb.Message{
Type: pb.MsgAccptResp, Index: ins.index, Reject: false,
From: randid, To: sp.id,
Entry: pb.PaxosEntry{
PropNum: ins.maxProposedNum, Value: ins.proposingValue}}
return msg
}
func randPropValue(cnt int) (uint64, [][]byte) {
reqid := RandUint64()
values := make([][]byte, cnt)
for i := 0; i < cnt; i++ {
values[i] = RandByte(10)
}
return reqid, values
}
func randTestPort() int {
return 20000 + rand.Intn(100)
}
func GetFunctionName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
func GetCurrentFuncName() string {
pc, _, _, ok := runtime.Caller(1)
assert(true == ok)
return runtime.FuncForPC(pc).Name()
}
func printIndicate() {
if nil == rd {
s := rand.NewSource(time.Now().UnixNano())
rd = rand.New(s)
}
pc, file, line, ok := runtime.Caller(1)
assert(true == ok)
fmt.Printf("[%s %s %d]\n", runtime.FuncForPC(pc).Name(), file, line)
}
func (ins *spaxosInstance) Equal(insB *spaxosInstance) bool {
return ins.chosen == insB.chosen &&
ins.index == insB.index &&
ins.maxProposedNum == insB.maxProposedNum &&
ins.promisedNum == insB.promisedNum &&
ins.acceptedNum == insB.acceptedNum &&
true == ins.acceptedValue.Equal(insB.acceptedValue)
}
func getMsg(
c chan pb.Message,
msgs []pb.Message) (chan pb.Message, pb.Message) {
if nil != msgs && 0 < len(msgs) {
return c, msgs[0]
}
return nil, pb.Message{}
}
func getMsgs(c chan []pb.Message, msgs []pb.Message) chan []pb.Message {
if 0 == len(msgs) {
return nil
}
return c
}
func generateTimeoutMsg(index, id, timestamp uint64) pb.Message {
return pb.Message{Type: pb.MsgTimeOut,
To: id, From: id, Index: index, Timestamp: timestamp}
}