Skip to content

Commit 9e7798c

Browse files
headerfs: fail gracefully on write
This commit adds recovery mechanism from failures that may happen during headers I/O write
1 parent dff3e02 commit 9e7798c

File tree

2 files changed

+304
-1
lines changed

2 files changed

+304
-1
lines changed

headerfs/file.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package headerfs
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67

78
"github.com/btcsuite/btcd/chaincfg/chainhash"
89
"github.com/btcsuite/btcd/wire"
@@ -16,10 +17,32 @@ type ErrHeaderNotFound struct {
1617

1718
// appendRaw appends a new raw header to the end of the flat file.
1819
func (h *headerStore) appendRaw(header []byte) error {
19-
if _, err := h.file.Write(header); err != nil {
20+
// Get current file position before writing. We'll use this position to
21+
// revert to if the write fails partially.
22+
currentPos, err := h.file.Seek(0, io.SeekCurrent)
23+
if err != nil {
2024
return err
2125
}
2226

27+
n, err := h.file.Write(header)
28+
if err != nil {
29+
// If we wrote some bytes but not all (partial write),
30+
// truncate the file back to its original size to maintain
31+
// consistency. This removes the partial/corrupt header.
32+
if n > 0 {
33+
truncErr := h.file.Truncate(currentPos)
34+
if truncErr != nil {
35+
return fmt.Errorf("failed to write header "+
36+
"type %s: partial write (%d bytes), "+
37+
"write error: %w, truncate error: %v",
38+
h.indexType, n, err, truncErr)
39+
}
40+
}
41+
42+
return fmt.Errorf("failed to write header type %s: write "+
43+
"error: %w", h.indexType, err)
44+
}
45+
2346
return nil
2447
}
2548

headerfs/file_test.go

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
package headerfs
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"os"
9+
"strings"
10+
"testing"
11+
)
12+
13+
// TestAppendRow verifies that headerStore.appendRaw correctly appends data to
14+
// the file, handles full and partial write errors, and properly recovers from
15+
// failures.
16+
func TestAppendRow(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
initialData []byte
20+
headerToWrite []byte
21+
writeFn func([]byte, File) (int, error)
22+
truncFn func(int64, File) error
23+
expected []byte
24+
wantErr bool
25+
errMsg string
26+
}{
27+
{
28+
name: "ValidWrite AppendsData",
29+
initialData: []byte{0x01, 0x02, 0x03},
30+
headerToWrite: []byte{0x04, 0x05, 0x06},
31+
expected: []byte{
32+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
33+
},
34+
},
35+
{
36+
name: "WriteError NoData Preserved",
37+
initialData: []byte{0x01, 0x02, 0x03},
38+
headerToWrite: []byte{0x04, 0x05, 0x06},
39+
writeFn: func(p []byte, _ File) (int, error) {
40+
return 0, errors.New("simulated write failure")
41+
},
42+
expected: []byte{0x01, 0x02, 0x03},
43+
wantErr: true,
44+
errMsg: "simulated write failure",
45+
},
46+
{
47+
name: "PartialWrite MidwayError Rollback",
48+
initialData: []byte{0x01, 0x02, 0x03},
49+
headerToWrite: []byte{0x04, 0x05, 0x06},
50+
writeFn: func(p []byte, file File) (int, error) {
51+
// Mock a partial write - write the first two
52+
// bytes.
53+
n, err := file.Write(p[:2])
54+
if err != nil {
55+
return n, err
56+
}
57+
58+
return n, errors.New("simulated partial " +
59+
"write failure")
60+
},
61+
expected: []byte{0x01, 0x02, 0x03},
62+
wantErr: true,
63+
errMsg: "simulated partial write failure",
64+
},
65+
{
66+
name: "TruncateError CompoundFail",
67+
initialData: []byte{0x01, 0x02, 0x03},
68+
headerToWrite: []byte{0x04, 0x05, 0x06},
69+
writeFn: func(p []byte, file File) (int, error) {
70+
// Mock a partial write - write just the first
71+
// byte.
72+
n, err := file.Write(p[:1])
73+
if err != nil {
74+
return n, err
75+
}
76+
77+
return n, errors.New("simulated partial " +
78+
"write failure")
79+
},
80+
truncFn: func(size int64, _ File) error {
81+
return errors.New("simulated truncate failure")
82+
},
83+
expected: []byte{0x01, 0x02, 0x03, 0x04},
84+
wantErr: true,
85+
errMsg: fmt.Sprintf("failed to write header type %s: "+
86+
"partial write (1 bytes), write error: "+
87+
"simulated partial write failure, truncate "+
88+
"error: simulated truncate failure", Block),
89+
},
90+
{
91+
name: "PartialWrite TruncateFail Unrecovered",
92+
initialData: []byte{0x01, 0x02, 0x03},
93+
headerToWrite: []byte{0x04, 0x05, 0x06},
94+
writeFn: func(p []byte, file File) (int, error) {
95+
// Mock a partial write - write the first two
96+
// bytes.
97+
n, err := file.Write(p[:2])
98+
if err != nil {
99+
return n, err
100+
}
101+
102+
return n, errors.New("simulated partial " +
103+
"write failure")
104+
},
105+
truncFn: func(size int64, file File) error {
106+
// Simulate an incomplete truncation: shrink the
107+
// file by just one byte, leaving part of the
108+
// partial write data in place in other words
109+
// not fully removing the partially written
110+
// header from the end of the file.
111+
err := file.Truncate(4)
112+
if err != nil {
113+
return err
114+
}
115+
116+
return errors.New("simulated truncate failure")
117+
},
118+
expected: []byte{0x01, 0x02, 0x03, 0x04},
119+
wantErr: true,
120+
errMsg: fmt.Sprintf("failed to write header type "+
121+
"%s: partial write (2 bytes), write error: "+
122+
"simulated partial write failure, truncate "+
123+
"error: simulated truncate failure", Block),
124+
},
125+
{
126+
name: "NormalWrite ValidHeader DataAppended",
127+
initialData: []byte{},
128+
headerToWrite: []byte{0x01, 0x02, 0x03},
129+
expected: []byte{0x01, 0x02, 0x03},
130+
},
131+
}
132+
133+
for _, test := range tests {
134+
t.Run(test.name, func(t *testing.T) {
135+
// Create a temporary file for testing.
136+
tmpFile, err := os.CreateTemp(
137+
t.TempDir(), "header_store_test",
138+
)
139+
if err != nil {
140+
t.Fatalf("Failed to create temp file: %v", err)
141+
}
142+
defer os.Remove(tmpFile.Name())
143+
defer tmpFile.Close()
144+
145+
// Write initial data.
146+
_, err = tmpFile.Write(test.initialData)
147+
if err != nil {
148+
t.Fatalf("Failed to write initial "+
149+
"data: %v", err)
150+
}
151+
152+
// Reset the file position to the end of initial data.
153+
_, err = tmpFile.Seek(
154+
int64(len(test.initialData)), io.SeekStart,
155+
)
156+
if err != nil {
157+
t.Fatalf("Failed to seek: %v", err)
158+
}
159+
160+
// Create a mock file that wraps the real file.
161+
mockFile := &mockFile{
162+
File: tmpFile,
163+
writeFn: test.writeFn,
164+
truncFn: test.truncFn,
165+
}
166+
167+
// Create a header store with our mock file.
168+
h := &headerStore{
169+
file: mockFile,
170+
headerIndex: &headerIndex{indexType: Block},
171+
}
172+
173+
// Call the function being tested.
174+
err = h.appendRaw(test.headerToWrite)
175+
if err == nil && test.wantErr {
176+
t.Fatal("expected an error, but got none")
177+
}
178+
if err != nil && !test.wantErr {
179+
t.Fatalf("unexpected error: %v", err)
180+
}
181+
if err != nil && test.wantErr &&
182+
!strings.Contains(err.Error(), test.errMsg) {
183+
184+
t.Errorf("expected error message %q to be "+
185+
"in %q", test.errMsg, err.Error())
186+
}
187+
188+
// Reset file position to start for reading.
189+
if _, err := tmpFile.Seek(0, io.SeekStart); err != nil {
190+
t.Fatalf("Failed to seek to start: %v", err)
191+
}
192+
193+
// Read the file contents.
194+
actualData, err := io.ReadAll(tmpFile)
195+
if err != nil {
196+
t.Fatalf("Failed to read file: %v", err)
197+
}
198+
199+
// Compare expected vs. actual file contents.
200+
if !bytes.Equal(actualData, test.expected) {
201+
t.Fatalf("Expected file data: %v, "+
202+
"got: %v", test.expected, actualData)
203+
}
204+
})
205+
}
206+
}
207+
208+
// BenchmarkHeaderStoreAppendRaw measures performance of headerStore.appendRaw
209+
// by writing 80-byte headers to a file and resetting position between writes
210+
// to isolate raw append performance from file size effects.
211+
func BenchmarkHeaderStoreAppendRaw(b *testing.B) {
212+
// Setup temporary file and headerStore.
213+
tmpFile, err := os.CreateTemp(os.TempDir(), "header_benchmark")
214+
if err != nil {
215+
b.Fatal(err)
216+
}
217+
defer os.Remove(tmpFile.Name())
218+
defer tmpFile.Close()
219+
220+
store := &headerStore{
221+
file: tmpFile,
222+
headerIndex: &headerIndex{indexType: Block},
223+
}
224+
225+
// Sample header data.
226+
header := make([]byte, 80)
227+
228+
// Reset timer to exclude setup time.
229+
b.ResetTimer()
230+
231+
// Run benchmark.
232+
for i := 0; i < b.N; i++ {
233+
if err := store.appendRaw(header); err != nil {
234+
b.Fatal(err)
235+
}
236+
237+
// Reset file position to beginning to maintain constant file
238+
// size. This isolates the appendRaw performance overhead
239+
// without measuring effects of increasing file size.
240+
if _, err := tmpFile.Seek(0, io.SeekStart); err != nil {
241+
b.Fatal(err)
242+
}
243+
}
244+
}
245+
246+
// mockFile wraps a real file but allows us to override the Write, Sync, and
247+
// Truncate methods.
248+
type mockFile struct {
249+
*os.File
250+
writeFn func([]byte, File) (int, error)
251+
syncFn func() error
252+
truncFn func(int64, File) error
253+
}
254+
255+
// Write implements the Write method for FileInterface.
256+
func (m *mockFile) Write(p []byte) (int, error) {
257+
if m.writeFn != nil {
258+
return m.writeFn(p, m.File)
259+
}
260+
return m.File.Write(p)
261+
}
262+
263+
// Sync implements the Sync method for FileInterface.
264+
func (m *mockFile) Sync() error {
265+
if m.syncFn != nil {
266+
return m.syncFn()
267+
}
268+
return m.File.Sync()
269+
}
270+
271+
// Truncate implements the Truncate method for FileInterface.
272+
func (m *mockFile) Truncate(size int64) error {
273+
if m.truncFn != nil {
274+
return m.truncFn(size, m.File)
275+
}
276+
return m.File.Truncate(size)
277+
}
278+
279+
// Ensure mockFile implements necessary interfaces.
280+
var _ io.Writer = &mockFile{}

0 commit comments

Comments
 (0)