2019-07-22 12:17:27 +03:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
2019-03-08 16:56:20 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rawdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
"os"
|
2019-03-14 09:59:47 +03:00
|
|
|
"path/filepath"
|
2020-08-03 20:40:46 +03:00
|
|
|
"sync"
|
2019-03-08 16:56:20 +03:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2024-03-22 17:37:47 +03:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
|
2019-03-08 16:56:20 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
2023-02-23 10:11:50 +03:00
|
|
|
"github.com/gofrs/flock"
|
2019-03-08 16:56:20 +03:00
|
|
|
)
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
var (
|
2021-03-22 21:06:30 +03:00
|
|
|
// errReadOnly is returned if the freezer is opened in read only mode. All the
|
|
|
|
// mutations are disallowed.
|
|
|
|
errReadOnly = errors.New("read only")
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
// errUnknownTable is returned if the user attempts to read from a table that is
|
|
|
|
// not tracked by the freezer.
|
|
|
|
errUnknownTable = errors.New("unknown table")
|
|
|
|
|
|
|
|
// errOutOrderInsertion is returned if the user attempts to inject out-of-order
|
|
|
|
// binary blobs into the freezer.
|
|
|
|
errOutOrderInsertion = errors.New("the append operation is out-order")
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
|
|
|
|
// errSymlinkDatadir is returned if the ancient directory specified by user
|
|
|
|
// is a symbolic link.
|
|
|
|
errSymlinkDatadir = errors.New("symbolic link datadir is not supported")
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
)
|
2019-03-08 16:56:20 +03:00
|
|
|
|
2022-05-06 14:28:42 +03:00
|
|
|
// freezerTableSize defines the maximum size of freezer data files.
|
|
|
|
const freezerTableSize = 2 * 1000 * 1000 * 1000
|
2019-03-08 16:56:20 +03:00
|
|
|
|
2022-05-06 14:28:42 +03:00
|
|
|
// Freezer is a memory mapped append-only database to store immutable ordered
|
|
|
|
// data into flat files:
|
2019-03-08 16:56:20 +03:00
|
|
|
//
|
2022-09-10 14:25:40 +03:00
|
|
|
// - The append-only nature ensures that disk writes are minimized.
|
2023-02-02 14:36:37 +03:00
|
|
|
// - The memory mapping ensures we can max out system memory for caching without
|
|
|
|
// reserving it for go-ethereum. This would also reduce the memory requirements
|
|
|
|
// of Geth, and thus also GC overhead.
|
2022-05-06 14:28:42 +03:00
|
|
|
type Freezer struct {
|
2023-03-21 14:10:23 +03:00
|
|
|
frozen atomic.Uint64 // Number of blocks already frozen
|
|
|
|
tail atomic.Uint64 // Number of the first stored item in the freezer
|
2019-05-17 01:45:05 +03:00
|
|
|
|
2021-10-25 17:24:27 +03:00
|
|
|
// This lock synchronizes writers and the truncate operation, as well as
|
|
|
|
// the "atomic" (batched) read operations.
|
|
|
|
writeLock sync.RWMutex
|
2021-09-07 13:31:17 +03:00
|
|
|
writeBatch *freezerBatch
|
|
|
|
|
2021-03-22 21:06:30 +03:00
|
|
|
readonly bool
|
2019-03-14 09:59:47 +03:00
|
|
|
tables map[string]*freezerTable // Data tables for storing everything
|
2023-02-23 10:11:50 +03:00
|
|
|
instanceLock *flock.Flock // File-system lock to prevent double opens
|
2022-05-06 14:28:42 +03:00
|
|
|
closeOnce sync.Once
|
2023-08-23 12:46:08 +03:00
|
|
|
offset uint64 // Starting BlockNumber in current freezer
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
|
2023-02-21 14:10:01 +03:00
|
|
|
// NewChainFreezer is a small utility method around NewFreezer that sets the
|
|
|
|
// default parameters for the chain storage.
|
2023-08-23 12:46:08 +03:00
|
|
|
func NewChainFreezer(datadir string, namespace string, readonly bool, offset uint64) (*Freezer, error) {
|
|
|
|
return NewFreezer(datadir, namespace, readonly, offset, freezerTableSize, chainFreezerNoSnappy)
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
|
|
|
|
2022-05-06 14:28:42 +03:00
|
|
|
// NewFreezer creates a freezer instance for maintaining immutable ordered
|
|
|
|
// data according to the given parameters.
|
2021-09-07 13:31:17 +03:00
|
|
|
//
|
|
|
|
// The 'tables' argument defines the data tables. If the value of a map
|
|
|
|
// entry is true, snappy compression is disabled for the table.
|
2024-03-22 17:37:47 +03:00
|
|
|
// additionTables indicates the new add tables for freezerDB, it has some special rules.
|
2023-08-23 12:46:08 +03:00
|
|
|
func NewFreezer(datadir string, namespace string, readonly bool, offset uint64, maxTableSize uint32, tables map[string]bool) (*Freezer, error) {
|
2019-03-08 16:56:20 +03:00
|
|
|
// Create the initial freezer object
|
|
|
|
var (
|
2019-09-10 14:39:07 +03:00
|
|
|
readMeter = metrics.NewRegisteredMeter(namespace+"ancient/read", nil)
|
|
|
|
writeMeter = metrics.NewRegisteredMeter(namespace+"ancient/write", nil)
|
|
|
|
sizeGauge = metrics.NewRegisteredGauge(namespace+"ancient/size", nil)
|
2019-03-08 16:56:20 +03:00
|
|
|
)
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
// Ensure the datadir is not a symbolic link if it exists.
|
|
|
|
if info, err := os.Lstat(datadir); !os.IsNotExist(err) {
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
log.Warn("Symbolic link ancient database is not supported", "path", datadir)
|
|
|
|
return nil, errSymlinkDatadir
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 10:11:50 +03:00
|
|
|
flockFile := filepath.Join(datadir, "FLOCK")
|
|
|
|
if err := os.MkdirAll(filepath.Dir(flockFile), 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
// Leveldb uses LOCK as the filelock filename. To prevent the
|
|
|
|
// name collision, we use FLOCK as the lock name.
|
2023-02-23 10:11:50 +03:00
|
|
|
lock := flock.New(flockFile)
|
2023-09-22 13:10:50 +03:00
|
|
|
tryLock := lock.TryLock
|
|
|
|
if readonly {
|
|
|
|
tryLock = lock.TryRLock
|
|
|
|
}
|
|
|
|
if locked, err := tryLock(); err != nil {
|
2019-03-14 09:59:47 +03:00
|
|
|
return nil, err
|
2023-02-23 10:11:50 +03:00
|
|
|
} else if !locked {
|
|
|
|
return nil, errors.New("locking failed")
|
2019-03-14 09:59:47 +03:00
|
|
|
}
|
2019-03-08 16:56:20 +03:00
|
|
|
// Open all the supported data tables
|
2022-05-06 14:28:42 +03:00
|
|
|
freezer := &Freezer{
|
2021-03-22 21:06:30 +03:00
|
|
|
readonly: readonly,
|
2019-03-14 09:59:47 +03:00
|
|
|
tables: make(map[string]*freezerTable),
|
|
|
|
instanceLock: lock,
|
2022-07-22 11:11:34 +03:00
|
|
|
offset: offset,
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
2021-09-07 13:31:17 +03:00
|
|
|
|
|
|
|
// Create the tables.
|
|
|
|
for name, disableSnappy := range tables {
|
2024-03-22 17:37:47 +03:00
|
|
|
var (
|
|
|
|
table *freezerTable
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if slices.Contains(additionTables, name) {
|
|
|
|
table, err = openAdditionTable(datadir, name, readMeter, writeMeter, sizeGauge, maxTableSize, disableSnappy, readonly)
|
|
|
|
} else {
|
|
|
|
table, err = newTable(datadir, name, readMeter, writeMeter, sizeGauge, maxTableSize, disableSnappy, readonly)
|
|
|
|
}
|
2019-03-08 16:56:20 +03:00
|
|
|
if err != nil {
|
|
|
|
for _, table := range freezer.tables {
|
|
|
|
table.Close()
|
|
|
|
}
|
2023-02-23 10:11:50 +03:00
|
|
|
lock.Unlock()
|
2019-03-08 16:56:20 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
freezer.tables[name] = table
|
|
|
|
}
|
2023-02-23 10:11:50 +03:00
|
|
|
var err error
|
2022-01-18 12:29:38 +03:00
|
|
|
if freezer.readonly {
|
|
|
|
// In readonly mode only validate, don't truncate.
|
|
|
|
// validate also sets `freezer.frozen`.
|
|
|
|
err = freezer.validate()
|
|
|
|
} else {
|
|
|
|
// Truncate all tables to common length.
|
|
|
|
err = freezer.repair()
|
|
|
|
}
|
|
|
|
if err != nil {
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
for _, table := range freezer.tables {
|
|
|
|
table.Close()
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
2023-02-23 10:11:50 +03:00
|
|
|
lock.Unlock()
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
return nil, err
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
2021-09-07 13:31:17 +03:00
|
|
|
|
2022-07-22 11:11:34 +03:00
|
|
|
// Some blocks in ancientDB may have already been frozen and been pruned, so adding the offset to
|
2024-01-12 10:36:26 +03:00
|
|
|
// represent the absolute number of blocks already frozen.
|
2023-08-23 12:46:08 +03:00
|
|
|
freezer.frozen.Add(offset)
|
|
|
|
freezer.tail.Add(offset)
|
2022-07-22 11:11:34 +03:00
|
|
|
|
2021-09-07 13:31:17 +03:00
|
|
|
// Create the write batch.
|
|
|
|
freezer.writeBatch = newFreezerBatch(freezer)
|
|
|
|
|
2023-09-08 11:36:16 +03:00
|
|
|
log.Info("Opened ancient database", "database", datadir, "readonly", readonly, "frozen", freezer.frozen.Load())
|
2019-03-08 16:56:20 +03:00
|
|
|
return freezer, nil
|
|
|
|
}
|
|
|
|
|
2024-03-22 17:37:47 +03:00
|
|
|
// openAdditionTable create table, it will auto create new files when it was first initialized
|
|
|
|
func openAdditionTable(datadir, name string, readMeter, writeMeter metrics.Meter, sizeGauge metrics.Gauge, maxTableSize uint32, disableSnappy, readonly bool) (*freezerTable, error) {
|
|
|
|
if readonly {
|
|
|
|
f, err := newTable(datadir, name, readMeter, writeMeter, sizeGauge, maxTableSize, disableSnappy, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = f.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newTable(datadir, name, readMeter, writeMeter, sizeGauge, maxTableSize, disableSnappy, readonly)
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:56:20 +03:00
|
|
|
// Close terminates the chain freezer, unmapping all the data files.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) Close() error {
|
2021-09-07 13:31:17 +03:00
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
2019-03-08 16:56:20 +03:00
|
|
|
var errs []error
|
2020-08-03 20:40:46 +03:00
|
|
|
f.closeOnce.Do(func() {
|
|
|
|
for _, table := range f.tables {
|
|
|
|
if err := table.Close(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 10:11:50 +03:00
|
|
|
if err := f.instanceLock.Unlock(); err != nil {
|
2019-03-08 16:56:20 +03:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2020-08-03 20:40:46 +03:00
|
|
|
})
|
2019-03-08 16:56:20 +03:00
|
|
|
if errs != nil {
|
|
|
|
return fmt.Errorf("%v", errs)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
// HasAncient returns an indicator whether the specified ancient data exists
|
|
|
|
// in the freezer.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) HasAncient(kind string, number uint64) (bool, error) {
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
if table := f.tables[kind]; table != nil {
|
2022-07-05 06:14:21 +03:00
|
|
|
return table.has(number - f.offset), nil
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ancient retrieves an ancient binary blob from the append-only immutable files.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) Ancient(kind string, number uint64) ([]byte, error) {
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
if table := f.tables[kind]; table != nil {
|
2022-07-05 06:14:21 +03:00
|
|
|
return table.Retrieve(number - f.offset)
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
return nil, errUnknownTable
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:24:27 +03:00
|
|
|
// AncientRange retrieves multiple items in sequence, starting from the index 'start'.
|
2021-08-13 11:51:01 +03:00
|
|
|
// It will return
|
2023-07-12 10:19:01 +03:00
|
|
|
// - at most 'count' items,
|
|
|
|
// - if maxBytes is specified: at least 1 item (even if exceeding the maxByteSize),
|
|
|
|
// but will otherwise return as many items as fit into maxByteSize.
|
|
|
|
// - if maxBytes is not specified, 'count' items will be returned if they are present.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) AncientRange(kind string, start, count, maxBytes uint64) ([][]byte, error) {
|
2021-08-13 11:51:01 +03:00
|
|
|
if table := f.tables[kind]; table != nil {
|
2024-05-16 07:07:13 +03:00
|
|
|
return table.RetrieveItems(start-f.offset, count, maxBytes)
|
2021-08-13 11:51:01 +03:00
|
|
|
}
|
|
|
|
return nil, errUnknownTable
|
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
// Ancients returns the length of the frozen items.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) Ancients() (uint64, error) {
|
2023-03-21 14:10:23 +03:00
|
|
|
return f.frozen.Load(), nil
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
|
2024-03-22 17:37:47 +03:00
|
|
|
func (f *Freezer) TableAncients(kind string) (uint64, error) {
|
|
|
|
f.writeLock.RLock()
|
|
|
|
defer f.writeLock.RUnlock()
|
2024-05-16 07:07:13 +03:00
|
|
|
return f.tables[kind].items.Load() + f.offset, nil
|
2024-03-22 17:37:47 +03:00
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// ItemAmountInAncient returns the actual length of current ancientDB.
|
2023-08-23 12:46:08 +03:00
|
|
|
func (f *Freezer) ItemAmountInAncient() (uint64, error) {
|
|
|
|
return f.frozen.Load() - atomic.LoadUint64(&f.offset), nil
|
2022-07-05 06:14:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AncientOffSet returns the offset of current ancientDB.
|
2023-08-23 12:46:08 +03:00
|
|
|
func (f *Freezer) AncientOffSet() uint64 {
|
2022-07-05 06:14:21 +03:00
|
|
|
return atomic.LoadUint64(&f.offset)
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:37:23 +03:00
|
|
|
// Tail returns the number of first stored item in the freezer.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) Tail() (uint64, error) {
|
2023-03-21 14:10:23 +03:00
|
|
|
return f.tail.Load(), nil
|
2022-03-10 11:37:23 +03:00
|
|
|
}
|
|
|
|
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
// AncientSize returns the ancient size of the specified category.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) AncientSize(kind string) (uint64, error) {
|
2021-09-07 13:31:17 +03:00
|
|
|
// This needs the write lock to avoid data races on table fields.
|
|
|
|
// Speed doesn't matter here, AncientSize is for debugging.
|
2021-10-25 17:24:27 +03:00
|
|
|
f.writeLock.RLock()
|
|
|
|
defer f.writeLock.RUnlock()
|
2021-09-07 13:31:17 +03:00
|
|
|
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
if table := f.tables[kind]; table != nil {
|
|
|
|
return table.size()
|
|
|
|
}
|
|
|
|
return 0, errUnknownTable
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:24:27 +03:00
|
|
|
// ReadAncients runs the given read operation while ensuring that no writes take place
|
|
|
|
// on the underlying freezer.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) ReadAncients(fn func(ethdb.AncientReaderOp) error) (err error) {
|
2021-10-25 17:24:27 +03:00
|
|
|
f.writeLock.RLock()
|
|
|
|
defer f.writeLock.RUnlock()
|
2022-05-06 14:28:42 +03:00
|
|
|
|
2021-10-25 17:24:27 +03:00
|
|
|
return fn(f)
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:31:17 +03:00
|
|
|
// ModifyAncients runs the given write operation.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) ModifyAncients(fn func(ethdb.AncientWriteOp) error) (writeSize int64, err error) {
|
2021-03-22 21:06:30 +03:00
|
|
|
if f.readonly {
|
2021-09-07 13:31:17 +03:00
|
|
|
return 0, errReadOnly
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
2021-09-07 13:31:17 +03:00
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
|
|
|
// Roll back all tables to the starting position in case of error.
|
2023-03-21 14:10:23 +03:00
|
|
|
prevItem := f.frozen.Load()
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2021-09-07 13:31:17 +03:00
|
|
|
// The write operation has failed. Go back to the previous item position.
|
|
|
|
for name, table := range f.tables {
|
2022-03-10 11:37:23 +03:00
|
|
|
err := table.truncateHead(prevItem)
|
2021-09-07 13:31:17 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Freezer table roll-back failed", "table", name, "index", prevItem, "err", err)
|
|
|
|
}
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-09-07 13:31:17 +03:00
|
|
|
|
|
|
|
f.writeBatch.reset()
|
|
|
|
if err := fn(f.writeBatch); err != nil {
|
|
|
|
return 0, err
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
2021-09-07 13:31:17 +03:00
|
|
|
item, writeSize, err := f.writeBatch.commit()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
f.frozen.Store(item)
|
2021-09-07 13:31:17 +03:00
|
|
|
return writeSize, nil
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
|
2022-03-10 11:37:23 +03:00
|
|
|
// TruncateHead discards any recent data above the provided threshold number.
|
2023-08-01 15:17:32 +03:00
|
|
|
// It returns the previous head number.
|
|
|
|
func (f *Freezer) TruncateHead(items uint64) (uint64, error) {
|
2021-03-22 21:06:30 +03:00
|
|
|
if f.readonly {
|
2023-08-01 15:17:32 +03:00
|
|
|
return 0, errReadOnly
|
2021-03-22 21:06:30 +03:00
|
|
|
}
|
2021-09-07 13:31:17 +03:00
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
2023-08-01 15:17:32 +03:00
|
|
|
oitems := f.frozen.Load()
|
|
|
|
if oitems <= items {
|
|
|
|
return oitems, nil
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
2024-03-22 17:37:47 +03:00
|
|
|
for kind, table := range f.tables {
|
|
|
|
err := table.truncateHead(items - f.offset)
|
|
|
|
if err == errTruncationBelowTail {
|
|
|
|
// This often happens in chain rewinds, but the blob table is special.
|
|
|
|
// It has the same head, but a different tail from other tables (like bodies, receipts).
|
|
|
|
// So if the chain is rewound to head below the blob's tail, it needs to reset again.
|
|
|
|
if kind != ChainFreezerBlobSidecarTable {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-03-28 11:35:39 +03:00
|
|
|
nt, err := table.resetItems(items - f.offset)
|
2024-03-22 17:37:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
f.tables[kind] = nt
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-08-01 15:17:32 +03:00
|
|
|
return 0, err
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
f.frozen.Store(items)
|
2023-08-01 15:17:32 +03:00
|
|
|
return oitems, nil
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
|
2022-03-10 11:37:23 +03:00
|
|
|
// TruncateTail discards any recent data below the provided threshold number.
|
2023-08-01 15:17:32 +03:00
|
|
|
func (f *Freezer) TruncateTail(tail uint64) (uint64, error) {
|
2022-03-10 11:37:23 +03:00
|
|
|
if f.readonly {
|
2023-08-01 15:17:32 +03:00
|
|
|
return 0, errReadOnly
|
2022-03-10 11:37:23 +03:00
|
|
|
}
|
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
2023-08-01 15:17:32 +03:00
|
|
|
old := f.tail.Load()
|
|
|
|
if old >= tail {
|
|
|
|
return old, nil
|
2022-03-10 11:37:23 +03:00
|
|
|
}
|
|
|
|
for _, table := range f.tables {
|
2023-08-23 12:46:08 +03:00
|
|
|
if err := table.truncateTail(tail - f.offset); err != nil {
|
2023-08-01 15:17:32 +03:00
|
|
|
return 0, err
|
2022-03-10 11:37:23 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
f.tail.Store(tail)
|
2023-08-01 15:17:32 +03:00
|
|
|
return old, nil
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
|
2020-07-29 22:53:59 +03:00
|
|
|
// Sync flushes all data tables to disk.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) Sync() error {
|
2019-03-08 16:56:20 +03:00
|
|
|
var errs []error
|
|
|
|
for _, table := range f.tables {
|
|
|
|
if err := table.Sync(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errs != nil {
|
|
|
|
return fmt.Errorf("%v", errs)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:10:31 +03:00
|
|
|
// validate checks that every table has the same boundary.
|
2022-01-18 12:29:38 +03:00
|
|
|
// Used instead of `repair` in readonly mode.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) validate() error {
|
2022-01-18 12:29:38 +03:00
|
|
|
if len(f.tables) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var (
|
2022-11-25 11:10:31 +03:00
|
|
|
head uint64
|
|
|
|
tail uint64
|
|
|
|
name string
|
2022-01-18 12:29:38 +03:00
|
|
|
)
|
2022-11-25 11:10:31 +03:00
|
|
|
// Hack to get boundary of any table
|
2022-01-18 12:29:38 +03:00
|
|
|
for kind, table := range f.tables {
|
2024-03-22 17:37:47 +03:00
|
|
|
// addition tables is special cases
|
|
|
|
if slices.Contains(additionTables, kind) {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
head = table.items.Load()
|
|
|
|
tail = table.itemHidden.Load()
|
2022-01-18 12:29:38 +03:00
|
|
|
name = kind
|
|
|
|
break
|
|
|
|
}
|
2022-11-25 11:10:31 +03:00
|
|
|
// Now check every table against those boundaries.
|
2022-01-18 12:29:38 +03:00
|
|
|
for kind, table := range f.tables {
|
2024-03-22 17:37:47 +03:00
|
|
|
// check addition tables, try to align with exist tables
|
|
|
|
if slices.Contains(additionTables, kind) {
|
|
|
|
// if the table is empty, just skip
|
|
|
|
if EmptyTable(table) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// otherwise, just align head
|
|
|
|
if head != table.items.Load() {
|
|
|
|
return fmt.Errorf("freezer tables %s and %s have differing head: %d != %d", kind, name, table.items.Load(), head)
|
|
|
|
}
|
|
|
|
if tail > table.itemHidden.Load() {
|
|
|
|
return fmt.Errorf("freezer tables %s and %s have differing tail: %d != %d", kind, name, table.itemHidden.Load(), tail)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
if head != table.items.Load() {
|
|
|
|
return fmt.Errorf("freezer tables %s and %s have differing head: %d != %d", kind, name, table.items.Load(), head)
|
2022-11-25 11:10:31 +03:00
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
if tail != table.itemHidden.Load() {
|
|
|
|
return fmt.Errorf("freezer tables %s and %s have differing tail: %d != %d", kind, name, table.itemHidden.Load(), tail)
|
2022-01-18 12:29:38 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
f.frozen.Store(head)
|
|
|
|
f.tail.Store(tail)
|
2022-01-18 12:29:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:31:17 +03:00
|
|
|
// repair truncates all data tables to the same length.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) repair() error {
|
2022-03-10 11:37:23 +03:00
|
|
|
var (
|
|
|
|
head = uint64(math.MaxUint64)
|
|
|
|
tail = uint64(0)
|
|
|
|
)
|
2024-03-22 17:37:47 +03:00
|
|
|
for kind, table := range f.tables {
|
|
|
|
// addition tables only align head
|
|
|
|
if slices.Contains(additionTables, kind) {
|
|
|
|
if EmptyTable(table) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
items := table.items.Load()
|
|
|
|
if head > items {
|
|
|
|
head = items
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
items := table.items.Load()
|
2022-03-10 11:37:23 +03:00
|
|
|
if head > items {
|
|
|
|
head = items
|
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
hidden := table.itemHidden.Load()
|
2022-03-10 11:37:23 +03:00
|
|
|
if hidden > tail {
|
|
|
|
tail = hidden
|
2021-09-07 13:31:17 +03:00
|
|
|
}
|
|
|
|
}
|
2024-03-22 17:37:47 +03:00
|
|
|
for kind, table := range f.tables {
|
|
|
|
// try to align with exist tables, skip empty table
|
|
|
|
if slices.Contains(additionTables, kind) && EmptyTable(table) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := table.truncateHead(head)
|
|
|
|
if err == errTruncationBelowTail {
|
|
|
|
// This often happens in chain rewinds, but the blob table is special.
|
|
|
|
// It has the same head, but a different tail from other tables (like bodies, receipts).
|
|
|
|
// So if the chain is rewound to head below the blob's tail, it needs to reset again.
|
|
|
|
if kind != ChainFreezerBlobSidecarTable {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-28 11:35:39 +03:00
|
|
|
nt, err := table.resetItems(head)
|
2024-03-22 17:37:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.tables[kind] = nt
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-09-07 13:31:17 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-03-10 11:37:23 +03:00
|
|
|
if err := table.truncateTail(tail); err != nil {
|
2021-09-07 13:31:17 +03:00
|
|
|
return err
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 14:10:23 +03:00
|
|
|
f.frozen.Store(head)
|
|
|
|
f.tail.Store(tail)
|
2021-09-07 13:31:17 +03:00
|
|
|
return nil
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
|
|
|
|
// delete leveldb data that save to ancientdb, split from func freeze
|
|
|
|
func gcKvStore(db ethdb.KeyValueStore, ancients []common.Hash, first uint64, frozen uint64, start time.Time) {
|
|
|
|
// Wipe out all data from the active database
|
|
|
|
batch := db.NewBatch()
|
|
|
|
for i := 0; i < len(ancients); i++ {
|
|
|
|
// Always keep the genesis block in active database
|
|
|
|
if blockNumber := first + uint64(i); blockNumber != 0 {
|
|
|
|
DeleteBlockWithoutNumber(batch, ancients[i], blockNumber)
|
|
|
|
DeleteCanonicalHash(batch, blockNumber)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
log.Crit("Failed to delete frozen canonical blocks", "err", err)
|
|
|
|
}
|
|
|
|
batch.Reset()
|
|
|
|
|
|
|
|
// Wipe out side chains also and track dangling side chians
|
|
|
|
var dangling []common.Hash
|
|
|
|
for number := first; number < frozen; number++ {
|
|
|
|
// Always keep the genesis block in active database
|
|
|
|
if number != 0 {
|
|
|
|
dangling = ReadAllHashes(db, number)
|
|
|
|
for _, hash := range dangling {
|
|
|
|
log.Trace("Deleting side chain", "number", number, "hash", hash)
|
|
|
|
DeleteBlock(batch, hash, number)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
log.Crit("Failed to delete frozen side blocks", "err", err)
|
|
|
|
}
|
|
|
|
batch.Reset()
|
|
|
|
|
|
|
|
// Log something friendly for the user
|
|
|
|
context := []interface{}{
|
|
|
|
"blocks", frozen - first, "elapsed", common.PrettyDuration(time.Since(start)), "number", frozen - 1,
|
|
|
|
}
|
|
|
|
if n := len(ancients); n > 0 {
|
|
|
|
context = append(context, []interface{}{"hash", ancients[n-1]}...)
|
|
|
|
}
|
|
|
|
log.Info("Deep froze chain segment", context...)
|
|
|
|
}
|
2023-08-23 12:46:08 +03:00
|
|
|
|
2022-03-23 22:57:32 +03:00
|
|
|
// convertLegacyFn takes a raw freezer entry in an older format and
|
|
|
|
// returns it in the new format.
|
|
|
|
type convertLegacyFn = func([]byte) ([]byte, error)
|
|
|
|
|
|
|
|
// MigrateTable processes the entries in a given table in sequence
|
|
|
|
// converting them to a new format if they're of an old format.
|
2022-05-06 14:28:42 +03:00
|
|
|
func (f *Freezer) MigrateTable(kind string, convert convertLegacyFn) error {
|
2022-03-23 22:57:32 +03:00
|
|
|
if f.readonly {
|
|
|
|
return errReadOnly
|
|
|
|
}
|
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
|
|
|
table, ok := f.tables[kind]
|
|
|
|
if !ok {
|
|
|
|
return errUnknownTable
|
|
|
|
}
|
|
|
|
// forEach iterates every entry in the table serially and in order, calling `fn`
|
|
|
|
// with the item as argument. If `fn` returns an error the iteration stops
|
|
|
|
// and that error will be returned.
|
|
|
|
forEach := func(t *freezerTable, offset uint64, fn func(uint64, []byte) error) error {
|
|
|
|
var (
|
2023-03-21 14:10:23 +03:00
|
|
|
items = t.items.Load()
|
2022-03-23 22:57:32 +03:00
|
|
|
batchSize = uint64(1024)
|
|
|
|
maxBytes = uint64(1024 * 1024)
|
|
|
|
)
|
|
|
|
for i := offset; i < items; {
|
|
|
|
if i+batchSize > items {
|
|
|
|
batchSize = items - i
|
|
|
|
}
|
|
|
|
data, err := t.RetrieveItems(i, batchSize, maxBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for j, item := range data {
|
|
|
|
if err := fn(i+uint64(j), item); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += uint64(len(data))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// TODO(s1na): This is a sanity-check since as of now no process does tail-deletion. But the migration
|
|
|
|
// process assumes no deletion at tail and needs to be modified to account for that.
|
2023-03-21 14:10:23 +03:00
|
|
|
if table.itemOffset.Load() > 0 || table.itemHidden.Load() > 0 {
|
2023-05-25 15:24:09 +03:00
|
|
|
return errors.New("migration not supported for tail-deleted freezers")
|
2022-03-23 22:57:32 +03:00
|
|
|
}
|
|
|
|
ancientsPath := filepath.Dir(table.index.Name())
|
|
|
|
// Set up new dir for the migrated table, the content of which
|
|
|
|
// we'll at the end move over to the ancients dir.
|
|
|
|
migrationPath := filepath.Join(ancientsPath, "migration")
|
2022-08-08 12:08:36 +03:00
|
|
|
newTable, err := newFreezerTable(migrationPath, kind, table.noCompression, false)
|
2022-03-23 22:57:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var (
|
2023-08-23 12:46:08 +03:00
|
|
|
batch = newTable.newBatch(f.offset)
|
2022-03-23 22:57:32 +03:00
|
|
|
out []byte
|
|
|
|
start = time.Now()
|
|
|
|
logged = time.Now()
|
2023-03-21 14:10:23 +03:00
|
|
|
offset = newTable.items.Load()
|
2022-03-23 22:57:32 +03:00
|
|
|
)
|
|
|
|
if offset > 0 {
|
|
|
|
log.Info("found previous migration attempt", "migrated", offset)
|
|
|
|
}
|
|
|
|
// Iterate through entries and transform them
|
|
|
|
if err := forEach(table, offset, func(i uint64, blob []byte) error {
|
|
|
|
if i%10000 == 0 && time.Since(logged) > 16*time.Second {
|
|
|
|
log.Info("Processing legacy elements", "count", i, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
logged = time.Now()
|
|
|
|
}
|
|
|
|
out, err = convert(blob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := batch.AppendRaw(i, out); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := batch.commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("Replacing old table files with migrated ones", "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
// Release and delete old table files. Note this won't
|
|
|
|
// delete the index file.
|
|
|
|
table.releaseFilesAfter(0, true)
|
|
|
|
|
|
|
|
if err := newTable.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-17 15:27:04 +03:00
|
|
|
files, err := os.ReadDir(migrationPath)
|
2022-03-23 22:57:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Move migrated files to ancients dir.
|
|
|
|
for _, f := range files {
|
|
|
|
// This will replace the old index file as a side-effect.
|
|
|
|
if err := os.Rename(filepath.Join(migrationPath, f.Name()), filepath.Join(ancientsPath, f.Name())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Delete by now empty dir.
|
|
|
|
if err := os.Remove(migrationPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-22 17:37:47 +03:00
|
|
|
|
2024-03-28 11:35:39 +03:00
|
|
|
// TruncateTableTail will truncate certain table to new tail
|
|
|
|
func (f *Freezer) TruncateTableTail(kind string, tail uint64) (uint64, error) {
|
|
|
|
if f.readonly {
|
|
|
|
return 0, errReadOnly
|
|
|
|
}
|
|
|
|
|
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
|
|
|
|
|
|
|
if !slices.Contains(additionTables, kind) {
|
|
|
|
return 0, errors.New("only new added table could be truncated independently")
|
|
|
|
}
|
|
|
|
if tail < f.offset {
|
|
|
|
return 0, errors.New("the input tail&head is less than offset")
|
|
|
|
}
|
|
|
|
t, exist := f.tables[kind]
|
|
|
|
if !exist {
|
|
|
|
return 0, errors.New("you reset a non-exist table")
|
|
|
|
}
|
|
|
|
|
|
|
|
old := t.itemHidden.Load() + f.offset
|
|
|
|
if err := t.truncateTail(tail - f.offset); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return old, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetTable will reset certain table with new start point
|
2024-03-22 17:37:47 +03:00
|
|
|
// only used for ChainFreezerBlobSidecarTable now
|
2024-03-28 11:35:39 +03:00
|
|
|
func (f *Freezer) ResetTable(kind string, startAt uint64, onlyEmpty bool) error {
|
2024-03-22 17:37:47 +03:00
|
|
|
if f.readonly {
|
|
|
|
return errReadOnly
|
|
|
|
}
|
|
|
|
|
|
|
|
f.writeLock.Lock()
|
|
|
|
defer f.writeLock.Unlock()
|
2024-03-28 11:35:39 +03:00
|
|
|
|
|
|
|
t, exist := f.tables[kind]
|
|
|
|
if !exist {
|
2024-03-22 17:37:47 +03:00
|
|
|
return errors.New("you reset a non-exist table")
|
|
|
|
}
|
2024-03-28 11:35:39 +03:00
|
|
|
|
2024-03-22 17:37:47 +03:00
|
|
|
// if you reset a non empty table just skip
|
2024-03-28 11:35:39 +03:00
|
|
|
if onlyEmpty && !EmptyTable(t) {
|
2024-03-22 17:37:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-28 11:35:39 +03:00
|
|
|
if err := f.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nt, err := t.resetItems(startAt - f.offset)
|
2024-03-22 17:37:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.tables[kind] = nt
|
|
|
|
|
2024-03-28 11:35:39 +03:00
|
|
|
// repair all tables with same tail & head
|
2024-03-22 17:37:47 +03:00
|
|
|
if err := f.repair(); err != nil {
|
|
|
|
for _, table := range f.tables {
|
|
|
|
table.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.frozen.Add(f.offset)
|
|
|
|
f.tail.Add(f.offset)
|
|
|
|
f.writeBatch = newFreezerBatch(f)
|
2024-04-10 09:31:23 +03:00
|
|
|
log.Debug("Reset Table", "kind", kind, "tail", f.tables[kind].itemHidden.Load(), "frozen", f.tables[kind].items.Load())
|
2024-03-22 17:37:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func EmptyTable(t *freezerTable) bool {
|
|
|
|
return t.items.Load() == 0
|
|
|
|
}
|