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

mdbx: pre-open read pagesize from db #10113

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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
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
Loading