-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverInstance.cpp
402 lines (333 loc) · 9.95 KB
/
serverInstance.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
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
/************************/
/* Your Name: Sining Ma */
/* Date: 05/10/2014 */
/* CS 244B */
/* Spring 2013 */
/************************/
#include "serverInstance.h"
int main(int argc, char *argv[]) {
if (argc != 7)
RFSError("Invalid command. Example: replFsServer -port 4137 -mount /folder1/fs244b -drop 3");
unsigned short port = (unsigned short)atoi(argv[2]);
std::string mount(argv[4]);
int packetLoss = atoi(argv[6]);
timeval ct;
gettimeofday(&ct, NULL);
srand(ct.tv_usec);
uint32_t nodeId = (uint32_t)rand();
printf("Server port: %u, mount: %s, packetLoss: %d, nodeId: %010u\n", port, mount.c_str(), packetLoss, nodeId);
ServerInstance *server = new ServerInstance(packetLoss, nodeId, mount.c_str());
if( mkdir(mount.c_str(), S_IRUSR | S_IWUSR) < 0) {
if (errno == EEXIST) {
RFSError("machine already in use");
} else {
printf("create mount directory error = %s\n", strerror(errno));
RFSError("ReplFsServer server exits");
}
}
server->rfs_NetInit(port);
server->execute();
return 0;
}
ServerInstance:: ServerInstance(int packetLoss, uint32_t nodeId, std::string mount): NetworkInstance(packetLoss, nodeId) {
this->mount = mount;
this->nodeType = SERVER_NODE;
this->nextUpdateId = 0;
this->backup = NULL;
this->fp = NULL;
}
void ServerInstance:: execute() {
while(1) {
bool isRecvPacket = rfs_IsRecvPacket();
// printf("receive packets: %d\n", isRecvPacket);
if (isRecvPacket) {
char buf[MAXBUFSIZE];
memset(buf, 0, MAXBUFSIZE);
ssize_t status = rfs_RecvFrom(buf, MAXBUFSIZE);
if (status > 0) {
unsigned char msgType = buf[0];
if (isMessageSentByMe(buf))
continue;
// message received not from client
if (!isRecvClientMsg(msgType))
continue;
if (isDropPacket(packetLoss)) {
uint32_t msg_nodeId = 0;
memcpy(&msg_nodeId, buf + 2, 4);
msg_nodeId = ntohl(msg_nodeId);
uint32_t msg_seqNum = 0;
memcpy(&msg_seqNum, buf + 6, 4);
msg_seqNum = ntohl(msg_seqNum);
printf("Drop Message: MsgType: 0x%02x, nodeId: %010u, msgSeqNum: %u\n", msgType, msg_nodeId, msg_seqNum);
continue;
}
switch(msgType) {
case INIT:
printf("Recv message size: %d, ", (int)status);
procInitMessage(buf);
break;
case OPENFILE:
printf("Recv message size: %d, ", (int)status);
procOpenFileMessage(buf);
break;
case WRITEBLOCK:
printf("Recv message size: %d, ", (int)status);
procWriteBlockMessage(buf);
break;
case VOTE:
printf("Recv message size: %d, ", (int)status);
procVoteMessage(buf);
break;
case COMMIT:
printf("Recv message size: %d, ", (int)status);
procCommitMessage(buf);
break;
case ABORT:
printf("Recv message size: %d, ", (int)status);
procAbortMessage(buf);
break;
case CLOSE:
printf("Recv message size: %d, ", (int)status);
procCloseMessage(buf);
break;
default:
break;
}
}
}
}
}
void ServerInstance:: reset() {
// delete memory
if ((int)updateMap.size() != 0) {
for (std::map<uint32_t, Update>::iterator it = updateMap.begin(); it != updateMap.end(); ++it) {
char *buff = it->second.buffer;
delete[] buff;
}
updateMap.clear();
}
nextUpdateId = 0;
}
void ServerInstance:: resetBackup() {
if (backup != NULL) {
delete[] backup;
backup = NULL;
}
}
bool ServerInstance:: isRecvClientMsg(unsigned char msgType) {
switch(msgType) {
case INIT:
return true;
break;
case OPENFILE:
return true;
break;
case WRITEBLOCK:
return true;
break;
case VOTE:
return true;
break;
case COMMIT:
return true;
break;
case ABORT:
return true;
break;
case CLOSE:
return true;
break;
default:
return false;
break;
}
return false;
}
void ServerInstance:: sendInitAckMessage() {
InitAckMessage initAckMsg(nodeId, msgSeqNum);
msgSeqNum = getNextNum(msgSeqNum);
sendMessage(&initAckMsg, HEADER_SIZE);
}
void ServerInstance:: procInitMessage(char *buf) {
InitMessage initMessage;
initMessage.deserialize(buf);
initMessage.print();
sendInitAckMessage();
}
void ServerInstance:: sendOpenFileAckMessage(int fileDesc) {
OpenFileAckMessage openFileAckMessage(nodeId, msgSeqNum, fileDesc);
msgSeqNum = getNextNum(msgSeqNum);
sendMessage(&openFileAckMessage, HEADER_SIZE + 4);
}
void ServerInstance:: procOpenFileMessage(char *buf) {
OpenFileMessage openFileMessage;
openFileMessage.deserialize(buf);
openFileMessage.print();
// opening file only gets new filename
// create or open the file when commit is done
std::string filename(openFileMessage.filename);
fileFullname.clear();
fileFullname = mount + "/" + filename;
printf("OpenFile phase: server receives new filename: %s\n", fileFullname.c_str());
sendOpenFileAckMessage(0);
}
void ServerInstance:: procWriteBlockMessage(char *buf) {
WriteBlockMessage writeBlockMessage;
writeBlockMessage.deserialize(buf);
writeBlockMessage.print();
std::map<uint32_t, Update>::iterator it = updateMap.find(writeBlockMessage.updateId);
if (it == updateMap.end()) {
Update update;
update.byteOffset = writeBlockMessage.byteOffset;
update.blockSize = writeBlockMessage.blockSize;
update.buffer = new char[writeBlockMessage.blockSize];
memset(update.buffer, 0, writeBlockMessage.blockSize);
memcpy(update.buffer, writeBlockMessage.buffer, writeBlockMessage.blockSize);
updateMap.insert(std::make_pair(writeBlockMessage.updateId, update));
}
}
void ServerInstance:: sendVoteAckMessage(int fileDesc, uint32_t updateId) {
VoteAckMessage voteAckMessage(nodeId, msgSeqNum, fileDesc, updateId);
msgSeqNum = getNextNum(msgSeqNum);
sendMessage(&voteAckMessage, HEADER_SIZE + 8);
}
void ServerInstance:: procVoteMessage(char *buf) {
VoteMessage voteMsg;
voteMsg.deserialize(buf);
voteMsg.print();
while(1) {
std::map<uint32_t, Update>::iterator it = updateMap.find(nextUpdateId);
if (it == updateMap.end()) // cannot found in memory
break;
else // found in memory
++nextUpdateId;
}
printf("Vote phase: server receives fileId: %u till updateId: %u\n", voteMsg.fileId, nextUpdateId);
sendVoteAckMessage(0, nextUpdateId);
}
void ServerInstance:: sendCommitAckMessage(int fileDesc) {
CommitAckMessage commitAckMessage(nodeId, msgSeqNum, fileDesc);
msgSeqNum = getNextNum(msgSeqNum);
sendMessage(&commitAckMessage, HEADER_SIZE + 4);
}
void ServerInstance:: procCommitMessage(char *buf) {
CommitMessage commitMsg;
commitMsg.deserialize(buf);
commitMsg.print();
printf("Commit phase: start to commit fileId: %u till updateId: %u\n", commitMsg.fileId, nextUpdateId);
if (!isFileExist(fileFullname.c_str()))
fp = fopen(fileFullname.c_str(), "w+b");
else
fp = fopen(fileFullname.c_str(), "r+b");
if (!fp) {
// open the file fails
printf("Commit phase: Create filename %s fail\n", fileFullname.c_str());
sendCommitAckMessage(-1);
return;
} else {
// open the file successfully
printf("Commit phase: Create filename %s successful\n", fileFullname.c_str());
}
// before the first commit, do backup
fseek(fp, 0, SEEK_END);
long fileSize = ftell(fp);
if (fileSize > 0) {
backup = new char[fileSize];
memset(backup, 0, fileSize);
fread(backup, 1, fileSize, fp);
}
// write from memory to the file
for (uint32_t i = 0; i < nextUpdateId; i++) {
std::map<uint32_t, Update>::iterator it = updateMap.find(i);
int byteOffset = it->second.byteOffset;
int blockSize = it->second.blockSize;
char *buffer = it->second.buffer;
// seek and write all updates to the file
fseek (fp, byteOffset, SEEK_SET);
fwrite (buffer, sizeof(char), blockSize, fp);
if (fflush(fp) < 0) {
printf("Commit phase: fflush error on updateId: %u\n", i);
sendCommitAckMessage(-1);
return;
}
}
printf("Commit phase: All commits are done update fileId: %u till updateId: %u\n", commitMsg.fileId, nextUpdateId);
// commit is done
resetBackup();
reset();
sendCommitAckMessage(0);
}
void ServerInstance:: sendAbortAckMessage(int fileDesc) {
AbortAckMessage abortAckMessage(nodeId, msgSeqNum, fileDesc);
msgSeqNum = getNextNum(msgSeqNum);
sendMessage(&abortAckMessage, HEADER_SIZE + 4);
}
void ServerInstance:: procAbortMessage(char *buf) {
AbortMessage abortMsg;
abortMsg.deserialize(buf);
abortMsg.print();
if (backup != NULL) {
printf("Abort phase: Server error happens during commit and rollback the file");
fclose(fp);
fp = NULL;
if (remove(fileFullname.c_str()) != 0) {
printf("Abort phase: Rollback delete filename %s fail\n", fileFullname.c_str());
sendAbortAckMessage(-1);
resetBackup();
reset();
return;
}
fp = fopen(fileFullname.c_str(), "w+b");
if (!fp) {
// fail to open file in order to do rollback
printf("Abort phase: Rollback recreate filename %s fail\n", fileFullname.c_str());
sendAbortAckMessage(-1);
resetBackup();
reset();
return;
}
fwrite (backup , sizeof(char), sizeof(backup), fp);
if (fflush(fp) < 0) {
printf("Abort phase: fflush error when rollback filename %s fail\n", fileFullname.c_str());
sendCommitAckMessage(-1);
resetBackup();
reset();
return;
}
resetBackup();
}
// abort is done
reset();
printf("Abort phase: Server aborts fileId: %u updates ok\n", abortMsg.fileId);
sendAbortAckMessage(0);
}
void ServerInstance:: sendCloseAckMessage(int fileDesc) {
CloseAckMessage closeAckMessage(nodeId, msgSeqNum, fileDesc);
msgSeqNum = getNextNum(msgSeqNum);
sendMessage(&closeAckMessage, HEADER_SIZE + 4);
}
void ServerInstance:: procCloseMessage(char *buf) {
CloseMessage closeMsg;
closeMsg.deserialize(buf);
closeMsg.print();
printf("Close phase: Server starts to close file\n");
if ((int)updateMap.size() != 0) {
printf("Close phase: Server has uncommitted file update\n");
sendCloseAckMessage(1);
return;
}
if (fp != NULL) {
int ret = fclose(fp);
if (ret == 0) {
printf("Close phase: Server closes fileId: %u ok\n", closeMsg.fileId);
sendCloseAckMessage(0);
fp = NULL;
}
else {
printf("Close phase: Server closes fileId: %u error\n", closeMsg.fileId);
sendCloseAckMessage(-1);
}
} else {
sendCloseAckMessage(0);
}
}