-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.cpp
More file actions
1555 lines (1089 loc) · 38.8 KB
/
DFS.cpp
File metadata and controls
1555 lines (1089 loc) · 38.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <mpi.h>
#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <map>
#include <unistd.h>
#include <set>
#include <thread>
#include <chrono>
#include <mutex>
#include <fstream>
#include <atomic>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
// declaring Mutex Lock
mutex hbMutex;
// constants
const int CHUNK_SIZE = 32;
const int REPLICATION_FACTOR = 3;
const int HEARTBEAT_INTERVAL = 1;
const int FAILURE_DETECTION_TIMEOUT = 3;
// MESSAGE TAGS
enum MessageType
{
UPLOAD_REQUEST = 1,
CHUNK_DATA = 2,
DOWNLOAD_REQUEST = 3,
SEARCH_REQUEST = 4,
HEARTBEAT = 5,
FAILOVER = 6,
RECOVER = 7,
EXIT_TAG = 999
};
// STRUCT for FILE METADATA
struct FileMetadata
{
string fileName;
string fileAddress;
int numChunks;
long long int startChunkID;
map<long long int, set<int>> chunkPositions; // chunkID : replicaNodes
};
// STRUCT to hold SEARCH RESULTS
struct SearchResult
{
int offset; // Offset within the chunk
bool isPartial; // True if word spans across chunks
int matchLength; // Length of the match found in current chunk
bool isPrefix; // True if this is prefix part of split word
};
// FUNCTIONS DECLARATIONs
void processMetadataServer(int numProcesses);
void processStorageNode(int myRank);
pair<int, string> opUploadMS(long long int &lastChunkID, string fileName, string fileAddress, map<string, FileMetadata> &metadata, unordered_set<int> &activeNodes, set<pair<int, int>> &loadPerNode);
pair<int, string> opRetrieveMS(string fileName, map<string, FileMetadata> &metadata, unordered_set<int> &activeNodes);
pair<int, string> opSearchMS(string fileName, const string &word, map<string, FileMetadata> &metadata, unordered_set<int> &activeNodes);
pair<int, string> opListFileMS(string fileName, map<string, FileMetadata> &metadata, unordered_set<int> &activeNodes);
void opUploadSS(map<int, string> &storedChunks);
void opRetrieveSS(map<int, string> &storedChunks);
void opSearchSS(map<int, string> &storedChunks);
void printFileData(string fileData);
pair<int, string> readFileInChunks(const string &fileName, const string &fileAddress, vector<string> &fileChunks);
pair<int, vector<size_t>> findWordOccurrences(const string &chunkContent, int chunkOffset, const string &word);
bool isCompleteMatch(const string &chunk, const string &word, size_t pos);
bool isPrefixMatch(const string &chunk, const string &word, size_t pos, char &prevChunkLastChar);
bool isSuffixMatch(const string &chunk, const string &word, int prevMatchLength);
vector<SearchResult> searchInChunk(const string &chunk, const string &searchWord, const int &chunkOffset, int &prevMatchLength);
void monitorHeartbeats(unordered_map<int, chrono::time_point<chrono::steady_clock>> &heartbeatTimestamps, bool &isRunning);
void monitorHeartbeatsWrapper(unordered_map<int, chrono::time_point<chrono::steady_clock>> &heartbeatTimestamps, bool &isRunning);
void thread_receiveHB(int nodeRank, unordered_map<int, chrono::time_point<chrono::steady_clock>> &heartbeatTimestamps, bool &isRunning);
void thread_receiveHBWrapper(int nodeRank, unordered_map<int, chrono::time_point<chrono::steady_clock>> &heartbeatTimestamps, bool &isRunning);
void thread_sendHB(bool &isRunning);
void thread_sendHBWrapper(bool &isRunning);
string convertCharArrToString(char *arr, int arrSize)
{
string str(arr, arrSize);
return str;
}
/*
*/
void monitorHeartbeats(unordered_map<int, chrono::time_point<chrono::steady_clock>> &heartbeatTimestamps, bool &isRunning)
{
while (isRunning)
{
this_thread::sleep_for(chrono::milliseconds(500));
auto now = chrono::steady_clock::now();
lock_guard<mutex> lock(hbMutex);
for (auto it = heartbeatTimestamps.begin(); it != heartbeatTimestamps.end();)
{
if (chrono::duration_cast<chrono::seconds>(now - it->second).count() > FAILURE_DETECTION_TIMEOUT)
{
// cout << "Node " << it->first << " : DOWN!" << endl;
it = heartbeatTimestamps.erase(it); // Remove the node from the map
}
else
{
++it;
}
}
}
return;
}
void monitorHeartbeatsWrapper(unordered_map<int, chrono::time_point<chrono::steady_clock>> &heartbeatTimestamps, bool &isRunning)
{
monitorHeartbeats(heartbeatTimestamps, isRunning);
}
void thread_receiveHB(int nodeRank, unordered_map<int, chrono::time_point<chrono::steady_clock>> &heartbeatTimestamps, bool &isRunning)
{
while (isRunning)
{
// receiving HB if sent
MPI_Status status;
int msgPresent;
MPI_Iprobe(nodeRank, HEARTBEAT, MPI_COMM_WORLD, &msgPresent, &status);
// checking if message is present
if (msgPresent)
{
// receiving the sent message
MPI_Recv(nullptr, 0, MPI_BYTE, nodeRank, HEARTBEAT, MPI_COMM_WORLD, &status);
// updating the timestamp
lock_guard<mutex> lock(hbMutex);
heartbeatTimestamps[nodeRank] = chrono::steady_clock::now();
}
}
return;
}
void thread_receiveHBWrapper(int nodeRank, unordered_map<int, chrono::time_point<chrono::steady_clock>> &heartbeatTimestamps, bool &isRunning)
{
thread_receiveHB(nodeRank, heartbeatTimestamps, isRunning);
}
void thread_sendHB(bool &isRunning)
{
// sending HB after every 2 seconds
while (isRunning)
{
// sending HB to only Root
MPI_Send(nullptr, 0, MPI_BYTE, 0, HEARTBEAT, MPI_COMM_WORLD);
this_thread::sleep_for(chrono::seconds(HEARTBEAT_INTERVAL));
}
}
void thread_sendHBWrapper(bool &isRunning)
{
thread_sendHB(isRunning);
}
/*
*/
// processing metadata server
void processMetadataServer(int numProcesses)
{
// Heartbeat Storage
unordered_map<int, chrono::time_point<chrono::steady_clock>> heartbeatTimestamps; // (nodeRank, timestamp)
bool isRunning = true;
// Metadata Storage
map<string, FileMetadata> metadata; // (fileName, FileMetadata)
unordered_set<int> activeNodes;
long long int lastChunkID = 0;
set<pair<int, int>> loadPerNode; // (load, nodeRank)
// initializing loadPerNode
for (int i = 1; i < numProcesses; i++)
loadPerNode.insert({0, i});
// updating active nodes
for (int i = 1; i < numProcesses; i++)
{
activeNodes.insert(i);
}
// creating a thread for monitoring heartbeats
thread t(monitorHeartbeatsWrapper, ref(heartbeatTimestamps), ref(isRunning));
// creating a thread for each process
vector<thread> allThreads;
for (int i = 1; i < numProcesses; i++)
{
allThreads.push_back(thread(thread_receiveHBWrapper, i, ref(heartbeatTimestamps), ref(isRunning)));
}
// getting constant input commands
string command;
while (isRunning)
{
// getting command
getline(cin, command);
// processing command
istringstream iss(command);
string operation;
iss >> operation;
// checking which operation
if (operation == "exit")
{
// if anything else : ERROR
string temp;
iss >> temp;
if (!temp.empty())
{
cout << -1 << endl;
continue;
}
// notifying all Storage Servers to terminate
for (int i = 1; i < numProcesses; i++)
{
MPI_Send(nullptr, 0, MPI_BYTE, i, EXIT_TAG, MPI_COMM_WORLD);
}
isRunning = false;
break;
}
else if (operation == "upload")
{
// getting file name
string fileName;
iss >> fileName;
string fileAddress;
iss >> fileAddress;
string temp;
iss >> temp;
// checking correct inputs
if (fileName.empty() || fileAddress.empty() || !temp.empty())
{
cout << -1 << endl;
continue;
}
// handling the op
pair<int, string> retVal = opUploadMS(lastChunkID, fileName, fileAddress, metadata, activeNodes, loadPerNode);
cout << retVal.first << endl;
// listing file if uploaded successfully
if (retVal.first == 1)
{
opListFileMS(fileName, metadata, activeNodes);
}
}
else if (operation == "retrieve")
{
string fileName;
iss >> fileName;
string temp;
iss >> temp;
// checking correct inputs
if (fileName.empty() || !temp.empty())
{
cout << -1 << endl;
continue;
}
// handling the op
pair<int, string> retVal = opRetrieveMS(fileName, metadata, activeNodes);
if (retVal.first == -1)
cout << retVal.first << endl;
else
{
printFileData(retVal.second);
}
}
else if (operation == "search")
{
// getting file name
string fileName;
iss >> fileName;
string word;
iss >> word;
string temp;
iss >> temp;
// checking correct inputs
if (fileName.empty() || word.empty() || !temp.empty())
{
cout << -1 << endl;
continue;
}
// handling the op
pair<int, string> retVal = opSearchMS(fileName, word, metadata, activeNodes);
if (retVal.first == -1)
{
cout << retVal.first << endl;
}
}
else if (operation == "list_file")
{
// getting file name
string fileName;
iss >> fileName;
string temp;
iss >> temp;
// if no fileName is entered then error
if (fileName.empty() || !temp.empty())
{
cout << -1 << endl;
continue;
}
// handling the op
pair<int, string> retVal = opListFileMS(fileName, metadata, activeNodes);
if (retVal.first == -1)
{
cout << retVal.first << endl;
}
}
else if (operation == "failover")
{
// getting node rank
int nodeRank;
iss >> nodeRank;
string temp;
iss >> temp;
// checking correct inputs
if (!temp.empty())
{
cout << -1 << endl;
continue;
}
if (nodeRank <= 0 || nodeRank >= numProcesses)
{
cout << -1 << endl;
continue;
}
if (activeNodes.find(nodeRank) == activeNodes.end())
{
cout << -1 << endl;
continue;
}
// sending failover command to the node
MPI_Send(nullptr, 0, MPI_BYTE, nodeRank, FAILOVER, MPI_COMM_WORLD);
// updating active nodes
activeNodes.erase(nodeRank);
cout << 1 << endl;
}
else if (operation == "recover")
{
// getting node rank
int nodeRank;
iss >> nodeRank;
string temp;
iss >> temp;
// checking correct inputs
if (!temp.empty())
{
cout << -1 << endl;
continue;
}
if (nodeRank <= 0 || nodeRank >= numProcesses)
{
cout << -1 << endl;
continue;
}
if (activeNodes.find(nodeRank) != activeNodes.end())
{
cout << -1 << endl;
continue;
}
// sending recovery command to the node
MPI_Send(nullptr, 0, MPI_BYTE, nodeRank, RECOVER, MPI_COMM_WORLD);
// updating active nodes
activeNodes.insert(nodeRank);
cout << 1 << endl;
}
else
{
cout << -1 << endl;
}
}
// joining all threads
t.join();
for (int i = 0; i < allThreads.size(); i++)
{
allThreads[i].join();
}
return;
}
// processing storage node
void processStorageNode(int myRank)
{
// storage for chunks
map<int, string> storedChunks;
bool isRunning = true;
// creating a thread to send msgs
thread t(thread_sendHBWrapper, ref(isRunning));
// looping to receive commands from Metadata Server
while (isRunning)
{
// checking for commands
MPI_Status status;
int msgPresent;
MPI_Iprobe(0, MPI_ANY_TAG, MPI_COMM_WORLD, &msgPresent, &status);
// checking if message is present
if (msgPresent)
{
// checking which command
if (status.MPI_TAG == EXIT_TAG)
{
// receiving the sent message & exiting
MPI_Recv(nullptr, 0, MPI_BYTE, 0, EXIT_TAG, MPI_COMM_WORLD, &status);
isRunning = false;
break;
}
else if (status.MPI_TAG == UPLOAD_REQUEST)
{
// receiving the sent message
MPI_Recv(nullptr, 0, MPI_BYTE, 0, UPLOAD_REQUEST, MPI_COMM_WORLD, &status);
// handling the request
opUploadSS(storedChunks);
}
else if (status.MPI_TAG == DOWNLOAD_REQUEST)
{
// receiving the sent message
MPI_Recv(nullptr, 0, MPI_BYTE, 0, DOWNLOAD_REQUEST, MPI_COMM_WORLD, &status);
// handling the request
opRetrieveSS(storedChunks);
}
else if (status.MPI_TAG == SEARCH_REQUEST)
{
// receiving the sent message
MPI_Recv(nullptr, 0, MPI_BYTE, 0, SEARCH_REQUEST, MPI_COMM_WORLD, &status);
// handling the request
opSearchSS(storedChunks);
}
else if (status.MPI_TAG == FAILOVER)
{
// receiving the sent message
MPI_Recv(nullptr, 0, MPI_BYTE, 0, FAILOVER, MPI_COMM_WORLD, &status);
// stopping the thread
isRunning = false;
t.join();
isRunning = true;
}
else if (status.MPI_TAG == RECOVER)
{
// receiving the sent message
MPI_Recv(nullptr, 0, MPI_BYTE, 0, RECOVER, MPI_COMM_WORLD, &status);
// recovering the thread for HB
t = thread(thread_sendHBWrapper, ref(isRunning));
}
else
{
// doing something
continue;
}
}
}
// joining the thread before exiting
if (t.joinable())
t.join();
return;
}
/*
*/
pair<int, string> opUploadMS(long long int &lastChunkID, string fileName, string fileAddress, map<string, FileMetadata> &metadata, unordered_set<int> &activeNodes, set<pair<int, int>> &loadPerNode)
{
// checking if already present
if (metadata.find(fileName) != metadata.end())
{
return {-1, "File already present in the system"};
}
// checking if no Nodes are present
if (activeNodes.empty())
{
return {-1, "No active nodes present"};
}
// chunking it
vector<string> fileChunks;
pair<int, string> retVal = readFileInChunks(fileName, fileAddress, fileChunks);
if (retVal.first == -1)
return retVal;
// updating metadata
metadata[fileName].fileName = fileName;
metadata[fileName].fileAddress = fileAddress;
metadata[fileName].startChunkID = lastChunkID;
metadata[fileName].numChunks = fileChunks.size();
// splitting chunks across ACTIVE servers, RR
for (int i = 0; i < fileChunks.size(); i++)
{
// getting chunk
string chunk = fileChunks[i];
// getting replica nodes
int totalReplicaNodes = min(REPLICATION_FACTOR, static_cast<int>(activeNodes.size()));
set<int> replicaNodes;
vector<pair<int, int>> loads(totalReplicaNodes);
pair<int, int> tempPair;
for (int j = 0; j < totalReplicaNodes; j++)
{
// getting first pair from loadPerNode, which is active too & removing it
for (auto it = loadPerNode.begin(); it != loadPerNode.end(); it++)
{
if (activeNodes.find(it->second) != activeNodes.end())
{
tempPair = *it;
loadPerNode.erase(it);
break;
}
}
// storing
loads[j] = tempPair;
replicaNodes.insert(tempPair.second);
}
// updating metadata
metadata[fileName].chunkPositions[lastChunkID] = replicaNodes;
// sending chunks to replica nodes
for (int j = 0; j < totalReplicaNodes; j++)
{
int nodeRank = loads[j].second;
// letting the node know that there is upload request
MPI_Send(nullptr, 0, MPI_BYTE, nodeRank, UPLOAD_REQUEST, MPI_COMM_WORLD);
// sending chunk ID
MPI_Send(&lastChunkID, 1, MPI_LONG_LONG, nodeRank, UPLOAD_REQUEST, MPI_COMM_WORLD);
// sending chunk data
int chunkSize = chunk.length();
MPI_Send(&chunkSize, 1, MPI_INT, nodeRank, CHUNK_DATA, MPI_COMM_WORLD);
MPI_Send(chunk.c_str(), chunkSize, MPI_CHAR, nodeRank, CHUNK_DATA, MPI_COMM_WORLD);
// MPI_Send(chunk.c_str(), CHUNK_SIZE, MPI_CHAR, nodeRank, CHUNK_DATA, MPI_COMM_WORLD);
// incrementing load
loads[j].first++;
// adding pair back to loadPerNode
loadPerNode.insert(loads[j]);
}
// incrementing last chunk ID
lastChunkID++;
}
return {1, "File uploaded successfully"};
}
void opUploadSS(map<int, string> &storedChunks)
{
// creating variables
char buffer[CHUNK_SIZE];
memset(buffer, 0, CHUNK_SIZE);
long long int chunkID;
// receiving chunk ID
MPI_Recv(&chunkID, 1, MPI_LONG_LONG, 0, UPLOAD_REQUEST, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// receiving chunk data
int chunkSize;
MPI_Recv(&chunkSize, 1, MPI_INT, 0, CHUNK_DATA, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(buffer, chunkSize, MPI_CHAR, 0, CHUNK_DATA, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (chunkSize < CHUNK_SIZE)
buffer[chunkSize] = '\0';
// converting buffer to string
string chunkData = convertCharArrToString(buffer, chunkSize);
// storing the chunk
storedChunks[chunkID] = chunkData;
return;
}
pair<int, string> readFileInChunks(const string &fileName, const string &fileAddress, vector<string> &fileChunks)
{
// opening the file with relative fileAddress
ifstream file(fileAddress, ios::binary); // binary mode
// ifstream file(fileAddress); // text mode
if (!file.is_open())
{
return {-1, "File not found"};
}
// reading the file in 32 byte sized chunks
char buffer[CHUNK_SIZE];
while (file.read(buffer, sizeof(buffer)))
{
// adding full chunk to vector
fileChunks.emplace_back(buffer, sizeof(buffer));
}
// handling the last chunk
streamsize bytesRead = file.gcount(); // Get the number of bytes read
if (bytesRead > 0)
{
// reading the remaning bytes
string lastChunk(buffer, bytesRead);
// // padding with null + `
// lastChunk += '\0';
// lastChunk.append(32 - bytesRead - 1, '`');
// // padding with null
// lastChunk.append(32 - bytesRead, '\0');
fileChunks.push_back(lastChunk);
}
// closing the file
file.close();
return {1, "File read successfully"};
}
/*
*/
void printFileData(string fileData)
{
// printing file data
cout << fileData << endl;
return;
}
pair<int, string> opRetrieveMS(string fileName, map<string, FileMetadata> &metadata, unordered_set<int> &activeNodes)
{
// checking if file is present or not
if (metadata.find(fileName) == metadata.end())
{
return {-1, "File not found"};
}
// getting metadata
FileMetadata fileMD = metadata[fileName];
// iterating over all chunks
string fileData;
for (int i = 0; i < fileMD.numChunks; i++)
{
// getting chunk ID
long long int chunkID = fileMD.startChunkID + i;
// iterating over replica nodes
bool chunkFound = false;
for (int nodeRank : fileMD.chunkPositions[chunkID])
{
// checking if nodeRank is active right now
if (activeNodes.find(nodeRank) != activeNodes.end())
{
// setting chunk as found
chunkFound = true;
// letting the node know taht download request has come
MPI_Send(nullptr, 0, MPI_BYTE, nodeRank, DOWNLOAD_REQUEST, MPI_COMM_WORLD);
// sending chunk ID
MPI_Send(&chunkID, 1, MPI_LONG_LONG, nodeRank, DOWNLOAD_REQUEST, MPI_COMM_WORLD);
// receiving chunk data
char buffer[CHUNK_SIZE];
memset(buffer, 0, CHUNK_SIZE);
int chunkSize;
MPI_Recv(&chunkSize, 1, MPI_INT, nodeRank, CHUNK_DATA, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(buffer, chunkSize, MPI_CHAR, nodeRank, CHUNK_DATA, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (chunkSize < CHUNK_SIZE)
buffer[chunkSize] = '\0';
// MPI_Recv(buffer, CHUNK_SIZE, MPI_CHAR, nodeRank, CHUNK_DATA, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// converting buffer to string
string chunkData = convertCharArrToString(buffer, chunkSize);
// adding to file data
fileData.append(chunkData);
break;
}
}
// checking if chunk was found
if (!chunkFound)
{
return {-1, "File retrieval failed. No active replica found"};
}
}
// // reducing all useless characters from the end of the file
// while (fileData.back() == '`')
// {
// fileData.pop_back();
// }
// while (fileData.back() == '\0')
// {
// fileData.pop_back();
// }
// // counting number of padded null characters at the end
// int count = 0;
// for (int i = fileData.length() - 1; i >= 0; i--)
// {
// if (fileData[i] == '\0')
// {
// count++;
// }
// else
// {
// break;
// }
// }
// cout << "Null Count " << count << endl;
// // printing last character of the file
// if (fileData[fileData.length() - 1] == '\0')
// {
// cout << "Its a null character" << endl;
// }
// else if (fileData[fileData.length() - 1] == '\n')
// {
// cout << "Its a new line character" << endl;
// }
// else if (fileData[fileData.length() - 1] == ' ')
// {
// cout << "Its a space character" << endl;
// }
// else
// {
// cout << "Its a valid character" << endl;
// }
return {1, fileData};
}
void opRetrieveSS(map<int, string> &storedChunks)
{
// receiving chunkID
long long int chunkID;
MPI_Recv(&chunkID, 1, MPI_LONG_LONG, 0, DOWNLOAD_REQUEST, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// sending chunk data
int chunkSize = storedChunks[chunkID].length();
if (chunkSize == CHUNK_SIZE + 1)
chunkSize--;
MPI_Send(&chunkSize, 1, MPI_INT, 0, CHUNK_DATA, MPI_COMM_WORLD);
MPI_Send(storedChunks[chunkID].c_str(), chunkSize, MPI_CHAR, 0, CHUNK_DATA, MPI_COMM_WORLD);
// MPI_Send(storedChunks[chunkID].c_str(), CHUNK_SIZE, MPI_CHAR, 0, CHUNK_DATA, MPI_COMM_WORLD);
}
/*