Repository navigation
Expand file tree
/
Copy pathexport_failure_test.go
More file actions
167 lines (158 loc) · 4.93 KB
/
Copy pathexport_failure_test.go
File metadata and controls
167 lines (158 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2026 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dragonboat
import (
"context"
"errors"
"strings"
"sync/atomic"
"syscall"
"testing"
"time"
"github.com/lni/dragonboat/v4/internal/vfs"
pb "github.com/lni/dragonboat/v4/raftpb"
)
type failExportRenameFS struct {
vfs.IFS
fail atomic.Bool
}
type failExportCleanupFS struct {
vfs.IFS
fail atomic.Bool
}
func (fs *failExportCleanupFS) RemoveAll(path string) error {
if strings.HasSuffix(path, ".generating") && fs.fail.CompareAndSwap(true, false) {
return syscall.EIO
}
return fs.IFS.RemoveAll(path)
}
func (fs *failExportRenameFS) Rename(oldname, newname string) error {
if strings.HasSuffix(oldname, ".generating") &&
strings.Contains(newname, "exported-snapshot-error") && fs.fail.CompareAndSwap(true, false) {
return syscall.EIO
}
return fs.IFS.Rename(oldname, newname)
}
func TestExportCommitErrorReturnsWithoutStoppingNodeHost(t *testing.T) {
baseFS := vfs.GetTestFS()
fs := &failExportRenameFS{IFS: baseFS}
to := &testOption{
defaultTestNode: true,
tf: func(nh *NodeHost) {
n, ok := nh.getShard(1)
if !ok {
t.Fatal("shard missing")
}
originalFS := n.snapshotter.fs
n.snapshotter.fs = fs
defer func() { n.snapshotter.fs = originalFS }()
path := "exported-snapshot-error"
if err := fs.MkdirAll(path, 0755); err != nil {
t.Fatal(err)
}
defer fs.RemoveAll(path)
propose := func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := nh.SyncPropose(ctx, nh.GetNoOPSession(1), []byte("record")); err != nil {
t.Fatal(err)
}
}
propose()
fs.fail.Store(true)
opt := SnapshotOption{Exported: true, ExportPath: path}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
_, err := nh.SyncRequestSnapshot(ctx, 1, opt)
cancel()
if !errors.Is(err, syscall.EIO) {
t.Fatalf("expected export EIO, got %v", err)
}
var exportErr *SnapshotExportError
if !errors.As(err, &exportErr) {
t.Fatalf("expected completed export failure, got %T", err)
}
if fs.fail.Load() {
t.Fatal("export rename injection was not reached")
}
ss, err := nh.mu.logdb.GetSnapshot(1, 1)
if err != nil || !pb.IsEmptySnapshot(ss) {
t.Fatalf("export changed internal snapshot: %v, %v", ss, err)
}
// The failed exported task must release the save slot so the same
// quiescent shard can export at the same index on retry.
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
_, err = nh.SyncRequestSnapshot(ctx, 1, opt)
cancel()
if err != nil {
t.Fatalf("retry export failed: %v", err)
}
},
}
runNodeHostTest(t, to, baseFS)
}
func TestExportAbortCleanupErrorReturnsWithoutStoppingNodeHost(t *testing.T) {
baseFS := vfs.GetTestFS()
fs := &failExportCleanupFS{IFS: baseFS}
to := &testOption{
defaultTestNode: true,
tf: func(nh *NodeHost) {
n, ok := nh.getShard(1)
if !ok {
t.Fatal("shard missing")
}
originalFS := n.snapshotter.fs
n.snapshotter.fs = fs
defer func() { n.snapshotter.fs = originalFS }()
path := "exported-snapshot-abort-cleanup"
if err := fs.MkdirAll(path, 0755); err != nil {
t.Fatal(err)
}
defer fs.RemoveAll(path)
propose := func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := nh.SyncPropose(ctx, nh.GetNoOPSession(1), []byte("record")); err != nil {
t.Fatal(err)
}
}
propose()
opt := SnapshotOption{Exported: true, ExportPath: path}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
_, err := nh.SyncRequestSnapshot(ctx, 1, opt)
cancel()
if err != nil {
t.Fatalf("initial export failed: %v", err)
}
// A second export at the same index aborts because the final
// directory exists. Failure to remove its temporary directory
// must reach the caller without panicking the snapshot worker.
fs.fail.Store(true)
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
_, err = nh.SyncRequestSnapshot(ctx, 1, opt)
cancel()
if !errors.Is(err, syscall.EIO) {
t.Fatalf("expected cleanup EIO, got %v", err)
}
var exportErr *SnapshotExportError
if !errors.As(err, &exportErr) {
t.Fatalf("expected export failure, got %T", err)
}
if fs.fail.Load() {
t.Fatal("export cleanup injection was not reached")
}
propose()
},
}
runNodeHostTest(t, to, baseFS)
}