-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_service.go
87 lines (78 loc) · 2.07 KB
/
start_service.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
package main
import (
"distributed-system/twopc"
"flag"
"log"
"net"
"os"
"runtime/pprof"
"rush-shopping/shopping"
"rush-shopping/util"
"strings"
)
func main() {
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
web := flag.Bool("s", false, "shopping web service")
coord := flag.Bool("c", false, "shopping kvstore coordinator")
parti := flag.Bool("p", false, "shopping kvstore participant")
config := flag.String("f", "cfg.json", "config file")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
cfg := util.ParseCfg(*config)
keyHashFunc := twopc.DefaultKeyHashFunc
addrs, _ := net.InterfaceAddrs()
host_ips := make(map[string]struct{})
for _, addr := range addrs {
host_ips[strings.Split(addr.String(), "/")[0]] = struct{}{}
}
blocked := false
if *parti {
for _, pptAddr := range cfg.KVStoreAddrs {
if domain, _, err := net.SplitHostPort(pptAddr); err == nil {
if ips, err := net.LookupHost(domain); err == nil {
if _, ok := host_ips[ips[0]]; ok {
blocked = true
go shopping.NewShoppingTxnKVStoreService(cfg.Protocol, pptAddr, cfg.CoordinatorAddr)
}
}
}
}
}
if *coord {
if domain, _, err := net.SplitHostPort(cfg.CoordinatorAddr); err == nil {
if ips, err := net.LookupHost(domain); err == nil {
if _, ok := host_ips[ips[0]]; ok {
blocked = true
go shopping.NewShoppingTxnCoordinator(cfg.Protocol, cfg.CoordinatorAddr,
cfg.KVStoreAddrs, keyHashFunc, cfg.TimeoutMS)
}
}
}
}
if *web {
for _, appAddr := range cfg.APPAddrs {
if domain, _, err := net.SplitHostPort(appAddr); err == nil {
if ips, err := net.LookupHost(domain); err == nil {
if _, ok := host_ips[ips[0]]; ok {
blocked = true
go shopping.InitService(cfg.Protocol, appAddr, cfg.CoordinatorAddr, cfg.UserCSV, cfg.ItemCSV,
cfg.KVStoreAddrs, keyHashFunc)
}
}
}
}
}
if blocked {
block := make(chan bool)
<-block
} else {
flag.PrintDefaults()
}
}