-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (137 loc) · 3.67 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
162
163
164
package main
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"time"
"github.com/dworznik/orderbook"
"github.com/dworznik/orderbook-raft/fsm"
"github.com/dworznik/orderbook-raft/server"
"github.com/hashicorp/raft"
raftboltdb "github.com/hashicorp/raft-boltdb"
"github.com/spf13/viper"
)
// configRaft configuration for raft node
type configRaft struct {
NodeId string `mapstructure:"node_id"`
Port int `mapstructure:"port"`
Addr string `mapstructure:"addr"`
VolumeDir string `mapstructure:"volume_dir"`
}
// configServer configuration for HTTP server
type configServer struct {
Port int `mapstructure:"port"`
Addr string `mapstructure:"addr"`
}
// config configuration
type config struct {
Server configServer `mapstructure:"server"`
Raft configRaft `mapstructure:"raft"`
}
const (
serverPort = "SERVER_PORT"
serverAddr = "SERVER_ADDR"
raftNodeId = "RAFT_NODE_ID"
raftPort = "RAFT_PORT"
raftAddr = "RAFT_ADDR"
raftVolDir = "RAFT_VOL_DIR"
)
var confKeys = []string{
serverPort,
serverAddr,
raftNodeId,
raftPort,
raftAddr,
raftVolDir,
}
const (
// The maxPool controls how many connections we will pool.
maxPool = 3
// The timeout is used to apply I/O deadlines. For InstallSnapshot, we multiply
// the timeout by (SnapshotSize / TimeoutScale).
// https://github.com/hashicorp/raft/blob/v1.1.2/net_transport.go#L177-L181
tcpTimeout = 10 * time.Second
// The `retain` parameter controls how many
// snapshots are retained. Must be at least 1.
raftSnapShotRetain = 2
// raftLogCacheSize is the maximum number of logs to cache in-memory.
// This is used to reduce disk I/O for the recently committed entries.
raftLogCacheSize = 512
)
// main entry point of application start
// run using CONFIG=config.yaml ./program
func main() {
v := viper.New()
v.AutomaticEnv()
if err := v.BindEnv(confKeys...); err != nil {
log.Fatal(err)
return
}
conf := config{
Server: configServer{
Port: v.GetInt(serverPort),
Addr: v.GetString(serverAddr),
},
Raft: configRaft{
NodeId: v.GetString(raftNodeId),
Port: v.GetInt(raftPort),
Addr: v.GetString(raftAddr),
VolumeDir: v.GetString(raftVolDir),
},
}
log.Printf("%+v\n", conf)
// Preparing OrderBook
ob := orderbook.NewOrderBook()
raftBinAddr := fmt.Sprintf("%s:%d", conf.Raft.Addr, conf.Raft.Port)
raftConf := raft.DefaultConfig()
raftConf.LocalID = raft.ServerID(conf.Raft.NodeId)
raftConf.SnapshotThreshold = 1024
fsmStore := fsm.NewOrderbookFSM(*ob)
store, err := raftboltdb.NewBoltStore(filepath.Join(conf.Raft.VolumeDir, "raft.dataRepo"))
if err != nil {
log.Fatal(err)
return
}
// Wrap the store in a LogCache to improve performance.
cacheStore, err := raft.NewLogCache(raftLogCacheSize, store)
if err != nil {
log.Fatal(err)
return
}
snapshotStore, err := raft.NewFileSnapshotStore(conf.Raft.VolumeDir, raftSnapShotRetain, os.Stdout)
if err != nil {
log.Fatal(err)
return
}
tcpAddr, err := net.ResolveTCPAddr("tcp", raftBinAddr)
if err != nil {
log.Fatal(err)
return
}
transport, err := raft.NewTCPTransport(raftBinAddr, tcpAddr, maxPool, tcpTimeout, os.Stdout)
if err != nil {
log.Fatal(err)
return
}
raftServer, err := raft.NewRaft(raftConf, fsmStore, cacheStore, store, snapshotStore, transport)
if err != nil {
log.Fatal(err)
return
}
// always start single server as a leader
configuration := raft.Configuration{
Servers: []raft.Server{
{
ID: raft.ServerID(conf.Raft.NodeId),
Address: transport.LocalAddr(),
},
},
}
raftServer.BootstrapCluster(configuration)
srv := server.New(fmt.Sprintf(":%d", conf.Server.Port), ob, raftServer)
if err := srv.Start(); err != nil {
log.Fatal(err)
}
}