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

[db] KvWithVersion to handle both versioned and non-versioned namespace #4518

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion chainservice/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ func (builder *Builder) createFactory(forTest bool) (factory.Factory, error) {
factory.RegistryStateDBOption(builder.cs.registry),
factory.DefaultPatchOption(),
}
if builder.cfg.Chain.EnableStateDBCaching {
if builder.cfg.Chain.EnableArchiveMode {
dao, err = db.CreateKVStoreVersioned(factoryDBCfg, builder.cfg.Chain.TrieDBPath, factory.VersionedNamespaces)
} else if builder.cfg.Chain.EnableStateDBCaching {
dao, err = db.CreateKVStoreWithCache(factoryDBCfg, builder.cfg.Chain.TrieDBPath, builder.cfg.Chain.StateDBCacheSize)
} else {
dao, err = db.CreateKVStore(factoryDBCfg, builder.cfg.Chain.TrieDBPath)
Expand Down
10 changes: 0 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ var (
// Validates is the collection config validation functions
Validates = []Validate{
ValidateRollDPoS,
ValidateArchiveMode,
ValidateDispatcher,
ValidateAPI,
ValidateActPool,
Expand Down Expand Up @@ -254,15 +253,6 @@ func ValidateRollDPoS(cfg Config) error {
return nil
}

// ValidateArchiveMode validates the state factory setting
func ValidateArchiveMode(cfg Config) error {
if !cfg.Chain.EnableArchiveMode || !cfg.Chain.EnableTrielessStateDB {
return nil
}

return errors.Wrap(ErrInvalidCfg, "Archive mode is incompatible with trieless state DB")
}

// ValidateAPI validates the api configs
func ValidateAPI(cfg Config) error {
if cfg.API.TpsWindow <= 0 {
Expand Down
17 changes: 0 additions & 17 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,23 +253,6 @@ func TestValidateRollDPoS(t *testing.T) {
)
}

func TestValidateArchiveMode(t *testing.T) {
cfg := Default
cfg.Chain.EnableArchiveMode = true
cfg.Chain.EnableTrielessStateDB = true
require.Error(t, ErrInvalidCfg, errors.Cause(ValidateArchiveMode(cfg)))
require.EqualError(t, ValidateArchiveMode(cfg), "Archive mode is incompatible with trieless state DB: invalid config value")
cfg.Chain.EnableArchiveMode = false
cfg.Chain.EnableTrielessStateDB = true
require.NoError(t, errors.Cause(ValidateArchiveMode(cfg)))
cfg.Chain.EnableArchiveMode = true
cfg.Chain.EnableTrielessStateDB = false
require.NoError(t, errors.Cause(ValidateArchiveMode(cfg)))
cfg.Chain.EnableArchiveMode = false
cfg.Chain.EnableTrielessStateDB = false
require.NoError(t, errors.Cause(ValidateArchiveMode(cfg)))
}

func TestValidateActPool(t *testing.T) {
cfg := Default
cfg.ActPool.MaxNumActsPerAcct = 0
Expand Down
19 changes: 19 additions & 0 deletions db/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
var (
// ErrEmptyDBPath is the error when db path is empty
ErrEmptyDBPath = errors.New("empty db path")
// ErrEmptyVersionedNamespace is the error of empty versioned namespace
ErrEmptyVersionedNamespace = errors.New("cannot create versioned KVStore with empty versioned namespace")
)

// CreateKVStore creates db from config and db path
Expand Down Expand Up @@ -32,3 +34,20 @@

return NewKvStoreWithCache(dao, cacheSize), nil
}

// CreateKVStoreVersioned creates versioned db from config and db path
func CreateKVStoreVersioned(cfg Config, dbPath string, vns []string) (KVStore, error) {
if len(dbPath) == 0 {
return nil, ErrEmptyDBPath
}
if len(vns) == 0 {
return nil, ErrEmptyVersionedNamespace
}
for i := range vns {
if len(vns[i]) == 0 {
return nil, ErrEmptyVersionedNamespace
}
}
cfg.DbPath = dbPath
return NewKVStoreWithVersion(cfg, VersionedNamespaceOption(vns...)), nil

Check failure on line 52 in db/builder.go

View workflow job for this annotation

GitHub Actions / ci flow

cannot use vns (variable of type []string) as []Namespace value in argument to VersionedNamespaceOption
}
3 changes: 3 additions & 0 deletions db/db_versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type (
// Delete deletes a record by (namespace, key)
Delete(uint64, string, []byte) error

// CommitBatch writes a batch to the underlying DB
CommitBatch(uint64, batch.KVStoreBatch) error

// Filter returns <k, v> pair in a bucket that meet the condition
Filter(uint64, string, Condition, []byte, []byte) ([][]byte, [][]byte, error)

Expand Down
85 changes: 85 additions & 0 deletions db/kvstore_versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package db

import (
"context"

"github.com/iotexproject/iotex-core/v2/db/batch"
"github.com/iotexproject/iotex-core/v2/pkg/lifecycle"
)

Expand Down Expand Up @@ -47,4 +50,86 @@ type (
// SetVersion sets the version, and returns a KVStore to call Put()/Get()
SetVersion(uint64) KVStore
}

// KvWithVersion wraps the versioned DB implementation with a certain version
KvWithVersion struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the value of this struct

db VersionedDB
version uint64 // the current version
vns []Namespace // versioned namespace
}
)

// Option sets an option
type Option func(*KvWithVersion)

// VersionedNamespaceOption pass in versioned namespaces
func VersionedNamespaceOption(ns ...Namespace) Option {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

return func(k *KvWithVersion) {
k.vns = ns
}
}

// NewKVStoreWithVersion implements a KVStore that can handle both versioned
// and non-versioned namespace
func NewKVStoreWithVersion(cfg Config, opts ...Option) *KvWithVersion {
kv := KvWithVersion{}
for _, opt := range opts {
opt(&kv)
}
var dbOpts []BoltDBVersionedOption
if len(kv.vns) > 0 {
dbOpts = append(dbOpts, VnsOption(kv.vns...))
}
kv.db = NewBoltDBVersioned(cfg, dbOpts...)
return &kv
}

// Start starts the DB
func (b *KvWithVersion) Start(ctx context.Context) error {
return b.db.Start(ctx)
}

// Stop stops the DB
func (b *KvWithVersion) Stop(ctx context.Context) error {
return b.db.Stop(ctx)
}

// Put writes a <key, value> record
func (b *KvWithVersion) Put(ns string, key, value []byte) error {
return b.db.Put(b.version, ns, key, value)
}

// Get retrieves a key's value
func (b *KvWithVersion) Get(ns string, key []byte) ([]byte, error) {
return b.db.Get(b.version, ns, key)
}

// Delete deletes a key
func (b *KvWithVersion) Delete(ns string, key []byte) error {
return b.db.Delete(b.version, ns, key)
}

// Filter returns <k, v> pair in a bucket that meet the condition
func (b *KvWithVersion) Filter(ns string, cond Condition, minKey, maxKey []byte) ([][]byte, [][]byte, error) {
return b.db.Filter(b.version, ns, cond, minKey, maxKey)
}

// WriteBatch commits a batch
func (b *KvWithVersion) WriteBatch(kvsb batch.KVStoreBatch) error {
return b.db.CommitBatch(b.version, kvsb)
}

// Version returns the key's most recent version
func (b *KvWithVersion) Version(ns string, key []byte) (uint64, error) {
return b.db.Version(ns, key)
}

// SetVersion sets the version, and returns a KVStore to call Put()/Get()
func (b *KvWithVersion) SetVersion(v uint64) KVStore {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KvVersioned is an extension of KVStore and SetVersion(v uint64) KvVersioned

kv := KvWithVersion{
db: b.db,
version: v,
vns: b.vns,
}
return &kv
}
Loading
Loading