-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
161 lines (137 loc) · 3.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"fmt"
"io"
stdlog "log"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/hashicorp/raft"
"github.com/hashicorp/raft-boltdb"
"github.com/rs/zerolog"
)
func main() {
logger := zerolog.New(os.Stdout)
rawConfig := readRawConfig()
config, err := resolveConfig(rawConfig)
if err != nil {
fmt.Fprintf(os.Stderr, "Configuration errors - %s\n", err)
os.Exit(1)
}
nodeLogger := logger.With().Str("component", "node").Logger()
node, err := NewNode(config, &nodeLogger)
if err != nil {
fmt.Fprintf(os.Stderr, "Error configuring node: %s", err)
os.Exit(1)
}
if config.JoinAddress != "" {
go func() {
retryJoin := func() error {
url := url.URL{
Scheme: "http",
Host: config.JoinAddress,
Path: "join",
}
req, err := http.NewRequest(http.MethodPost, url.String(), nil)
if err != nil {
return err
}
req.Header.Add("Peer-Address", config.RaftAddress.String())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("non 200 status code: %d", resp.StatusCode)
}
return nil
}
for {
if err := retryJoin(); err != nil {
logger.Error().Err(err).Str("component", "join").Msg("Error joining cluster")
time.Sleep(1 * time.Second)
} else {
break
}
}
}()
}
httpLogger := logger.With().Str("component", "http").Logger()
httpServer := &httpServer{
node: node,
address: config.HTTPAddress,
logger: &httpLogger,
}
httpServer.Start()
}
type node struct {
config *Config
raftNode *raft.Raft
fsm *fsm
log *zerolog.Logger
}
func NewNode(config *Config, log *zerolog.Logger) (*node, error) {
fsm := &fsm{
stateValue: 0,
}
if err := os.MkdirAll(config.DataDir, 0700); err != nil {
return nil, err
}
raftConfig := raft.DefaultConfig()
raftConfig.LocalID = raft.ServerID(config.RaftAddress.String())
raftConfig.Logger = stdlog.New(log, "", 0)
transportLogger := log.With().Str("component", "raft-transport").Logger()
transport, err := raftTransport(config.RaftAddress, transportLogger)
if err != nil {
return nil, err
}
snapshotStoreLogger := log.With().Str("component", "raft-snapshots").Logger()
snapshotStore, err := raft.NewFileSnapshotStore(config.DataDir, 1, snapshotStoreLogger)
if err != nil {
return nil, err
}
logStore, err := raftboltdb.NewBoltStore(filepath.Join(config.DataDir, "raft-log.bolt"))
if err != nil {
return nil, err
}
stableStore, err := raftboltdb.NewBoltStore(filepath.Join(config.DataDir, "raft-stable.bolt"))
if err != nil {
return nil, err
}
raftNode, err := raft.NewRaft(raftConfig, fsm, logStore, stableStore,
snapshotStore, transport)
if err != nil {
return nil, err
}
if config.Bootstrap {
configuration := raft.Configuration{
Servers: []raft.Server{
{
ID: raftConfig.LocalID,
Address: transport.LocalAddr(),
},
},
}
raftNode.BootstrapCluster(configuration)
}
return &node{
config: config,
raftNode: raftNode,
log: log,
fsm: fsm,
}, nil
}
func raftTransport(raftAddr net.Addr, log io.Writer) (*raft.NetworkTransport, error) {
address, err := net.ResolveTCPAddr("tcp", raftAddr.String())
if err != nil {
return nil, err
}
transport, err := raft.NewTCPTransport(address.String(), address, 3, 10*time.Second, log)
if err != nil {
return nil, err
}
return transport, nil
}