2019-05-10 10:26:57 +00:00
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-28 13:50:33 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/errors"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
|
2019-05-10 10:26:57 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2021-01-14 22:15:13 +00:00
|
|
|
"github.com/status-im/status-go/whisper"
|
2019-05-10 10:26:57 +00:00
|
|
|
)
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
type LevelDB struct {
|
2019-05-10 10:26:57 +00:00
|
|
|
// We can't embed as there are some state problems with go-routines
|
2020-04-07 08:21:39 +00:00
|
|
|
ldb *leveldb.DB
|
|
|
|
name string
|
|
|
|
done chan struct{}
|
2019-05-10 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LevelDBIterator struct {
|
|
|
|
iterator.Iterator
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
func (i *LevelDBIterator) DBKey() (*DBKey, error) {
|
2019-05-10 10:26:57 +00:00
|
|
|
return &DBKey{
|
|
|
|
raw: i.Key(),
|
2019-05-13 09:25:46 +00:00
|
|
|
}, nil
|
2019-05-10 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *LevelDBIterator) GetEnvelope(bloom []byte) ([]byte, error) {
|
|
|
|
var envelopeBloom []byte
|
|
|
|
rawValue := make([]byte, len(i.Value()))
|
|
|
|
copy(rawValue, i.Value())
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
key, err := i.DBKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-10 10:26:57 +00:00
|
|
|
if len(key.Bytes()) != DBKeyLength {
|
|
|
|
var err error
|
|
|
|
envelopeBloom, err = extractBloomFromEncodedEnvelope(rawValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-23 17:57:05 +00:00
|
|
|
envelopeBloom = types.TopicToBloom(key.Topic())
|
2019-05-10 10:26:57 +00:00
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
if !types.BloomFilterMatch(bloom, envelopeBloom) {
|
2019-05-10 10:26:57 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return rawValue, nil
|
2020-01-21 07:11:24 +00:00
|
|
|
}
|
2019-05-10 10:26:57 +00:00
|
|
|
|
2020-01-21 07:11:24 +00:00
|
|
|
func (i *LevelDBIterator) Release() error {
|
|
|
|
i.Iterator.Release()
|
|
|
|
return nil
|
2019-05-10 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
func NewLevelDB(dataDir string) (*LevelDB, error) {
|
2019-05-10 10:26:57 +00:00
|
|
|
// Open opens an existing leveldb database
|
2020-01-08 11:12:23 +00:00
|
|
|
db, err := leveldb.OpenFile(dataDir, nil)
|
|
|
|
if _, corrupted := err.(*errors.ErrCorrupted); corrupted {
|
|
|
|
log.Info("database is corrupted trying to recover", "path", dataDir)
|
|
|
|
db, err = leveldb.RecoverFile(dataDir, nil)
|
2019-05-10 10:26:57 +00:00
|
|
|
}
|
2020-04-07 08:21:39 +00:00
|
|
|
|
|
|
|
instance := LevelDB{
|
|
|
|
ldb: db,
|
|
|
|
name: dataDir, // name is used for metrics labels
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the metric value
|
|
|
|
instance.updateArchivedEnvelopesCount()
|
|
|
|
// checking count on every insert is inefficient
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-instance.done:
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second * envelopeCountCheckInterval):
|
|
|
|
instance.updateArchivedEnvelopesCount()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return &instance, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEnvelope get an envelope by its key
|
|
|
|
func (db *LevelDB) GetEnvelope(key *DBKey) ([]byte, error) {
|
|
|
|
defer recoverLevelDBPanics("GetEnvelope")
|
|
|
|
return db.ldb.Get(key.Bytes(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *LevelDB) updateArchivedEnvelopesCount() {
|
|
|
|
if count, err := db.envelopesCount(); err != nil {
|
|
|
|
log.Warn("db query for envelopes count failed", "err", err)
|
|
|
|
} else {
|
|
|
|
archivedEnvelopesGauge.WithLabelValues(db.name).Set(float64(count))
|
|
|
|
}
|
2019-05-10 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build iterator returns an iterator given a start/end and a cursor
|
2019-05-13 09:25:46 +00:00
|
|
|
func (db *LevelDB) BuildIterator(query CursorQuery) (Iterator, error) {
|
2019-05-10 10:26:57 +00:00
|
|
|
defer recoverLevelDBPanics("BuildIterator")
|
|
|
|
|
|
|
|
i := db.ldb.NewIterator(&util.Range{Start: query.start, Limit: query.end}, nil)
|
|
|
|
// seek to the end as we want to return envelopes in a descending order
|
|
|
|
if len(query.cursor) == CursorLength {
|
|
|
|
i.Seek(query.cursor)
|
|
|
|
}
|
2019-05-13 09:25:46 +00:00
|
|
|
return &LevelDBIterator{i}, nil
|
2019-05-10 10:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prune removes envelopes older than time
|
2019-05-13 09:25:46 +00:00
|
|
|
func (db *LevelDB) Prune(t time.Time, batchSize int) (int, error) {
|
2019-05-10 10:26:57 +00:00
|
|
|
defer recoverLevelDBPanics("Prune")
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
var zero types.Hash
|
|
|
|
var emptyTopic types.TopicType
|
2019-05-10 10:26:57 +00:00
|
|
|
kl := NewDBKey(0, emptyTopic, zero)
|
|
|
|
ku := NewDBKey(uint32(t.Unix()), emptyTopic, zero)
|
|
|
|
query := CursorQuery{
|
|
|
|
start: kl.Bytes(),
|
|
|
|
end: ku.Bytes(),
|
|
|
|
}
|
2019-05-13 09:25:46 +00:00
|
|
|
i, err := db.BuildIterator(query)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-01-21 07:11:24 +00:00
|
|
|
defer func() { _ = i.Release() }()
|
2019-05-10 10:26:57 +00:00
|
|
|
|
|
|
|
batch := leveldb.Batch{}
|
|
|
|
removed := 0
|
|
|
|
|
|
|
|
for i.Next() {
|
2019-05-13 09:25:46 +00:00
|
|
|
dbKey, err := i.DBKey()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.Delete(dbKey.Bytes())
|
2019-05-10 10:26:57 +00:00
|
|
|
|
|
|
|
if batch.Len() == batchSize {
|
|
|
|
if err := db.ldb.Write(&batch, nil); err != nil {
|
|
|
|
return removed, err
|
|
|
|
}
|
|
|
|
|
|
|
|
removed = removed + batch.Len()
|
|
|
|
batch.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if batch.Len() > 0 {
|
|
|
|
if err := db.ldb.Write(&batch, nil); err != nil {
|
|
|
|
return removed, err
|
|
|
|
}
|
|
|
|
|
|
|
|
removed = removed + batch.Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
return removed, nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:21:39 +00:00
|
|
|
func (db *LevelDB) envelopesCount() (int, error) {
|
|
|
|
defer recoverLevelDBPanics("envelopesCount")
|
|
|
|
iterator, err := db.BuildIterator(CursorQuery{})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// LevelDB does not have API for getting a count
|
|
|
|
var count int
|
|
|
|
for iterator.Next() {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2019-05-10 10:26:57 +00:00
|
|
|
// SaveEnvelope stores an envelope in leveldb and increments the metrics
|
2020-01-08 11:12:23 +00:00
|
|
|
func (db *LevelDB) SaveEnvelope(env types.Envelope) error {
|
2019-05-10 10:26:57 +00:00
|
|
|
defer recoverLevelDBPanics("SaveEnvelope")
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
key := NewDBKey(env.Expiry()-env.TTL(), env.Topic(), env.Hash())
|
2020-01-21 07:11:24 +00:00
|
|
|
rawEnvelope, err := rlp.EncodeToBytes(env.Unwrap())
|
2019-05-10 10:26:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("rlp.EncodeToBytes failed: %s", err))
|
2020-04-07 08:21:39 +00:00
|
|
|
archivedErrorsCounter.WithLabelValues(db.name).Inc()
|
2019-05-10 10:26:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.ldb.Put(key.Bytes(), rawEnvelope, nil); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Writing to DB failed: %s", err))
|
2020-04-07 08:21:39 +00:00
|
|
|
archivedErrorsCounter.WithLabelValues(db.name).Inc()
|
2019-05-10 10:26:57 +00:00
|
|
|
}
|
2020-04-07 08:21:39 +00:00
|
|
|
archivedEnvelopesGauge.WithLabelValues(db.name).Inc()
|
|
|
|
archivedEnvelopeSizeMeter.WithLabelValues(db.name).Observe(
|
|
|
|
float64(whisper.EnvelopeHeaderLength + env.Size()))
|
2019-05-10 10:26:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
func (db *LevelDB) Close() error {
|
2020-04-07 08:21:39 +00:00
|
|
|
select {
|
|
|
|
case <-db.done:
|
|
|
|
default:
|
|
|
|
close(db.done)
|
|
|
|
}
|
2019-05-10 10:26:57 +00:00
|
|
|
return db.ldb.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func recoverLevelDBPanics(calleMethodName string) {
|
|
|
|
// Recover from possible goleveldb panics
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if errString, ok := r.(string); ok {
|
|
|
|
log.Error(fmt.Sprintf("recovered from panic in %s: %s", calleMethodName, errString))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|