Skip to content

Commit

Permalink
Merge pull request #1255 from gohornet/feat/db-health-tool
Browse files Browse the repository at this point in the history
Add tool to check database health
  • Loading branch information
muXxer authored Dec 23, 2021
2 parents 3fa0446 + 5b68324 commit 2ccd81d
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/model/storage/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ func (s *StoreHealthTracker) IsTainted() (bool, error) {
return contains, nil
}

// DatabaseVersion returns the database version.
func (s *StoreHealthTracker) DatabaseVersion() (int, error) {

value, err := s.store.Get([]byte("dbVersion"))
if err != nil {
return 0, errors.Wrap(NewDatabaseError(err), "failed to read database version")
}

if len(value) < 1 {
return 0, errors.Wrap(NewDatabaseError(err), "failed to read database version")
}

return int(value[0]), nil
}

func (s *StoreHealthTracker) setDatabaseVersion(version byte) error {

_, err := s.store.Get([]byte("dbVersion"))
Expand Down
83 changes: 83 additions & 0 deletions pkg/toolset/database_health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package toolset

import (
"fmt"
"os"
"path"

flag "github.com/spf13/pflag"

"github.com/gohornet/hornet/pkg/database"
"github.com/gohornet/hornet/pkg/model/storage"

"github.com/iotaledger/hive.go/configuration"
)

func databaseHealth(_ *configuration.Configuration, args []string) error {

fs := flag.NewFlagSet("", flag.ExitOnError)
databasePath := fs.String("database", "", "the path to the database folder that should be checked")

fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", ToolDatabaseHealth)
fs.PrintDefaults()
}

if err := fs.Parse(args); err != nil {
return err
}

// Check if all parameters were parsed
if len(args) == 0 || fs.NArg() != 0 {
fs.Usage()
os.Exit(2)
}

checkDatabaseHealth := func(path string, name string) error {

dbExists, err := database.DatabaseExists(path)
if err != nil {
return err
}

if !dbExists {
print(fmt.Sprintf("database %s does not exist (%s)!\n", name, path))
return nil
}

dbStore, err := database.StoreWithDefaultSettings(path, false)
if err != nil {
return fmt.Errorf("%s database initialization failed: %w", name, err)
}
defer func() { _ = dbStore.Close() }()

healthTracker := storage.NewStoreHealthTracker(dbStore)

dbVersion, err := healthTracker.DatabaseVersion()
if err != nil {
return err
}

isCorrupted, err := healthTracker.IsCorrupted()
if err != nil {
return err
}

isTainted, err := healthTracker.IsTainted()
if err != nil {
return err
}

print(fmt.Sprintf("Database: '%s', Version: %d, IsCorrupted: %t, IsTainted: %t\n", name, dbVersion, isCorrupted, isTainted))
return nil
}

dbPath := *databasePath
dbName := path.Base(dbPath)

if err := checkDatabaseHealth(dbPath, dbName); err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions pkg/toolset/toolset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
ToolBenchmarkCPU = "bench-cpu"
ToolDatabaseMigration = "db-migration"
ToolDatabaseLedgerHash = "db-hash"
ToolDatabaseHealth = "db-health"
ToolDatabaseSplit = "db-split"
ToolCoordinatorFixStateFile = "coo-fix-state"
)
Expand Down Expand Up @@ -64,6 +65,7 @@ func HandleTools(nodeConfig *configuration.Configuration) {
ToolBenchmarkCPU: benchmarkCPU,
ToolDatabaseMigration: databaseMigration,
ToolDatabaseLedgerHash: databaseLedgerHash,
ToolDatabaseHealth: databaseHealth,
ToolDatabaseSplit: databaseSplit,
ToolCoordinatorFixStateFile: coordinatorFixStateFile,
}
Expand Down Expand Up @@ -98,6 +100,7 @@ func listTools() {
fmt.Printf("%-20s benchmarks the CPU performance\n", fmt.Sprintf("%s:", ToolBenchmarkCPU))
fmt.Printf("%-20s migrates the database to another engine\n", fmt.Sprintf("%s:", ToolDatabaseMigration))
fmt.Printf("%-20s calculates the sha256 hash of the ledger state of a database\n", fmt.Sprintf("%s:", ToolDatabaseLedgerHash))
fmt.Printf("%-20s checks the health status of the database\n", fmt.Sprintf("%s:", ToolDatabaseHealth))
fmt.Printf("%-20s split a legacy database into `tangle` and `utxo`\n", fmt.Sprintf("%s:", ToolDatabaseSplit))
fmt.Printf("%-20s applies the latest milestone in the database to the coordinator state file\n", fmt.Sprintf("%s:", ToolCoordinatorFixStateFile))
}

0 comments on commit 2ccd81d

Please sign in to comment.