Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/integration: collection of commands to export db data into csv #11991

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

taratorio
Copy link
Member

@taratorio taratorio commented Sep 13, 2024

Commands to export table+snapshot data for various tables:

  • integration export_header_td
  • integration export_heimdall_events
  • integration export_heimdall_events_per_block
  • integration export_heimdall_spans
  • integration export_heimdall_span_block_producer_selections
  • can add more in the future...

The csv outputs can then be opened in an IDE. Makes life easier when debugging issues. Example, heimdall events table + snapshots:
Screenshot 2024-09-14 at 16 04 31

@AskAlexSharov
Copy link
Collaborator

FYI: geth has geth db export geth db import:

   import               Imports leveldb-data from an exported RLP dump.
   export               Exports the chain data into an RLP dump. If the <dumpfile> has .gz suffix, gzip compression will be used.

@taratorio
Copy link
Member Author

FYI: geth has geth db export geth db import:

   import               Imports leveldb-data from an exported RLP dump.
   export               Exports the chain data into an RLP dump. If the <dumpfile> has .gz suffix, gzip compression will be used.

@AskAlexSharov thanks, I'll check it out.

I'm exporting tables in CSV because it is easy to visualise in an IDE. I find it quite helpful for debugging purposes. E.g. I just run integration export_heimdall_events --datadir=<datadir> --output.csv.file=<output_csv_file> and open the file in GoLand.

Screenshot 2024-09-14 at 16 04 31

@taratorio taratorio marked this pull request as ready for review September 15, 2024 19:50
label := kv.ChainDB
db, err := openDB(dbCfg(label, path.Join(datadirCli, label.String())), true, logger)
if err != nil {
logger.Error(err.Error())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would advise move all logic to a func like doExportHeaderTD - which will do usual return err and here log error once.

copy(blockHash[8:], k)

td := new(big.Int)
if err := rlp.Decode(bytes.NewReader(v), td); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use rlp.DecodeBytes - it will do less allocs. (see: #12013 )

getter := seg.MakeGetter()
for getter.HasNext() {
var buf []byte
buf, _ = getter.Next(buf)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably you want getter.Next(buf[:0])

seg := eventSegments[i]
getter := seg.MakeGetter()
for getter.HasNext() {
var buf []byte
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare buf outside of loop. then passing it to getter.Next will give an effect (it will not alloc buf on every iteration)

var buf []byte
buf, _ = getter.Next(buf)

eventBytes := rlp.RawValue(libcommon.Copy(buf[length.Hash+length.BlockNum+8:]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont use eventBytes outside of loop iteration, then no reason to common.Copy

return
}

out, err := os.Create(outputCsvFile)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably you want defer f.Close()

snapDir := path.Join(datadirCli, "snapshots")
logger.Info("snapshot dir info", "dir", snapDir)

allSnapshots := freezeblocks.NewRoSnapshots(ethconfig.BlocksFreezing{}, snapDir, 0, logger)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls use allSnapshots() helper in integration. because it using global singletones - and in future they may conflict with your objects (theoreticaly)

}

defer tx.Rollback()
snapDir := path.Join(datadirCli, "snapshots")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls use:
dirs := datadir.New(datadirCli)
dirs.Snap

from := make([]byte, 8)
binary.BigEndian.PutUint64(from, max(fromNum, lastFrozenEventId+1))
var k, v []byte
for k, v, err = c.Seek(from); iterateDb && err == nil && k != nil; k, v, err = c.Next() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theoretically logic of db read and snapshots read - better add to BlockReader object (like: .ForEachBorEvent()) - but maybe you need very-custom logic - then fine.

@taratorio taratorio marked this pull request as draft October 1, 2024 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants