2020-05-30 07:00:35 +00:00
|
|
|
package etl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-06-13 15:03:38 +00:00
|
|
|
"io"
|
2020-08-17 06:45:52 +00:00
|
|
|
"reflect"
|
2020-08-21 06:30:30 +00:00
|
|
|
"runtime"
|
2020-06-13 15:03:38 +00:00
|
|
|
"time"
|
|
|
|
|
2020-08-05 15:33:58 +00:00
|
|
|
"github.com/c2h5oh/datasize"
|
2020-05-30 07:00:35 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
2020-06-10 20:07:14 +00:00
|
|
|
"github.com/ugorji/go/codec"
|
2020-05-30 07:00:35 +00:00
|
|
|
)
|
|
|
|
|
2020-05-31 07:32:33 +00:00
|
|
|
var (
|
2020-06-10 20:07:14 +00:00
|
|
|
cbor codec.CborHandle
|
2020-05-31 07:32:33 +00:00
|
|
|
)
|
2020-05-30 07:00:35 +00:00
|
|
|
|
|
|
|
type Decoder interface {
|
2020-06-10 20:07:14 +00:00
|
|
|
Reset(reader io.Reader)
|
2020-05-30 07:00:35 +00:00
|
|
|
Decode(interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type State interface {
|
|
|
|
Get([]byte) ([]byte, error)
|
2020-05-30 13:44:54 +00:00
|
|
|
Stopped() error
|
2020-05-30 07:00:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
type ExtractNextFunc func(originalK, k []byte, v []byte) error
|
2020-05-30 07:00:35 +00:00
|
|
|
type ExtractFunc func(k []byte, v []byte, next ExtractNextFunc) error
|
|
|
|
|
2020-06-01 14:14:40 +00:00
|
|
|
// NextKey generates the possible next key w/o changing the key length.
|
|
|
|
// for [0x01, 0x01, 0x01] it will generate [0x01, 0x01, 0x02], etc
|
|
|
|
func NextKey(key []byte) ([]byte, error) {
|
|
|
|
if len(key) == 0 {
|
|
|
|
return key, fmt.Errorf("could not apply NextKey for the empty key")
|
|
|
|
}
|
|
|
|
nextKey := common.CopyBytes(key)
|
|
|
|
for i := len(key) - 1; i >= 0; i-- {
|
|
|
|
b := nextKey[i]
|
|
|
|
if b < 0xFF {
|
|
|
|
nextKey[i] = b + 1
|
|
|
|
return nextKey, nil
|
|
|
|
}
|
|
|
|
if b == 0xFF {
|
|
|
|
nextKey[i] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return key, fmt.Errorf("overflow while applying NextKey")
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadCommitHandler is a callback called each time a new batch is being
|
|
|
|
// loaded from files into a DB
|
|
|
|
// * `key`: last commited key to the database (use etl.NextKey helper to use in LoadStartKey)
|
|
|
|
// * `isDone`: true, if everything is processed
|
2020-08-05 10:13:35 +00:00
|
|
|
type LoadCommitHandler func(db ethdb.Putter, key []byte, isDone bool) error
|
2020-06-01 14:14:40 +00:00
|
|
|
|
|
|
|
type TransformArgs struct {
|
|
|
|
ExtractStartKey []byte
|
2020-06-10 20:07:14 +00:00
|
|
|
ExtractEndKey []byte
|
|
|
|
FixedBits int
|
|
|
|
BufferType int
|
|
|
|
BufferSize int
|
2020-07-05 06:18:21 +00:00
|
|
|
Quit <-chan struct{}
|
2020-06-01 14:14:40 +00:00
|
|
|
OnLoadCommit LoadCommitHandler
|
|
|
|
loadBatchSize int // used in testing
|
2020-05-31 12:23:34 +00:00
|
|
|
}
|
2020-05-30 07:00:35 +00:00
|
|
|
|
2020-05-31 12:23:34 +00:00
|
|
|
func Transform(
|
|
|
|
db ethdb.Database,
|
2020-08-10 23:55:32 +00:00
|
|
|
fromBucket string,
|
|
|
|
toBucket string,
|
2020-05-31 12:23:34 +00:00
|
|
|
datadir string,
|
|
|
|
extractFunc ExtractFunc,
|
|
|
|
loadFunc LoadFunc,
|
2020-06-01 14:14:40 +00:00
|
|
|
args TransformArgs,
|
2020-05-31 12:23:34 +00:00
|
|
|
) error {
|
2020-06-10 20:07:14 +00:00
|
|
|
bufferSize := BufferOptimalSize
|
|
|
|
if args.BufferSize > 0 {
|
|
|
|
bufferSize = args.BufferSize
|
2020-05-30 07:00:35 +00:00
|
|
|
}
|
2020-06-10 20:07:14 +00:00
|
|
|
buffer := getBufferByType(args.BufferType, bufferSize)
|
|
|
|
collector := NewCollector(datadir, buffer)
|
|
|
|
|
|
|
|
t := time.Now()
|
2020-06-28 06:10:27 +00:00
|
|
|
if err := extractBucketIntoFiles(db, fromBucket, args.ExtractStartKey, args.ExtractEndKey, args.FixedBits, collector, extractFunc, args.Quit); err != nil {
|
|
|
|
disposeProviders(collector.dataProviders)
|
|
|
|
return err
|
2020-06-10 20:07:14 +00:00
|
|
|
}
|
2020-07-07 04:00:25 +00:00
|
|
|
log.Debug("Extraction finished", "it took", time.Since(t))
|
2020-06-10 20:07:14 +00:00
|
|
|
|
2020-07-07 04:00:25 +00:00
|
|
|
defer func(t time.Time) { log.Debug("Collection finished", "it took", time.Since(t)) }(time.Now())
|
2020-06-01 14:14:40 +00:00
|
|
|
return collector.Load(db, toBucket, loadFunc, args)
|
2020-05-31 12:23:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractBucketIntoFiles(
|
|
|
|
db ethdb.Database,
|
2020-08-10 23:55:32 +00:00
|
|
|
bucket string,
|
2020-05-31 12:23:34 +00:00
|
|
|
startkey []byte,
|
2020-06-10 20:07:14 +00:00
|
|
|
endkey []byte,
|
|
|
|
fixedBits int,
|
2020-05-31 12:23:34 +00:00
|
|
|
collector *Collector,
|
|
|
|
extractFunc ExtractFunc,
|
2020-07-05 06:18:21 +00:00
|
|
|
quit <-chan struct{},
|
2020-05-31 12:23:34 +00:00
|
|
|
) error {
|
2020-08-21 06:30:30 +00:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
putTimer := time.Now()
|
|
|
|
var m runtime.MemStats
|
|
|
|
|
2020-06-10 20:07:14 +00:00
|
|
|
if err := db.Walk(bucket, startkey, fixedBits, func(k, v []byte) (bool, error) {
|
2020-05-31 12:23:34 +00:00
|
|
|
if err := common.Stopped(quit); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-08-21 06:30:30 +00:00
|
|
|
i++
|
|
|
|
i, putTimer = printProgressIfNeeded(i, putTimer, k, func(progress int) {
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
log.Info(
|
|
|
|
"ETL [1/2] Extracting",
|
|
|
|
"from", bucket,
|
|
|
|
"keys", fmt.Sprintf("%.1fM", float64(i)/1_000_000),
|
|
|
|
"progress", progress, // extracting is the first stage, from 0..50
|
|
|
|
"current key", makeCurrentKeyStr(k),
|
|
|
|
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys), "numGC", int(m.NumGC),
|
|
|
|
)
|
|
|
|
})
|
2020-06-10 20:07:14 +00:00
|
|
|
if endkey != nil && bytes.Compare(k, endkey) > 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-05-31 12:23:34 +00:00
|
|
|
if err := extractFunc(k, v, collector.extractNextFunc); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return collector.flushBuffer(nil, true)
|
2020-05-30 07:00:35 +00:00
|
|
|
}
|
2020-05-31 07:32:33 +00:00
|
|
|
func disposeProviders(providers []dataProvider) {
|
2020-08-05 15:33:58 +00:00
|
|
|
totalSize := uint64(0)
|
2020-05-31 07:32:33 +00:00
|
|
|
for _, p := range providers {
|
2020-08-05 15:33:58 +00:00
|
|
|
providerSize, err := p.Dispose()
|
2020-05-30 07:00:35 +00:00
|
|
|
if err != nil {
|
2020-05-31 07:32:33 +00:00
|
|
|
log.Warn("promoting hashed state, error while disposing provider", "provier", p, "err", err)
|
2020-05-30 07:00:35 +00:00
|
|
|
}
|
2020-08-05 15:33:58 +00:00
|
|
|
totalSize += providerSize
|
2020-05-30 07:00:35 +00:00
|
|
|
}
|
2020-08-06 07:33:09 +00:00
|
|
|
if totalSize > 0 {
|
|
|
|
log.Info("etl: temp files removed successfully", "total size", datasize.ByteSize(totalSize).HumanReadable())
|
|
|
|
}
|
2020-05-30 07:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type bucketState struct {
|
|
|
|
getter ethdb.Getter
|
2020-08-10 23:55:32 +00:00
|
|
|
bucket string
|
2020-07-05 06:18:21 +00:00
|
|
|
quit <-chan struct{}
|
2020-05-30 07:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *bucketState) Get(key []byte) ([]byte, error) {
|
|
|
|
return s.getter.Get(s.bucket, key)
|
|
|
|
}
|
2020-05-30 13:44:54 +00:00
|
|
|
|
|
|
|
func (s *bucketState) Stopped() error {
|
|
|
|
return common.Stopped(s.quit)
|
|
|
|
}
|
2020-05-31 12:23:34 +00:00
|
|
|
|
|
|
|
// IdentityLoadFunc loads entries as they are, without transformation
|
2020-08-12 03:49:52 +00:00
|
|
|
var IdentityLoadFunc LoadFunc = func(k []byte, value []byte, _ State, next LoadNextFunc) error {
|
2020-07-01 14:56:56 +00:00
|
|
|
return next(k, k, value)
|
2020-05-31 12:23:34 +00:00
|
|
|
}
|
2020-08-17 06:45:52 +00:00
|
|
|
|
|
|
|
func isIdentityLoadFunc(f LoadFunc) bool {
|
|
|
|
return f == nil || reflect.ValueOf(IdentityLoadFunc).Pointer() == reflect.ValueOf(f).Pointer()
|
|
|
|
}
|