-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
157 lines (139 loc) · 4.29 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
package main
import (
"fmt"
"github.com/ardanlabs/conf"
"github.com/cockroachdb/pebble"
"github.com/pkg/errors"
"github.com/qubic/go-archiver/processor"
"github.com/qubic/go-archiver/rpc"
"github.com/qubic/go-archiver/store"
"github.com/qubic/go-archiver/validator/tick"
qubic "github.com/qubic/go-node-connector"
"log"
"os"
"os/signal"
"syscall"
"time"
)
const prefix = "QUBIC_ARCHIVER"
func main() {
if err := run(); err != nil {
log.Fatalf("main: exited with error: %s", err.Error())
}
}
func run() error {
var cfg struct {
Server struct {
ReadTimeout time.Duration `conf:"default:5s"`
WriteTimeout time.Duration `conf:"default:5s"`
ShutdownTimeout time.Duration `conf:"default:5s"`
HttpHost string `conf:"default:0.0.0.0:8000"`
GrpcHost string `conf:"default:0.0.0.0:8001"`
NodeSyncThreshold int `conf:"default:3"`
ChainTickFetchUrl string `conf:"default:http://127.0.0.1:8080/max-tick"`
}
Pool struct {
NodeFetcherUrl string `conf:"default:http://127.0.0.1:8080/status"`
NodeFetcherTimeout time.Duration `conf:"default:2s"`
InitialCap int `conf:"default:5"`
MaxIdle int `conf:"default:20"`
MaxCap int `conf:"default:30"`
IdleTimeout time.Duration `conf:"default:15s"`
}
Qubic struct {
NodePort string `conf:"default:21841"`
StorageFolder string `conf:"default:store"`
ProcessTickTimeout time.Duration `conf:"default:5s"`
}
Store struct {
ResetEmptyTickKeys bool `conf:"default:false"`
}
}
if err := conf.Parse(os.Args[1:], prefix, &cfg); err != nil {
switch err {
case conf.ErrHelpWanted:
usage, err := conf.Usage(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case conf.ErrVersionWanted:
version, err := conf.VersionString(prefix, &cfg)
if err != nil {
return errors.Wrap(err, "generating config version")
}
fmt.Println(version)
return nil
}
return errors.Wrap(err, "parsing config")
}
out, err := conf.String(&cfg)
if err != nil {
return errors.Wrap(err, "generating config for output")
}
log.Printf("main: Config :\n%v\n", out)
levelOptions := pebble.LevelOptions{
BlockRestartInterval: 16,
BlockSize: 4096,
BlockSizeThreshold: 90,
Compression: pebble.ZstdCompression,
FilterPolicy: nil,
FilterType: pebble.TableFilter,
IndexBlockSize: 4096,
TargetFileSize: 2097152,
}
pebbleOptions := pebble.Options{
Levels: []pebble.LevelOptions{levelOptions},
}
db, err := pebble.Open(cfg.Qubic.StorageFolder, &pebbleOptions)
if err != nil {
return errors.Wrap(err, "opening db with zstd compression")
}
defer db.Close()
ps := store.NewPebbleStore(db, nil)
if cfg.Store.ResetEmptyTickKeys {
fmt.Printf("Resetting empty ticks for all epochs...\n")
err = tick.ResetEmptyTicksForAllEpochs(ps)
if err != nil {
return errors.Wrap(err, "resetting empty ticks keys")
}
}
err = tick.CalculateEmptyTicksForAllEpochs(ps)
if err != nil {
return errors.Wrap(err, "calculating empty ticks for all epochs")
}
p, err := qubic.NewPoolConnection(qubic.PoolConfig{
InitialCap: cfg.Pool.InitialCap,
MaxCap: cfg.Pool.MaxCap,
MaxIdle: cfg.Pool.MaxIdle,
IdleTimeout: cfg.Pool.IdleTimeout,
NodeFetcherUrl: cfg.Pool.NodeFetcherUrl,
NodeFetcherTimeout: cfg.Pool.NodeFetcherTimeout,
NodePort: cfg.Qubic.NodePort,
})
if err != nil {
return errors.Wrap(err, "creating qubic pool")
}
rpcServer := rpc.NewServer(cfg.Server.GrpcHost, cfg.Server.HttpHost, cfg.Server.NodeSyncThreshold, cfg.Server.ChainTickFetchUrl, ps, p)
err = rpcServer.Start()
if err != nil {
return errors.Wrap(err, "starting rpc server")
}
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
proc := processor.NewProcessor(p, ps, cfg.Qubic.ProcessTickTimeout)
procErrors := make(chan error, 1)
// Start the service listening for requests.
go func() {
procErrors <- proc.Start()
}()
for {
select {
case <-shutdown:
return errors.New("shutting down")
case err := <-procErrors:
return errors.Wrap(err, "archiver error")
}
}
}