-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
172 lines (156 loc) · 3.75 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"sync"
"github.com/imdea-software/swiftpaxos/client"
"github.com/imdea-software/swiftpaxos/config"
"github.com/imdea-software/swiftpaxos/curp"
"github.com/imdea-software/swiftpaxos/dlog"
"github.com/imdea-software/swiftpaxos/master"
"github.com/imdea-software/swiftpaxos/replica/defs"
"github.com/imdea-software/swiftpaxos/swift"
)
var (
confs = flag.String("config", "", "Deployment config `file` (required)")
latency = flag.String("latency", "", "Latency config `file`")
logFile = flag.String("log", "", "Path to the log `file`")
machineAlias = flag.String("alias", "", "An `alias` of this participant")
machineType = flag.String("run", "server", "Run a `participant`, which is either a server (or replica), a client or a master")
protocol = flag.String("protocol", "", "Protocol to run. Overwrites `protocol` field of the config file")
quorum = flag.String("quorum", "", "Quorum config `file`")
)
func main() {
flag.Parse()
if *confs == "" {
flag.Usage()
os.Exit(1)
}
c, err := config.Read(*confs, *machineAlias)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if *protocol != "" {
c.Protocol = *protocol
}
defs.LatencyConf = *latency
switch *machineType {
case "replica":
fallthrough
case "server":
c.MachineType = config.ReplicaMachine
case "client":
c.MachineType = config.ClientMachine
case "master":
c.MachineType = config.MasterMachine
default:
fmt.Println("Unknown participant type")
flag.Usage()
os.Exit(1)
}
c.Quorum = *quorum
run(c)
}
func run(c *config.Config) {
switch c.MachineType {
case config.MasterMachine:
runMaster(c)
case config.ClientMachine:
runClient(c, true)
case config.ReplicaMachine:
runReplica(c, dlog.New(*logFile, true))
}
}
func runMaster(c *config.Config) {
m := master.New(len(c.ReplicaAddrs), c.MasterPort, dlog.New(*logFile, true))
m.Run()
}
func runClient(c *config.Config, verbose bool) {
var wg sync.WaitGroup
for i := 0; i < c.Clones+1; i++ {
wg.Add(1)
go func(i int) {
runSingleClient(c, i, verbose)
wg.Done()
}(i)
}
wg.Wait()
}
func runSingleClient(c *config.Config, i int, verbose bool) {
var l *dlog.Logger
if i == 0 {
l = dlog.New(*logFile, verbose)
} else {
f := *logFile
if f == "" {
f = "client_"
}
l = dlog.New(f+strconv.Itoa(i), verbose)
// TODO: remove if already exists
}
switch strings.ToLower(c.Protocol) {
case "swiftpaxos":
case "curp":
case "fastpaxos":
c.Fast = true
c.WaitClosest = true
case "n2paxos":
c.Fast = true
c.WaitClosest = true
case "epaxos":
c.Leaderless = true
c.Fast = false
case "paxos":
c.WaitClosest = false
c.Fast = false
}
server := c.Proxy.ProxyOf(c.ClientAddrs[c.Alias])
server = c.ReplicaAddrs[server]
cl := client.NewClientLog(server, c.MasterAddr, c.MasterPort, c.Fast, c.Leaderless, verbose, l)
b := client.NewBufferClient(cl, c.Reqs, c.CommandSize, c.Conflicts, c.Writes, int64(c.Key))
if c.Pipeline {
b.Pipeline(c.Syncs, int32(c.Pendings))
}
if err := b.Connect(); err != nil {
log.Fatal(err)
}
if p := strings.ToLower(c.Protocol); p == "swiftpaxos" {
cl := swift.NewClient(b, len(c.ReplicaAddrs))
if cl == nil {
return
}
cl.Loop()
} else if p == "curp" {
cls := []string{}
for a := range c.ClientAddrs {
cls = append(cls, a)
}
sort.Slice(cls, func(i, j int) bool {
return cls[i] < cls[j]
})
pclients := 0
for i, a := range cls {
if a == c.Alias {
pclients = (c.Clones + 1) * i
}
}
cl := curp.NewClient(b, len(c.ReplicaAddrs), c.Reqs, pclients)
if cl == nil {
return
}
cl.Loop()
} else {
waitFrom := b.LeaderId
if b.Fast || b.Leaderless || c.WaitClosest {
waitFrom = b.ClosestId
}
b.WaitReplies(waitFrom)
b.Loop()
}
}