Skip to content

Commit

Permalink
mdbx: pre-open read pagesize from db (#10113)
Browse files Browse the repository at this point in the history
Problem: if --pageSize parameter not set - we using `default pagesize`
instead of `real pagesize of db`. And it causing different `dirtySpace`
size (because it's accounted in "pages")
  • Loading branch information
AskAlexSharov authored Apr 29, 2024
1 parent 3b72b5a commit a12a99c
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions erigon-lib/kv/mdbx/kv_mdbx.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ func (opts MdbxOpts) Open(ctx context.Context) (kv.RwDB, error) {
// before env.Open() we don't know real pageSize. but will be implemented soon: https://gitflic.ru/project/erthink/libmdbx/issue/15
// but we want call all `SetOption` before env.Open(), because:
// - after they will require rwtx-lock, which is not acceptable in ACCEDEE mode.
pageSize := opts.pageSize
if pageSize == 0 {
pageSize = kv.DefaultPageSize()
opts.pageSize, err = preOpenPageSize(opts)
if err != nil {
return nil, err
}

var dirtySpace uint64
Expand All @@ -341,7 +341,7 @@ func (opts MdbxOpts) Open(ctx context.Context) (kv.RwDB, error) {
}
}
//can't use real pagesize here - it will be known only after env.Open()
if err = env.SetOption(mdbx.OptTxnDpLimit, dirtySpace/pageSize); err != nil {
if err = env.SetOption(mdbx.OptTxnDpLimit, dirtySpace/opts.pageSize); err != nil {
return nil, err
}

Expand Down Expand Up @@ -459,6 +459,33 @@ func (opts MdbxOpts) Open(ctx context.Context) (kv.RwDB, error) {
return db, nil
}

func preOpenPageSize(opts MdbxOpts) (uint64, error) {
// before env.Open() we don't know real pageSize. but will be implemented soon: https://gitflic.ru/project/erthink/libmdbx/issue/15
// but we want call all `SetOption` before env.Open(), because:
// - after they will require rwtx-lock, which is not acceptable in ACCEDEE mode.
if !dir.FileExist(filepath.Join(opts.path, "mdbx.dat")) {
pageSize := opts.pageSize
if pageSize == 0 {
pageSize = kv.DefaultPageSize()
}
return pageSize, nil
}

env, err := mdbx.NewEnv()
if err != nil {
return 0, err
}
if err = env.Open(opts.path, mdbx.Accede|mdbx.Readonly, 0644); err != nil {
return 0, err
}
defer env.Close()
in, err := env.Info(nil)
if err != nil {
return 0, fmt.Errorf("%w, label: %s, trace: %s", err, opts.label.String(), stack2.Trace().String())
}
return uint64(in.PageSize), nil
}

func (opts MdbxOpts) MustOpen() kv.RwDB {
db, err := opts.Open(context.Background())
if err != nil {
Expand Down

0 comments on commit a12a99c

Please sign in to comment.