2019-10-16 13:41:50 +00:00
|
|
|
package storage
|
|
|
|
|
2021-09-22 15:58:48 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-06 12:54:44 +00:00
|
|
|
"context"
|
2021-09-22 15:58:48 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
|
|
|
)
|
storage: allow accessing MemCachedStore during Persist
Persist by its definition doesn't change MemCachedStore visible state, all KV
pairs that were acessible via it before Persist remain accessible after
Persist. The only thing it does is flushing of the current set of KV pairs
from memory to peristent store. To do that it needs read-only access to the
current KV pair set, but technically it then replaces maps, so we have to use
full write lock which makes MemCachedStore inaccessible for the duration of
Persist. And Persist can take a lot of time, it's about disk access for
regular DBs.
What we do here is we create new in-memory maps for MemCachedStore before
flushing old ones to the persistent store. Then a fake persistent store is
created which actually is a MemCachedStore with old maps, so it has exactly
the same visible state. This Store is never accessed for writes, so we can
read it without taking any internal locks and at the same time we no longer
need write locks for original MemCachedStore, we're not using it. All of this
makes it possible to use MemCachedStore as normally reads are handled going
down to whatever level is needed and writes are handled by new maps. So while
Persist for (*Blockchain).dao does its most time-consuming work we can process
other blocks (reading data for transactions and persisting storeBlock caches
to (*Blockchain).dao).
The change was tested for performance with neo-bench (single node, 10 workers,
LevelDB) on two machines and block dump processing (RC4 testnet up to 62800
with VerifyBlocks set to false) on i7-8565U.
Reference results (bbe4e9cd7bb33428633586f080f64494cd6ac9cf):
Ryzen 9 5950X:
RPS 23616.969 22817.086 23222.378 ≈ 23218 ± 1.72%
TPS 23047.316 22608.578 22735.540 ≈ 22797 ± 0.99%
CPU % 23.434 25.553 23.848 ≈ 24.3 ± 4.63%
Mem MB 600.636 503.060 582.043 ≈ 562 ± 9.22%
Core i7-8565U:
RPS 6594.007 6499.501 6572.902 ≈ 6555 ± 0.76%
TPS 6561.680 6444.545 6510.120 ≈ 6505 ± 0.90%
CPU % 58.452 60.568 62.474 ≈ 60.5 ± 3.33%
Mem MB 234.893 285.067 269.081 ≈ 263 ± 9.75%
DB restore:
real 0m22.237s 0m23.471s 0m23.409s ≈ 23.04 ± 3.02%
user 0m35.435s 0m38.943s 0m39.247s ≈ 37.88 ± 5.59%
sys 0m3.085s 0m3.360s 0m3.144s ≈ 3.20 ± 4.53%
After the change:
Ryzen 9 5950X:
RPS 27747.349 27407.726 27520.210 ≈ 27558 ± 0.63% ↑ 18.69%
TPS 26992.010 26993.468 27010.966 ≈ 26999 ± 0.04% ↑ 18.43%
CPU % 28.928 28.096 29.105 ≈ 28.7 ± 1.88% ↑ 18.1%
Mem MB 760.385 726.320 756.118 ≈ 748 ± 2.48% ↑ 33.10%
Core i7-8565U:
RPS 7783.229 7628.409 7542.340 ≈ 7651 ± 1.60% ↑ 16.72%
TPS 7708.436 7607.397 7489.459 ≈ 7602 ± 1.44% ↑ 16.85%
CPU % 74.899 71.020 72.697 ≈ 72.9 ± 2.67% ↑ 20.50%
Mem MB 438.047 436.967 416.350 ≈ 430 ± 2.84% ↑ 63.50%
DB restore:
real 0m20.838s 0m21.895s 0m21.794s ≈ 21.51 ± 2.71% ↓ 6.64%
user 0m39.091s 0m40.565s 0m41.493s ≈ 40.38 ± 3.00% ↑ 6.60%
sys 0m3.184s 0m2.923s 0m3.062s ≈ 3.06 ± 4.27% ↓ 4.38%
It obviously uses more memory now and utilizes CPU more aggressively, but at
the same time it allows to improve all relevant metrics and finally reach a
situation where we process 50K transactions in less than second on Ryzen 9
5950X (going higher than 25K TPS). The other observation is much more stable
block time, on Ryzen 9 it's as close to 1 second as it could be.
2021-07-30 20:35:03 +00:00
|
|
|
|
2019-10-16 13:41:50 +00:00
|
|
|
// MemCachedStore is a wrapper around persistent store that caches all changes
|
|
|
|
// being made for them to be later flushed in one batch.
|
|
|
|
type MemCachedStore struct {
|
|
|
|
MemoryStore
|
|
|
|
|
storage: allow accessing MemCachedStore during Persist
Persist by its definition doesn't change MemCachedStore visible state, all KV
pairs that were acessible via it before Persist remain accessible after
Persist. The only thing it does is flushing of the current set of KV pairs
from memory to peristent store. To do that it needs read-only access to the
current KV pair set, but technically it then replaces maps, so we have to use
full write lock which makes MemCachedStore inaccessible for the duration of
Persist. And Persist can take a lot of time, it's about disk access for
regular DBs.
What we do here is we create new in-memory maps for MemCachedStore before
flushing old ones to the persistent store. Then a fake persistent store is
created which actually is a MemCachedStore with old maps, so it has exactly
the same visible state. This Store is never accessed for writes, so we can
read it without taking any internal locks and at the same time we no longer
need write locks for original MemCachedStore, we're not using it. All of this
makes it possible to use MemCachedStore as normally reads are handled going
down to whatever level is needed and writes are handled by new maps. So while
Persist for (*Blockchain).dao does its most time-consuming work we can process
other blocks (reading data for transactions and persisting storeBlock caches
to (*Blockchain).dao).
The change was tested for performance with neo-bench (single node, 10 workers,
LevelDB) on two machines and block dump processing (RC4 testnet up to 62800
with VerifyBlocks set to false) on i7-8565U.
Reference results (bbe4e9cd7bb33428633586f080f64494cd6ac9cf):
Ryzen 9 5950X:
RPS 23616.969 22817.086 23222.378 ≈ 23218 ± 1.72%
TPS 23047.316 22608.578 22735.540 ≈ 22797 ± 0.99%
CPU % 23.434 25.553 23.848 ≈ 24.3 ± 4.63%
Mem MB 600.636 503.060 582.043 ≈ 562 ± 9.22%
Core i7-8565U:
RPS 6594.007 6499.501 6572.902 ≈ 6555 ± 0.76%
TPS 6561.680 6444.545 6510.120 ≈ 6505 ± 0.90%
CPU % 58.452 60.568 62.474 ≈ 60.5 ± 3.33%
Mem MB 234.893 285.067 269.081 ≈ 263 ± 9.75%
DB restore:
real 0m22.237s 0m23.471s 0m23.409s ≈ 23.04 ± 3.02%
user 0m35.435s 0m38.943s 0m39.247s ≈ 37.88 ± 5.59%
sys 0m3.085s 0m3.360s 0m3.144s ≈ 3.20 ± 4.53%
After the change:
Ryzen 9 5950X:
RPS 27747.349 27407.726 27520.210 ≈ 27558 ± 0.63% ↑ 18.69%
TPS 26992.010 26993.468 27010.966 ≈ 26999 ± 0.04% ↑ 18.43%
CPU % 28.928 28.096 29.105 ≈ 28.7 ± 1.88% ↑ 18.1%
Mem MB 760.385 726.320 756.118 ≈ 748 ± 2.48% ↑ 33.10%
Core i7-8565U:
RPS 7783.229 7628.409 7542.340 ≈ 7651 ± 1.60% ↑ 16.72%
TPS 7708.436 7607.397 7489.459 ≈ 7602 ± 1.44% ↑ 16.85%
CPU % 74.899 71.020 72.697 ≈ 72.9 ± 2.67% ↑ 20.50%
Mem MB 438.047 436.967 416.350 ≈ 430 ± 2.84% ↑ 63.50%
DB restore:
real 0m20.838s 0m21.895s 0m21.794s ≈ 21.51 ± 2.71% ↓ 6.64%
user 0m39.091s 0m40.565s 0m41.493s ≈ 40.38 ± 3.00% ↑ 6.60%
sys 0m3.184s 0m2.923s 0m3.062s ≈ 3.06 ± 4.27% ↓ 4.38%
It obviously uses more memory now and utilizes CPU more aggressively, but at
the same time it allows to improve all relevant metrics and finally reach a
situation where we process 50K transactions in less than second on Ryzen 9
5950X (going higher than 25K TPS). The other observation is much more stable
block time, on Ryzen 9 it's as close to 1 second as it could be.
2021-07-30 20:35:03 +00:00
|
|
|
// plock protects Persist from double entrance.
|
|
|
|
plock sync.Mutex
|
2019-10-16 13:41:50 +00:00
|
|
|
// Persistent Store.
|
|
|
|
ps Store
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:11:32 +00:00
|
|
|
type (
|
|
|
|
// KeyValue represents key-value pair.
|
|
|
|
KeyValue struct {
|
|
|
|
Key []byte
|
|
|
|
Value []byte
|
2021-10-05 06:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// KeyValueExists represents key-value pair with indicator whether the item
|
|
|
|
// exists in the persistent storage.
|
|
|
|
KeyValueExists struct {
|
|
|
|
KeyValue
|
2020-02-07 12:08:25 +00:00
|
|
|
|
|
|
|
Exists bool
|
2020-02-06 13:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MemBatch represents a changeset to be persisted.
|
|
|
|
MemBatch struct {
|
2021-10-05 06:54:03 +00:00
|
|
|
Put []KeyValueExists
|
|
|
|
Deleted []KeyValueExists
|
2020-02-06 13:11:32 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-10-16 13:41:50 +00:00
|
|
|
// NewMemCachedStore creates a new MemCachedStore object.
|
|
|
|
func NewMemCachedStore(lower Store) *MemCachedStore {
|
|
|
|
return &MemCachedStore{
|
|
|
|
MemoryStore: *NewMemoryStore(),
|
|
|
|
ps: lower,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get implements the Store interface.
|
|
|
|
func (s *MemCachedStore) Get(key []byte) ([]byte, error) {
|
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
k := string(key)
|
|
|
|
if val, ok := s.mem[k]; ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
if _, ok := s.del[k]; ok {
|
|
|
|
return nil, ErrKeyNotFound
|
|
|
|
}
|
|
|
|
return s.ps.Get(key)
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:11:32 +00:00
|
|
|
// GetBatch returns currently accumulated changeset.
|
|
|
|
func (s *MemCachedStore) GetBatch() *MemBatch {
|
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
|
|
|
|
|
|
|
var b MemBatch
|
|
|
|
|
2021-10-05 06:54:03 +00:00
|
|
|
b.Put = make([]KeyValueExists, 0, len(s.mem))
|
2020-02-06 13:11:32 +00:00
|
|
|
for k, v := range s.mem {
|
2020-02-07 12:08:25 +00:00
|
|
|
key := []byte(k)
|
|
|
|
_, err := s.ps.Get(key)
|
2021-10-05 06:54:03 +00:00
|
|
|
b.Put = append(b.Put, KeyValueExists{KeyValue: KeyValue{Key: key, Value: v}, Exists: err == nil})
|
2020-02-06 13:11:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 06:54:03 +00:00
|
|
|
b.Deleted = make([]KeyValueExists, 0, len(s.del))
|
2020-02-06 13:11:32 +00:00
|
|
|
for k := range s.del {
|
2020-02-07 12:08:25 +00:00
|
|
|
key := []byte(k)
|
|
|
|
_, err := s.ps.Get(key)
|
2021-10-05 06:54:03 +00:00
|
|
|
b.Deleted = append(b.Deleted, KeyValueExists{KeyValue: KeyValue{Key: key}, Exists: err == nil})
|
2020-02-06 13:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:41:50 +00:00
|
|
|
// Seek implements the Store interface.
|
|
|
|
func (s *MemCachedStore) Seek(key []byte, f func(k, v []byte)) {
|
2021-10-19 15:03:47 +00:00
|
|
|
s.seek(context.Background(), key, false, f)
|
2021-10-04 14:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SeekAsync returns non-buffered channel with matching KeyValue pairs. Key and
|
|
|
|
// value slices may not be copied and may be modified. SeekAsync can guarantee
|
|
|
|
// that key-value items are sorted by key in ascending way.
|
2021-10-06 12:54:44 +00:00
|
|
|
func (s *MemCachedStore) SeekAsync(ctx context.Context, key []byte, cutPrefix bool) chan KeyValue {
|
2021-10-19 15:03:47 +00:00
|
|
|
res := make(chan KeyValue)
|
|
|
|
go func() {
|
|
|
|
s.seek(ctx, key, cutPrefix, func(k, v []byte) {
|
|
|
|
res <- KeyValue{
|
|
|
|
Key: k,
|
|
|
|
Value: v,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
close(res)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemCachedStore) seek(ctx context.Context, key []byte, cutPrefix bool, f func(k, v []byte)) {
|
2021-09-22 15:58:48 +00:00
|
|
|
// Create memory store `mem` and `del` snapshot not to hold the lock.
|
2021-10-19 10:29:43 +00:00
|
|
|
var memRes []KeyValueExists
|
2021-09-22 15:58:48 +00:00
|
|
|
sk := string(key)
|
2019-10-16 13:41:50 +00:00
|
|
|
s.mut.RLock()
|
2021-09-22 15:58:48 +00:00
|
|
|
for k, v := range s.MemoryStore.mem {
|
|
|
|
if strings.HasPrefix(k, sk) {
|
|
|
|
memRes = append(memRes, KeyValueExists{
|
|
|
|
KeyValue: KeyValue{
|
|
|
|
Key: []byte(k),
|
2021-10-19 10:29:43 +00:00
|
|
|
Value: v,
|
2021-09-22 15:58:48 +00:00
|
|
|
},
|
|
|
|
Exists: true,
|
|
|
|
})
|
2019-10-16 13:41:50 +00:00
|
|
|
}
|
2021-09-22 15:58:48 +00:00
|
|
|
}
|
|
|
|
for k := range s.MemoryStore.del {
|
|
|
|
if strings.HasPrefix(k, sk) {
|
|
|
|
memRes = append(memRes, KeyValueExists{
|
|
|
|
KeyValue: KeyValue{
|
|
|
|
Key: []byte(k),
|
|
|
|
},
|
|
|
|
})
|
2019-10-16 13:41:50 +00:00
|
|
|
}
|
2021-09-22 15:58:48 +00:00
|
|
|
}
|
2021-10-18 16:13:56 +00:00
|
|
|
ps := s.ps
|
2021-09-22 15:58:48 +00:00
|
|
|
s.mut.RUnlock()
|
|
|
|
// Sort memRes items for further comparison with ps items.
|
|
|
|
sort.Slice(memRes, func(i, j int) bool {
|
|
|
|
return bytes.Compare(memRes[i].Key, memRes[j].Key) < 0
|
2019-10-16 13:41:50 +00:00
|
|
|
})
|
2021-09-22 15:58:48 +00:00
|
|
|
|
2021-10-19 15:03:47 +00:00
|
|
|
var (
|
|
|
|
done bool
|
|
|
|
iMem int
|
|
|
|
kvMem KeyValueExists
|
|
|
|
haveMem bool
|
|
|
|
)
|
|
|
|
if iMem < len(memRes) {
|
|
|
|
kvMem = memRes[iMem]
|
|
|
|
haveMem = true
|
|
|
|
iMem++
|
|
|
|
}
|
|
|
|
// Merge results of seek operations in ascending order.
|
|
|
|
ps.Seek(key, func(k, v []byte) {
|
|
|
|
if done {
|
|
|
|
return
|
2021-10-19 10:29:43 +00:00
|
|
|
}
|
2021-10-19 15:03:47 +00:00
|
|
|
kvPs := KeyValue{
|
|
|
|
Key: slice.Copy(k),
|
|
|
|
Value: slice.Copy(v),
|
|
|
|
}
|
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
done = true
|
|
|
|
break loop
|
|
|
|
default:
|
|
|
|
var isMem = haveMem && (bytes.Compare(kvMem.Key, kvPs.Key) < 0)
|
|
|
|
if isMem {
|
|
|
|
if kvMem.Exists {
|
|
|
|
if cutPrefix {
|
|
|
|
kvMem.Key = kvMem.Key[len(key):]
|
2021-10-19 12:10:12 +00:00
|
|
|
}
|
2021-10-19 15:03:47 +00:00
|
|
|
f(kvMem.Key, kvMem.Value)
|
|
|
|
}
|
|
|
|
if iMem < len(memRes) {
|
|
|
|
kvMem = memRes[iMem]
|
|
|
|
haveMem = true
|
|
|
|
iMem++
|
2021-10-19 12:10:12 +00:00
|
|
|
} else {
|
2021-10-19 15:03:47 +00:00
|
|
|
haveMem = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !bytes.Equal(kvMem.Key, kvPs.Key) {
|
|
|
|
if cutPrefix {
|
|
|
|
kvPs.Key = kvPs.Key[len(key):]
|
2021-10-19 12:10:12 +00:00
|
|
|
}
|
2021-10-19 15:03:47 +00:00
|
|
|
f(kvPs.Key, kvPs.Value)
|
2021-09-22 15:58:48 +00:00
|
|
|
}
|
2021-10-19 15:03:47 +00:00
|
|
|
break loop
|
2021-09-22 15:58:48 +00:00
|
|
|
}
|
2021-10-19 12:10:12 +00:00
|
|
|
}
|
2021-10-19 15:03:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if !done && haveMem {
|
|
|
|
loop:
|
|
|
|
for i := iMem - 1; i < len(memRes); i++ {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
break loop
|
|
|
|
default:
|
|
|
|
kvMem = memRes[i]
|
|
|
|
if kvMem.Exists {
|
|
|
|
if cutPrefix {
|
|
|
|
kvMem.Key = kvMem.Key[len(key):]
|
2021-10-04 14:01:42 +00:00
|
|
|
}
|
2021-10-19 15:03:47 +00:00
|
|
|
f(kvMem.Key, kvMem.Value)
|
2021-09-22 15:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-19 15:03:47 +00:00
|
|
|
}
|
2019-10-16 13:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Persist flushes all the MemoryStore contents into the (supposedly) persistent
|
2021-11-22 07:41:40 +00:00
|
|
|
// store ps. MemCachedStore remains accessible for the most part of this action
|
|
|
|
// (any new changes will be cached in memory).
|
2019-10-16 13:41:50 +00:00
|
|
|
func (s *MemCachedStore) Persist() (int, error) {
|
2021-11-22 07:41:40 +00:00
|
|
|
return s.persist(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PersistSync flushes all the MemoryStore contents into the (supposedly) persistent
|
|
|
|
// store ps. It's different from Persist in that it blocks MemCachedStore completely
|
|
|
|
// while flushing things from memory to persistent store.
|
|
|
|
func (s *MemCachedStore) PersistSync() (int, error) {
|
|
|
|
return s.persist(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemCachedStore) persist(isSync bool) (int, error) {
|
2020-03-27 12:40:23 +00:00
|
|
|
var err error
|
|
|
|
var keys, dkeys int
|
|
|
|
|
storage: allow accessing MemCachedStore during Persist
Persist by its definition doesn't change MemCachedStore visible state, all KV
pairs that were acessible via it before Persist remain accessible after
Persist. The only thing it does is flushing of the current set of KV pairs
from memory to peristent store. To do that it needs read-only access to the
current KV pair set, but technically it then replaces maps, so we have to use
full write lock which makes MemCachedStore inaccessible for the duration of
Persist. And Persist can take a lot of time, it's about disk access for
regular DBs.
What we do here is we create new in-memory maps for MemCachedStore before
flushing old ones to the persistent store. Then a fake persistent store is
created which actually is a MemCachedStore with old maps, so it has exactly
the same visible state. This Store is never accessed for writes, so we can
read it without taking any internal locks and at the same time we no longer
need write locks for original MemCachedStore, we're not using it. All of this
makes it possible to use MemCachedStore as normally reads are handled going
down to whatever level is needed and writes are handled by new maps. So while
Persist for (*Blockchain).dao does its most time-consuming work we can process
other blocks (reading data for transactions and persisting storeBlock caches
to (*Blockchain).dao).
The change was tested for performance with neo-bench (single node, 10 workers,
LevelDB) on two machines and block dump processing (RC4 testnet up to 62800
with VerifyBlocks set to false) on i7-8565U.
Reference results (bbe4e9cd7bb33428633586f080f64494cd6ac9cf):
Ryzen 9 5950X:
RPS 23616.969 22817.086 23222.378 ≈ 23218 ± 1.72%
TPS 23047.316 22608.578 22735.540 ≈ 22797 ± 0.99%
CPU % 23.434 25.553 23.848 ≈ 24.3 ± 4.63%
Mem MB 600.636 503.060 582.043 ≈ 562 ± 9.22%
Core i7-8565U:
RPS 6594.007 6499.501 6572.902 ≈ 6555 ± 0.76%
TPS 6561.680 6444.545 6510.120 ≈ 6505 ± 0.90%
CPU % 58.452 60.568 62.474 ≈ 60.5 ± 3.33%
Mem MB 234.893 285.067 269.081 ≈ 263 ± 9.75%
DB restore:
real 0m22.237s 0m23.471s 0m23.409s ≈ 23.04 ± 3.02%
user 0m35.435s 0m38.943s 0m39.247s ≈ 37.88 ± 5.59%
sys 0m3.085s 0m3.360s 0m3.144s ≈ 3.20 ± 4.53%
After the change:
Ryzen 9 5950X:
RPS 27747.349 27407.726 27520.210 ≈ 27558 ± 0.63% ↑ 18.69%
TPS 26992.010 26993.468 27010.966 ≈ 26999 ± 0.04% ↑ 18.43%
CPU % 28.928 28.096 29.105 ≈ 28.7 ± 1.88% ↑ 18.1%
Mem MB 760.385 726.320 756.118 ≈ 748 ± 2.48% ↑ 33.10%
Core i7-8565U:
RPS 7783.229 7628.409 7542.340 ≈ 7651 ± 1.60% ↑ 16.72%
TPS 7708.436 7607.397 7489.459 ≈ 7602 ± 1.44% ↑ 16.85%
CPU % 74.899 71.020 72.697 ≈ 72.9 ± 2.67% ↑ 20.50%
Mem MB 438.047 436.967 416.350 ≈ 430 ± 2.84% ↑ 63.50%
DB restore:
real 0m20.838s 0m21.895s 0m21.794s ≈ 21.51 ± 2.71% ↓ 6.64%
user 0m39.091s 0m40.565s 0m41.493s ≈ 40.38 ± 3.00% ↑ 6.60%
sys 0m3.184s 0m2.923s 0m3.062s ≈ 3.06 ± 4.27% ↓ 4.38%
It obviously uses more memory now and utilizes CPU more aggressively, but at
the same time it allows to improve all relevant metrics and finally reach a
situation where we process 50K transactions in less than second on Ryzen 9
5950X (going higher than 25K TPS). The other observation is much more stable
block time, on Ryzen 9 it's as close to 1 second as it could be.
2021-07-30 20:35:03 +00:00
|
|
|
s.plock.Lock()
|
|
|
|
defer s.plock.Unlock()
|
2019-10-16 13:41:50 +00:00
|
|
|
s.mut.Lock()
|
2020-03-27 12:40:23 +00:00
|
|
|
|
|
|
|
keys = len(s.mem)
|
|
|
|
dkeys = len(s.del)
|
|
|
|
if keys == 0 && dkeys == 0 {
|
storage: allow accessing MemCachedStore during Persist
Persist by its definition doesn't change MemCachedStore visible state, all KV
pairs that were acessible via it before Persist remain accessible after
Persist. The only thing it does is flushing of the current set of KV pairs
from memory to peristent store. To do that it needs read-only access to the
current KV pair set, but technically it then replaces maps, so we have to use
full write lock which makes MemCachedStore inaccessible for the duration of
Persist. And Persist can take a lot of time, it's about disk access for
regular DBs.
What we do here is we create new in-memory maps for MemCachedStore before
flushing old ones to the persistent store. Then a fake persistent store is
created which actually is a MemCachedStore with old maps, so it has exactly
the same visible state. This Store is never accessed for writes, so we can
read it without taking any internal locks and at the same time we no longer
need write locks for original MemCachedStore, we're not using it. All of this
makes it possible to use MemCachedStore as normally reads are handled going
down to whatever level is needed and writes are handled by new maps. So while
Persist for (*Blockchain).dao does its most time-consuming work we can process
other blocks (reading data for transactions and persisting storeBlock caches
to (*Blockchain).dao).
The change was tested for performance with neo-bench (single node, 10 workers,
LevelDB) on two machines and block dump processing (RC4 testnet up to 62800
with VerifyBlocks set to false) on i7-8565U.
Reference results (bbe4e9cd7bb33428633586f080f64494cd6ac9cf):
Ryzen 9 5950X:
RPS 23616.969 22817.086 23222.378 ≈ 23218 ± 1.72%
TPS 23047.316 22608.578 22735.540 ≈ 22797 ± 0.99%
CPU % 23.434 25.553 23.848 ≈ 24.3 ± 4.63%
Mem MB 600.636 503.060 582.043 ≈ 562 ± 9.22%
Core i7-8565U:
RPS 6594.007 6499.501 6572.902 ≈ 6555 ± 0.76%
TPS 6561.680 6444.545 6510.120 ≈ 6505 ± 0.90%
CPU % 58.452 60.568 62.474 ≈ 60.5 ± 3.33%
Mem MB 234.893 285.067 269.081 ≈ 263 ± 9.75%
DB restore:
real 0m22.237s 0m23.471s 0m23.409s ≈ 23.04 ± 3.02%
user 0m35.435s 0m38.943s 0m39.247s ≈ 37.88 ± 5.59%
sys 0m3.085s 0m3.360s 0m3.144s ≈ 3.20 ± 4.53%
After the change:
Ryzen 9 5950X:
RPS 27747.349 27407.726 27520.210 ≈ 27558 ± 0.63% ↑ 18.69%
TPS 26992.010 26993.468 27010.966 ≈ 26999 ± 0.04% ↑ 18.43%
CPU % 28.928 28.096 29.105 ≈ 28.7 ± 1.88% ↑ 18.1%
Mem MB 760.385 726.320 756.118 ≈ 748 ± 2.48% ↑ 33.10%
Core i7-8565U:
RPS 7783.229 7628.409 7542.340 ≈ 7651 ± 1.60% ↑ 16.72%
TPS 7708.436 7607.397 7489.459 ≈ 7602 ± 1.44% ↑ 16.85%
CPU % 74.899 71.020 72.697 ≈ 72.9 ± 2.67% ↑ 20.50%
Mem MB 438.047 436.967 416.350 ≈ 430 ± 2.84% ↑ 63.50%
DB restore:
real 0m20.838s 0m21.895s 0m21.794s ≈ 21.51 ± 2.71% ↓ 6.64%
user 0m39.091s 0m40.565s 0m41.493s ≈ 40.38 ± 3.00% ↑ 6.60%
sys 0m3.184s 0m2.923s 0m3.062s ≈ 3.06 ± 4.27% ↓ 4.38%
It obviously uses more memory now and utilizes CPU more aggressively, but at
the same time it allows to improve all relevant metrics and finally reach a
situation where we process 50K transactions in less than second on Ryzen 9
5950X (going higher than 25K TPS). The other observation is much more stable
block time, on Ryzen 9 it's as close to 1 second as it could be.
2021-07-30 20:35:03 +00:00
|
|
|
s.mut.Unlock()
|
2020-03-27 12:40:23 +00:00
|
|
|
return 0, nil
|
2019-10-16 13:41:50 +00:00
|
|
|
}
|
2020-03-27 12:40:23 +00:00
|
|
|
|
storage: allow accessing MemCachedStore during Persist
Persist by its definition doesn't change MemCachedStore visible state, all KV
pairs that were acessible via it before Persist remain accessible after
Persist. The only thing it does is flushing of the current set of KV pairs
from memory to peristent store. To do that it needs read-only access to the
current KV pair set, but technically it then replaces maps, so we have to use
full write lock which makes MemCachedStore inaccessible for the duration of
Persist. And Persist can take a lot of time, it's about disk access for
regular DBs.
What we do here is we create new in-memory maps for MemCachedStore before
flushing old ones to the persistent store. Then a fake persistent store is
created which actually is a MemCachedStore with old maps, so it has exactly
the same visible state. This Store is never accessed for writes, so we can
read it without taking any internal locks and at the same time we no longer
need write locks for original MemCachedStore, we're not using it. All of this
makes it possible to use MemCachedStore as normally reads are handled going
down to whatever level is needed and writes are handled by new maps. So while
Persist for (*Blockchain).dao does its most time-consuming work we can process
other blocks (reading data for transactions and persisting storeBlock caches
to (*Blockchain).dao).
The change was tested for performance with neo-bench (single node, 10 workers,
LevelDB) on two machines and block dump processing (RC4 testnet up to 62800
with VerifyBlocks set to false) on i7-8565U.
Reference results (bbe4e9cd7bb33428633586f080f64494cd6ac9cf):
Ryzen 9 5950X:
RPS 23616.969 22817.086 23222.378 ≈ 23218 ± 1.72%
TPS 23047.316 22608.578 22735.540 ≈ 22797 ± 0.99%
CPU % 23.434 25.553 23.848 ≈ 24.3 ± 4.63%
Mem MB 600.636 503.060 582.043 ≈ 562 ± 9.22%
Core i7-8565U:
RPS 6594.007 6499.501 6572.902 ≈ 6555 ± 0.76%
TPS 6561.680 6444.545 6510.120 ≈ 6505 ± 0.90%
CPU % 58.452 60.568 62.474 ≈ 60.5 ± 3.33%
Mem MB 234.893 285.067 269.081 ≈ 263 ± 9.75%
DB restore:
real 0m22.237s 0m23.471s 0m23.409s ≈ 23.04 ± 3.02%
user 0m35.435s 0m38.943s 0m39.247s ≈ 37.88 ± 5.59%
sys 0m3.085s 0m3.360s 0m3.144s ≈ 3.20 ± 4.53%
After the change:
Ryzen 9 5950X:
RPS 27747.349 27407.726 27520.210 ≈ 27558 ± 0.63% ↑ 18.69%
TPS 26992.010 26993.468 27010.966 ≈ 26999 ± 0.04% ↑ 18.43%
CPU % 28.928 28.096 29.105 ≈ 28.7 ± 1.88% ↑ 18.1%
Mem MB 760.385 726.320 756.118 ≈ 748 ± 2.48% ↑ 33.10%
Core i7-8565U:
RPS 7783.229 7628.409 7542.340 ≈ 7651 ± 1.60% ↑ 16.72%
TPS 7708.436 7607.397 7489.459 ≈ 7602 ± 1.44% ↑ 16.85%
CPU % 74.899 71.020 72.697 ≈ 72.9 ± 2.67% ↑ 20.50%
Mem MB 438.047 436.967 416.350 ≈ 430 ± 2.84% ↑ 63.50%
DB restore:
real 0m20.838s 0m21.895s 0m21.794s ≈ 21.51 ± 2.71% ↓ 6.64%
user 0m39.091s 0m40.565s 0m41.493s ≈ 40.38 ± 3.00% ↑ 6.60%
sys 0m3.184s 0m2.923s 0m3.062s ≈ 3.06 ± 4.27% ↓ 4.38%
It obviously uses more memory now and utilizes CPU more aggressively, but at
the same time it allows to improve all relevant metrics and finally reach a
situation where we process 50K transactions in less than second on Ryzen 9
5950X (going higher than 25K TPS). The other observation is much more stable
block time, on Ryzen 9 it's as close to 1 second as it could be.
2021-07-30 20:35:03 +00:00
|
|
|
// tempstore technically copies current s in lower layer while real s
|
|
|
|
// starts using fresh new maps. This tempstore is only known here and
|
|
|
|
// nothing ever changes it, therefore accesses to it (reads) can go
|
|
|
|
// unprotected while writes are handled by s proper.
|
|
|
|
var tempstore = &MemCachedStore{MemoryStore: MemoryStore{mem: s.mem, del: s.del}, ps: s.ps}
|
|
|
|
s.ps = tempstore
|
|
|
|
s.mem = make(map[string][]byte)
|
|
|
|
s.del = make(map[string]bool)
|
2021-11-22 07:41:40 +00:00
|
|
|
if !isSync {
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
storage: allow accessing MemCachedStore during Persist
Persist by its definition doesn't change MemCachedStore visible state, all KV
pairs that were acessible via it before Persist remain accessible after
Persist. The only thing it does is flushing of the current set of KV pairs
from memory to peristent store. To do that it needs read-only access to the
current KV pair set, but technically it then replaces maps, so we have to use
full write lock which makes MemCachedStore inaccessible for the duration of
Persist. And Persist can take a lot of time, it's about disk access for
regular DBs.
What we do here is we create new in-memory maps for MemCachedStore before
flushing old ones to the persistent store. Then a fake persistent store is
created which actually is a MemCachedStore with old maps, so it has exactly
the same visible state. This Store is never accessed for writes, so we can
read it without taking any internal locks and at the same time we no longer
need write locks for original MemCachedStore, we're not using it. All of this
makes it possible to use MemCachedStore as normally reads are handled going
down to whatever level is needed and writes are handled by new maps. So while
Persist for (*Blockchain).dao does its most time-consuming work we can process
other blocks (reading data for transactions and persisting storeBlock caches
to (*Blockchain).dao).
The change was tested for performance with neo-bench (single node, 10 workers,
LevelDB) on two machines and block dump processing (RC4 testnet up to 62800
with VerifyBlocks set to false) on i7-8565U.
Reference results (bbe4e9cd7bb33428633586f080f64494cd6ac9cf):
Ryzen 9 5950X:
RPS 23616.969 22817.086 23222.378 ≈ 23218 ± 1.72%
TPS 23047.316 22608.578 22735.540 ≈ 22797 ± 0.99%
CPU % 23.434 25.553 23.848 ≈ 24.3 ± 4.63%
Mem MB 600.636 503.060 582.043 ≈ 562 ± 9.22%
Core i7-8565U:
RPS 6594.007 6499.501 6572.902 ≈ 6555 ± 0.76%
TPS 6561.680 6444.545 6510.120 ≈ 6505 ± 0.90%
CPU % 58.452 60.568 62.474 ≈ 60.5 ± 3.33%
Mem MB 234.893 285.067 269.081 ≈ 263 ± 9.75%
DB restore:
real 0m22.237s 0m23.471s 0m23.409s ≈ 23.04 ± 3.02%
user 0m35.435s 0m38.943s 0m39.247s ≈ 37.88 ± 5.59%
sys 0m3.085s 0m3.360s 0m3.144s ≈ 3.20 ± 4.53%
After the change:
Ryzen 9 5950X:
RPS 27747.349 27407.726 27520.210 ≈ 27558 ± 0.63% ↑ 18.69%
TPS 26992.010 26993.468 27010.966 ≈ 26999 ± 0.04% ↑ 18.43%
CPU % 28.928 28.096 29.105 ≈ 28.7 ± 1.88% ↑ 18.1%
Mem MB 760.385 726.320 756.118 ≈ 748 ± 2.48% ↑ 33.10%
Core i7-8565U:
RPS 7783.229 7628.409 7542.340 ≈ 7651 ± 1.60% ↑ 16.72%
TPS 7708.436 7607.397 7489.459 ≈ 7602 ± 1.44% ↑ 16.85%
CPU % 74.899 71.020 72.697 ≈ 72.9 ± 2.67% ↑ 20.50%
Mem MB 438.047 436.967 416.350 ≈ 430 ± 2.84% ↑ 63.50%
DB restore:
real 0m20.838s 0m21.895s 0m21.794s ≈ 21.51 ± 2.71% ↓ 6.64%
user 0m39.091s 0m40.565s 0m41.493s ≈ 40.38 ± 3.00% ↑ 6.60%
sys 0m3.184s 0m2.923s 0m3.062s ≈ 3.06 ± 4.27% ↓ 4.38%
It obviously uses more memory now and utilizes CPU more aggressively, but at
the same time it allows to improve all relevant metrics and finally reach a
situation where we process 50K transactions in less than second on Ryzen 9
5950X (going higher than 25K TPS). The other observation is much more stable
block time, on Ryzen 9 it's as close to 1 second as it could be.
2021-07-30 20:35:03 +00:00
|
|
|
|
2021-08-12 10:35:09 +00:00
|
|
|
err = tempstore.ps.PutChangeSet(tempstore.mem, tempstore.del)
|
|
|
|
|
2021-11-22 07:41:40 +00:00
|
|
|
if !isSync {
|
|
|
|
s.mut.Lock()
|
|
|
|
}
|
2019-10-16 13:41:50 +00:00
|
|
|
if err == nil {
|
storage: allow accessing MemCachedStore during Persist
Persist by its definition doesn't change MemCachedStore visible state, all KV
pairs that were acessible via it before Persist remain accessible after
Persist. The only thing it does is flushing of the current set of KV pairs
from memory to peristent store. To do that it needs read-only access to the
current KV pair set, but technically it then replaces maps, so we have to use
full write lock which makes MemCachedStore inaccessible for the duration of
Persist. And Persist can take a lot of time, it's about disk access for
regular DBs.
What we do here is we create new in-memory maps for MemCachedStore before
flushing old ones to the persistent store. Then a fake persistent store is
created which actually is a MemCachedStore with old maps, so it has exactly
the same visible state. This Store is never accessed for writes, so we can
read it without taking any internal locks and at the same time we no longer
need write locks for original MemCachedStore, we're not using it. All of this
makes it possible to use MemCachedStore as normally reads are handled going
down to whatever level is needed and writes are handled by new maps. So while
Persist for (*Blockchain).dao does its most time-consuming work we can process
other blocks (reading data for transactions and persisting storeBlock caches
to (*Blockchain).dao).
The change was tested for performance with neo-bench (single node, 10 workers,
LevelDB) on two machines and block dump processing (RC4 testnet up to 62800
with VerifyBlocks set to false) on i7-8565U.
Reference results (bbe4e9cd7bb33428633586f080f64494cd6ac9cf):
Ryzen 9 5950X:
RPS 23616.969 22817.086 23222.378 ≈ 23218 ± 1.72%
TPS 23047.316 22608.578 22735.540 ≈ 22797 ± 0.99%
CPU % 23.434 25.553 23.848 ≈ 24.3 ± 4.63%
Mem MB 600.636 503.060 582.043 ≈ 562 ± 9.22%
Core i7-8565U:
RPS 6594.007 6499.501 6572.902 ≈ 6555 ± 0.76%
TPS 6561.680 6444.545 6510.120 ≈ 6505 ± 0.90%
CPU % 58.452 60.568 62.474 ≈ 60.5 ± 3.33%
Mem MB 234.893 285.067 269.081 ≈ 263 ± 9.75%
DB restore:
real 0m22.237s 0m23.471s 0m23.409s ≈ 23.04 ± 3.02%
user 0m35.435s 0m38.943s 0m39.247s ≈ 37.88 ± 5.59%
sys 0m3.085s 0m3.360s 0m3.144s ≈ 3.20 ± 4.53%
After the change:
Ryzen 9 5950X:
RPS 27747.349 27407.726 27520.210 ≈ 27558 ± 0.63% ↑ 18.69%
TPS 26992.010 26993.468 27010.966 ≈ 26999 ± 0.04% ↑ 18.43%
CPU % 28.928 28.096 29.105 ≈ 28.7 ± 1.88% ↑ 18.1%
Mem MB 760.385 726.320 756.118 ≈ 748 ± 2.48% ↑ 33.10%
Core i7-8565U:
RPS 7783.229 7628.409 7542.340 ≈ 7651 ± 1.60% ↑ 16.72%
TPS 7708.436 7607.397 7489.459 ≈ 7602 ± 1.44% ↑ 16.85%
CPU % 74.899 71.020 72.697 ≈ 72.9 ± 2.67% ↑ 20.50%
Mem MB 438.047 436.967 416.350 ≈ 430 ± 2.84% ↑ 63.50%
DB restore:
real 0m20.838s 0m21.895s 0m21.794s ≈ 21.51 ± 2.71% ↓ 6.64%
user 0m39.091s 0m40.565s 0m41.493s ≈ 40.38 ± 3.00% ↑ 6.60%
sys 0m3.184s 0m2.923s 0m3.062s ≈ 3.06 ± 4.27% ↓ 4.38%
It obviously uses more memory now and utilizes CPU more aggressively, but at
the same time it allows to improve all relevant metrics and finally reach a
situation where we process 50K transactions in less than second on Ryzen 9
5950X (going higher than 25K TPS). The other observation is much more stable
block time, on Ryzen 9 it's as close to 1 second as it could be.
2021-07-30 20:35:03 +00:00
|
|
|
// tempstore.mem and tempstore.del are completely flushed now
|
|
|
|
// to tempstore.ps, so all KV pairs are the same and this
|
|
|
|
// substitution has no visible effects.
|
|
|
|
s.ps = tempstore.ps
|
|
|
|
} else {
|
|
|
|
// We're toast. We'll try to still keep proper state, but OOM
|
|
|
|
// killer will get to us eventually.
|
|
|
|
for k := range s.mem {
|
|
|
|
tempstore.put(k, s.mem[k])
|
|
|
|
}
|
|
|
|
for k := range s.del {
|
|
|
|
tempstore.drop(k)
|
|
|
|
}
|
|
|
|
s.ps = tempstore.ps
|
|
|
|
s.mem = tempstore.mem
|
|
|
|
s.del = tempstore.del
|
2019-10-16 13:41:50 +00:00
|
|
|
}
|
storage: allow accessing MemCachedStore during Persist
Persist by its definition doesn't change MemCachedStore visible state, all KV
pairs that were acessible via it before Persist remain accessible after
Persist. The only thing it does is flushing of the current set of KV pairs
from memory to peristent store. To do that it needs read-only access to the
current KV pair set, but technically it then replaces maps, so we have to use
full write lock which makes MemCachedStore inaccessible for the duration of
Persist. And Persist can take a lot of time, it's about disk access for
regular DBs.
What we do here is we create new in-memory maps for MemCachedStore before
flushing old ones to the persistent store. Then a fake persistent store is
created which actually is a MemCachedStore with old maps, so it has exactly
the same visible state. This Store is never accessed for writes, so we can
read it without taking any internal locks and at the same time we no longer
need write locks for original MemCachedStore, we're not using it. All of this
makes it possible to use MemCachedStore as normally reads are handled going
down to whatever level is needed and writes are handled by new maps. So while
Persist for (*Blockchain).dao does its most time-consuming work we can process
other blocks (reading data for transactions and persisting storeBlock caches
to (*Blockchain).dao).
The change was tested for performance with neo-bench (single node, 10 workers,
LevelDB) on two machines and block dump processing (RC4 testnet up to 62800
with VerifyBlocks set to false) on i7-8565U.
Reference results (bbe4e9cd7bb33428633586f080f64494cd6ac9cf):
Ryzen 9 5950X:
RPS 23616.969 22817.086 23222.378 ≈ 23218 ± 1.72%
TPS 23047.316 22608.578 22735.540 ≈ 22797 ± 0.99%
CPU % 23.434 25.553 23.848 ≈ 24.3 ± 4.63%
Mem MB 600.636 503.060 582.043 ≈ 562 ± 9.22%
Core i7-8565U:
RPS 6594.007 6499.501 6572.902 ≈ 6555 ± 0.76%
TPS 6561.680 6444.545 6510.120 ≈ 6505 ± 0.90%
CPU % 58.452 60.568 62.474 ≈ 60.5 ± 3.33%
Mem MB 234.893 285.067 269.081 ≈ 263 ± 9.75%
DB restore:
real 0m22.237s 0m23.471s 0m23.409s ≈ 23.04 ± 3.02%
user 0m35.435s 0m38.943s 0m39.247s ≈ 37.88 ± 5.59%
sys 0m3.085s 0m3.360s 0m3.144s ≈ 3.20 ± 4.53%
After the change:
Ryzen 9 5950X:
RPS 27747.349 27407.726 27520.210 ≈ 27558 ± 0.63% ↑ 18.69%
TPS 26992.010 26993.468 27010.966 ≈ 26999 ± 0.04% ↑ 18.43%
CPU % 28.928 28.096 29.105 ≈ 28.7 ± 1.88% ↑ 18.1%
Mem MB 760.385 726.320 756.118 ≈ 748 ± 2.48% ↑ 33.10%
Core i7-8565U:
RPS 7783.229 7628.409 7542.340 ≈ 7651 ± 1.60% ↑ 16.72%
TPS 7708.436 7607.397 7489.459 ≈ 7602 ± 1.44% ↑ 16.85%
CPU % 74.899 71.020 72.697 ≈ 72.9 ± 2.67% ↑ 20.50%
Mem MB 438.047 436.967 416.350 ≈ 430 ± 2.84% ↑ 63.50%
DB restore:
real 0m20.838s 0m21.895s 0m21.794s ≈ 21.51 ± 2.71% ↓ 6.64%
user 0m39.091s 0m40.565s 0m41.493s ≈ 40.38 ± 3.00% ↑ 6.60%
sys 0m3.184s 0m2.923s 0m3.062s ≈ 3.06 ± 4.27% ↓ 4.38%
It obviously uses more memory now and utilizes CPU more aggressively, but at
the same time it allows to improve all relevant metrics and finally reach a
situation where we process 50K transactions in less than second on Ryzen 9
5950X (going higher than 25K TPS). The other observation is much more stable
block time, on Ryzen 9 it's as close to 1 second as it could be.
2021-07-30 20:35:03 +00:00
|
|
|
s.mut.Unlock()
|
2019-10-16 13:41:50 +00:00
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements Store interface, clears up memory and closes the lower layer
|
|
|
|
// Store.
|
|
|
|
func (s *MemCachedStore) Close() error {
|
|
|
|
// It's always successful.
|
|
|
|
_ = s.MemoryStore.Close()
|
|
|
|
return s.ps.Close()
|
|
|
|
}
|