/
app.go
315 lines (261 loc) · 7.71 KB
/
app.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package horizon
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"runtime"
"sync"
"time"
"github.com/garyburd/redigo/redis"
metrics "github.com/rcrowley/go-metrics"
"github.com/stellar/go/build"
"github.com/stellar/go/support/db"
"github.com/stellar/horizon/db2/core"
"github.com/stellar/horizon/db2/history"
"github.com/stellar/horizon/friendbot"
"github.com/stellar/horizon/ingest"
"github.com/stellar/horizon/ledger"
"github.com/stellar/horizon/log"
"github.com/stellar/horizon/paths"
"github.com/stellar/horizon/reap"
"github.com/stellar/horizon/render/sse"
"github.com/stellar/horizon/txsub"
"golang.org/x/net/context"
"golang.org/x/net/http2"
graceful "gopkg.in/tylerb/graceful.v1"
)
// You can override this variable using: gb build -ldflags "-X main.version aabbccdd"
var version = ""
// App represents the root of the state of a horizon instance.
type App struct {
config Config
web *Web
historyQ *history.Q
coreQ *core.Q
ctx context.Context
cancel func()
redis *redis.Pool
coreVersion string
horizonVersion string
networkPassphrase string
protocolVersion int32
submitter *txsub.System
paths paths.Finder
friendbot *friendbot.Bot
ingester *ingest.System
reaper *reap.System
ticks *time.Ticker
// metrics
metrics metrics.Registry
historyLatestLedgerGauge metrics.Gauge
historyElderLedgerGauge metrics.Gauge
horizonConnGauge metrics.Gauge
coreLatestLedgerGauge metrics.Gauge
coreElderLedgerGauge metrics.Gauge
coreConnGauge metrics.Gauge
goroutineGauge metrics.Gauge
}
// SetVersion records the provided version string in the package level `version`
// var, which will be used for the reported horizon version.
func SetVersion(v string) {
version = v
}
// NewApp constructs an new App instance from the provided config.
func NewApp(config Config) (*App, error) {
result := &App{config: config}
result.horizonVersion = version
result.networkPassphrase = build.TestNetwork.Passphrase
result.ticks = time.NewTicker(1 * time.Second)
result.init()
return result, nil
}
// Serve starts the horizon web server, binding it to a socket, setting up
// the shutdown signals.
func (a *App) Serve() {
a.web.router.Compile()
http.Handle("/", a.web.router)
addr := fmt.Sprintf(":%d", a.config.Port)
srv := &graceful.Server{
Timeout: 10 * time.Second,
Server: &http.Server{
Addr: addr,
Handler: http.DefaultServeMux,
},
ShutdownInitiated: func() {
log.Info("received signal, gracefully stopping")
a.Close()
},
}
http2.ConfigureServer(srv.Server, nil)
log.Infof("Starting horizon on %s", addr)
go a.run()
var err error
if a.config.TLSCert != "" {
err = srv.ListenAndServeTLS(a.config.TLSCert, a.config.TLSKey)
} else {
err = srv.ListenAndServe()
}
if err != nil {
log.Panic(err)
}
log.Info("stopped")
}
// Close cancels the app and forces the closure of db connections
func (a *App) Close() {
a.cancel()
a.ticks.Stop()
a.historyQ.Session.DB.Close()
a.coreQ.Session.DB.Close()
}
// HistoryQ returns a helper object for performing sql queries against the
// history portion of horizon's database.
func (a *App) HistoryQ() *history.Q {
return a.historyQ
}
// HorizonSession returns a new session that loads data from the horizon
// database. The returned session is bound to `ctx`.
func (a *App) HorizonSession(ctx context.Context) *db.Session {
return &db.Session{DB: a.historyQ.Session.DB, Ctx: ctx}
}
// CoreSession returns a new session that loads data from the stellar core
// database. The returned session is bound to `ctx`.
func (a *App) CoreSession(ctx context.Context) *db.Session {
return &db.Session{DB: a.coreQ.Session.DB, Ctx: ctx}
}
// CoreQ returns a helper object for performing sql queries aginst the
// stellar core database.
func (a *App) CoreQ() *core.Q {
return a.coreQ
}
// IsHistoryStale returns true if the latest history ledger is more than
// `StaleThreshold` ledgers behind the latest core ledger
func (a *App) IsHistoryStale() bool {
if a.config.StaleThreshold == 0 {
return false
}
ls := ledger.CurrentState()
return (ls.CoreLatest - ls.HistoryLatest) > int32(a.config.StaleThreshold)
}
// UpdateLedgerState triggers a refresh of several metrics gauges, such as open
// db connections and ledger state
func (a *App) UpdateLedgerState() {
var err error
var next ledger.State
err = a.CoreQ().LatestLedger(&next.CoreLatest)
if err != nil {
goto Failed
}
err = a.CoreQ().ElderLedger(&next.CoreElder)
if err != nil {
goto Failed
}
err = a.HistoryQ().LatestLedger(&next.HistoryLatest)
if err != nil {
goto Failed
}
err = a.HistoryQ().ElderLedger(&next.HistoryElder)
if err != nil {
goto Failed
}
ledger.SetState(next)
return
Failed:
log.WithStack(err).
WithField("err", err.Error()).
Error("failed to load ledger state")
}
// UpdateStellarCoreInfo updates the value of coreVersion and networkPassphrase
// from the Stellar core API.
func (a *App) UpdateStellarCoreInfo() {
if a.config.StellarCoreURL == "" {
return
}
fail := func(err error) {
log.Warnf("could not load stellar-core info: %s", err)
}
resp, err := http.Get(fmt.Sprint(a.config.StellarCoreURL, "/info"))
if err != nil {
fail(err)
return
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
fail(err)
return
}
var responseJSON map[string]*json.RawMessage
err = json.Unmarshal(contents, &responseJSON)
if err != nil {
fail(err)
return
}
var serverInfo map[string]interface{}
err = json.Unmarshal(*responseJSON["info"], &serverInfo)
if err != nil {
fail(err)
return
}
// TODO: make resilient to changes in stellar-core's info output
a.coreVersion = serverInfo["build"].(string)
a.networkPassphrase = serverInfo["network"].(string)
a.protocolVersion = int32(serverInfo["protocol_version"].(float64))
}
// UpdateMetrics triggers a refresh of several metrics gauges, such as open
// db connections and ledger state
func (a *App) UpdateMetrics() {
a.goroutineGauge.Update(int64(runtime.NumGoroutine()))
ls := ledger.CurrentState()
a.historyLatestLedgerGauge.Update(int64(ls.HistoryLatest))
a.historyElderLedgerGauge.Update(int64(ls.HistoryElder))
a.coreLatestLedgerGauge.Update(int64(ls.CoreLatest))
a.coreElderLedgerGauge.Update(int64(ls.CoreElder))
a.horizonConnGauge.Update(int64(a.historyQ.Session.DB.Stats().OpenConnections))
a.coreConnGauge.Update(int64(a.coreQ.Session.DB.Stats().OpenConnections))
}
// DeleteUnretainedHistory forwards to the app's reaper. See
// `reap.DeleteUnretainedHistory` for details
func (a *App) DeleteUnretainedHistory() error {
return a.reaper.DeleteUnretainedHistory()
}
// Tick triggers horizon to update all of it's background processes such as
// transaction submission, metrics, ingestion and reaping.
func (a *App) Tick() {
var wg sync.WaitGroup
log.Debug("ticking app")
// update ledger state and stellar-core info in parallel
wg.Add(2)
go func() { a.UpdateLedgerState(); wg.Done() }()
go func() { a.UpdateStellarCoreInfo(); wg.Done() }()
wg.Wait()
if a.ingester != nil {
go a.ingester.Tick()
}
wg.Add(2)
go func() { a.reaper.Tick(); wg.Done() }()
go func() { a.submitter.Tick(a.ctx); wg.Done() }()
wg.Wait()
sse.Tick()
// finally, update metrics
a.UpdateMetrics()
log.Debug("finished ticking app")
}
// Init initializes app, using the config to populate db connections and
// whatnot.
func (a *App) init() {
appInit.Run(a)
}
// run is the function that runs in the background that triggers Tick each
// second
func (a *App) run() {
for {
select {
case <-a.ticks.C:
a.Tick()
case <-a.ctx.Done():
log.Info("finished background ticker")
return
}
}
}