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: attempt to create table if need - in Accedee mode #12988

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
55 changes: 40 additions & 15 deletions erigon-lib/kv/mdbx/kv_mdbx.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (opts MdbxOpts) Open(ctx context.Context) (kv.RwDB, error) {
}

buckets := bucketSlice(db.buckets)
if err := db.openDBIs(buckets); err != nil {
if err := db.openDBIs(ctx, buckets); err != nil {
return nil, err
}

Expand Down Expand Up @@ -461,26 +461,50 @@ func (db *MdbxKV) CHandle() unsafe.Pointer {
// openDBIs - first trying to open existing DBI's in RO transaction
// otherwise re-try by RW transaction
// it allow open DB from another process - even if main process holding long RW transaction
func (db *MdbxKV) openDBIs(buckets []string) error {
if db.ReadOnly() || db.Accede() {
return db.View(context.Background(), func(tx kv.Tx) error {
for _, name := range buckets {
if db.buckets[name].IsDeprecated {
continue
}
func (db *MdbxKV) openDBIs(ctx context.Context, buckets []string) error {
nonDeprecatedBuckets := make([]string, 0, len(buckets))
for _, name := range buckets {
if db.buckets[name].IsDeprecated {
continue
}
nonDeprecatedBuckets = append(nonDeprecatedBuckets, name)
}

if db.ReadOnly() { // open or fail
return db.View(ctx, func(tx kv.Tx) error {
for _, name := range nonDeprecatedBuckets {
if err := tx.(kv.BucketMigrator).CreateBucket(name); err != nil {
return err
}
}
return tx.(*MdbxTx).Commit() // when open db as read-only, commit of this RO transaction is required
// when open db as read-only, commit of this RO transaction is required.
// it's weird - opening DBI on RO-db is "write/mutation operation" - which will be rolled-back if not committed.
return tx.(*MdbxTx).Commit()
})
}

return db.Update(context.Background(), func(tx kv.RwTx) error {
for _, name := range buckets {
if db.buckets[name].IsDeprecated {
continue
if db.Accede() { // open or create
err := db.View(ctx, func(tx kv.Tx) error {
for _, name := range nonDeprecatedBuckets {
if err := tx.(kv.BucketMigrator).CreateBucket(name); err != nil {
return err
}
}
// when open db as read-only, commit of this RO transaction is required.
// it's weird - opening DBI on RO-db is "write/mutation operation" - which will be rolled-back if not committed.
return tx.(*MdbxTx).Commit()
})
if err == nil { // success
return nil
}
recoverable := !errors.Is(err, ErrTableDoesntExists)
if !recoverable {
return err
}
}

return db.Update(ctx, func(tx kv.RwTx) error {
for _, name := range nonDeprecatedBuckets {
if err := tx.(kv.BucketMigrator).CreateBucket(name); err != nil {
return err
}
Expand Down Expand Up @@ -803,16 +827,17 @@ func (tx *MdbxTx) CreateBucket(name string) error {
}

dbi, err = tx.tx.OpenDBI(name, nativeFlags, nil, nil)

if err != nil {
return fmt.Errorf("db-talbe doesn't exists: %s, lable: %s, %w. Tip: try run `integration run_migrations` to create non-existing tables", name, tx.db.opts.label, err)
return fmt.Errorf("%w: %s, lable: %s, %w. Tip: try run `integration run_migrations`", ErrTableDoesntExists, name, tx.db.opts.label, err)
}
cnfCopy.DBI = kv.DBI(dbi)

tx.db.buckets[name] = cnfCopy
return nil
}

var ErrTableDoesntExists = errors.New("table does not exist")

func (tx *MdbxTx) dropEvenIfBucketIsNotDeprecated(name string) error {
dbi := tx.db.buckets[name].DBI
// if bucket was not open on db start, then it's may be deprecated
Expand Down
Loading