-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswiftpaxos.go
130 lines (115 loc) · 2.48 KB
/
swiftpaxos.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
package main
import "math"
type SwiftPaxos struct {
rs []string
fastQ Quorum
leader string
latency *LatencyTable
}
func NewSwiftPaxos(rs []string, t *LatencyTable) *SwiftPaxos {
return &SwiftPaxos{
rs: rs,
fastQ: nil,
leader: "",
latency: t,
}
}
func (s *SwiftPaxos) SetReplicas(rs []string) {
s.rs = rs
}
func (s *SwiftPaxos) GetReplicas() []string {
return s.rs
}
func (s *SwiftPaxos) Accept(client string, fast bool) float64 {
m := 0.0
if fast {
for r := range s.fastQ {
l := s.Propagate(client, r) + s.FastAck(r, client)
m = math.Max(m, l)
}
return math.Min(m, s.Accept(client, false))
}
m = math.Inf(1)
slowQs := QuorumsOfSize(len(s.rs)/2+1, s.rs, NoFilter)
for _, q := range slowQs {
qm := 0.0
for r := range q {
l := s.SlowAck(client, r, client)
qm = math.Max(qm, l)
}
m = math.Min(m, qm)
}
return m
}
func (s *SwiftPaxos) Propagate(client, replica string) float64 {
return s.latency.OneWayLatency(client, replica)
}
func (s *SwiftPaxos) FastAck(replica, to string) float64 {
return s.latency.OneWayLatency(replica, to)
}
func (s *SwiftPaxos) SlowAck(client, replica, to string) float64 {
l1 := s.Propagate(client, replica)
l2 := s.Propagate(client, s.leader) + s.FastAck(s.leader, replica)
return math.Max(l1, l2) + s.latency.OneWayLatency(replica, to)
}
func (s *SwiftPaxos) SetAverageBestLeader(cs []string) (string, float64) {
min := math.Inf(1)
leader := ""
for r := range s.fastQ {
s.leader = r
if MinWorstLatency {
l := Average(s, cs, false)
if l < min {
min = l
leader = r
} else if l == min {
l1 := Average(s, cs, true)
s.leader = leader
l2 := Average(s, cs, true)
if l1 < l2 {
leader = r
}
}
} else {
l := Average(s, cs, true)
if l < min {
min = l
leader = r
}
}
}
s.leader = leader
return leader, min
}
func (s *SwiftPaxos) SetAverageBestFixedQuorumAndLeader(cs []string, f QuorumFilter) (Quorum, string, float64) {
var (
fastQ Quorum
leader string
)
min := math.Inf(1)
fastQs := QuorumsOfSize(len(s.rs)/2+1, s.rs, f)
for _, q := range fastQs {
s.fastQ = q
l, m := s.SetAverageBestLeader(cs)
if m < min {
min = m
fastQ = q
leader = l
} else if m == min {
l1 := Average(s, cs, true)
s.fastQ = fastQ
s.leader = leader
l2 := Average(s, cs, true)
if l1 < l2 {
leader = l
fastQ = q
}
}
}
s.fastQ = fastQ
s.leader = leader
return fastQ, leader, min
}
func (s *SwiftPaxos) String() string {
return "SP"
}