From 18bf148017c75e520008af107ef46898b9fe4fa9 Mon Sep 17 00:00:00 2001 From: Mikhail Wall Date: Wed, 14 Jul 2021 10:28:56 +0300 Subject: [PATCH] implemented statedb status in /health endpoint (#967) * implemented statedb status * added new field to swagger.yml * added comments for exported types and functions related to the statedb health checking --- api/api.go | 4 ++++ api/api_test.go | 2 ++ api/health.go | 4 ++++ api/swagger.yml | 18 ++++++++++++++++- db/kvdb/kvdb.go | 2 -- db/statedb/statedb.go | 1 - health/checkers/statedb.go | 41 ++++++++++++++++++++++++++++++++++++++ node/node.go | 4 ++++ 8 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 health/checkers/statedb.go diff --git a/api/api.go b/api/api.go index 4f615731f..10730e44d 100644 --- a/api/api.go +++ b/api/api.go @@ -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" @@ -45,6 +46,7 @@ type API struct { h *historydb.HistoryDB cg *configAPI l2 *l2db.L2DB + stateDB *statedb.StateDB hermezAddress ethCommon.Address validate *validator.Validate } @@ -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) { @@ -80,6 +83,7 @@ func NewAPI( ChainID: consts.ChainID, }, l2: l2db, + stateDB: stateDB, hermezAddress: consts.HermezAddress, validate: newValidate(), } diff --git a/api/api_test.go b/api/api_test.go index 7624f686f..4dad03478 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -259,6 +259,7 @@ func TestMain(m *testing.M) { l2DB, nil, nil, + nil, ) if err != nil { log.Error(err) @@ -668,6 +669,7 @@ func TestTimeout(t *testing.T) { l2DBTO, nil, nil, + nil, ) require.NoError(t, err) diff --git a/api/health.go b/api/health.go index a7a80e59d..9e9815208 100644 --- a/api/health.go +++ b/api/health.go @@ -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) diff --git a/api/swagger.yml b/api/swagger.yml index 00d2e43d9..57c191945 100644 --- a/api/swagger.yml +++ b/api/swagger.yml @@ -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 @@ -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 diff --git a/db/kvdb/kvdb.go b/db/kvdb/kvdb.go index d65bfba0b..64bab1829 100644 --- a/db/kvdb/kvdb.go +++ b/db/kvdb/kvdb.go @@ -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{ @@ -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) diff --git a/db/statedb/statedb.go b/db/statedb/statedb.go index b11e74a12..0d1193db4 100644 --- a/db/statedb/statedb.go +++ b/db/statedb/statedb.go @@ -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, diff --git a/health/checkers/statedb.go b/health/checkers/statedb.go new file mode 100644 index 000000000..4705fb4d6 --- /dev/null +++ b/health/checkers/statedb.go @@ -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 +} diff --git a/node/node.go b/node/node.go index c53666c22..8ca527d39 100644 --- a/node/node.go +++ b/node/node.go @@ -459,6 +459,7 @@ func NewNode(mode Mode, cfg *config.Node, version string) (*Node, error) { server, historyDB, l2DB, + stateDB, ethClient, &cfg.Coordinator.ForgerAddress, ) @@ -573,6 +574,7 @@ func NewAPIServer(mode Mode, cfg *config.APIServer, version string, ethClient *e server, historyDB, l2DB, + nil, ethClient, forgerAddress, ) @@ -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) { @@ -640,6 +643,7 @@ func NewNodeAPI( engine, hdb, l2db, + stateDB, ethClient, forgerAddress, )