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

feat: ADR 040: Implement in-memory DB backend #9952

Merged
merged 12 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (errors) [\#8845](https://github.com/cosmos/cosmos-sdk/pull/8845) Add `Error.Wrap` handy method
* [\#8518](https://github.com/cosmos/cosmos-sdk/pull/8518) Help users of multisig wallets debug signature issues.
* [\#9573](https://github.com/cosmos/cosmos-sdk/pull/9573) ADR 040 implementation: New DB interface
* [\#9952](https://github.com/cosmos/cosmos-sdk/pull/9952) ADR 040: Implement in-memory DB backend

roysc marked this conversation as resolved.
Show resolved Hide resolved

### Client Breaking Changes
Expand Down
39 changes: 39 additions & 0 deletions db/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Key-Value Database

Databases supporting mappings of arbitrary byte sequences.
roysc marked this conversation as resolved.
Show resolved Hide resolved

## Interfaces

The database interface types consist of objects to encapsulate the singular connection to the DB, transactions being made to it, historical version state, and iteration.

### `DBConnection`

This interface represents a connection to a versioned key-value database. All versioning operations are performed using methods on this type.
* The `Versions` method returns a `VersionSet` which represents an immutable view of the version history at the current state.
* Version history is modified via the `{Save,Delete}Version` methods.
* Operations on version history do not modify any database contents.

### `DBReader`, `DBWriter`, and `DBReadWriter`

These types represent transactions on the database contents. Their methods provide CRUD operations as well as iteration.
* Writeable transactions call `Commit` flushes operations to the source DB.
* All open transactions must be closed with `Discard` or `Commit` before a new version can be saved on the source DB.
* The maximum number of safely concurrent transactions is dependent on the backend implementation.
* A single transaction object is not safe for concurrent use.
* Write conflicts on concurrent transactions will cause an error at commit time (optimistic concurrency control).

#### `Iterator`

* An iterator is invalidated by any writes within its `Domain` to the source transaction while it is open.
* An iterator must call `Close` before its source transaction is closed.

### `VersionSet`

This represents a self-contained and immutable view of a database's version history state. It is therefore safe to retain and conccurently access any instance of this object.

## Implementations

### In-memory DB

The in-memory DB in the `db/memdb` package cannot be persisted to disk. It is implemented using the Google [btree](https://pkg.go.dev/github.com/google/btree) library.
* This currently does not perform write conflict detection, so it only supports a single open write-transaction at a time. Multiple and concurrent read-transactions are supported.
109 changes: 109 additions & 0 deletions db/dbtest/benchmark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package dbtest

import (
"bytes"
"encoding/binary"
"math/rand"
"testing"

"github.com/stretchr/testify/require"

dbm "github.com/cosmos/cosmos-sdk/db"
)

func Int64ToBytes(i int64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}

func BytesToInt64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf))
}

func BenchmarkRangeScans(b *testing.B, db dbm.DBReadWriter, dbSize int64) {
b.StopTimer()

rangeSize := int64(10000)
if dbSize < rangeSize {
b.Errorf("db size %v cannot be less than range size %v", dbSize, rangeSize)
}

for i := int64(0); i < dbSize; i++ {
bytes := Int64ToBytes(i)
err := db.Set(bytes, bytes)
if err != nil {
// require.NoError() is very expensive (according to profiler), so check manually
b.Fatal(b, err)
}
}
b.StartTimer()

for i := 0; i < b.N; i++ {
start := rand.Int63n(dbSize - rangeSize) // nolint: gosec
end := start + rangeSize
iter, err := db.Iterator(Int64ToBytes(start), Int64ToBytes(end))
require.NoError(b, err)
count := 0
for iter.Next() {
count++
}
iter.Close()
require.EqualValues(b, rangeSize, count)
}
}

func BenchmarkRandomReadsWrites(b *testing.B, db dbm.DBReadWriter) {
b.StopTimer()

// create dummy data
const numItems = int64(1000000)
internal := map[int64]int64{}
for i := 0; i < int(numItems); i++ {
internal[int64(i)] = int64(0)
}

b.StartTimer()

for i := 0; i < b.N; i++ {
{
idx := rand.Int63n(numItems) // nolint: gosec
internal[idx]++
val := internal[idx]
idxBytes := Int64ToBytes(idx)
valBytes := Int64ToBytes(val)
err := db.Set(idxBytes, valBytes)
if err != nil {
// require.NoError() is very expensive (according to profiler), so check manually
b.Fatal(b, err)
}
}

{
idx := rand.Int63n(numItems) // nolint: gosec
valExp := internal[idx]
idxBytes := Int64ToBytes(idx)
valBytes, err := db.Get(idxBytes)
if err != nil {
b.Fatal(b, err)
}
if valExp == 0 {
if !bytes.Equal(valBytes, nil) {
b.Errorf("Expected %v for %v, got %X", nil, idx, valBytes)
break
}
} else {
if len(valBytes) != 8 {
b.Errorf("Expected length 8 for %v, got %X", idx, valBytes)
break
}
valGot := BytesToInt64(valBytes)
if valExp != valGot {
b.Errorf("Expected %v for %v, got %v", valExp, idx, valGot)
break
}
}
}

}
}
Loading