Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
implemented statedb status in /health endpoint (#967)
Browse files Browse the repository at this point in the history
* implemented statedb status

* added new field to swagger.yml

* added comments for exported types and functions related to the statedb health checking
  • Loading branch information
Mikelle authored Jul 14, 2021
1 parent 6a0bb24 commit 18bf148
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
4 changes: 4 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/hermeznetwork/hermez-node/api/parsers"
"github.com/hermeznetwork/hermez-node/db/historydb"
"github.com/hermeznetwork/hermez-node/db/l2db"
"github.com/hermeznetwork/hermez-node/db/statedb"
"github.com/hermeznetwork/hermez-node/metric"
"github.com/hermeznetwork/tracerr"
"gopkg.in/go-playground/validator.v9"
Expand All @@ -45,6 +46,7 @@ type API struct {
h *historydb.HistoryDB
cg *configAPI
l2 *l2db.L2DB
stateDB *statedb.StateDB
hermezAddress ethCommon.Address
validate *validator.Validate
}
Expand All @@ -56,6 +58,7 @@ func NewAPI(
server *gin.Engine,
hdb *historydb.HistoryDB,
l2db *l2db.L2DB,
stateDB *statedb.StateDB,
ethClient *ethclient.Client,
forgerAddress *ethCommon.Address,
) (*API, error) {
Expand All @@ -80,6 +83,7 @@ func NewAPI(
ChainID: consts.ChainID,
},
l2: l2db,
stateDB: stateDB,
hermezAddress: consts.HermezAddress,
validate: newValidate(),
}
Expand Down
2 changes: 2 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func TestMain(m *testing.M) {
l2DB,
nil,
nil,
nil,
)
if err != nil {
log.Error(err)
Expand Down Expand Up @@ -668,6 +669,7 @@ func TestTimeout(t *testing.T) {
l2DBTO,
nil,
nil,
nil,
)
require.NoError(t, err)

Expand Down
4 changes: 4 additions & 0 deletions api/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (a *API) healthRoute(version string, ethClient *ethclient.Client, forgerAdd
historyDBChecker := checkers.NewCheckerWithDB(a.h.DB().DB)
healthHandler.AddChecker("historyDB", historyDBChecker)
}
if a.stateDB != nil {
stateDBChecker := checkers.NewStateDBChecker(a.stateDB)
healthHandler.AddChecker("stateDB", stateDBChecker)
}
healthHandler.AddInfo("version", version)
t := time.Now().UTC()
healthHandler.AddInfo("timestamp", t)
Expand Down
18 changes: 17 additions & 1 deletion api/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3410,6 +3410,22 @@ components:
- status
- version
additionalProperties: false
statedb:
type: object
description: state db connection status
properties:
status:
type: string
description: Status of the connection. Possible responses - UP or DOWN
example: UP
batchNum:
type: integer
description: current batch num in last checkpoint of the state db
example: 24025
root:
type: integer
description: root of the underlying Merkle Tree in the last checkpoint of the state db
example: 20723800499378797360494097450413598946834655273323161758167114779532027183222
status:
type: string
description: Status of the hermez node. Possible responses - UP or DOWN
Expand All @@ -3424,7 +3440,7 @@ components:
example: v1.2.0
coordinatorForgerBalance:
type: string
description: Coordinator balance in ether. Returned with 18 decimals.
description: Coordinator balance in ether. Returned with 18 decimals. Not returned if node not in coord mode
example: 48901270356066543960
required:
- historyDB
Expand Down
2 changes: 0 additions & 2 deletions db/kvdb/kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func NewKVDB(cfg Config) (*KVDB, error) {
if err != nil {
return nil, tracerr.Wrap(err)
}

var last *Last
if !cfg.NoLast {
last = &Last{
Expand Down Expand Up @@ -435,7 +434,6 @@ func (k *KVDB) MakeCheckpoint() error {
return tracerr.Wrap(err)
}
}

// execute Checkpoint
if err := k.db.Pebble().Checkpoint(checkpointPath); err != nil {
return tracerr.Wrap(err)
Expand Down
1 change: 0 additions & 1 deletion db/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ func NewStateDB(cfg Config) (*StateDB, error) {
return nil, tracerr.Wrap(
fmt.Errorf("invalid StateDB parameters: StateDB type==TypeStateDB can not have nLevels!=0"))
}

return &StateDB{
cfg: cfg,
db: kv,
Expand Down
41 changes: 41 additions & 0 deletions health/checkers/statedb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package checkers

import (
"github.com/dimiro1/health"
"github.com/hermeznetwork/hermez-node/db/statedb"
)

// StateDBChecker struct for state db connection checker
type StateDBChecker struct {
stateDB *statedb.StateDB
}

// NewStateDBChecker init state db connection checker
func NewStateDBChecker(sdb *statedb.StateDB) StateDBChecker {
return StateDBChecker{
stateDB: sdb,
}
}

// Check state db health
func (sdb StateDBChecker) Check() health.Health {
h := health.NewHealth()

batchNum, err := sdb.stateDB.LastGetCurrentBatch()
if err != nil {
h.Down().AddInfo("error", err.Error())
return h
}

root, err := sdb.stateDB.LastMTGetRoot()
if err != nil {
h.Down().AddInfo("error", err.Error())
return h
}

h.Up().
AddInfo("batchNum", batchNum).
AddInfo("root", root)

return h
}
4 changes: 4 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ func NewNode(mode Mode, cfg *config.Node, version string) (*Node, error) {
server,
historyDB,
l2DB,
stateDB,
ethClient,
&cfg.Coordinator.ForgerAddress,
)
Expand Down Expand Up @@ -573,6 +574,7 @@ func NewAPIServer(mode Mode, cfg *config.APIServer, version string, ethClient *e
server,
historyDB,
l2DB,
nil,
ethClient,
forgerAddress,
)
Expand Down Expand Up @@ -629,6 +631,7 @@ func NewNodeAPI(
server *gin.Engine,
hdb *historydb.HistoryDB,
l2db *l2db.L2DB,
stateDB *statedb.StateDB,
ethClient *ethclient.Client,
forgerAddress *ethCommon.Address,
) (*NodeAPI, error) {
Expand All @@ -640,6 +643,7 @@ func NewNodeAPI(
engine,
hdb,
l2db,
stateDB,
ethClient,
forgerAddress,
)
Expand Down

0 comments on commit 18bf148

Please sign in to comment.