-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmart_array_raid_6_reader.cpp
More file actions
269 lines (223 loc) · 8.54 KB
/
smart_array_raid_6_reader.cpp
File metadata and controls
269 lines (223 loc) · 8.54 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "smart_array_raid_6_reader.hpp"
#include <math.h>
#include <memory.h>
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <algorithm>
#include <memory>
namespace sg
{
SmartArrayRaid6Reader::SmartArrayRaid6Reader(const SmartArrayRaid6ReaderOptions &options)
{
this->driveName = options.readerName;
this->parityDelay = options.parityDelay;
this->stripeSizeInBytes = options.stripeSize * 1024;
if (options.driveReaders.size() < 4)
{
throw std::invalid_argument("For RAID 6 at least 4 drives must be provided (two can be missing but still they have to be in the driveReaders list represented with nullptr)");
}
int missingDrives = 0;
std::vector<u64> drivesSizes;
for (auto& drive : options.driveReaders)
{
if (!drive)
{
if (missingDrives > 0)
{
throw std::invalid_argument("Sowwi but for now only 1 missing drive is supported with RAID 6. Reed Solomon's kinda hard :(");
}
if (missingDrives > 1)
{
throw std::invalid_argument("For RAID 6 only 2 missing drives are allowed.");
}
missingDrives++;
this->drives.push_back(drive);
continue;
}
drivesSizes.push_back(drive->driveSize());
this->drives.push_back(drive);
}
this->singleDriveSize = *std::min_element(drivesSizes.begin(), drivesSizes.end());
// 32MiB from the end of drive are stored controller metadata.
this->singleDriveSize -= 1024 * 1024 * 32;
this->setPhysicalDriveOffset(options.offset);
u64 wholeStripesOnDrive = (this->singleDriveSize - this->getPhysicalDriveOffset()) / this->stripeSizeInBytes;
u64 defaultSize = wholeStripesOnDrive * this->stripeSizeInBytes * (drives.size() - 2);
u64 maximumSize = (this->singleDriveSize - this->getPhysicalDriveOffset()) * (drives.size() - 2);
this->setSize(defaultSize, 0);
if (options.size > 0)
{
this->setSize(options.size, maximumSize);
}
}
int SmartArrayRaid6Reader::read(void *buf, u32 len, u64 offset)
{
if (offset >= this->driveSize())
{
std::cerr << this->name() << ": Tried to read from offset exceeding array size. Skipping." << std::endl;
std::cerr << "Offset: " << offset << std::endl;
std::cerr << "Drive Size: " << this->driveSize() << std::endl;
return -1;
}
u64 stripenum = this->stripeNumber(offset);
u32 stripeRelativeOffset = this->stripeRelativeOffset(stripenum, offset);
while (len != 0)
{
u32 read = this->readFromStripe(buf, stripenum, stripeRelativeOffset, len);
len -= read;
buf = static_cast<char*>(buf) + read;
if (len > 0)
{
stripenum++;
stripeRelativeOffset = 0;
}
}
return 0;
}
u64 SmartArrayRaid6Reader::stripeNumber(u64 offset)
{
u64 stripenum = offset / this->stripeSizeInBytes;
if (this->isLastRow(stripenum / (drives.size() - 2)))
{
u64 o = offset - (stripenum * this->stripeSizeInBytes);
u64 lastStripeNum = o / this->lastRowStripeSize();
stripenum += lastStripeNum;
}
return stripenum;
}
u32 SmartArrayRaid6Reader::stripeRelativeOffset(u64 stripenum, u64 offset)
{
u32 o = offset - stripenum * this->stripeSizeInBytes;;
if (this->isLastRow(stripenum / (drives.size() - 2)))
{
o %= this->lastRowStripeSize();
}
return o;
}
u16 SmartArrayRaid6Reader::stripeDriveNumber(u64 stripenum)
{
u64 currentStripeRow = stripenum / (drives.size() - 2);
u32 parityCycle = (this->drives.size() * this->parityDelay);
u32 reedSolomonCycleRow = currentStripeRow % parityCycle;
u32 parityCycleRow = (currentStripeRow + this->parityDelay) % parityCycle;
u16 reedSolomonDrive = drives.size() - (reedSolomonCycleRow / this->parityDelay) - 1;
u16 parityDrive = drives.size() - (parityCycleRow / this->parityDelay) - 1;
u16 stripeDrive = stripenum % (drives.size() - 2);
// ORDER OF THOSE IFS MATTERS!
if ((stripeDrive - parityDrive) >= 0)
{
stripeDrive++;
}
if ((stripeDrive - reedSolomonDrive) >= 0)
{
stripeDrive++;
}
return stripeDrive;
}
u64 SmartArrayRaid6Reader::stripeDriveOffset(u64 stripenum, u32 stripeRelativeOffset)
{
u64 currentStripeRow = stripenum / (drives.size() - 2);
return (currentStripeRow * this->stripeSizeInBytes) + stripeRelativeOffset + this->getPhysicalDriveOffset();
}
u32 SmartArrayRaid6Reader::lastRowStripeSize()
{
u32 fullStripeSize = this->stripeSizeInBytes * (this->drives.size() - 2);
u64 wholeStripesOnDrive = this->driveSize() / fullStripeSize;
u32 lastFullStripeSize = this->driveSize() - wholeStripesOnDrive * fullStripeSize;
return lastFullStripeSize / (this->drives.size() - 2);
}
bool SmartArrayRaid6Reader::isLastRow(u64 rownum)
{
u32 fullStripeSize = this->stripeSizeInBytes * (this->drives.size() - 2);
u64 wholeStripesOnDrive = this->driveSize() / fullStripeSize;
return rownum == wholeStripesOnDrive;
}
u32 SmartArrayRaid6Reader::readFromStripe(void *buf, u64 stripenum, u32 stripeRelativeOffset, u32 len)
{
auto drivenum = stripeDriveNumber(stripenum);
auto driveOffset = stripeDriveOffset(stripenum, stripeRelativeOffset);
auto stripeSize = this->stripeSizeInBytes;
if (this->isLastRow(stripenum / (drives.size() - 2)))
{
stripeSize = this->lastRowStripeSize();
}
if ((len + stripeRelativeOffset) > stripeSize)
{
// We will to the end of the stripe if
// stripe area is exceed. We will return len
// from this method so called will know that
// some data must be read from another stripe
len = stripeSize - stripeRelativeOffset;
}
auto& drivePtr = this->drives[drivenum];
if (!drivePtr)
{
return this->recoverForDrive(buf, drivenum, driveOffset, len);
}
drivePtr->read(buf, len, driveOffset);
return len;
}
bool SmartArrayRaid6Reader::isReedSolomonDrive(u16 drivenum, u64 driveOffset)
{
u64 currentStripeRow = (driveOffset - this->getPhysicalDriveOffset()) / this->stripeSizeInBytes;
u32 parityCycle = (this->drives.size() * this->parityDelay);
u32 reedSolomonCycleRow = currentStripeRow % parityCycle;
u16 reedSolomonDrive = drives.size() - (reedSolomonCycleRow / this->parityDelay) - 1;
return drivenum == reedSolomonDrive;
}
bool SmartArrayRaid6Reader::isParityDrive(u16 drivenum, u64 driveOffset)
{
u64 currentStripeRow = (driveOffset - this->getPhysicalDriveOffset()) / this->stripeSizeInBytes;
u32 parityCycle = (this->drives.size() * this->parityDelay);
u32 parityCycleRow = (currentStripeRow + this->parityDelay) % parityCycle;
u16 parityDrive = drives.size() - (parityCycleRow / this->parityDelay) - 1;
return drivenum == parityDrive;
}
u32 SmartArrayRaid6Reader::recoverForDrive(void *buf, u16 drivenum, u64 driveOffset, u32 len)
{
std::vector<std::shared_ptr<DriveReader>> otherDrives;
for (int i = 0; i < this->drives.size(); i++)
{
if (i != drivenum && !this->isReedSolomonDrive(i, driveOffset))
{
auto drive = this->drives[i];
if (!drive)
{
// We have another failed data drive, we need to use recovery for 2 missing drives
return this->recoverForTwoDrives(buf, drivenum, i, driveOffset, len);
}
otherDrives.push_back(this->drives[i]);
}
}
std::unique_ptr<char[]> _uniq_out(new char[len]);
std::unique_ptr<char[]> _uniq_temp(new char[len]);
// I am getting raw pointers coz for array operations
// compiler will use SSE for them with -O3
// with unique pointer that is not the case
// https://godbolt.org/z/aTYrhqPGb
// I am using unique here only to delete it automatically ^^
char* out = _uniq_out.get();
char* temp = _uniq_temp.get();
memset(out, 0, len);
// Parallel reading is not necessary here
// On 12 drives RAID 5 with 1 missing drive I have 341 MB/s of sequential read.
for (int i = 0; i < otherDrives.size(); i++)
{
auto drive = otherDrives[i];
drive->read(temp, len, driveOffset);
for (int i = 0; i < len; i++)
{
// Compiler with flag -O3 should optimize it using sse ^^
out[i] ^= temp[i];
}
}
memcpy(buf, out, len);
return len;
}
u32 SmartArrayRaid6Reader::recoverForTwoDrives(void *buf, u16 drive1num, u16 drive2num, u64 driveOffset, u32 len)
{
throw std::runtime_error("Not implemented yet :c");
return u32();
}
} // end namespace sg