-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtool.go
113 lines (104 loc) · 3.98 KB
/
tool.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
package build
import (
"fmt"
"runtime"
"strings"
)
func GenerateBatchByIpTable(nodenum, shardnum int) error {
// read IP table file first
ipMap := readIpTable("./ipTable.json")
// determine the formats of commands and fileNames, according to operating system
var fileNameFormat, commandFormat string
os := runtime.GOOS
switch os {
case "windows":
fileNameFormat = "complie_run_IpAddr=%s.bat"
commandFormat = "start cmd /k go run main.go"
default:
fileNameFormat = "complie_run_IpAddr=%s.sh"
commandFormat = "go run main.go"
}
// generate file for each ip
for i := 0; i < shardnum; i++ {
// if this shard is not existed, return
if _, shard_exist := ipMap[uint64(i)]; !shard_exist {
return fmt.Errorf("the shard (shardID = %d) is not existed in the IP Table file", i)
}
// if this shard is existed.
for j := 0; j < nodenum; j++ {
if nodeIp, node_exist := ipMap[uint64(i)][uint64(j)]; node_exist {
// attach this command to this file
ipAddr := strings.Split(nodeIp, ":")[0]
batFilePath := fmt.Sprintf(fileNameFormat, strings.ReplaceAll(ipAddr, ".", "_"))
command := fmt.Sprintf(commandFormat+" -n %d -N %d -s %d -S %d & \n", j, nodenum, i, shardnum)
if err := attachLineToFile(batFilePath, command); nil != err {
return err
}
} else {
return fmt.Errorf("the node (shardID = %d, nodeID = %d) is not existed in the IP Table file", i, j)
}
}
}
// generate command for supervisor
if supervisorShard, shard_exist := ipMap[2147483647]; shard_exist {
if nodeIp, node_exist := supervisorShard[0]; node_exist {
ipAddr := strings.Split(nodeIp, ":")[0]
batFilePath := fmt.Sprintf(fileNameFormat, strings.ReplaceAll(ipAddr, ".", "_"))
supervisor_command := fmt.Sprintf(commandFormat+" -c -N %d -S %d & \n", nodenum, shardnum)
if err := attachLineToFile(batFilePath, supervisor_command); nil != err {
return err
}
return nil
}
}
return fmt.Errorf("the supervisor (shardID = 2147483647, nodeID = 0) is not existed in the IP Table file")
}
func GenerateExeBatchByIpTable(nodenum, shardnum int) error {
// read IP table file first
ipMap := readIpTable("./ipTable.json")
// determine the formats of commands and fileNames, according to operating system
var fileNameFormat, commandFormat string
os := runtime.GOOS
switch os {
case "windows":
fileNameFormat = os + "_exe_run_IpAddr=%s.bat"
commandFormat = "start cmd /k blockEmulator_Windows_Precompile.exe"
default:
fileNameFormat = os + "_exe_run_IpAddr=%s.sh"
commandFormat = "./blockEmulator_" + os + "_Precompile"
}
// generate file for each ip
for i := 0; i < shardnum; i++ {
// if this shard is not existed, return
if _, shard_exist := ipMap[uint64(i)]; !shard_exist {
return fmt.Errorf("the shard (shardID = %d) is not existed in the IP Table file", i)
}
// if this shard is existed.
for j := 0; j < nodenum; j++ {
if nodeIp, node_exist := ipMap[uint64(i)][uint64(j)]; node_exist {
// attach this command to this file
ipAddr := strings.Split(nodeIp, ":")[0]
batFilePath := fmt.Sprintf(fileNameFormat, strings.ReplaceAll(ipAddr, ".", "_"))
command := fmt.Sprintf(commandFormat+" -n %d -N %d -s %d -S %d & \n", j, nodenum, i, shardnum)
if err := attachLineToFile(batFilePath, command); nil != err {
return err
}
} else {
return fmt.Errorf("the node (shardID = %d, nodeID = %d) is not existed in the IP Table file", i, j)
}
}
}
// generate command for supervisor
if supervisorShard, shard_exist := ipMap[2147483647]; shard_exist {
if nodeIp, node_exist := supervisorShard[0]; node_exist {
ipAddr := strings.Split(nodeIp, ":")[0]
batFilePath := fmt.Sprintf(fileNameFormat, strings.ReplaceAll(ipAddr, ".", "_"))
supervisor_command := fmt.Sprintf(commandFormat+" -c -N %d -S %d & \n", nodenum, shardnum)
if err := attachLineToFile(batFilePath, supervisor_command); nil != err {
return err
}
return nil
}
}
return fmt.Errorf("the supervisor (shardID = 2147483647, nodeID = 0) is not existed in the IP Table file")
}