Skip to content
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

fix: snapshotter's failure is not propogated #16588

Merged
merged 5 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* [#16321](https://github.com/cosmos/cosmos-sdk/pull/16321) QueryInterface defines its own request and response types instead of relying on comet/abci & returns an error

### Bug Fixes

* [#16588](https://github.com/cosmos/cosmos-sdk/pull/16588) Propogate the Snapshotter's failure to the caller, (it will create a empty snapshot silently before).

## [v0.1.0-alpha.1](https://github.com/cosmos/cosmos-sdk/releases/tag/store%2Fv0.1.0-alpha.1) - 2023-03-17

### Features
Expand Down
8 changes: 5 additions & 3 deletions store/snapshots/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ func (w *ChunkWriter) Close() error {
// CloseWithError closes the writer and sends an error to the reader.
func (w *ChunkWriter) CloseWithError(err error) {
if !w.closed {
if w.pipe == nil {
// create a dummy pipe just to propagate the error to the reader, it always returns nil
_ = w.chunk()
}
w.closed = true
close(w.ch)
if w.pipe != nil {
_ = w.pipe.CloseWithError(err) // CloseWithError always returns nil
}
_ = w.pipe.CloseWithError(err) // CloseWithError always returns nil
}
}

Expand Down
32 changes: 32 additions & 0 deletions store/snapshots/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,38 @@ func (m *mockSnapshotter) SetSnapshotInterval(snapshotInterval uint64) {
m.snapshotInterval = snapshotInterval
}

type mockErrorSnapshotter struct{}

var _ snapshottypes.Snapshotter = (*mockErrorSnapshotter)(nil)

func (m *mockErrorSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) error {
return errors.New("mock snapshot error")
}

func (m *mockErrorSnapshotter) Restore(
height uint64, format uint32, protoReader protoio.Reader,
) (snapshottypes.SnapshotItem, error) {
return snapshottypes.SnapshotItem{}, errors.New("mock restore error")
}

func (m *mockErrorSnapshotter) SnapshotFormat() uint32 {
return snapshottypes.CurrentFormat
}

func (m *mockErrorSnapshotter) SupportedFormats() []uint32 {
return []uint32{snapshottypes.CurrentFormat}
}

func (m *mockErrorSnapshotter) PruneSnapshotHeight(height int64) {
}

func (m *mockErrorSnapshotter) GetSnapshotInterval() uint64 {
return 0
}

func (m *mockErrorSnapshotter) SetSnapshotInterval(snapshotInterval uint64) {
}

// setupBusyManager creates a manager with an empty store that is busy creating a snapshot at height 1.
// The snapshot will complete when the returned closer is called.
func setupBusyManager(t *testing.T) *snapshots.Manager {
Expand Down
11 changes: 11 additions & 0 deletions store/snapshots/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"testing"

db "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -243,3 +244,13 @@ func TestManager_Restore(t *testing.T) {
})
require.NoError(t, err)
}

func TestManager_TakeError(t *testing.T) {
snapshotter := &mockErrorSnapshotter{}
store, err := snapshots.NewStore(db.NewMemDB(), GetTempDir(t))
require.NoError(t, err)
manager := snapshots.NewManager(store, opts, snapshotter, nil, log.NewNopLogger())

_, err = manager.Create(1)
require.Error(t, err)
}