forked from poy/talaria
/
main.go
108 lines (95 loc) · 2.36 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
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/apoydence/talaria/broker"
"github.com/apoydence/talaria/kvstore"
"github.com/apoydence/talaria/logging"
"github.com/apoydence/talaria/orchestrator"
"github.com/codegangsta/cli"
)
const (
dataDir = "dataDir"
logLevel = "logLevel"
segmentLength = "segmentLength"
numSegments = "numSegments"
numReplicas = "numReplicas"
port = "port"
healthPort = "healthPort"
)
func main() {
rand.Seed(time.Now().UnixNano())
app := cli.NewApp()
app.Name = "talaria"
app.Usage = "Distribute your data"
app.Action = func(c *cli.Context) {
run(c)
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: dataDir + ", d",
Usage: "The directory where the segmented files are stored",
},
cli.StringFlag{
Name: logLevel,
Value: "INFO",
Usage: "The log level",
},
cli.IntFlag{
Name: segmentLength + ", l",
Value: 1024 * 1024,
Usage: "The desired number of bytes for each segment",
},
cli.IntFlag{
Name: numSegments + ", n",
Value: 1024 * 1024,
Usage: "The desired number of bytes for each segment",
},
cli.IntFlag{
Name: numReplicas + ", r",
Value: 2,
Usage: "The number of replicas for each partition",
},
cli.IntFlag{
Name: port + ", p",
Value: 8888,
Usage: "The port to use",
},
cli.IntFlag{
Name: healthPort + ", hp",
Value: 8889,
Usage: "The port to use for health checking",
},
}
app.Run(os.Args)
}
func run(c *cli.Context) {
validateFlags(c)
setLogLevel(c)
clientAddr := fmt.Sprintf("http://localhost:%d", c.Int(port))
kvStore := kvstore.New(clientAddr, c.Int(healthPort))
ioProvider := broker.NewFileProvider(c.String(dataDir), uint64(c.Int(segmentLength)), uint64(c.Int(numSegments)), time.Second)
orch := orchestrator.New(clientAddr, uint(c.Int(numReplicas)), ioProvider, kvStore)
broker.StartBrokerServer(c.Int(port), orch, ioProvider)
}
func setLogLevel(c *cli.Context) {
logFlag := c.String(logLevel)
var logLevel logging.LogLevel
err := logLevel.UnmarshalJSON([]byte(logFlag))
if err != nil {
quit(fmt.Sprintf("Unable to parse log level: %s", logFlag), c)
}
logging.SetLevel(logLevel)
}
func validateFlags(c *cli.Context) {
if !c.IsSet(dataDir) {
quit(fmt.Sprintf("%s is required", dataDir), c)
}
}
func quit(msg string, c *cli.Context) {
fmt.Println(msg)
cli.ShowAppHelp(c)
os.Exit(1)
}