-
Notifications
You must be signed in to change notification settings - Fork 5
Read concurrency #437
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
base: master
Are you sure you want to change the base?
Read concurrency #437
Changes from 6 commits
ac89ac9
db7a3a3
21a3ef3
d4981a6
f0f5400
6cdb243
ea2e279
98a7da2
bcad357
1661443
94d628e
38d9717
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| default: patch | ||
| --- | ||
|
|
||
| # Change Chain Manager Mutex to RWMutex to enable parallel reads | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,6 +65,14 @@ type DB interface { | |
| CreateBucket(name []byte) (DBBucket, error) | ||
| Flush() error | ||
| Cancel() | ||
|
|
||
| // View calls fn with a read-only DB. Implementations must guarantee | ||
| // that the DB passed to fn observes a consistent snapshot for the | ||
| // duration of fn, and that fn may run concurrently with other View | ||
| // calls. Buckets obtained via the DB inside fn must not be used | ||
| // after fn returns. Writes via the DB inside fn are not durable | ||
| // and may panic. | ||
| View(fn func(DB) error) error | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this makes me sad maybe we could add a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure you'd like that more but if you want to be able to do multiple operations within a single snap := m.store.Snapshot()
defer snap.Close()
for range 10 {
_, _ = snap.Block(...)
}Where
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implemented Chris's suggestion
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do like the idea of making it impossible to call mutating methods, but to make it airtight, we need a read-only
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this in 38d9717 |
||
| } | ||
|
|
||
| // A DBBucket is a set of key-value pairs. | ||
|
|
@@ -105,6 +113,12 @@ func (db *MemDB) Flush() error { | |
| return nil | ||
| } | ||
|
|
||
| // View implements DB. MemDB has no MVCC; callers must ensure that no writes | ||
| // occur concurrently with the View call. | ||
| func (db *MemDB) View(fn func(DB) error) error { | ||
| return fn(db) | ||
| } | ||
|
|
||
| // Cancel implements DB. | ||
| func (db *MemDB) Cancel() { | ||
| for k := range db.puts { | ||
|
|
@@ -336,6 +350,12 @@ func (db *CacheDB) Cancel() { | |
| db.db.Cancel() | ||
| } | ||
|
|
||
| // View implements DB. CacheDB has no MVCC; callers must ensure that no writes | ||
| // occur concurrently with the View call. | ||
| func (db *CacheDB) View(fn func(DB) error) error { | ||
| return fn(db) | ||
| } | ||
|
|
||
| // NewCacheDB returns a new CacheDB that wraps the given DB. | ||
| func NewCacheDB(db DB) DB { | ||
| return &CacheDB{ | ||
|
|
@@ -948,6 +968,16 @@ func (db *DBStore) Flush() error { | |
| return err | ||
| } | ||
|
|
||
| // View implements Store. It calls fn with a read-only Store backed by a | ||
| // consistent snapshot of the underlying DB. The Store passed to fn must not be | ||
| // used after fn returns. Writes performed via the Store will fail. | ||
| func (db *DBStore) View(fn func(Store)) error { | ||
| return db.db.View(func(rdb DB) error { | ||
| fn(&DBStore{db: rdb, n: db.n}) | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| // NewDBStore creates a new DBStore using the provided database. The tip state | ||
| // is also returned. The DB will be automatically migrated if necessary. The | ||
| // provided logger may be nil. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be
minorconsidering we're extending the exportedStoreandDBinterfaces? Anyone implementing those will have to add Snapshot for it to compile.