Skip to content

Commit

Permalink
dump
Browse files Browse the repository at this point in the history
  • Loading branch information
naveenpaul1 committed Dec 11, 2024
1 parent 8b2a005 commit 5cd3837
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ func CmdIntCheck() *cobra.Command {

cmd.Flags().String("kubeconfig", "", "kubeconfig path")
cmd.Flags().Bool("dump-data-map", false, "if set to true will dump a json object with a full map of object metadata")

cmd.Flags().String("dir", "", "dump dir path")
return cmd
}
64 changes: 48 additions & 16 deletions pkg/diagnostics/intcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"os/exec"
"time"

"github.com/spf13/cobra"

Expand All @@ -15,28 +16,56 @@ import (

const defaultDBName = "nbcore"

var dbStatDataFolderPath = ""

func RunIntCheck(cmd *cobra.Command, args []string) {
kubeconfig, _ := cmd.Flags().GetString("kubeconfig")
dataDump, _ := cmd.Flags().GetBool("dump-data-map")
destDir, _ := cmd.Flags().GetString("dir")

dbname := os.Getenv("NOOBAA_DB")
if dbname == "" {
dbname = defaultDBName
}

runIntCheck(kubeconfig, dbname, dataDump)
runIntCheck(kubeconfig, dbname, dataDump, destDir)
}

func runIntCheck(kubeconfig, dbname string, dataDump bool) {
func runIntCheck(kubeconfig, dbname string, dataDump bool, destDir string) {
kubecommand := util.GetAvailabeKubeCli()
objectmds, err := getObjectMds(kubecommand, dbname, 0)

var folderName = fmt.Sprintf("%s_%d", "noobaa_db_int_check", time.Now().Unix())
if destDir != "" {
dbStatDataFolderPath = fmt.Sprintf("%s/%s", destDir, folderName)
} else {
dbStatDataFolderPath = folderName
}

// Create the folder containing the stat file
err := os.Mkdir(dbStatDataFolderPath, os.ModePerm)
if err != nil {
log.Fatalf(`❌ Could not create directory %s, reason: %s`, folderName, err)
}

statFilePath := fmt.Sprintf("%s/%s.sql", dbStatDataFolderPath, folderName)

// Create the stat file
outfile, err := os.Create(statFilePath)
if err != nil {
log.Printf(`❌ can not create db stat at path %v: %v`, dbStatDataFolderPath, err)
}

// Redirect the command's output to the local stat file
defer outfile.Close()

objectmds, err := getObjectMds(kubeconfig, kubecommand, dbname, 0)
if err != nil {
log.Fatalln(err)
}

partsIntegrityIssues := 0
for _, objectmd := range objectmds {
parts_data, err := getObjectParts(kubecommand, dbname, objectmd["_id"].(string))
parts_data, err := getObjectParts(kubeconfig, kubecommand, dbname, objectmd["_id"].(string))
if err != nil {
log.Fatalln("failed to get object part data for object:", objectmd)
}
Expand All @@ -59,7 +88,7 @@ func runIntCheck(kubeconfig, dbname string, dataDump bool) {
chunkIntegrityIssues := 0
for _, objectmd := range objectmds {
for _, objpart := range objectmd["parts_data"].([]map[string]any) {
chunksdata, err := getChunkData(kubecommand, dbname, objpart["chunk"].(string))
chunksdata, err := getChunkData(kubeconfig, kubecommand, dbname, objpart["chunk"].(string))
if err != nil {
log.Fatalf(
"failed to get chunk data for part %s of object %s\n",
Expand Down Expand Up @@ -89,7 +118,7 @@ func runIntCheck(kubeconfig, dbname string, dataDump bool) {
fragIDs = append(fragIDs, frag["_id"].(string))
}

realFragData, err := getFragsData(kubecommand, dbname, fragIDs)
realFragData, err := getFragsData(kubeconfig, kubecommand, dbname, fragIDs)
if err != nil {
fragsIntegrityIssues += 1
log.Println("failed to fragment data for chunk - ", chunkdata["_id"])
Expand All @@ -110,26 +139,26 @@ func runIntCheck(kubeconfig, dbname string, dataDump bool) {
log.Println("Object Chunks integrity issues - ", chunkIntegrityIssues)

if dataDump {
json.NewEncoder(os.Stdout).Encode(objectmds)
json.NewEncoder(outfile).Encode(objectmds)

Check failure on line 142 in pkg/diagnostics/intcheck.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `(*encoding/json.Encoder).Encode` is not checked (errcheck)

Check failure on line 142 in pkg/diagnostics/intcheck.go

View workflow job for this annotation

GitHub Actions / run-tests

Error return value of `(*encoding/json.Encoder).Encode` is not checked (errcheck)
}
}

func getObjectMds(kubecommand, dbname string, page int) ([]map[string]any, error) {
func getObjectMds(kubeconfig, kubecommand, dbname string, page int) ([]map[string]any, error) {
sql := fmt.Sprintf("SELECT json_agg(data) FROM objectmds LIMIT 1000 OFFSET %d;", page*1000)
return getJSONDataFromDB(kubecommand, dbname, sql)
return getJSONDataFromDB(kubeconfig, kubecommand, dbname, sql)
}

func getObjectParts(kubecommand, dbname, objectID string) ([]map[string]any, error) {
func getObjectParts(kubeconfig, kubecommand, dbname, objectID string) ([]map[string]any, error) {
sql := fmt.Sprintf("SELECT json_agg(data) FROM objectparts WHERE data->>'obj' = '%s';", objectID)
return getJSONDataFromDB(kubecommand, dbname, sql)
return getJSONDataFromDB(kubeconfig, kubecommand, dbname, sql)
}

func getChunkData(kubecommand, dbname, chunkID string) ([]map[string]any, error) {
func getChunkData(kubeconfig, kubecommand, dbname, chunkID string) ([]map[string]any, error) {
sql := fmt.Sprintf("SELECT json_agg(data) FROM datachunks WHERE data->>'_id' = '%s';", chunkID)
return getJSONDataFromDB(kubecommand, dbname, sql)
return getJSONDataFromDB(kubeconfig, kubecommand, dbname, sql)
}

func getFragsData(kubecommand, dbname string, frags []string) ([]map[string]any, error) {
func getFragsData(kubeconfig, kubecommand, dbname string, frags []string) ([]map[string]any, error) {
ids := ""
for idx, frag := range frags {
ids += ("'" + frag + "'")
Expand All @@ -139,10 +168,10 @@ func getFragsData(kubecommand, dbname string, frags []string) ([]map[string]any,
}

sql := fmt.Sprintf("SELECT json_agg(data) FROM datablocks WHERE data->>'frag' IN (%s);", ids)
return getJSONDataFromDB(kubecommand, dbname, sql)
return getJSONDataFromDB(kubeconfig, kubecommand, dbname, sql)
}

func getJSONDataFromDB(kubecommand, dbname, sql string) ([]map[string]any, error) {
func getJSONDataFromDB(kubeconfig, kubecommand, dbname, sql string) ([]map[string]any, error) {
cmd := exec.Command(
kubecommand,
"exec",
Expand All @@ -157,6 +186,9 @@ func getJSONDataFromDB(kubecommand, dbname, sql string) ([]map[string]any, error
"-q",
"-t",
)
if len(kubeconfig) > 0 {
cmd.Env = append(cmd.Env, "KUBECONFIG="+kubeconfig)
}

output, err := cmd.Output()
if err != nil {
Expand Down

0 comments on commit 5cd3837

Please sign in to comment.