-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.go
97 lines (87 loc) · 2 KB
/
algorithm.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
package main
import "sort"
var (
MinWorstLatency = true
)
type Algorithm interface {
String() string
SetReplicas(rs []string)
GetReplicas() []string
Accept(client string, fast bool) float64
}
func Average(alg Algorithm, cs []string, fast bool) float64 {
l := 0.0
for _, c := range cs {
l += alg.Accept(c, fast)
}
return Div(l, float64(len(cs)))
}
type Configuration struct {
rs []string
cs []string
r int
}
// Computes all possible configurations of `repNum` number of replicas and
// `repNum` + `clientNum` number of clients with `repNum` being co-located
// with servers. The resulting list is ordered by ratio between alg1 and alg2
// in decreasing order.
func Configs(ms []string, repNum, clientNum int, alg1, alg2 Algorithm, fast1, fast2 bool, reconf1, reconf2 func(rs, cs []string), t *LatencyTable) []*Configuration {
var configs []*Configuration
qs := QuorumsOfSize(repNum, ms, func(r string, rs []string) bool {
if len(rs) < repNum-1 {
return true
}
if _, exists := t.us[r]; exists {
return true
}
for _, r := range rs {
if _, exists := t.us[r]; exists {
return true
}
}
return false
})
for _, q := range qs {
rs := SliceOfQuorum(q)
var ns []string
for _, s := range ms {
ok := true
for _, z := range rs {
if s == z {
ok = false
break
}
}
if ok {
ns = append(ns, s)
}
}
ps := QuorumsOfSize(clientNum-2, ns, NoFilter)
for _, p := range ps {
zs := SliceOfQuorum(p)
pps := QuorumsOfSize(2, rs, NoFilter)
for _, q := range pps {
ys := SliceOfQuorum(q)
cs := make([]string, len(zs)+2)
copy(cs, zs)
copy(cs[len(zs):], ys)
reconf1(rs, cs)
reconf2(rs, cs)
ai1 := Average(alg1, cs, fast1)
ai2 := Average(alg2, cs, fast2)
r := int(10000 * (1 - Div(ai1, ai2)))
if r >= 1000 {
configs = append(configs, &Configuration{
rs: rs,
cs: cs,
r: r,
})
}
}
}
}
sort.Slice(configs, func(i, j int) bool {
return configs[i].r > configs[j].r
})
return configs
}