-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdedicated_parity.go
246 lines (184 loc) · 4.33 KB
/
dedicated_parity.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package streammux
import (
"io"
"sync"
"syscall"
)
// DedicatedParity is a redundancy behavior with a stripe and a parity device similar to RAID-4.
type DedicatedParity struct {
sync.Mutex
stripe []*Member
parity *Member
state State
replaced chan int
}
func NewDedicatedParity(parity io.ReadWriteCloser, stripe []io.ReadWriteCloser, opts ...MemberOption) *DedicatedParity {
dp := &DedicatedParity{
stripe: make([]*Member, len(stripe)),
parity: NewMember(parity, opts...),
replaced: make(chan int),
}
for i, rwc := range stripe {
dp.stripe[i] = NewMember(rwc, opts...)
}
return dp
}
func (dp *DedicatedParity) Health() State {
return dp.state
}
func (dp *DedicatedParity) Open() State {
dp.Lock()
var failed bool
for _, rwc := range append(dp.stripe, dp.parity) {
state := rwc.Open()
switch state {
case FAILED:
if failed {
dp.state = FAILED
} else {
dp.state = DEGRADED
}
case DEGRADED:
if dp.state == FAILED {
break
}
dp.state = DEGRADED
}
}
return dp.state
}
func (dp *DedicatedParity) Close() (err error) {
defer dp.Unlock()
for _, closer := range dp.stripe {
err = closer.Close()
}
err = dp.parity.Close()
return
}
func (dp *DedicatedParity) Read(p []byte) (n int, err error) {
// THIS IS PRETTY HAIRY STUFF
// bail out if we're already marked as FAILED
if dp.state == FAILED {
return 0, syscall.EIO
}
// create a channel for I/O requests
ch := make(chan rwT)
// an esoteric counter (to get a nice range loop later)
var active []struct{}
stripe := split(p, len(dp.stripe))
reconstructIdx := -1
// loop over all members (stripe members and the parity member)
for i, reader := range append(dp.stripe, dp.parity) {
// check if the member is failed and record the index
if reader.State() != OK {
reconstructIdx = i
// don't issue a read request to this member if not OK
continue
}
// issue the read request in a seperate process
go reader.read(i, make([]byte, len(p)/len(dp.stripe)), ch)
// record that we issues a request and must get an answer
active = append(active, struct{}{})
}
tmp := make(StripeBufferList, len(dp.stripe)+1)
for range active {
rc := <-ch
n += rc.n
err = rc.err
if err != nil && err != io.EOF {
if dp.state == DEGRADED {
// if already DEGRADED mark us as FAILED
dp.state = FAILED
} else {
// if not, just mark us DEGRADED and record the index to reconstruct
dp.state = DEGRADED
reconstructIdx = rc.idx
// mark the correct stripe member or the parity member
if rc.idx == len(dp.stripe) {
dp.parity.SetState(FAILED)
} else {
dp.stripe[rc.idx].SetState(FAILED)
}
}
continue
}
// save for reconstruction
tmp[rc.idx] = rc.p[:rc.n]
}
// perform XOR only if one of the stripe members is FAILED
if dp.state == DEGRADED && reconstructIdx != len(dp.stripe) {
tmp2 := make(StripeBufferList, len(dp.stripe))
var j int
for i, buf := range tmp {
if i == reconstructIdx {
continue
}
tmp2[j] = buf
copy(stripe[j], buf)
j++
}
copy(stripe[reconstructIdx], tmp2.XOR())
return
}
// if all is good, just copy the buffers into the stripe
for i, buf := range tmp[:len(tmp)-1] {
copy(stripe[i], buf)
}
return
}
func (dp *DedicatedParity) Write(p []byte) (n int, err error) {
if dp.state == FAILED {
return 0, syscall.EIO
}
ch := make(chan rwT)
var active []struct{}
stripe := split(p, len(dp.stripe))
if dp.parity.State() == OK {
go func() {
r := stripe.XOR()
dp.parity.write(len(dp.stripe), r, ch)
}()
active = append(active, struct{}{})
}
for i, writer := range dp.stripe {
if writer.State() != OK {
continue
}
go writer.write(i, stripe[i], ch)
active = append(active, struct{}{})
}
var writeSucceeded bool
var numGoodWrites int
for range active {
rc := <-ch
n += rc.n
if rc.err != nil && rc.err != io.EOF {
if dp.state == DEGRADED {
dp.state = FAILED
} else {
dp.state = DEGRADED
if rc.idx == len(dp.stripe) {
dp.parity.SetState(FAILED)
} else {
dp.stripe[rc.idx].SetState(FAILED)
}
}
n -= rc.n
if !writeSucceeded {
err = rc.err
}
continue
}
numGoodWrites++
if numGoodWrites == len(dp.stripe) {
writeSucceeded = true
}
if writeSucceeded {
err = rc.err
}
}
if dp.state == OK {
n -= len(p) / len(dp.stripe)
}
return
}