Repository navigation
Expand file tree
/
Copy pathternrun.go
More file actions
398 lines (362 loc) · 12.4 KB
/
Copy pathternrun.go
File metadata and controls
398 lines (362 loc) · 12.4 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2025 XTX Markets Technologies Limited
//
// SPDX-License-Identifier: GPL-2.0-or-later
// Utility to quickly bring up a full ternfs, including all its components,
// while hopefully not leaking processes left and right when this process dies.
package main
import (
"flag"
"fmt"
"github.com/XTXMarkets/ternfs/go/client"
"github.com/XTXMarkets/ternfs/go/core/log"
"github.com/XTXMarkets/ternfs/go/core/managedprocess"
"github.com/XTXMarkets/ternfs/go/msgs"
"os"
"path"
"runtime"
"strings"
"time"
)
func noRunawayArgs() {
if flag.NArg() > 0 {
fmt.Fprintf(os.Stderr, "Unexpected extra arguments %v\n", flag.Args())
os.Exit(2)
}
}
func main() {
buildType := flag.String("build-type", "release", "C++ build type")
verbose := flag.Bool("verbose", false, "")
trace := flag.Bool("trace", false, "")
dataDir := flag.String("data-dir", "", "Directory where to store the TernFS data. If not present a temporary directory will be used.")
failureDomains := flag.Uint("failure-domains", 16, "Number of failure domains.")
hddBlockServices := flag.Uint("hdd-block-services", 2, "Number of HDD block services per failure domain.")
flashBlockServices := flag.Uint("flash-block-services", 2, "Number of HDD block services per failure domain.")
profile := flag.Bool("profile", false, "Whether to run code (both Go and C++) with profiling.")
registryBincodePort := flag.Uint("registry-bincode-port", 10001, "")
webHttpPort := flag.Uint("registry-http-port", 10000, "")
repoDir := flag.String("repo-dir", "", "Used to build C++/Go binaries. If not provided, the path will be derived form the filename at build time (so will only work locally).")
binariesDir := flag.String("binaries-dir", "", "If provided, nothing will be built, instead it'll be assumed that the binaries will be in the specified directory.")
xmon := flag.String("xmon", "", "")
noFuse := flag.Bool("no-fuse", false, "")
leaderOnly := flag.Bool("leader-only", false, "Run only LogsDB leader with LEADER_NO_FOLLOWERS")
locations := flag.String("locations", "loc1", "Comma separated list of locations to simulate multi datacenter. Default `loc1`")
noGc := flag.Bool("no-gc", false, "Don't start GC processes")
flag.Parse()
noRunawayArgs()
validPort := func(port uint) {
if port > uint(^uint16(0)) {
fmt.Fprintf(os.Stderr, "Invalid port %v.\n", port)
os.Exit(2)
}
}
if *registryBincodePort == 0 {
fmt.Fprintf(os.Stderr, "-registry-bincode-port can't be automatically picked.\n")
os.Exit(2)
}
validPort(*registryBincodePort)
validPort(*webHttpPort)
if *repoDir == "" {
_, filename, _, ok := runtime.Caller(0)
if !ok {
panic("no caller information")
}
*repoDir = path.Dir(path.Dir(path.Dir(filename)))
}
if *dataDir == "" {
dir, err := os.MkdirTemp("", "ternrun.")
if err != nil {
panic(fmt.Errorf("could not create tmp data dir: %w", err))
}
*dataDir = dir
fmt.Printf("running with temp data dir %v\n", *dataDir)
} else {
if err := os.Mkdir(*dataDir, 0777); err != nil && !os.IsExist(err) {
panic(fmt.Errorf("could not create data dir: %w", err))
}
}
locationNames := strings.Split(*locations, ",")
if len(locationNames) == 0 {
fmt.Fprintf(os.Stderr, "-locations needs to contain at least 1 location")
os.Exit(2)
}
logFile := path.Join(*dataDir, "go-log")
var logOut *os.File
{
var err error
logOut, err = os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "could not open file %v: %v", logFile, err)
os.Exit(1)
}
defer logOut.Close()
}
level := log.INFO
if *verbose {
level = log.DEBUG
}
if *trace {
level = log.TRACE
}
l := log.NewLogger(logOut, &log.LoggerOptions{Level: level, Syslog: false, PrintQuietAlerts: true})
var cppExes *managedprocess.CppExes
var goExes *managedprocess.GoExes
if *binariesDir != "" {
cppExes = &managedprocess.CppExes{
RegistryExe: path.Join(*binariesDir, "ternregistry"),
ShardExe: path.Join(*binariesDir, "ternshard"),
CDCExe: path.Join(*binariesDir, "terncdc"),
}
goExes = &managedprocess.GoExes{
BlocksExe: path.Join(*binariesDir, "ternblocks"),
FuseExe: path.Join(*binariesDir, "ternfuse"),
RegistryProxyExe: path.Join(*binariesDir, "ternregistryproxy"),
GcExe: path.Join(*binariesDir, "terngc"),
WebExe: path.Join(*binariesDir, "ternweb"),
}
} else {
fmt.Printf("building shard/cdc/blockservice/registry\n")
cppExes = managedprocess.BuildCppExes(l, *repoDir, *buildType)
goExes = managedprocess.BuildGoExes(l, *repoDir, false)
}
terminateChan := make(chan any, 1)
procs := managedprocess.New(terminateChan)
defer procs.Close()
fmt.Printf("starting components\n")
replicaCount := uint(5)
if *leaderOnly {
replicaCount = 1
}
lastUsedPort := *registryBincodePort - 1
getNextAddress := func() string {
lastUsedPort++
return fmt.Sprintf("127.0.0.1:%v", lastUsedPort)
}
// Start registry
registryAddress := getNextAddress()
{
for r := uint8(0); r < uint8(replicaCount); r++ {
dir := path.Join(*dataDir, fmt.Sprintf("registry_%d", r))
if r == 0 {
dir = path.Join(*dataDir, "registry")
}
opts := managedprocess.RegistryOpts{
Exe: cppExes.RegistryExe,
LogLevel: level,
Dir: dir,
RegistryAddress: registryAddress,
Replica: msgs.ReplicaId(r),
Xmon: *xmon,
Addr1: "127.0.0.1:0",
UsingDynamicPorts: true,
}
if r == 0 {
if *leaderOnly {
opts.LogsDBFlags = []string{"-logsdb-leader", "-logsdb-no-replication"}
} else {
opts.LogsDBFlags = []string{"-logsdb-leader"}
}
}
if r == 0 {
opts.Addr1 = registryAddress
}
procs.StartRegistry(l, &opts)
}
}
// Waiting for registry replicas
fmt.Printf("waiting for %d registry replicas...\n", replicaCount)
_, err := client.WaitForRegistryReplicas(l, registryAddress, uint8(replicaCount), 0, 10*time.Second)
if err != nil {
panic(fmt.Errorf("failed to wait for registry replicas: %v", err))
}
err = client.WaitForRegistry(l, registryAddress, 10*time.Second)
if err != nil {
panic(fmt.Errorf("failed to wait for writable registry: %v", err))
}
registryLocationAddresses := []string{registryAddress}
for i, locName := range locationNames {
_, err = client.RegistryRequest(l, nil, registryAddress, &msgs.CreateLocationReq{msgs.Location(i), locName})
if err != nil {
// it's possible location already exits, try renaming it
_, err = client.RegistryRequest(l, nil, registryAddress, &msgs.RenameLocationReq{msgs.Location(i), locName})
if err != nil {
panic(fmt.Errorf("failed to create location: %v", err))
}
}
if i == 0 {
// we already started main registry
continue
}
registryProxyAddress := getNextAddress()
procs.StartRegistryProxy(
l, &managedprocess.RegistryProxyOpts{
Exe: goExes.RegistryProxyExe,
LogLevel: level,
Dir: path.Join(*dataDir, "registryproxy"),
Xmon: *xmon,
Addr1: registryProxyAddress,
RegistryAddress: registryAddress,
Location: 1,
},
)
err = client.WaitForRegistry(l, registryProxyAddress, 10*time.Second)
if err != nil {
panic(fmt.Errorf("failed to connect to registry proxy for location %d, err %v", i, err))
}
registryLocationAddresses = append(registryLocationAddresses, registryProxyAddress)
}
// Start block services
storageClasses := make([]msgs.StorageClass, *hddBlockServices+*flashBlockServices)
for i := range storageClasses {
if i >= int(*hddBlockServices) {
storageClasses[i] = msgs.HDD_STORAGE
} else {
storageClasses[i] = msgs.FLASH_STORAGE
}
}
numLocations := uint(len(locationNames))
for loc := uint(0); loc < numLocations; loc++ {
registryAddressToUse := registryLocationAddresses[loc]
for i := uint(0); i < *failureDomains; i++ {
dirName := fmt.Sprintf("bs_%d", i)
if loc > 0 {
dirName = fmt.Sprintf("%s_loc%d", dirName, loc)
}
failureDomain := fmt.Sprintf("%d", i)
if numLocations > 1 {
failureDomain = fmt.Sprintf("%d_%d", i, loc)
}
opts := managedprocess.BlockServiceOpts{
Exe: goExes.BlocksExe,
Path: path.Join(*dataDir, dirName),
StorageClasses: storageClasses,
FailureDomain: failureDomain,
Location: msgs.Location(loc),
LogLevel: level,
RegistryAddress: registryAddressToUse,
Profile: *profile,
Xmon: *xmon,
Addr1: "127.0.0.1:0",
ReserverdStorage: 10 << 30, // 10GB
}
procs.StartBlockService(l, &opts)
}
}
waitForServices := 60 * time.Second
fmt.Printf("waiting for block services for %v...\n", waitForServices)
_, err = client.WaitForBlockServices(l, registryAddress, int(*failureDomains*(*hddBlockServices+*flashBlockServices)*numLocations), waitForServices)
if err != nil {
panic(fmt.Errorf("failed to wait for block services: %v", err))
}
// Start CDC
{
for r := uint8(0); r < uint8(replicaCount); r++ {
dir := path.Join(*dataDir, fmt.Sprintf("cdc_%d", r))
if r == 0 {
dir = path.Join(*dataDir, "cdc")
}
opts := managedprocess.CDCOpts{
ReplicaId: msgs.ReplicaId(r),
Exe: cppExes.CDCExe,
Dir: dir,
LogLevel: level,
Valgrind: *buildType == "valgrind",
RegistryAddress: registryAddress,
Perf: *profile,
Addr1: "127.0.0.1:0",
Xmon: *xmon,
}
if r == 0 {
if *leaderOnly {
opts.LogsDBFlags = []string{"-logsdb-leader", "-logsdb-no-replication"}
} else {
opts.LogsDBFlags = []string{"-logsdb-leader"}
}
}
procs.StartCDC(l, *repoDir, &opts)
}
}
// Start shards
for loc := uint(0); loc < numLocations; loc++ {
registryAddressToUse := registryLocationAddresses[loc]
for i := 0; i < 256; i++ {
for r := uint8(0); r < uint8(replicaCount); r++ {
shrid := msgs.MakeShardReplicaId(msgs.ShardId(i), msgs.ReplicaId(r))
dirName := fmt.Sprintf("shard_%03d_%d", i, r)
if loc > 0 {
dirName = fmt.Sprintf("%s_loc%d", dirName, loc)
}
noWritableDelay := time.Duration(0)
opts := managedprocess.ShardOpts{
Exe: cppExes.ShardExe,
Shrid: shrid,
Dir: path.Join(*dataDir, dirName),
LogLevel: level,
Valgrind: *buildType == "valgrind",
RegistryAddress: registryAddressToUse,
Perf: *profile,
Xmon: *xmon,
Location: msgs.Location(loc),
LogsDBFlags: nil,
Addr1: "127.0.0.1:0",
BlockServiceWritableDelay: &noWritableDelay,
}
if r == 0 {
if *leaderOnly {
opts.LogsDBFlags = []string{"-logsdb-leader", "-logsdb-no-replication"}
} else {
opts.LogsDBFlags = []string{"-logsdb-leader"}
}
}
procs.StartShard(l, *repoDir, &opts)
}
}
}
fmt.Printf("waiting for shards/cdc for %v...\n", waitForServices)
client.WaitForClient(l, registryAddress, waitForServices)
if !*noFuse {
for loc := uint(0); loc < numLocations; loc++ {
registryAddressToUse := registryLocationAddresses[loc]
fuseDir := "fuse_" + locationNames[loc]
fuseMountPoint := procs.StartFuse(l, &managedprocess.FuseOpts{
Exe: goExes.FuseExe,
Path: path.Join(*dataDir, fuseDir),
LogLevel: level,
Wait: true,
RegistryAddress: registryAddressToUse,
})
fmt.Printf("operational, mounted at %v\n", fuseMountPoint)
}
} else {
fmt.Printf("operational\n")
}
if !*noGc {
procs.StartGc(l, &managedprocess.GcOptions{
Exe: goExes.GcExe,
Dir: path.Join(*dataDir, "gc"),
LogLevel: level,
Xmon: *xmon,
RegistryAddress: registryAddress,
Addr1: "127.0.0.1:0",
Migrate: true,
CollectDirectories: true,
DestructFiles: true,
Scrub: true,
})
fmt.Printf("started scrub/collect directores/destruct files/migrate\n")
}
if *webHttpPort != 0 {
procs.StartWeb(l, &managedprocess.WebOptions{
Exe: goExes.WebExe,
Dir: path.Join(*dataDir, "web"),
LogLevel: level,
Xmon: *xmon,
HttpPort: fmt.Sprintf("%d", *webHttpPort),
RegistryAddress: registryAddress,
})
fmt.Printf("web started at http://127.0.0.1:%d\n", *webHttpPort)
}
errT := <-terminateChan
if errT != nil {
panic(errT)
}
}