-
Notifications
You must be signed in to change notification settings - Fork 20.9k
core/rawdb, cmd, ethdb, eth: implement freezer tail deletion #23954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
716adb2
core/rawdb, cmd, ethdb, eth: implement freezer tail deletion
rjl493456442 539c9df
core/rawdb: address comments from martin and sina
rjl493456442 964ae61
core/rawdb: fixes cornercase in tail deletion
rjl493456442 a44239f
core/rawdb: separate metadata into a standalone file
rjl493456442 267eaa7
core/rawdb: remove unused code
rjl493456442 3161d7e
core/rawdb: add random test
rjl493456442 991bba0
core/rawdb: polish code
rjl493456442 62333e8
core/rawdb: fsync meta file before manipulating the index
rjl493456442 b23cdeb
core/rawdb: fix typo
rjl493456442 940cb9d
core/rawdb: address comments
rjl493456442 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2022 The go-ethereum Authors | ||
// 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 ( | ||
"io" | ||
"os" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
) | ||
|
||
const freezerVersion = 1 // The initial version tag of freezer table metadata | ||
|
||
// freezerTableMeta wraps all the metadata of the freezer table. | ||
type freezerTableMeta struct { | ||
// Version is the versioning descriptor of the freezer table. | ||
Version uint16 | ||
|
||
// VirtualTail indicates how many items have been marked as deleted. | ||
// Its value is equal to the number of items removed from the table | ||
// plus the number of items hidden in the table, so it should never | ||
// be lower than the "actual tail". | ||
VirtualTail uint64 | ||
} | ||
|
||
// newMetadata initializes the metadata object with the given virtual tail. | ||
func newMetadata(tail uint64) *freezerTableMeta { | ||
return &freezerTableMeta{ | ||
Version: freezerVersion, | ||
VirtualTail: tail, | ||
} | ||
} | ||
|
||
// readMetadata reads the metadata of the freezer table from the | ||
// given metadata file. | ||
func readMetadata(file *os.File) (*freezerTableMeta, error) { | ||
_, err := file.Seek(0, io.SeekStart) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var meta freezerTableMeta | ||
if err := rlp.Decode(file, &meta); err != nil { | ||
return nil, err | ||
} | ||
return &meta, nil | ||
} | ||
|
||
// writeMetadata writes the metadata of the freezer table into the | ||
// given metadata file. | ||
func writeMetadata(file *os.File, meta *freezerTableMeta) error { | ||
_, err := file.Seek(0, io.SeekStart) | ||
if err != nil { | ||
return err | ||
} | ||
return rlp.Encode(file, meta) | ||
} | ||
|
||
// loadMetadata loads the metadata from the given metadata file. | ||
// Initializes the metadata file with the given "actual tail" if | ||
// it's empty. | ||
func loadMetadata(file *os.File, tail uint64) (*freezerTableMeta, error) { | ||
stat, err := file.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Write the metadata with the given actual tail into metadata file | ||
// if it's non-existent. There are two possible scenarios here: | ||
// - the freezer table is empty | ||
// - the freezer table is legacy | ||
// In both cases, write the meta into the file with the actual tail | ||
// as the virtual tail. | ||
if stat.Size() == 0 { | ||
m := newMetadata(tail) | ||
if err := writeMetadata(file, m); err != nil { | ||
return nil, err | ||
} | ||
return m, nil | ||
} | ||
m, err := readMetadata(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Update the virtual tail with the given actual tail if it's even | ||
// lower than it. Theoretically it shouldn't happen at all, print | ||
// a warning here. | ||
if m.VirtualTail < tail { | ||
log.Warn("Updated virtual tail", "have", m.VirtualTail, "now", tail) | ||
m.VirtualTail = tail | ||
if err := writeMetadata(file, m); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return m, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2022 The go-ethereum Authors | ||
// 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 ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestReadWriteFreezerTableMeta(t *testing.T) { | ||
f, err := ioutil.TempFile(os.TempDir(), "*") | ||
if err != nil { | ||
t.Fatalf("Failed to create file %v", err) | ||
} | ||
err = writeMetadata(f, newMetadata(100)) | ||
if err != nil { | ||
t.Fatalf("Failed to write metadata %v", err) | ||
} | ||
meta, err := readMetadata(f) | ||
if err != nil { | ||
t.Fatalf("Failed to read metadata %v", err) | ||
} | ||
if meta.Version != freezerVersion { | ||
t.Fatalf("Unexpected version field") | ||
} | ||
if meta.VirtualTail != uint64(100) { | ||
t.Fatalf("Unexpected virtual tail field") | ||
} | ||
} | ||
|
||
func TestInitializeFreezerTableMeta(t *testing.T) { | ||
f, err := ioutil.TempFile(os.TempDir(), "*") | ||
if err != nil { | ||
t.Fatalf("Failed to create file %v", err) | ||
} | ||
meta, err := loadMetadata(f, uint64(100)) | ||
if err != nil { | ||
t.Fatalf("Failed to read metadata %v", err) | ||
} | ||
if meta.Version != freezerVersion { | ||
t.Fatalf("Unexpected version field") | ||
} | ||
if meta.VirtualTail != uint64(100) { | ||
t.Fatalf("Unexpected virtual tail field") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.