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

File Exporter: fixes for simple configuration #1232

Merged
merged 4 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions exporters/filewriter/file_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const exporterName = "filewriter"

type fileExporter struct {
round uint64
blockMetadataFile *os.File
blockMetadataFile string
blockMetadata BlockMetaData
cfg ExporterConfig
logger *logrus.Logger
Expand Down Expand Up @@ -68,18 +68,26 @@ func (exp *fileExporter) Init(cfg plugins.PluginConfig, logger *logrus.Logger) e
}
// initialize block metadata
file := path.Join(exp.cfg.BlocksDir, "metadata.json")
if _, err = os.Stat(file); errors.Is(err, os.ErrNotExist) {
exp.blockMetadataFile, err = os.Create(file)
exp.blockMetadataFile = file
var stat os.FileInfo
if stat, err = os.Stat(file); errors.Is(err, os.ErrNotExist) || (stat != nil && stat.Size() == 0) {
if stat != nil && stat.Size() == 0 {
// somehow it did not finish initializing
err = os.Remove(file)
if err != nil {
return fmt.Errorf("Init(): error creating file: %w", err)
}
}
err = exp.encodeMetadataToFile()
if err != nil {
return fmt.Errorf("error creating file: %w", err)
return fmt.Errorf("Init(): error creating file: %w", err)
}
exp.blockMetadata = BlockMetaData{
GenesisHash: "",
Network: "",
NextRound: exp.round,
}
} else {
exp.blockMetadataFile, err = os.OpenFile(file, os.O_WRONLY, 0775)
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}
Expand All @@ -102,37 +110,56 @@ func (exp *fileExporter) Config() plugins.PluginConfig {
return plugins.PluginConfig(ret)
}

func (exp *fileExporter) Close() error {
defer exp.blockMetadataFile.Close()
exp.logger.Infof("latest round on file: %d", exp.round)
err := json.NewEncoder(exp.blockMetadataFile).Encode(exp.blockMetadata)
func (exp *fileExporter) encodeMetadataToFile() error {
tempFilename := fmt.Sprintf("%s.temp", exp.blockMetadataFile)
file, err := os.Create(tempFilename)
if err != nil {
return fmt.Errorf("encodeMetadataToFile(): failed to create temp metadata file: %w", err)
}
defer file.Close()
err = json.NewEncoder(file).Encode(exp.blockMetadata)
if err != nil {
return fmt.Errorf("Close() encoding err %w", err)
return fmt.Errorf("encodeMetadataToFile(): failed to write temp metadata: %w", err)
}

err = os.Rename(tempFilename, exp.blockMetadataFile)
if err != nil {
return fmt.Errorf("encodeMetadataToFile(): failed to replace metadata file: %w", err)
}

return nil
}

func (exp *fileExporter) Close() error {
exp.logger.Infof("latest round on file: %d", exp.round)
return nil
}

func (exp *fileExporter) Receive(exportData data.BlockData) error {
if exp.blockMetadataFile == nil {
if exp.blockMetadataFile == "" {
return fmt.Errorf("exporter not initialized")
}
if exportData.Round() != exp.round {
return fmt.Errorf("wrong block. received round %d, expected round %d", exportData.Round(), exp.round)
return fmt.Errorf("Receive(): wrong block: received round %d, expected round %d", exportData.Round(), exp.round)
}
//write block to file
blockFile := path.Join(exp.cfg.BlocksDir, fmt.Sprintf("block_%d.json", exportData.Round()))
file, err := os.OpenFile(blockFile, os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("error opening file %s, %w", blockFile, err)
return fmt.Errorf("Receive(): error opening file %s: %w", blockFile, err)
}
defer file.Close()
err = json.NewEncoder(file).Encode(exportData)
if err != nil {
return fmt.Errorf("error encoding exportData in Receive(), %w", err)
return fmt.Errorf("Receive(): error encoding exportData: %w", err)
}
exp.logger.Infof("Added block %d", exportData.Round())
exp.round++
exp.blockMetadata.NextRound = exp.round
err = exp.encodeMetadataToFile()
if err != nil {
return fmt.Errorf("Receive() metadata encoding err %w", err)
}
return nil
}

Expand All @@ -142,9 +169,13 @@ func (exp *fileExporter) HandleGenesis(genesis bookkeeping.Genesis) error {
if exp.blockMetadata.GenesisHash == "" {
exp.blockMetadata.GenesisHash = gh
exp.blockMetadata.Network = string(genesis.Network)
err := exp.encodeMetadataToFile()
if err != nil {
return fmt.Errorf("HandleGenesis() metadata encoding err %w", err)
}
} else {
if exp.blockMetadata.GenesisHash != gh {
return fmt.Errorf("genesis hash in metadata does not match expected value. actual %s, expected %s ", gh, exp.blockMetadata.GenesisHash)
return fmt.Errorf("HandleGenesis() genesis hash in metadata does not match expected value: actual %s, expected %s", gh, exp.blockMetadata.GenesisHash)
}
}
return nil
Expand Down
11 changes: 10 additions & 1 deletion exporters/filewriter/file_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,17 @@ func TestExporterInit(t *testing.T) {
err = fileExp.Init(plugins.PluginConfig(config), logger)
assert.NoError(t, err)
fileExp.Close()

// re-initializes empty file
path := "/tmp/blocks/metadata.json"
assert.NoError(t, os.Remove(path))
f, err := os.Create(path)
f.Close()
assert.NoError(t, err)
err = fileExp.Init(plugins.PluginConfig(config), logger)
assert.NoError(t, err)
fileExp.Close()
}

func TestExporterHandleGenesis(t *testing.T) {
fileExp := fileCons.New()
fileExp.Init(plugins.PluginConfig(config), logger)
Expand Down