-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplica.cpp
More file actions
239 lines (194 loc) · 6.66 KB
/
Copy pathReplica.cpp
File metadata and controls
239 lines (194 loc) · 6.66 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
#include "Replica.h"
#include <string>
#include <mutex>
std::mutex commmit_abort_mtx {};
std::mutex push_chunk_mtx {};
#define SERIALIZE_WRITE
int commit_count = 0;
Chunk::Chunk()
{
this->data = "";
}
Chunk::Chunk(std::string arg_data)
{
this->data = arg_data;
}
Json::Value *
FileChunk::dumpJ()
{
Json::Value * result_ptr = new Json::Value();
if (this->data != "")
{
(*result_ptr)["data"] = this->data;
}
return result_ptr;
}
bool
FileChunk::Jdump(Json::Value *input_json_ptr)
{
if ((input_json_ptr == NULL) ||
((*input_json_ptr).isNull() == true) ||
((*input_json_ptr).isObject() != true))
{
return false;
}
if ((((*input_json_ptr)["data"]).isNull() == true) ||
(((*input_json_ptr)["data"]).isString() != true))
{
return false;
}
this->data = ((*input_json_ptr)["data"]).asString();
return true;
}
Replica::Replica
(std::string core_arg_host_url, std::string core_arg_owner_vsID,
std::string core_arg_class_id, std::string core_arg_object_id)
: Core { core_arg_host_url, core_arg_owner_vsID,
core_arg_class_id, core_arg_object_id }
{
std::cout << "a shadow has been created" << std::endl;
}
Replica::Replica
(std::string core_arg_host_url, std::string core_arg_owner_vsID,
std::string core_arg_class_id, std::string core_arg_object_id,
std::string arg_data)
: Core { core_arg_host_url, core_arg_owner_vsID,
core_arg_class_id, core_arg_object_id }
{
(this->committed_data[0]).data = arg_data;
(this->committed_data[1]).data = arg_data;
}
Json::Value
Replica::CommitAbort
(std::string arg_name, std::string arg_fhandle, std::string arg_chunk_index,
std::string arg_commitorabort)
{
Json::Value result;
// this lock is for muti-thread sync, NOT write ordering serialization
std::unique_lock<std::mutex> commmit_abort_lock_section { commmit_abort_mtx };
// std::cout << arg_chunk_index.substr(1, 1).data() << '\n'
int chunk_num = std::atoi(arg_chunk_index.substr(0, 1).data());
if (arg_commitorabort != "commit") {
result["status"] = "abort";
return result;
}
commit_count += 1;
std::cout << std::boolalpha << uncommitted_data[0].valid << " " << uncommitted_data[1].valid << '\n';
std::cout << "Committ to chunk " << chunk_num << '\n';
#ifdef SERIALIZE_WRITE
if (commit_count == 2) {
// both clients have tried to commit, sequence them now
// in general, in the GFS paper, this is handled through locking "lease"
// from master. Active and valid changes to leased files are in an LRU cache
if (uncommitted_data[0].timestamp < uncommitted_data[1].timestamp) {
// commit 0 and then 1
if (uncommitted_data[0].valid)
committed_data[uncommitted_data[0].chunk_index].data = uncommitted_data[0].data;
if (uncommitted_data[1].valid)
committed_data[uncommitted_data[1].chunk_index].data = uncommitted_data[1].data;
}
else {
// commit 1 and then 0
if (uncommitted_data[1].valid)
committed_data[uncommitted_data[1].chunk_index].data = uncommitted_data[1].data;
if (uncommitted_data[0].valid)
committed_data[uncommitted_data[0].chunk_index].data = uncommitted_data[0].data;
}
// invalidate both uncommitted data
uncommitted_data[0].valid = false;
uncommitted_data[1].valid = false;
std::cout << "Chunk 1: " << committed_data[0].data << '\n';
std::cout << "Chunk 2: " << committed_data[1].data << '\n';
}
#else // we just flush the chunk of that client/request
for (auto& data : uncommitted_data) {
if (data.valid && data.chunk_index == chunk_num) {
committed_data[chunk_num].data = data.data;
data.valid = false;
}
}
std::cout << "Chunk " << chunk_num << ": " << committed_data[chunk_num].data << '\n';
#endif
result["vote"] = "commit";
return result;
}
Json::Value
Replica::PushChunk2Replica
(std::string arg_name, std::string arg_fhandle, std::string arg_chunk_index, std::string arg_chunk)
{
Json::Value result;
// this lock is for muti-thread sync, NOT write ordering serialization
std::unique_lock<std::mutex> push_chunk_lock { push_chunk_mtx };
std::cout <<" Line "<< __LINE__ << " Inside the Replica.PushChunk2Replica" << std::endl;
std::cout << arg_chunk << " " << arg_chunk_index << '\n';
int client_num = std::atoi(arg_chunk.substr(1, 1).data()); // chuck is of form "cx-data" where x is client number
int chunk_num = std::atoi(arg_chunk_index.substr(0, 1).data());
std::cout << "Client " << client_num << " pushed to chunk " << chunk_num << " " << arg_chunk << '\n';
#ifdef SERIALIZE_WRITE
int64_t timestamp = std::stoll(arg_chunk_index.substr(1, arg_chunk_index.size() - 1).data());
if (uncommitted_data[client_num].valid) {
if (timestamp > uncommitted_data[client_num].timestamp) {
// the chunk just pushed has a newer timestamp, so replace the uncomitted with this
uncommitted_data[client_num].data = arg_chunk;
uncommitted_data[client_num].timestamp = timestamp;
uncommitted_data[client_num].chunk_index = chunk_num;
}
else {
// this means that the chunk we just received has an older timestamp, arrived late
// so just ignore it
}
}
else {
// new chunk received, add to "queue" (LRU buffer in paper)
uncommitted_data[client_num].valid = true;
uncommitted_data[client_num].data = arg_chunk;
uncommitted_data[client_num].timestamp = timestamp;
uncommitted_data[client_num].chunk_index = chunk_num;
}
#else
// concurrent, just replace the chunk
uncommitted_data[client_num].data = arg_chunk;
uncommitted_data[client_num].chunk_index = chunk_num;
uncommitted_data[client_num].valid = true;
#endif
result["status"] = "success";
return result;
}
Json::Value *
Replica::dumpJ()
{
Json::Value * result_ptr = new Json::Value();
if (this->name != "")
{
(*result_ptr)["name"] = this->name;
}
if (this->fhandle != "")
{
(*result_ptr)["fhandle"] = this->fhandle;
}
// if (this->chunk_index != "")
// {
(*result_ptr)["name"] = this->committed_data[0].data + ", " + this->committed_data[1].data;
// }
return result_ptr;
}
bool
Replica::Jdump(Json::Value *input_json_ptr)
{
if ((input_json_ptr == NULL) ||
((*input_json_ptr).isNull() == true) ||
((*input_json_ptr).isObject() != true))
{
return false;
}
if ((((*input_json_ptr)["name"]).isNull() == true) ||
(((*input_json_ptr)["fhandle"]).isNull() == true) ||
(((*input_json_ptr)["name"]).isString() != true) ||
(((*input_json_ptr)["fhandle"]).isString() != true))
{
return false;
}
this->name = ((*input_json_ptr)["name"]).asString();
this->fhandle = ((*input_json_ptr)["fhandle"]).asString();
return true;
}