-
Notifications
You must be signed in to change notification settings - Fork 502
/
Copy pathapp.go
613 lines (523 loc) · 16.5 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package horizon
import (
"context"
"database/sql"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/stellar/go/clients/stellarcore"
"github.com/stellar/go/services/horizon/internal/corestate"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/httpx"
"github.com/stellar/go/services/horizon/internal/ingest"
"github.com/stellar/go/services/horizon/internal/ledger"
"github.com/stellar/go/services/horizon/internal/operationfeestats"
"github.com/stellar/go/services/horizon/internal/paths"
"github.com/stellar/go/services/horizon/internal/reap"
"github.com/stellar/go/services/horizon/internal/txsub"
"github.com/stellar/go/support/app"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/log"
"github.com/stellar/go/support/logmetrics"
)
// App represents the root of the state of a horizon instance.
type App struct {
done chan struct{}
doneOnce sync.Once
config Config
webServer *httpx.Server
historyQ *history.Q
primaryHistoryQ *history.Q
ctx context.Context
cancel func()
horizonVersion string
coreState corestate.Store
orderBookStream *ingest.OrderBookStream
submitter *txsub.System
paths paths.Finder
ingester ingest.System
reaper *reap.System
ticks *time.Ticker
ledgerState *ledger.State
// metrics
prometheusRegistry *prometheus.Registry
buildInfoGauge *prometheus.GaugeVec
ingestingGauge prometheus.Gauge
}
func (a *App) GetCoreState() corestate.State {
return a.coreState.Get()
}
const tickerMaxFrequency = 1 * time.Second
const tickerMaxDuration = 5 * time.Second
// NewApp constructs an new App instance from the provided config.
func NewApp(config Config) (*App, error) {
a := &App{
config: config,
ledgerState: &ledger.State{},
horizonVersion: app.Version(),
ticks: time.NewTicker(tickerMaxFrequency),
done: make(chan struct{}),
}
if err := a.init(); err != nil {
return nil, err
}
return a, nil
}
// Serve starts the horizon web server, binding it to a socket, setting up
// the shutdown signals.
func (a *App) Serve() error {
log.Infof("Starting horizon on :%d (ingest: %v)", a.config.Port, a.config.Ingest)
if a.config.AdminPort != 0 {
log.Infof("Starting internal server on :%d", a.config.AdminPort)
}
go a.run()
if !a.config.DisablePathFinding {
go a.orderBookStream.Run(a.ctx)
}
// WaitGroup for all go routines. Makes sure that DB is closed when
// all services gracefully shutdown.
var wg sync.WaitGroup
if a.ingester != nil {
wg.Add(1)
go func() {
a.ingester.Run()
wg.Done()
}()
}
if a.reaper != nil {
wg.Add(1)
go func() {
a.reaper.Run()
wg.Done()
}()
}
// configure shutdown signal handler
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-signalChan:
a.Close()
case <-a.done:
return
}
}()
wg.Add(1)
go func() {
a.waitForDone()
wg.Done()
}()
log.Infof("Starting to serve requests")
err := a.webServer.Serve()
if err != nil && err != http.ErrServerClosed {
return err
}
wg.Wait()
a.CloseDB()
log.Info("stopped")
return nil
}
// Close cancels the app. It does not close DB connections - use App.CloseDB().
func (a *App) Close() {
a.doneOnce.Do(func() {
close(a.done)
})
}
func (a *App) waitForDone() {
<-a.done
a.Shutdown()
}
func (a *App) Shutdown() {
if a.webServer != nil {
webShutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
a.webServer.Shutdown(webShutdownCtx)
}
a.cancel()
if a.ingester != nil {
a.ingester.Shutdown()
}
if a.reaper != nil {
a.reaper.Shutdown()
}
a.ticks.Stop()
}
// CloseDB closes DB connections. When using during web server shut down make
// sure all requests are first properly finished to avoid "sql: database is
// closed" errors.
func (a *App) CloseDB() {
a.historyQ.SessionInterface.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.
func (a *App) HorizonSession() db.SessionInterface {
return a.historyQ.SessionInterface.Clone()
}
func (a *App) Config() Config {
return a.config
}
// Paths returns the paths.Finder instance used by horizon
func (a *App) Paths() paths.Finder {
return a.paths
}
func isLocalAddress(url string, port uint) bool {
localHostURL := fmt.Sprintf("http://localhost:%d", port)
localIPURL := fmt.Sprintf("http://127.0.0.1:%d", port)
return strings.HasPrefix(url, localHostURL) || strings.HasPrefix(url, localIPURL)
}
// UpdateCoreLedgerState triggers a refresh of Stellar-Core ledger state.
// This is done separately from Horizon ledger state update to prevent issues
// in case Stellar-Core query timeout.
func (a *App) UpdateCoreLedgerState(ctx context.Context) {
var next ledger.CoreStatus
if a.config.StellarCoreURL == "" {
return
}
// #4446 If the ingestion state machine is in the build state, the query can time out
// because the captive-core buffer may be full. In this case, skip the check.
if a.config.CaptiveCoreToml != nil &&
isLocalAddress(a.config.StellarCoreURL, a.config.CaptiveCoreToml.HTTPPort) &&
a.ingester != nil && a.ingester.GetCurrentState() == ingest.Build {
return
}
logErr := func(err error, msg string) {
log.WithStack(err).WithField("err", err.Error()).Error(msg)
}
coreClient := &stellarcore.Client{
HTTP: http.DefaultClient,
URL: a.config.StellarCoreURL,
}
coreInfo, err := coreClient.Info(ctx)
if err != nil {
logErr(err, "failed to load the stellar-core info")
return
}
next.CoreLatest = int32(coreInfo.Info.Ledger.Num)
a.ledgerState.SetCoreStatus(next)
}
// UpdateHorizonLedgerState triggers a refresh of Horizon ledger state.
// This is done separately from Core ledger state update to prevent issues
// in case Stellar-Core query timeout.
func (a *App) UpdateHorizonLedgerState(ctx context.Context) {
var next ledger.HorizonStatus
logErr := func(err error, msg string) {
log.WithStack(err).WithField("err", err.Error()).Error(msg)
}
var err error
next.HistoryLatest, next.HistoryLatestClosedAt, err =
a.HistoryQ().LatestLedgerSequenceClosedAt(ctx)
if err != nil {
logErr(err, "failed to load the latest known ledger state from history DB")
return
}
err = a.HistoryQ().ElderLedger(ctx, &next.HistoryElder)
if err != nil {
logErr(err, "failed to load the oldest known ledger state from history DB")
return
}
next.ExpHistoryLatest, err = a.HistoryQ().GetLastLedgerIngestNonBlocking(ctx)
if err != nil {
logErr(err, "failed to load the oldest known exp ledger state from history DB")
return
}
a.ledgerState.SetHorizonStatus(next)
}
// UpdateFeeStatsState triggers a refresh of several operation fee metrics.
func (a *App) UpdateFeeStatsState(ctx context.Context) {
var (
next operationfeestats.State
latest history.LatestLedger
feeStats history.FeeStats
capacityStats history.LedgerCapacityUsageStats
)
logErr := func(err error, msg string) {
// If DB is empty ignore the error
if errors.Cause(err) == sql.ErrNoRows {
return
}
log.WithStack(err).WithField("err", err.Error()).Error(msg)
}
cur, ok := operationfeestats.CurrentState()
err := a.HistoryQ().LatestLedgerBaseFeeAndSequence(ctx, &latest)
if err != nil {
logErr(err, "failed to load the latest known ledger's base fee and sequence number")
return
}
// finish early if no new ledgers
if ok && cur.LastLedger == uint32(latest.Sequence) {
return
}
next.LastBaseFee = int64(latest.BaseFee)
next.LastLedger = uint32(latest.Sequence)
err = a.HistoryQ().FeeStats(ctx, latest.Sequence, &feeStats)
if err != nil {
logErr(err, "failed to load operation fee stats")
return
}
err = a.HistoryQ().LedgerCapacityUsageStats(ctx, latest.Sequence, &capacityStats)
if err != nil {
logErr(err, "failed to load ledger capacity usage stats")
return
}
next.LedgerCapacityUsage = capacityStats.CapacityUsage.String
// if no transactions in last 5 ledgers, return
// latest ledger's base fee for all
if !feeStats.MaxFeeMode.Valid && !feeStats.MaxFeeMin.Valid {
// MaxFee
next.MaxFeeMax = next.LastBaseFee
next.MaxFeeMin = next.LastBaseFee
next.MaxFeeMode = next.LastBaseFee
next.MaxFeeP10 = next.LastBaseFee
next.MaxFeeP20 = next.LastBaseFee
next.MaxFeeP30 = next.LastBaseFee
next.MaxFeeP40 = next.LastBaseFee
next.MaxFeeP50 = next.LastBaseFee
next.MaxFeeP60 = next.LastBaseFee
next.MaxFeeP70 = next.LastBaseFee
next.MaxFeeP80 = next.LastBaseFee
next.MaxFeeP90 = next.LastBaseFee
next.MaxFeeP95 = next.LastBaseFee
next.MaxFeeP99 = next.LastBaseFee
// FeeCharged
next.FeeChargedMax = next.LastBaseFee
next.FeeChargedMin = next.LastBaseFee
next.FeeChargedMode = next.LastBaseFee
next.FeeChargedP10 = next.LastBaseFee
next.FeeChargedP20 = next.LastBaseFee
next.FeeChargedP30 = next.LastBaseFee
next.FeeChargedP40 = next.LastBaseFee
next.FeeChargedP50 = next.LastBaseFee
next.FeeChargedP60 = next.LastBaseFee
next.FeeChargedP70 = next.LastBaseFee
next.FeeChargedP80 = next.LastBaseFee
next.FeeChargedP90 = next.LastBaseFee
next.FeeChargedP95 = next.LastBaseFee
next.FeeChargedP99 = next.LastBaseFee
} else {
// MaxFee
next.MaxFeeMax = feeStats.MaxFeeMax.Int64
next.MaxFeeMin = feeStats.MaxFeeMin.Int64
next.MaxFeeMode = feeStats.MaxFeeMode.Int64
next.MaxFeeP10 = feeStats.MaxFeeP10.Int64
next.MaxFeeP20 = feeStats.MaxFeeP20.Int64
next.MaxFeeP30 = feeStats.MaxFeeP30.Int64
next.MaxFeeP40 = feeStats.MaxFeeP40.Int64
next.MaxFeeP50 = feeStats.MaxFeeP50.Int64
next.MaxFeeP60 = feeStats.MaxFeeP60.Int64
next.MaxFeeP70 = feeStats.MaxFeeP70.Int64
next.MaxFeeP80 = feeStats.MaxFeeP80.Int64
next.MaxFeeP90 = feeStats.MaxFeeP90.Int64
next.MaxFeeP95 = feeStats.MaxFeeP95.Int64
next.MaxFeeP99 = feeStats.MaxFeeP99.Int64
// FeeCharged
next.FeeChargedMax = feeStats.FeeChargedMax.Int64
next.FeeChargedMin = feeStats.FeeChargedMin.Int64
next.FeeChargedMode = feeStats.FeeChargedMode.Int64
next.FeeChargedP10 = feeStats.FeeChargedP10.Int64
next.FeeChargedP20 = feeStats.FeeChargedP20.Int64
next.FeeChargedP30 = feeStats.FeeChargedP30.Int64
next.FeeChargedP40 = feeStats.FeeChargedP40.Int64
next.FeeChargedP50 = feeStats.FeeChargedP50.Int64
next.FeeChargedP60 = feeStats.FeeChargedP60.Int64
next.FeeChargedP70 = feeStats.FeeChargedP70.Int64
next.FeeChargedP80 = feeStats.FeeChargedP80.Int64
next.FeeChargedP90 = feeStats.FeeChargedP90.Int64
next.FeeChargedP95 = feeStats.FeeChargedP95.Int64
next.FeeChargedP99 = feeStats.FeeChargedP99.Int64
}
operationfeestats.SetState(next)
}
// UpdateStellarCoreInfo updates the value of CoreVersion,
// CurrentProtocolVersion, and CoreSupportedProtocolVersion from the Stellar
// core API.
//
// Warning: This method should only return an error if it is fatal. See usage
// in `App.Tick`
func (a *App) UpdateStellarCoreInfo(ctx context.Context) error {
if a.config.StellarCoreURL == "" {
return nil
}
// #4446 If the ingestion state machine is in the build state, the query can time out
// because the captive-core buffer may be full. In this case, skip the check.
if a.config.CaptiveCoreToml != nil &&
isLocalAddress(a.config.StellarCoreURL, a.config.CaptiveCoreToml.HTTPPort) &&
a.ingester != nil && a.ingester.GetCurrentState() == ingest.Build {
return nil
}
core := &stellarcore.Client{
URL: a.config.StellarCoreURL,
}
resp, err := core.Info(ctx)
if err != nil {
log.Warnf("could not load stellar-core info: %s", err)
return nil
}
// Check if NetworkPassphrase is different, if so exit Horizon as it can break the
// state of the application.
if resp.Info.Network != a.config.NetworkPassphrase {
return fmt.Errorf(
"Network passphrase of stellar-core (%s) does not match Horizon configuration (%s). Exiting...",
resp.Info.Network,
a.config.NetworkPassphrase,
)
}
a.coreState.Set(resp)
return nil
}
// DeleteUnretainedHistory forwards to the app's reaper. See
// `reap.DeleteUnretainedHistory` for details
func (a *App) DeleteUnretainedHistory(ctx context.Context) error {
return a.reaper.DeleteUnretainedHistory(ctx)
}
// Tick triggers horizon to update all of it's background processes such as
// transaction submission, metrics, ingestion and reaping.
func (a *App) Tick(ctx context.Context) error {
var wg sync.WaitGroup
log.Debug("ticking app")
// update ledger state, operation fee state, and stellar-core info in parallel
wg.Add(4)
var err error
go func() { a.UpdateCoreLedgerState(ctx); wg.Done() }()
go func() { a.UpdateHorizonLedgerState(ctx); wg.Done() }()
go func() { a.UpdateFeeStatsState(ctx); wg.Done() }()
go func() { err = a.UpdateStellarCoreInfo(ctx); wg.Done() }()
wg.Wait()
if err != nil {
return err
}
wg.Add(1)
go func() { a.submitter.Tick(ctx); wg.Done() }()
wg.Wait()
log.Debug("finished ticking app")
return ctx.Err()
}
// Init initializes app, using the config to populate db connections and
// whatnot.
func (a *App) init() error {
// app-context
a.ctx, a.cancel = context.WithCancel(context.Background())
// log
log.DefaultLogger.SetLevel(a.config.LogLevel)
logMetrics := logmetrics.New("horizon")
log.DefaultLogger.AddHook(logMetrics)
// sentry
initSentry(a)
// loggly
initLogglyLog(a)
// metrics and log.metrics
a.prometheusRegistry = prometheus.NewRegistry()
for _, counter := range logMetrics {
a.prometheusRegistry.MustRegister(counter)
}
// stellarCoreInfo
a.UpdateStellarCoreInfo(a.ctx)
// horizon-db and core-db
mustInitHorizonDB(a)
if a.config.Ingest {
// ingester
initIngester(a)
}
initPathFinder(a)
// txsub
initSubmissionSystem(a)
// reaper
a.reaper = reap.New(
a.config.HistoryRetentionCount,
a.config.HistoryRetentionReapCount,
a.HorizonSession(),
a.ledgerState)
// go metrics
initGoMetrics(a)
// process metrics
initProcessMetrics(a)
// db-metrics
initDbMetrics(a)
// ingest.metrics
initIngestMetrics(a)
// txsub.metrics
initTxSubMetrics(a)
routerConfig := httpx.RouterConfig{
DBSession: a.historyQ.SessionInterface,
TxSubmitter: a.submitter,
RateQuota: a.config.RateQuota,
BehindCloudflare: a.config.BehindCloudflare,
BehindAWSLoadBalancer: a.config.BehindAWSLoadBalancer,
SSEUpdateFrequency: a.config.SSEUpdateFrequency,
StaleThreshold: a.config.StaleThreshold,
ConnectionTimeout: a.config.ConnectionTimeout,
ClientQueryTimeout: a.config.ClientQueryTimeout,
MaxConcurrentRequests: a.config.MaxConcurrentRequests,
MaxHTTPRequestSize: a.config.MaxHTTPRequestSize,
NetworkPassphrase: a.config.NetworkPassphrase,
MaxPathLength: a.config.MaxPathLength,
MaxAssetsPerPathRequest: a.config.MaxAssetsPerPathRequest,
PathFinder: a.paths,
PrometheusRegistry: a.prometheusRegistry,
CoreGetter: a,
HorizonVersion: a.horizonVersion,
FriendbotURL: a.config.FriendbotURL,
DisableTxSub: a.config.DisableTxSub,
StellarCoreURL: a.config.StellarCoreURL,
HealthCheck: healthCheck{
session: a.historyQ.SessionInterface,
ctx: a.ctx,
core: &stellarcore.Client{
HTTP: &http.Client{Timeout: infoRequestTimeout},
URL: a.config.StellarCoreURL,
},
cache: newHealthCache(healthCacheTTL),
},
SkipTxMeta: a.config.SkipTxmeta,
}
if a.primaryHistoryQ != nil {
routerConfig.PrimaryDBSession = a.primaryHistoryQ.SessionInterface
}
var err error
config := httpx.ServerConfig{
Port: uint16(a.config.Port),
AdminPort: uint16(a.config.AdminPort),
}
if a.config.TLSCert != "" && a.config.TLSKey != "" {
config.TLSConfig = &httpx.TLSConfig{
CertPath: a.config.TLSCert,
KeyPath: a.config.TLSKey,
}
}
a.webServer, err = httpx.NewServer(config, routerConfig, a.ledgerState)
if err != nil {
return err
}
// web.metrics
initWebMetrics(a)
return nil
}
// run is the function that runs in the background that triggers Tick each
// second
func (a *App) run() {
for {
select {
case <-a.ticks.C:
ctx, cancel := context.WithTimeout(a.ctx, tickerMaxDuration)
err := a.Tick(ctx)
if err != nil {
log.Warnf("error ticking app: %s", err)
}
cancel() // Release timer
case <-a.ctx.Done():
log.Info("finished background ticker")
return
}
}
}