Skip to content

Commit b3addca

Browse files
yihuangmergify[bot]
authored andcommitted
fix: snapshotter's failure is not propogated (#16588)
(cherry picked from commit aeccbc9)
1 parent c06d8d7 commit b3addca

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

store/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
4141

4242
* [#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
4343

44+
### Bug Fixes
45+
46+
* [#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).
47+
4448
## [v0.1.0-alpha.1](https://github.com/cosmos/cosmos-sdk/releases/tag/store%2Fv0.1.0-alpha.1) - 2023-03-17
4549

4650
### Features

store/snapshots/chunk.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ func (w *ChunkWriter) Close() error {
5959
// CloseWithError closes the writer and sends an error to the reader.
6060
func (w *ChunkWriter) CloseWithError(err error) {
6161
if !w.closed {
62+
if w.pipe == nil {
63+
// create a dummy pipe just to propagate the error to the reader, it always returns nil
64+
_ = w.chunk()
65+
}
6266
w.closed = true
6367
close(w.ch)
64-
if w.pipe != nil {
65-
_ = w.pipe.CloseWithError(err) // CloseWithError always returns nil
66-
}
68+
_ = w.pipe.CloseWithError(err) // CloseWithError always returns nil
6769
}
6870
}
6971

store/snapshots/helpers_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,38 @@ func (m *mockSnapshotter) SetSnapshotInterval(snapshotInterval uint64) {
171171
m.snapshotInterval = snapshotInterval
172172
}
173173

174+
type mockErrorSnapshotter struct{}
175+
176+
var _ snapshottypes.Snapshotter = (*mockErrorSnapshotter)(nil)
177+
178+
func (m *mockErrorSnapshotter) Snapshot(height uint64, protoWriter protoio.Writer) error {
179+
return errors.New("mock snapshot error")
180+
}
181+
182+
func (m *mockErrorSnapshotter) Restore(
183+
height uint64, format uint32, protoReader protoio.Reader,
184+
) (snapshottypes.SnapshotItem, error) {
185+
return snapshottypes.SnapshotItem{}, errors.New("mock restore error")
186+
}
187+
188+
func (m *mockErrorSnapshotter) SnapshotFormat() uint32 {
189+
return snapshottypes.CurrentFormat
190+
}
191+
192+
func (m *mockErrorSnapshotter) SupportedFormats() []uint32 {
193+
return []uint32{snapshottypes.CurrentFormat}
194+
}
195+
196+
func (m *mockErrorSnapshotter) PruneSnapshotHeight(height int64) {
197+
}
198+
199+
func (m *mockErrorSnapshotter) GetSnapshotInterval() uint64 {
200+
return 0
201+
}
202+
203+
func (m *mockErrorSnapshotter) SetSnapshotInterval(snapshotInterval uint64) {
204+
}
205+
174206
// setupBusyManager creates a manager with an empty store that is busy creating a snapshot at height 1.
175207
// The snapshot will complete when the returned closer is called.
176208
func setupBusyManager(t *testing.T) *snapshots.Manager {

store/snapshots/manager_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"testing"
66

7+
db "github.com/cosmos/cosmos-db"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
910

@@ -243,3 +244,13 @@ func TestManager_Restore(t *testing.T) {
243244
})
244245
require.NoError(t, err)
245246
}
247+
248+
func TestManager_TakeError(t *testing.T) {
249+
snapshotter := &mockErrorSnapshotter{}
250+
store, err := snapshots.NewStore(db.NewMemDB(), GetTempDir(t))
251+
require.NoError(t, err)
252+
manager := snapshots.NewManager(store, opts, snapshotter, nil, log.NewNopLogger())
253+
254+
_, err = manager.Create(1)
255+
require.Error(t, err)
256+
}

0 commit comments

Comments
 (0)