-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmserver.cpp
More file actions
420 lines (374 loc) · 14.8 KB
/
mserver.cpp
File metadata and controls
420 lines (374 loc) · 14.8 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
@author: antriksh
Version 0: 3/14/2018
Version 0.1: 4/11/2018
* Documentation updated
* More structure to directories
* #includes optimized
* Ready for Version 0.1
Version 1: Project 3 complete
*/
#include "header/Info/Meta/utils.h"
#include "header/Socket.h"
class Mserver : public Socket {
/*
M-Server:
* Manages files and file paths
* Takes client requests to read and write
* Replies to client with meta information
* Asks random servers to create chunks of file
* maintains information about all files
* maintains and keeps updating information about all servers
* Does not do anything for maintaining mutual exclusion
*/
private:
vector<File*> files;
priority_queue<Message*> readWriteQueue;
bool ready = true;
public:
Mserver(char* argv[]) : Socket(argv) {
files = readFileInfo(files, "csvs/files.csv");
}
// Infinite thread to accept connection and detach a thread as
// a receiver and checker of messages
void listener() {
while (1) {
// Accept a connection with the accept() system call
int newsockfd =
accept(personalfd, (struct sockaddr*)&cli_addr, &clilen);
if (newsockfd < 0) {
error("ERROR on accept");
}
std::thread connectedThread(&Mserver::processMessages, this,
newsockfd);
connectedThread.detach();
}
}
// Starts as a thread which receives a message and checks the message
// @newsockfd - fd socket stream from which message would be received
void processMessages(int newsockfd) {
try {
Message* message = receive(newsockfd);
close(newsockfd);
checkMessage(message);
} catch (const char* e) {
Logger(e);
close(newsockfd);
// break;
}
}
// Checks the message for different types of incoming messages
// 1. heartbeat - just a hello from some server
// 2. something else (means a request to be granted)
// @m - Message just received
// @newsockfd - socket stream it was received from
void checkMessage(Message* m) {
if (m->type == "heartbeat") {
int index = findServerIndex(allServers, m->sourceID);
registerHeartBeat(m, index);
} else if (m->type == "inform") {
ready = true;
if (readWriteQueue.size() > 0) {
m = readWriteQueue.top();
readWriteQueue.pop();
checkReadWrite(m);
}
} else {
readWriteQueue.push(m);
if (ready) {
m = readWriteQueue.top();
readWriteQueue.pop();
checkReadWrite(m);
throw "BREAKING CONNECTION";
}
}
}
// Checks the request type
// 1 - Read: replies with meta-data for the file
// 2 - Write: replies with meta-data for the file
// @m - Message just received
// @newsockfd - socket stream it was received from
void checkReadWrite(Message* m) {
ready = false;
switch (m->readWrite) {
case 1: {
checkRead(m);
break;
}
case 2: {
checkWrite(m);
break;
}
default: {
string line = "UNRECOGNIZED MESSAGE !";
connectAndReply(m, "", line);
break;
}
}
updateCsv("csvs/files.csv", files);
Logger("Updated file info !");
}
// Checks what response should be given if the client wants to read a file
void checkRead(Message* m) {
try {
File* file = findInVector(files, m->fileName);
if (file == NULL) {
connectAndReply(m, "FAILED", "FAILED");
}
string chunkName = to_string(getChunkNum(m->offset));
string name = getChunkFile(m->fileName, chunkName);
vector<ProcessInfo> threeServers =
findFileServers(allServers, name);
if (getOffset(m->offset) + m->byteCount > CHUNKSIZE) {
readGreater(m, file, to_string(getChunkNum(m->offset)),
threeServers, m->offset, m->byteCount);
} else {
sizeSmaller(m, file, to_string(getChunkNum(m->offset)),
threeServers, m->offset, m->byteCount, 0);
}
} catch (char* e) {
Logger(e);
Logger("[FAILED]");
connectAndReply(m, "FAILED", "FAILED");
}
}
// Checks what response should be given if the client wants to write to a
// file
void checkWrite(Message* m) {
try {
File* file;
file = findInVector(files, m->fileName);
if (file == NULL) {
file = createNewFile(m, new File);
}
string chunkName = to_string(file->chunks - 1);
string name = getChunkFile(file->name, chunkName);
vector<ProcessInfo> threeServers =
findFileServers(allServers, name);
int messageSize = m->message.length();
int chunkSize = getOffset(file->size);
if (messageSize > (CHUNKSIZE - chunkSize)) {
sizeGreater(m, file, chunkName, threeServers, chunkSize,
messageSize);
} else {
sizeSmaller(m, file, chunkName, threeServers, 0, messageSize,
0);
}
file->size += messageSize;
} catch (char* e) {
Logger(e);
Logger("[FAILED]");
connectAndReply(m, "FAILED", "FAILED");
}
}
// Creates new file:
// * adds a new file to the file infos
// * creates a zero sized chunk of the file at a random server
// * returns the FileInfo
File* createNewFile(Message* m, File* file) {
Logger("[Creating new file]: " + m->fileName);
file->name = m->fileName;
createNewChunk(file, to_string(file->chunks));
files.push_back(file);
return file;
}
// When size of the write message size + size of the chunk is greater than
// 8192 bytes.
// The message is divided into two parts. The first part completes
// 8192 bytes on the last available chunk and the second part is written to
// a newly created chunk
void readGreater(Message* m, File* file, string chunkName,
vector<ProcessInfo> server, int offset, int byteCount) {
int sizeThisChunk = byteCount - (CHUNKSIZE - offset);
int extraByteCount = byteCount - sizeThisChunk;
int queued = 1;
string source = m->sourceID;
sizeSmaller(m, file, chunkName, server, offset, sizeThisChunk,
queued--);
chunkName = to_string(stoi(chunkName) + 1);
vector<ProcessInfo> threeServers;
try {
threeServers = findFileServers(allServers,
getChunkFile(file->name, chunkName));
} catch (char* e) {
connectAndReply(m, "FAILED", "FAILED");
return;
}
m->sourceID = source; // TODO: I should not have to do this
sizeSmaller(m, file, chunkName, threeServers, 0, extraByteCount,
queued);
}
// When size of the write message size + size of the chunk is greater than
// 8192 bytes.
// The message is divided into two parts. The first part completes
// 8192 bytes on the last available chunk and the second part is written to
// a newly created chunk
void sizeGreater(Message* m, File* file, string chunkName,
vector<ProcessInfo> server, int chunkSize,
int messageSize) {
int sizeThisChunk = messageSize - (CHUNKSIZE - chunkSize);
int sizeNewChunk = messageSize - sizeThisChunk;
int queued = 1;
sizeSmaller(m, file, chunkName, server, 0, sizeThisChunk, queued--);
chunkName = to_string(stoi(chunkName) + 1);
createNewChunk(file, chunkName);
vector<ProcessInfo> threeServers;
try {
threeServers = findFileServers(allServers,
getChunkFile(file->name, chunkName));
} catch (char* e) {
connectAndReply(m, "FAILED", "FAILED");
return;
}
sizeSmaller(m, file, chunkName, threeServers, sizeThisChunk,
sizeNewChunk, queued);
}
// When size of the write message size + size of the chunk is less than
// 8192 bytes.
// The message is written to the last available chunk of the file.
void sizeSmaller(Message* m, File* file, string chunkName,
vector<ProcessInfo> servers, int offset, int byteCount,
int queued) {
m->offset = offset;
m->byteCount = byteCount;
replyMeta(m, file->name, chunkName, servers, queued);
}
// reply with the meta information of reading or writing to a file. The
// meta-server replies with the chunk number, the server the file is on, and
// the file name
void replyMeta(Message* m, string fileName, string chunkName,
vector<ProcessInfo> servers, int queued) {
vector<ProcessInfo> set;
for (ProcessInfo server : servers) {
if (server.getReady())
set.push_back(server);
else {
int index = findServerIndex(allServers, server.processID);
Logger("[SERVER DOWN]: " + allServers[index].processID +
" needs to update chunk " +
getChunkFile(fileName, chunkName));
// cout << getChunkFile(fileName, chunkName) << endl;
allServers[index].chunksNeedUpdate.insert(
getChunkFile(fileName, chunkName));
}
}
MetaInfo* meta =
new MetaInfo(fileName, chunkName, makeTuple(set), queued);
string line = infoToString(meta);
if (!set.empty()) {
connectAndReply(m, "meta", line);
} else {
connectAndReply(m, "FAILED", "FAILED");
}
}
// Creating new chunk:
// * randomly selects a server.
// * connects to the server and tells it to create a new chunk
// * updates meta data about the server
void createNewChunk(File* file, string chunkName) {
int selected = 0;
vector<ProcessInfo> threeServers = randomSelectThree(allServers);
file->chunks++;
for (ProcessInfo server : threeServers) {
Logger("[Creating new file chunk at]: " + server.processID);
string fileName = getChunkFile(file->name, chunkName);
connectAndSend(server.processID, "create", "", 2, fileName);
updateMetaData(file, server);
}
}
// Updating meta-data in the meta Directory
void updateMetaData(File* file, ProcessInfo server) {
server.addFile(getChunkFile(file->name, to_string(file->chunks)));
Logger("[Meta-Data Updated]");
updateServers(server);
}
// Update information about servers
void updateServers(ProcessInfo server) {
int index = 0;
for (ProcessInfo s : allServers) {
if (s.processID == server.processID) break;
index++;
}
allServers.at(index) = server;
}
// Handle the case when a chunk comes back alive after being dead for some
// time
void recoveringServer(int index) {
if (!allServers[index].chunksNeedUpdate.empty())
for (string chunk : allServers[index].chunksNeedUpdate) {
Logger("[RECOVERING]: Updating file: " + chunk);
// Connecting to server which was down
ProcessInfo p = allServers[index];
cout << p.processID << endl;
int fdBroken = connectTo(p.hostname, p.port);
send(personalfd, fdBroken, "head", "", p.processID, 2, chunk);
Message* msg = receive(fdBroken);
vector<ProcessInfo> others = findFileServers(allServers, chunk);
for (ProcessInfo another : others) {
if (another.processID == p.processID || !another.getReady())
continue;
// Connecting to a server which was alive
cout << another.processID << endl;
int fdUpdated = connectTo(another.hostname, another.port);
send(personalfd, fdUpdated, "recover", msg->message,
another.processID, 2, chunk);
msg = receive(fdUpdated);
close(fdUpdated);
break;
}
// Connecting to broken server again
fdBroken = connectTo(p.hostname, p.port);
send(personalfd, fdBroken, "update", msg->message, p.processID,
2, chunk);
msg = receive(fdBroken);
}
else {
Logger("[RECOVERING]: No chunks need an update.");
}
allServers[index].setReady();
}
// Register the read heartbeat
// * Updates the alive time at the Server info
void registerHeartBeat(Message* m, int index) {
Logger("[HEARTBEAT] " + m->sourceID, false);
if (allServers[index].getAlive()) {
allServers[index].setAlive();
allServers[index].updateFiles(m->message);
} else {
allServers[index].setAlive();
allServers[index].updateFiles(m->message);
Logger("[RECOVERING]: " + m->sourceID);
recoveringServer(index);
}
}
// Continuous thread to check if which server is not alive.
// * checks seconds from last alive time from the server info
// * declares as dead if last alive was 15 seconds ago.
void serversAlive() {
while (1) {
for (int i = 0; i < allServers.size(); i++) {
allServers[i].checkAlive();
}
sleep(1);
}
}
~Mserver() { updateCsv("csvs/files.csv", files); }
};
int main(int argc, char* argv[]) {
if (argc < 3) {
fprintf(stderr, "usage %s ID port\n", argv[0]);
exit(1);
}
Mserver* server = new Mserver(argv);
// int c1 = getChunkSize("server1Directory/file6_0");
// int c2 = getChunkSize("server4Directory/file6_0");
// cout << c1 << endl;
// cout << c2 << endl;
// cout << readFile("server1Directory/file6_0", c2, c1 - c2) << endl;
std::thread listenerThread(&Mserver::listener, server);
std::thread countDown(&Mserver::serversAlive, server);
countDown.join();
listenerThread.join();
logger.close();
return 0;
}