-
Notifications
You must be signed in to change notification settings - Fork 0
/
extent_server_dist.cc
203 lines (159 loc) · 5.97 KB
/
extent_server_dist.cc
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
#include "extent_server_dist.h"
chfs_raft *extent_server_dist::leader() const {
EXTENT_SERVER_DIST_LOG("step in");
int leader = this->raft_group->check_exact_one_leader();
if (leader < 0) {
EXTENT_SERVER_DIST_LOG("leader < 0");
return this->raft_group->nodes[0];
} else {
EXTENT_SERVER_DIST_LOG("leader >= 0");
return this->raft_group->nodes[leader];
}
}
int extent_server_dist::create(uint32_t type, extent_protocol::extentid_t &id) {
EXTENT_SERVER_DIST_LOG("step in");
// Lab3: your code here
int term, index;
// construct command
chfs_command_raft cmd(chfs_command_raft::command_type::CMD_CRT, type, id, std::string(""));
EXTENT_SERVER_DIST_LOG("chfs_command_raft constructed");
bool new_command_sent = false;
do {
new_command_sent = leader()->new_command(cmd, term, index);
mssleep(sleep_interval_milliseconds);
} while (!new_command_sent);
EXTENT_SERVER_DIST_LOG("new_command successfully sent to leader");
if (new_command_sent) {
std::unique_lock<std::mutex> lock(cmd.res->mtx);
if (!cmd.res->done) {
ASSERT(
cmd.res->cv.wait_until(lock,
std::chrono::system_clock::now() + std::chrono::milliseconds(cv_timeout_milliseconds))
== std::cv_status::no_timeout,
"extent_server_dist::create command timeout");
}
// get return result
id = cmd.res->id;
return extent_protocol::OK;
}
return extent_protocol::xxstatus::RPCERR;
}
int extent_server_dist::put(extent_protocol::extentid_t id, std::string buf, int &) {
EXTENT_SERVER_DIST_LOG("step in");
// Lab3: your code here
int term, index;
uint32_t type;
// construct command
chfs_command_raft cmd(chfs_command_raft::command_type::CMD_PUT, type, id, buf);
EXTENT_SERVER_DIST_LOG("chfs_command_raft constructed");
bool new_command_sent = false;
do {
EXTENT_SERVER_DIST_LOG("prepare to send new_command to leader");
new_command_sent = leader()->new_command(cmd, term, index);
mssleep(sleep_interval_milliseconds);
} while (!new_command_sent);
EXTENT_SERVER_DIST_LOG("new_command successfully sent to leader, id %llu, buf size %llu", cmd.id, buf.size());
if (new_command_sent) {
std::unique_lock<std::mutex> lock(cmd.res->mtx);
if (!cmd.res->done) {
EXTENT_SERVER_DIST_LOG("cv.wait_until start");
ASSERT(
cmd.res->cv.wait_until(lock,
std::chrono::system_clock::now() + std::chrono::milliseconds(cv_timeout_milliseconds))
== std::cv_status::no_timeout,
"extent_server_dist::put command timeout");
}
// get return result
return extent_protocol::OK;
}
return extent_protocol::xxstatus::RPCERR;
}
int extent_server_dist::get(extent_protocol::extentid_t id, std::string &buf) {
EXTENT_SERVER_DIST_LOG("step in");
// Lab3: your code here
int term, index;
uint32_t type;
std::string buf_para;
// construct command
chfs_command_raft cmd(chfs_command_raft::command_type::CMD_GET, type, id, buf_para);
bool new_command_sent = false;
do {
new_command_sent = leader()->new_command(cmd, term, index);
mssleep(sleep_interval_milliseconds);
} while (!new_command_sent);
if (new_command_sent) {
std::unique_lock<std::mutex> lock(cmd.res->mtx);
EXTENT_SERVER_DIST_LOG("new_command successfully sent to leader, id %llu", cmd.id);
if (!cmd.res->done) {
ASSERT(
cmd.res->cv.wait_until(lock,
std::chrono::system_clock::now() + std::chrono::milliseconds(cv_timeout_milliseconds))
== std::cv_status::no_timeout,
"extent_server_dist::get command timeout");
}
// get return result
buf = cmd.res->buf;
return extent_protocol::OK;
}
return extent_protocol::xxstatus::RPCERR;
}
int extent_server_dist::getattr(extent_protocol::extentid_t id, extent_protocol::attr &a) {
EXTENT_SERVER_DIST_LOG("step in");
// Lab3: your code here
int term, index;
uint32_t type;
std::string buf_para;
// construct command
chfs_command_raft cmd(chfs_command_raft::command_type::CMD_GETA, type, id, buf_para);
bool new_command_sent = false;
do {
new_command_sent = leader()->new_command(cmd, term, index);
mssleep(sleep_interval_milliseconds);
} while (!new_command_sent);
if (new_command_sent) {
std::unique_lock<std::mutex> lock(cmd.res->mtx);
EXTENT_SERVER_DIST_LOG("new_command successfully sent to leader, id %llu", cmd.id);
if (!cmd.res->done) {
ASSERT(
cmd.res->cv.wait_until(lock,
std::chrono::system_clock::now() + std::chrono::milliseconds(cv_timeout_milliseconds))
== std::cv_status::no_timeout,
"extent_server_dist::getattr command timeout");
}
// get return result
a = cmd.res->attr;
return extent_protocol::OK;
}
return extent_protocol::xxstatus::RPCERR;
}
int extent_server_dist::remove(extent_protocol::extentid_t id, int &) {
EXTENT_SERVER_DIST_LOG("step in");
// Lab3: your code here
int term, index;
uint32_t type;
std::string buf_para;
// construct command
chfs_command_raft cmd(chfs_command_raft::command_type::CMD_RMV, type, id, buf_para);
bool new_command_sent = false;
do {
new_command_sent = leader()->new_command(cmd, term, index);
mssleep(sleep_interval_milliseconds);
} while (!new_command_sent);
if (new_command_sent) {
std::unique_lock<std::mutex> lock(cmd.res->mtx);
EXTENT_SERVER_DIST_LOG("new_command successfully sent to leader, id %llu", cmd.id);
if (!cmd.res->done) {
ASSERT(
cmd.res->cv.wait_until(lock,
std::chrono::system_clock::now() + std::chrono::milliseconds(cv_timeout_milliseconds))
== std::cv_status::no_timeout,
"extent_server_dist::remove command timeout");
}
// get return result
return extent_protocol::OK;
}
return extent_protocol::xxstatus::RPCERR;
}
extent_server_dist::~extent_server_dist() {
delete this->raft_group;
}