-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (56 loc) · 1.41 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
package main
import (
"fmt"
"os"
"strconv"
"github.com/pysel/dkvs/balancer"
"github.com/pysel/dkvs/partition"
"github.com/pysel/dkvs/shared"
"github.com/pysel/dkvs/types"
)
/*
If node is a partition, arguments should be (in mentioned order):
- mode (partition)
- port
- db path
*/
func main() {
args := os.Args
if len(args) < 2 {
panic("Provide a mode of this node")
}
mode := args[1]
switch mode {
case types.PARTITION_MODE:
if len(args) < 3 {
panic(fmt.Sprintf("Need a port, db path, but got: %T", args))
}
port, err := strconv.Atoi(args[2])
if err != nil {
panic(err)
}
dbPath := args[3]
p := partition.NewPartition(dbPath)
server := partition.RegisterPartitionServer(p)
wg, addr := shared.StartListeningOnPort(server, uint64(port))
fmt.Println("Address: ", addr)
wg.Wait() // wait while server is still running
case types.BALANCER_MODE:
if len(args) < 3 {
panic(fmt.Sprintf("Need a port, amount of partitions, but got: %T", args))
}
port, err := strconv.Atoi(args[2])
if err != nil {
panic(err)
}
goalReplicas, err := strconv.Atoi(args[3])
if err != nil {
panic("invalid parameter for desired amount of partitions")
}
b := balancer.NewBalancer(balancer.BalancerDBPath, goalReplicas)
server := balancer.RegisterBalancerServer(b)
wg, addr := shared.StartListeningOnPort(server, uint64(port))
fmt.Println("Address: ", addr)
wg.Wait()
}
}