-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (86 loc) · 2.42 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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"github.com/vrnvu/distributed-leveldb/httpd"
"github.com/vrnvu/distributed-leveldb/store"
)
// Command line defaults
// We added the 127.0.0.1
const (
DefaultHTTPAddr = "127.0.0.1:11000"
DefaultRaftAddr = "127.0.0.1:12000"
)
// Command line parameters
var inmem bool
var httpAddr string
var raftAddr string
var joinAddr string
var nodeID string
func init() {
flag.BoolVar(&inmem, "inmem", false, "Use in-memory storage for Raft")
flag.StringVar(&httpAddr, "haddr", DefaultHTTPAddr, "Set the HTTP bind address")
flag.StringVar(&raftAddr, "raddr", DefaultRaftAddr, "Set Raft bind address")
flag.StringVar(&joinAddr, "join", "", "Set join address, if any")
flag.StringVar(&nodeID, "id", "", "Node ID")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <data-path> \n", os.Args[0])
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if flag.NArg() == 0 {
fmt.Fprintf(os.Stderr, "No storage directory specified\n")
os.Exit(1)
}
// Ensure storage directory exists.
storageDir := flag.Arg(0)
if storageDir == "" {
fmt.Fprintf(os.Stderr, "Invalid input: \"\" as storage directory specified\n")
os.Exit(1)
}
os.MkdirAll(storageDir, 0700)
s, err := store.New(inmem, storageDir, storageDir, raftAddr)
if err != nil {
log.Fatalf("failed to create a leveldb store at path %s: %s", storageDir, err.Error())
}
s.RaftDir = storageDir
s.RaftBind = raftAddr
if err := s.Open(joinAddr == "", nodeID); err != nil {
log.Fatalf("failed to open store: %s", err.Error())
}
h := httpd.New(httpAddr, s)
if err := h.Start(); err != nil {
log.Fatalf("failed to start HTTP service: %s", err.Error())
}
// If join was specified, make the join request.
if joinAddr != "" {
if err := join(joinAddr, raftAddr, nodeID); err != nil {
log.Fatalf("failed to join node at %s: %s", joinAddr, err.Error())
}
}
log.Println("dleveldb started successfully")
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, os.Interrupt)
<-terminate
log.Println("dleveldb exiting")
}
func join(joinAddr, raftAddr, nodeID string) error {
b, err := json.Marshal(map[string]string{"addr": raftAddr, "id": nodeID})
if err != nil {
return err
}
resp, err := http.Post(fmt.Sprintf("http://%s/join", joinAddr), "application-type/json", bytes.NewReader(b))
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}