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

Don't delete redundant postdata files #239

Closed
wants to merge 6 commits into from
Closed
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
41 changes: 7 additions & 34 deletions initialization/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,6 @@ func (init *Initializer) Initialize(ctx context.Context) error {
zap.Int("firstFileIndex", layout.FirstFileIdx),
zap.Int("lastFileIndex", layout.LastFileIdx),
)
if err := removeRedundantFiles(init.cfg, init.opts, init.logger); err != nil {
return err
}

numLabels := uint64(init.opts.NumUnits) * init.cfg.LabelsPerUnit
difficulty := init.powDifficultyFunc(numLabels)
Expand Down Expand Up @@ -395,36 +392,6 @@ func (init *Initializer) Initialize(ctx context.Context) error {
return fmt.Errorf("no nonce found")
}

func removeRedundantFiles(cfg config.Config, opts config.InitOpts, logger *zap.Logger) error {
// Go over all postdata_N.bin files in the data directory and remove the ones that are not needed.
// The files with indices from 0 to init.opts.TotalFiles(init.cfg.LabelsPerUnit) - 1 are preserved.
// The rest are redundant and can be removed.
maxFileIndex := opts.TotalFiles(cfg.LabelsPerUnit) - 1
logger.Debug("attempting to remove redundant files above index", zap.Int("maxFileIndex", maxFileIndex))

files, err := os.ReadDir(opts.DataDir)
if err != nil {
return err
}
for _, file := range files {
name := file.Name()
fileIndex, err := shared.ParseFileIndex(name)
if err != nil && name != MetadataFileName {
// TODO(mafa): revert back to warning, see https://github.com/spacemeshos/go-spacemesh/issues/4789
logger.Debug("found unrecognized file", zap.String("fileName", name))
continue
}
if fileIndex > maxFileIndex {
logger.Info("removing redundant file", zap.String("fileName", name))
path := filepath.Join(opts.DataDir, name)
if err := os.Remove(path); err != nil {
return fmt.Errorf("failed to delete file (%v): %w", path, err)
}
}
}
return nil
}

func (init *Initializer) NumLabelsWritten() uint64 {
return init.numLabelsWritten.Load()
}
Expand Down Expand Up @@ -670,12 +637,18 @@ func (init *Initializer) verifyMetadata(m *shared.PostMetadata) error {
if init.opts.NumUnits > m.NumUnits {
return ConfigMismatchError{
Param: "NumUnits",
Expected: fmt.Sprintf(">= %d", init.opts.NumUnits),
Expected: fmt.Sprintf("%d", init.opts.NumUnits),
Found: fmt.Sprintf("%d", m.NumUnits),
DataDir: init.opts.DataDir,
}
}

if init.opts.NumUnits < m.NumUnits {
init.logger.Warn("initialization: found more units than expected, did you forget to delete some files?",
zap.Uint32("expected", init.opts.NumUnits),
zap.Uint32("found", m.NumUnits))
}

return nil
}

Expand Down
48 changes: 17 additions & 31 deletions initialization/initialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,9 @@ func TestInitialize_RedundantFiles(t *testing.T) {
r.NoError(err)
newLayout, err := deriveFilesLayout(cfg, newOpts)
r.NoError(err)
r.Equal(newLayout.NumFiles(), numFiles)

// we allow extra files
r.LessOrEqual(newLayout.NumFiles(), numFiles)
r.Less(newLayout.NumFiles(), layout.NumFiles())

cancel()
Expand Down Expand Up @@ -664,6 +666,7 @@ func TestValidateMetadata(t *testing.T) {
r := require.New(t)

cfg, opts := getTestConfig(t)
opts.NumUnits++

init, err := NewInitializer(
WithNodeId(nodeId),
Expand Down Expand Up @@ -735,6 +738,19 @@ func TestValidateMetadata(t *testing.T) {
)
r.ErrorAs(err, &errConfigMismatch)
r.Equal("NumUnits", errConfigMismatch.Param)

// Attempt to initialize with a lower `opts.NumUnits`.
newOpts = opts
newOpts.NumUnits--
_, err = NewInitializer(
WithNodeId(nodeId),
WithCommitmentAtxId(commitmentAtxId),
WithConfig(cfg),
WithInitOpts(newOpts),
WithLogger(zaptest.NewLogger(t, zaptest.Level(zap.DebugLevel))),
)
// this is allowed
r.NoError(err)
}

func TestStop(t *testing.T) {
Expand Down Expand Up @@ -1094,33 +1110,3 @@ func TestInitializeLastFileIsSmaller(t *testing.T) {
r.NoError(err)
r.Equal(cfg.UnitSize(), uint64(file.Size()))
}

func TestRemoveRedundantFiles(t *testing.T) {
cfg := config.DefaultConfig()

opts := config.DefaultInitOpts()
opts.DataDir = t.TempDir()
opts.NumUnits = 3
opts.MaxFileSize = 2 * cfg.UnitSize()

expectedFilesCount := opts.TotalFiles(cfg.LabelsPerUnit)
// Create 2 redundant files
for i := 0; i < expectedFilesCount+2; i++ {
f, err := os.Create(filepath.Join(opts.DataDir, shared.InitFileName(i)))
require.NoError(t, err)
_, err = f.Write([]byte("test"))
require.NoError(t, err)
require.NoError(t, f.Close())
}

removeRedundantFiles(cfg, opts, zap.NewNop())

files, err := os.ReadDir(opts.DataDir)
require.NoError(t, err)
require.Len(t, files, expectedFilesCount)

for i := 0; i < expectedFilesCount; i++ {
_, err := os.Stat(filepath.Join(opts.DataDir, shared.InitFileName(i)))
require.NoError(t, err)
}
}