-
Notifications
You must be signed in to change notification settings - Fork 0
/
raid.cpp
316 lines (269 loc) · 8.85 KB
/
raid.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* Name: Jones, Samuel
* Email: [email protected]
* Project: PA-2 (RAID)
* Instructor: Feng Chen
* Class: cs4103-au21
* Login ID: cs410348
* Date: 11/22/2021
*
* @brief Simulates a RAID-5 Array
*
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
/**
* @brief Obtains null padded block of data from input filestream.
*
* @param blockSize Bytes per block
* @param file Input file stream to pull data from
* @param block Byte array to deposit data into
*
* @return int Index of the last byte of data written to block array
*/
int getBlock(int blockSize, ifstream *file, char* block) {
for(int i = 0; i < blockSize; i++) block[i] = '\0';
file->read(block, blockSize);
block[blockSize] = '\0';
if(file->eof()) {
int i;
for(i = blockSize-1; i >= 0; i--) {
if(block[i] != '\0') {
break;
}
}
return i;
} else {
return blockSize-1;
}
}
/**
* @brief Write from file to RAID-5 Array
*
* @param numDisks Number of disks in the array
* @param blockSize Size of data block in bytes
* @param inputFilePath Path to file to read from
*/
void writeToDisksFromFile(int numDisks, int blockSize, string inputFilePath) {
//get disk file streams
ofstream disks[numDisks];
for(int i = 0; i < numDisks; i++) {
disks[i] = ofstream("disk." + to_string(i));
}
//get input file stream
ifstream in(inputFilePath);
//keep track of which disk is the parity disk for the current stripe
int parityDisk = numDisks-1;
//loop until finished inputting data
while(!in.eof()) {
//get blocks to write
vector<char*> blocks;
vector<int> bytesToWrite;
for(int i = 0; i < numDisks-1; i++) {
char* newBlock = (char*) malloc(blockSize + 1);
int lastDataIndex = getBlock(blockSize, &in, newBlock);
blocks.push_back(newBlock);
bytesToWrite.push_back(lastDataIndex+1);
}
//calculate parity block
char* parityBlock = (char*) malloc(blockSize+1);
for(int i = 0; i < blockSize; i++) {
char parityByte = 0;
for(char* block : blocks) {
parityByte ^= block[i];
}
parityBlock[i] = parityByte;
}
parityBlock[blockSize] = '\0';
//write blocks
int index = 0;
for(int i = 0; i < numDisks; i++) {
if(i == parityDisk) {
disks[i].write(parityBlock, blockSize);
} else {
disks[i].write(blocks.at(index), bytesToWrite.at(index));
index++;
}
}
//free allocated block arrays
free(parityBlock);
for(char* block : blocks) {
free(block);
}
//calculate next parity disk
parityDisk--;
if(parityDisk == -1) parityDisk = numDisks-1;
}
//close file streams
for(int i = 0; i < numDisks; i++) {
disks[i].close();
}
in.close();
}
/**
* @brief Read data from RAID-5 array into file
*
* @param numDisks Number of disks in RAID-5 array
* @param blockSize Size of data blocks in bytes
* @param outputFilePath Path to file to write to
*/
void readFromDiskToFile(int numDisks, int blockSize, string outputFilePath) {
//get disk file streams
ifstream disks[numDisks];
for(int i = 0; i < numDisks; i++) {
disks[i] = ifstream("disk." + to_string(i));
}
//get output file stream1
ofstream out(outputFilePath);
//vector to hold blocks to write to output
vector<char*> blocks;
//vector to keep track of number of bytes to write to
//output file when end of file is encountered so to
//not write trailing null characters
vector<int> bytesToWrite;
//keep track of parity disk for current stripe
int parityDisk = numDisks - 1;
//loop until finished getting data blocks to write to file
while(!disks[0].eof()) {
for(int i = 0; i < numDisks; i++) {
char* block = (char*) malloc(blockSize + 1);
int lastDataIndex = getBlock(blockSize, &disks[i], block);
if(i == parityDisk){
free(block);
continue;
}
blocks.push_back(block);
bytesToWrite.push_back(lastDataIndex+1);
}
parityDisk--;
if(parityDisk == -1) parityDisk = numDisks-1;
}
//write data blocks to file
int numBlocks = blocks.size();
for(int i = 0; i < numBlocks; i++) {
char* block = blocks.at(i);
out.write(block, bytesToWrite.at(i));
free(block);
}
//close file streams
for(int i = 0; i < numDisks; i++) {
disks[i].close();
}
out.close();
}
/**
* @brief Rebuild a disk of the RAID-5 array
*
* @param numDisks Number of disks in the RAID-5 array
* @param blockSize Size of data block in bytes
* @param brokenDiskPath The file name of the broken disk file
* @param brokenDisk The integer value of the file extension of the broken disk file that indicates its index
*/
void rebuildDisk(int numDisks, int blockSize, string brokenDiskPath, int brokenDisk) {
//get input streams for good disks
ifstream goodDisks[numDisks-1];
int index = 0;
for(int i = 0; i < numDisks; i++) {
if(i == brokenDisk) continue;
goodDisks[index] = ifstream("disk." + to_string(i));
index++;
}
//get output stream for disk to rebuild
ofstream broken(brokenDiskPath);
//vectors to hold blocks to write to bad disk and
//to keep track of how many bytes to write from each block
vector<char*> writeBlocks;
vector<int> bytesToWrite;
//loop until all data read from other disks
while(!goodDisks[0].eof()) {
//get blocks from good disks
vector<char*> blocks;
for(int i = 0; i < numDisks-1; i++) {
char* block = (char*) malloc(blockSize + 1);
int lastDataIndex = getBlock(blockSize, &goodDisks[i], block);
blocks.push_back(block);
}
//rebuild missing block (block from bad disk) of current stripe
char* rebuiltBlock = (char*) malloc(blockSize+1);
for(int i = 0; i < blockSize; i++) {
char parityByte = 0;
for(char* block : blocks) {
parityByte ^= block[i];
}
rebuiltBlock[i] = parityByte;
}
rebuiltBlock[blockSize] = '\0';
//add rebuilt blocks to blocks to write to bad disk
writeBlocks.push_back(rebuiltBlock);
//check if at end of file to make sure don't need to only write part of current blocks
if(goodDisks[0].eof()) {
int i;
for(i = blockSize-1; i >= 0; i--) {
if(rebuiltBlock[i] != '\0') {
break;
}
}
bytesToWrite.push_back(i+1);
} else {
bytesToWrite.push_back(blockSize);
}
//free allocated blocks
for(char* block : blocks) {
free(block);
}
}
//write blocks to broken disk file and free
int numBlocks = writeBlocks.size();
for(int i = 0; i < numBlocks; i++) {
char* block = writeBlocks.at(i);
broken.write(block, bytesToWrite.at(i));
free(block);
}
}
/**
* @brief Pulls data from command line arguments and determines
* which operation to run, then calls the respective function
* for that operation
*
* @param argc Number of command line arguments, should be 4
* @param argv Command line arguments, should be of format
* [Number of disks, Number of bytes per data block, command, File path]
*
* @return int 0 if successful, -1 if encoutered issue with arguments.
*/
int main(int argc, char** argv) {
if(argc != 5) {
cerr << "Wrong number of command-line args" << endl;
return -1;
}
//parse command line arguments
int numDisks = stoi(argv[1]),
blockSize = stoi(argv[2]);
string cmd = argv[3],
filePath = argv[4];
if(!numDisks || !blockSize || cmd.empty() || filePath.empty()) {
cerr << "Invalid command line arguments" << endl;
return -1;
}
//call corresponding function for given command
if(cmd == "write") {
writeToDisksFromFile(numDisks, blockSize, filePath);
} else if(cmd == "read") {
readFromDiskToFile(numDisks, blockSize, filePath);
} else if(cmd == "rebuild") {
int brokenDisk = filePath[filePath.length()-1] - '0';
if(brokenDisk < 0 || brokenDisk > numDisks-1) {
cerr << "Invalid disk number/disk file path: " << brokenDisk << endl;
return -1;
}
rebuildDisk(numDisks, blockSize, filePath, brokenDisk);
} else {
cerr << "Invalid command (arg3)" << endl;
return -1;
}
//successful exit
return 0;
}