cosmos-sdk/snapshots/chunk.go

183 lines
3.9 KiB
Go
Raw Normal View History

package snapshots
import (
feat: ADR-040: add state sync for v2 store (#10794) ## Description Closes: #10705 State Sync for V2 Store (ADR-40) --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-03-03 22:05:23 -08:00
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
"io"
feat: ADR-040: add state sync for v2 store (#10794) ## Description Closes: #10705 State Sync for V2 Store (ADR-40) --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-03-03 22:05:23 -08:00
"math"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// ChunkWriter reads an input stream, splits it into fixed-size chunks, and writes them to a
// sequence of io.ReadClosers via a channel.
type ChunkWriter struct {
ch chan<- io.ReadCloser
pipe *io.PipeWriter
chunkSize uint64
written uint64
closed bool
}
// NewChunkWriter creates a new ChunkWriter. If chunkSize is 0, no chunking will be done.
func NewChunkWriter(ch chan<- io.ReadCloser, chunkSize uint64) *ChunkWriter {
return &ChunkWriter{
ch: ch,
chunkSize: chunkSize,
}
}
// chunk creates a new chunk.
func (w *ChunkWriter) chunk() error {
if w.pipe != nil {
err := w.pipe.Close()
if err != nil {
return err
}
}
pr, pw := io.Pipe()
w.ch <- pr
w.pipe = pw
w.written = 0
return nil
}
// Close implements io.Closer.
func (w *ChunkWriter) Close() error {
if !w.closed {
w.closed = true
close(w.ch)
var err error
if w.pipe != nil {
err = w.pipe.Close()
}
return err
}
return nil
}
// CloseWithError closes the writer and sends an error to the reader.
func (w *ChunkWriter) CloseWithError(err error) {
if !w.closed {
w.closed = true
close(w.ch)
if w.pipe != nil {
w.pipe.CloseWithError(err)
}
}
}
// Write implements io.Writer.
func (w *ChunkWriter) Write(data []byte) (int, error) {
if w.closed {
return 0, sdkerrors.Wrap(sdkerrors.ErrLogic, "cannot write to closed ChunkWriter")
}
nTotal := 0
for len(data) > 0 {
if w.pipe == nil || (w.written >= w.chunkSize && w.chunkSize > 0) {
err := w.chunk()
if err != nil {
return nTotal, err
}
}
var writeSize uint64
if w.chunkSize == 0 {
writeSize = uint64(len(data))
} else {
writeSize = w.chunkSize - w.written
}
if writeSize > uint64(len(data)) {
writeSize = uint64(len(data))
}
n, err := w.pipe.Write(data[:writeSize])
w.written += uint64(n)
nTotal += n
if err != nil {
return nTotal, err
}
data = data[writeSize:]
}
return nTotal, nil
}
// ChunkReader reads chunks from a channel of io.ReadClosers and outputs them as an io.Reader
type ChunkReader struct {
ch <-chan io.ReadCloser
reader io.ReadCloser
}
// NewChunkReader creates a new ChunkReader.
func NewChunkReader(ch <-chan io.ReadCloser) *ChunkReader {
return &ChunkReader{ch: ch}
}
// next fetches the next chunk from the channel, or returns io.EOF if there are no more chunks.
func (r *ChunkReader) next() error {
reader, ok := <-r.ch
if !ok {
return io.EOF
}
r.reader = reader
return nil
}
// Close implements io.ReadCloser.
func (r *ChunkReader) Close() error {
var err error
if r.reader != nil {
err = r.reader.Close()
r.reader = nil
}
for reader := range r.ch {
if e := reader.Close(); e != nil && err == nil {
err = e
}
}
return err
}
// Read implements io.Reader.
func (r *ChunkReader) Read(p []byte) (int, error) {
if r.reader == nil {
err := r.next()
if err != nil {
return 0, err
}
}
n, err := r.reader.Read(p)
if err == io.EOF {
err = r.reader.Close()
r.reader = nil
if err != nil {
return 0, err
}
return r.Read(p)
}
return n, err
}
// DrainChunks drains and closes all remaining chunks from a chunk channel.
func DrainChunks(chunks <-chan io.ReadCloser) {
for chunk := range chunks {
_ = chunk.Close()
}
}
feat: ADR-040: add state sync for v2 store (#10794) ## Description Closes: #10705 State Sync for V2 Store (ADR-40) --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2022-03-03 22:05:23 -08:00
// ValidRestoreHeight will check height is valid for snapshot restore or not
func ValidRestoreHeight(format uint32, height uint64) error {
if format != snapshottypes.CurrentFormat {
return sdkerrors.Wrapf(snapshottypes.ErrUnknownFormat, "format %v", format)
}
if height == 0 {
return sdkerrors.Wrap(sdkerrors.ErrLogic, "cannot restore snapshot at height 0")
}
if height > uint64(math.MaxInt64) {
return sdkerrors.Wrapf(snapshottypes.ErrInvalidMetadata,
"snapshot height %v cannot exceed %v", height, int64(math.MaxInt64))
}
return nil
}