Skip to content

Commit

Permalink
Fixed race into E3 salt-blocks.txt (#11622)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 authored Aug 15, 2024
1 parent 8a28557 commit 9889b11
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
42 changes: 29 additions & 13 deletions erigon-lib/downloader/snaptype/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,7 @@ func (f IndexBuilderFunc) Build(ctx context.Context, info FileInfo, salt uint32,
var saltMap = map[string]uint32{}
var saltLock sync.RWMutex

// GetIndicesSalt - try read salt for all indices from DB. Or fall-back to new salt creation.
// if db is Read-Only (for example remote RPCDaemon or utilities) - we will not create new indices -
// and existing indices have salt in metadata.
func GetIndexSalt(baseDir string) (uint32, error) {
saltLock.RLock()
salt, ok := saltMap[baseDir]
saltLock.RUnlock()

if ok {
return salt, nil
}

func ReadAndCreateSaltIfNeeded(baseDir string) (uint32, error) {
fpath := filepath.Join(baseDir, "salt-blocks.txt")
exists, err := dir.FileExist(fpath)
if err != nil {
Expand All @@ -123,10 +112,37 @@ func GetIndexSalt(baseDir string) (uint32, error) {
if err != nil {
return 0, err
}
if len(saltBytes) != 4 {
dir.MustExist(baseDir)

salt = binary.BigEndian.Uint32(saltBytes)
saltBytes := make([]byte, 4)
binary.BigEndian.PutUint32(saltBytes, rand.Uint32())
if err := dir.WriteFileWithFsync(fpath, saltBytes, os.ModePerm); err != nil {
return 0, err
}
}

return binary.BigEndian.Uint32(saltBytes), nil

}

// GetIndicesSalt - try read salt for all indices from DB. Or fall-back to new salt creation.
// if db is Read-Only (for example remote RPCDaemon or utilities) - we will not create new indices -
// and existing indices have salt in metadata.
func GetIndexSalt(baseDir string) (uint32, error) {
saltLock.RLock()
salt, ok := saltMap[baseDir]
saltLock.RUnlock()
if ok {
return salt, nil
}

saltLock.Lock()
salt, err := ReadAndCreateSaltIfNeeded(baseDir)
if err != nil {
return 0, err
}

saltMap[baseDir] = salt
saltLock.Unlock()

Expand Down
4 changes: 4 additions & 0 deletions turbo/snapshotsync/freezeblocks/block_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,10 @@ func (s *RoSnapshots) buildMissedIndices(logPrefix string, ctx context.Context,
return nil
}

if _, err := snaptype.ReadAndCreateSaltIfNeeded(dirs.Snap); err != nil {
return err
}

dir, tmpDir := dirs.Snap, dirs.Tmp
//log.Log(lvl, "[snapshots] Build indices", "from", min)

Expand Down

0 comments on commit 9889b11

Please sign in to comment.