Skip to content

Commit

Permalink
Adding healthcheck for solo mode
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Nov 4, 2024
1 parent 0206c1f commit 17c2ce9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
25 changes: 16 additions & 9 deletions cmd/thor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"fmt"
"io"
"math"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -167,7 +168,7 @@ func defaultAction(ctx *cli.Context) error {
return errors.Wrap(err, "parse verbosity flag")
}
logLevel := initLogger(lvl, ctx.Bool(jsonLogsFlag.Name))
healthStatus := &health.Health{}
healthStatus := health.New(time.Duration(thor.BlockInterval))

// enable metrics as soon as possible
metricsURL := ""
Expand Down Expand Up @@ -313,7 +314,18 @@ func soloAction(ctx *cli.Context) error {
}

logLevel := initLogger(lvl, ctx.Bool(jsonLogsFlag.Name))
healthStatus := &health.Health{}

onDemandBlockProduction := ctx.Bool(onDemandFlag.Name)
blockProductionInterval := ctx.Uint64(blockInterval.Name)
if blockProductionInterval == 0 {
return errors.New("block-interval cannot be zero")
}

blockProductionHealthCheck := time.Duration(blockProductionInterval) * time.Second
if onDemandBlockProduction {
blockProductionHealthCheck = math.MaxUint16 * time.Second
}
healthStatus := health.NewSolo(blockProductionHealthCheck)

// enable metrics as soon as possible
metricsURL := ""
Expand Down Expand Up @@ -436,11 +448,6 @@ func soloAction(ctx *cli.Context) error {
srvCloser()
}()

blockInterval := ctx.Uint64(blockInterval.Name)
if blockInterval == 0 {
return errors.New("block-interval cannot be zero")
}

printStartupMessage2(gene, apiURL, "", metricsURL, adminURL)

optimizer := optimizer.New(mainDB, repo, !ctx.Bool(disablePrunerFlag.Name))
Expand All @@ -452,9 +459,9 @@ func soloAction(ctx *cli.Context) error {
healthStatus,
txPool,
ctx.Uint64(gasLimitFlag.Name),
ctx.Bool(onDemandFlag.Name),
onDemandBlockProduction,
skipLogs,
blockInterval,
blockProductionInterval,
forkConfig).Run(exitSignal)
}

Expand Down
39 changes: 29 additions & 10 deletions health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package health

import (
"fmt"
"sync"
"time"

Expand All @@ -24,18 +25,26 @@ type Status struct {
}

type Health struct {
lock sync.RWMutex
newBestBlock time.Time
bestBlockID *thor.Bytes32
bootstrapStatus bool
lock sync.RWMutex
newBestBlock time.Time
bestBlockID *thor.Bytes32
bootstrapStatus bool
timeBetweenBlocks time.Duration
}

func (h *Health) NewBestBlock(ID thor.Bytes32) {
h.lock.Lock()
defer h.lock.Unlock()
const delayBuffer = 5 * time.Second

h.newBestBlock = time.Now()
h.bestBlockID = &ID
func NewSolo(timeBetweenBlocks time.Duration) *Health {
return &Health{
timeBetweenBlocks: timeBetweenBlocks + delayBuffer,
// there is no bootstrap in solo mode
bootstrapStatus: true,
}
}
func New(timeBetweenBlocks time.Duration) *Health {
return &Health{
timeBetweenBlocks: timeBetweenBlocks + delayBuffer,
}
}

func (h *Health) Status() (*Status, error) {
Expand All @@ -47,16 +56,26 @@ func (h *Health) Status() (*Status, error) {
BestBlockIngestionTimestamp: &h.newBestBlock,
}

healthy := time.Since(h.newBestBlock) <= 10*time.Second && // less than 10 secs have passed since a new block was received
healthy := time.Since(h.newBestBlock) <= h.timeBetweenBlocks && // less than 10 secs have passed since a new block was received
h.bootstrapStatus

fmt.Println("time between blocks", time.Since(h.newBestBlock).Seconds(), "of max", h.timeBetweenBlocks.Seconds())

return &Status{
Healthy: healthy,
BlockIngestion: blockIngest,
ChainBootstrapped: h.bootstrapStatus,
}, nil
}

func (h *Health) NewBestBlock(ID thor.Bytes32) {
h.lock.Lock()
defer h.lock.Unlock()

h.newBestBlock = time.Now()
h.bestBlockID = &ID
}

func (h *Health) BootstrapStatus(bootstrapStatus bool) {
h.lock.Lock()
defer h.lock.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestHealth_NewBestBlock(t *testing.T) {
h := &Health{}
h := New(time.Duration(thor.BlockInterval) * time.Second)
blockID := thor.Bytes32{0x01, 0x02, 0x03}

h.NewBestBlock(blockID)
Expand Down

0 comments on commit 17c2ce9

Please sign in to comment.