-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_server_cache.cc
395 lines (363 loc) · 13.4 KB
/
lock_server_cache.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
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
// the caching lock server implementation
/*
This is an implementation of two phase locking protocol.
First acquire rpc call is always a prepare, even if the
lock was actually free. This reduces the amount of veri-
-fication we need to do in order to grant the locking pe-
-ermissions. The client resends the acquire message after
it actually receives the retry rpc call from the server.
*/
#include "lock_server_cache.h"
#include "lock_client_cache.h"
#include "connection_handler.cc"
#include "marshall.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <cstdlib>
#include <stdlib.h>
#include <arpa/inet.h>
#include <map>
#include <sys/stat.h>
//#include <PubSub/PubSub.h>
//#include <Foundation/Foundation.h>
pthread_mutex_t retryer_lock;
pthread_mutex_t revoker_lock;
std::vector<client_info> retry_queue;
std::vector<client_info> revoke_queue;
pthread_mutex_t table_lock;
pthread_mutex_t connections_lock;
std::map<lock_protocol::lockid_t, lock_t> lock_catalog;
std::map<int, connection_handler *> connections;
std::string itoa(int i) {
std::string s = "";
if (i == 0)
return "0";
while (i > 0) {
s = (char)(i%10 + 48) + s;
i = i / 10;
}
return s;
}
int portaddr(std::string addr) {
int p;
addr = addr.substr(addr.find(":") + 1, addr.length() - addr.find(":") + 1);
p = atoi(addr.c_str());
return p;
}
static void *revokethread(void *x) {
lock_server_cache *sc = (lock_server_cache *) x;
sc->revoker();
return 0;
}
static void *retrythread(void *x) {
lock_server_cache *sc = (lock_server_cache *) x;
sc->retryer();
return 0;
}
lock_t ParseLock(std::vector<std::string> &v, lock_protocol::lockid_t &lid);
client_info ParseClientInfo(std::vector<std::string> &v);
std::vector<client_info> ParseFifo(std::vector<std::string> &v);
void PackLock(std::vector<std::string> &v,lock_protocol::lockid_t lid, lock_t l);
void PackClientInfo(std::vector<std::string> &v, client_info cl);
void PackFifo(std::vector<std::string> &v, std::vector <client_info> f);
lock_server_cache::lock_server_cache(class rsm *_rsm)
: rsm(_rsm) {
pthread_mutex_init(&table_lock, NULL);
pthread_mutex_init(&retryer_lock, NULL);
pthread_mutex_init(&revoker_lock, NULL);
pthread_t th;
rsm->set_state_transfer(this);
int r = pthread_create(&th, NULL, &revokethread, (void *) this);
assert (r == 0);
r = pthread_create(&th, NULL, &retrythread, (void *) this);
assert (r == 0);
}
void
lock_server_cache::revoker() {
/* when woke up create a new thread to handle the revoke so that multiple revokes can be invoked at the same time */
client_info revoke_client;
connection_handler *cl_handle;
std::map<int, connection_handler *>::iterator it;
int ret;
while (1) {
pthread_mutex_lock(&revoker_lock);
if(revoke_queue.size() == 0) {
pthread_mutex_unlock(&revoker_lock);
continue;
}
revoke_client = revoke_queue.front();
revoke_queue.erase(revoke_queue.begin());
pthread_mutex_unlock(&revoker_lock);
ret = portaddr(revoke_client.client_address);
pthread_mutex_lock(&connections_lock);
it = connections.find(ret);
if (it != connections.end()) {
cl_handle = connections[ret];
pthread_mutex_unlock(&connections_lock);
}
else {
cl_handle = new connection_handler(revoke_client.client_address);
connections[ret] = cl_handle;
pthread_mutex_unlock(&connections_lock);
}
if (rsm->amiprimary()) {
bool t = true;
pthread_mutex_lock(&lock_catalog[revoke_client.lockid].entry_lock);
if (lock_catalog[revoke_client.lockid].current.client_address != revoke_client.client_address)
t = false;
pthread_mutex_unlock(&lock_catalog[revoke_client.lockid].entry_lock);
if (t)
cl_handle->revoke(revoke_client.lockid, ret);
}
}
}
void
lock_server_cache::retryer() {
/* when wake up, create a new thread to handle the retry so that multiple retries at the same time */
client_info retry_client;
connection_handler *cl_handle;
std::map<int, connection_handler *>::iterator it;
int ret;
while (1) {
pthread_mutex_lock(&retryer_lock);
if(retry_queue.size() == 0) {
pthread_mutex_unlock(&retryer_lock);
continue;
}
retry_client = retry_queue.front();
retry_queue.erase(retry_queue.begin());
pthread_mutex_unlock(&retryer_lock);
ret = portaddr(retry_client.client_address);
pthread_mutex_lock(&connections_lock);
it = connections.find(ret);
if (it != connections.end()) {
cl_handle = connections[ret];
pthread_mutex_unlock(&connections_lock);
}
else {
cl_handle = new connection_handler(retry_client.client_address);
connections[ret] = cl_handle;
pthread_mutex_unlock(&connections_lock);
}
if (rsm->amiprimary())
cl_handle->retry(retry_client.lockid, ret);
}
}
lock_protocol::status lock_server_cache::acquire(std::string clt, lock_protocol::lockid_t lid, int &r) {
client_info lock_request;
lock_request.client_address = clt;
lock_request.lockid = lid;
lock_request.seq_number = 0;
pthread_mutex_lock(&table_lock);
std::map<lock_protocol::lockid_t, lock_t>::iterator it = lock_catalog.find(lid);
if (it != lock_catalog.end()) {
/*Existing Lock*/
pthread_mutex_lock(&(it->second).entry_lock);
pthread_mutex_unlock(&table_lock);
std::vector<client_info>::iterator itv;
switch ((it->second).status) {
case LOCKED:
if ((it->second).current.client_address == clt) {
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::OK;
}
itv = (it->second).waiting_queue.begin();
for (; itv != (it->second).waiting_queue.end(); ++itv) {
if ((*itv).client_address == clt || clt == (it->second).next.client_address) {
revoke_queue.push_back((it->second).current);
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::RETRY;
}
}
(it->second).status = REVOKING;
(it->second).waiting_queue.push_back(lock_request);
revoke_queue.push_back((it->second).current);
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::RETRY;
case REVOKING:
if ((it->second).current.client_address == clt) {
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::OK;
}
itv = (it->second).waiting_queue.begin();
for (; itv != (it->second).waiting_queue.end(); ++itv) {
if ((*itv).client_address == clt || clt == (it->second).next.client_address) {
revoke_queue.push_back((it->second).current);
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::RETRY;
}
}
(it->second).waiting_queue.push_back(lock_request);
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::RETRY;
case RETRYING:
if (clt == (it->second).next.client_address) {
/*GRANT*/
if ((it->second).waiting_queue.size()) {
(it->second).current = (it->second).next;
revoke_queue.push_back((it->second).current);
(it->second).status = REVOKING;
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::OK;
}
else {
(it->second).current = (it->second).next;
(it->second).status = LOCKED;
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::OK;
}
}
else {
itv = (it->second).waiting_queue.begin();
for (; itv != (it->second).waiting_queue.end(); ++itv) {
if ((*itv).client_address == clt || clt == (it->second).next.client_address) {
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::RETRY;
}
}
(it->second).waiting_queue.push_back(lock_request);
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::RETRY;
}
default:
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::RETRY;
}
}
else {
/*New Lock*/
lock_t newlock;
newlock.status = LOCKED;
newlock.current = lock_request;
pthread_mutex_init(&newlock.entry_lock, NULL);
client_info next;
next.client_address = "";
next.lockid = lid;
next.seq_number = 0;
newlock.next = next;
lock_catalog[lid] = newlock;
pthread_mutex_unlock(&table_lock);
return lock_protocol::OK;
}
}
lock_protocol::status lock_server_cache::release(std::string clt, lock_protocol::lockid_t lid, int &r) {
pthread_mutex_lock(&table_lock);
std::map<lock_protocol::lockid_t, lock_t>::iterator it = lock_catalog.find(lid);
pthread_mutex_lock(&(it->second).entry_lock);
pthread_mutex_unlock(&table_lock);
if (clt == (it->second).current.client_address) {
(it->second).status = RETRYING;
(it->second).next = (it->second).waiting_queue.front();
(it->second).waiting_queue.erase((it->second).waiting_queue.begin());
retry_queue.push_back((it->second).next);
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::OK;
}
else {
pthread_mutex_unlock(&(it->second).entry_lock);
return lock_protocol::OK;
}
}
lock_protocol::status lock_server_cache::stat(lock_protocol::lockid_t lid, int &r) {
return lock_protocol::OK;
}
std::string lock_server_cache::marshal_state() {
std::vector<std::string> v;
v.clear();
pthread_mutex_lock(&table_lock);
v.push_back(itoa(lock_catalog.size()));
for(std::map<lock_protocol::lockid_t, lock_t>::iterator it = lock_catalog.begin(); it != lock_catalog.end(); ++it)
{
PackLock(v, it->first, it->second);
}
pthread_mutex_unlock(&table_lock);
pthread_mutex_lock(&retryer_lock);
PackFifo(v, retry_queue);
pthread_mutex_unlock(&retryer_lock);
pthread_mutex_lock(&revoker_lock);
PackFifo(v, revoke_queue);
pthread_mutex_unlock(&revoker_lock);
marshall m;
m << v;
return m.str();
}
void lock_server_cache::unmarshal_state(std::string st) {
unmarshall u(st);
std::vector<std::string> v;
v.clear();
u >> v;
int n = atoi(v.front().c_str());
v.erase(v.begin());
pthread_mutex_lock(&table_lock);
for (int i = 0; i < n; ++i)
{
lock_protocol::lockid_t lid;
lock_t l = ParseLock(v, lid);
lock_catalog[lid] = l;
}
pthread_mutex_unlock(&table_lock);
pthread_mutex_lock(&retryer_lock);
retry_queue = ParseFifo(v);
pthread_mutex_unlock(&retryer_lock);
pthread_mutex_lock(&revoker_lock);
revoke_queue = ParseFifo(v);
pthread_mutex_unlock(&revoker_lock);
}
lock_t ParseLock(std::vector<std::string> &v,lock_protocol::lockid_t &lid) {
lid = atoi(v.front().c_str());
v.erase(v.begin());
int stat = atoi(v.front().c_str());
v.erase(v.begin());
client_info current = ParseClientInfo(v);
client_info next = ParseClientInfo(v);
std::vector <client_info> wq = ParseFifo(v);
lock_t l;
l.status = (lock_status_)stat;
l.current = current;
l.next = next;
pthread_mutex_init(&l.entry_lock, NULL);
l.waiting_queue = wq;
return l;
}
client_info ParseClientInfo(std::vector<std::string> &v) {
client_info cl;
cl.lockid = atoi(v.front().c_str());
v.erase(v.begin());
cl.client_address = v.front();
v.erase(v.begin());
cl.seq_number = atoi(v.front().c_str());
v.erase(v.begin());
return cl;
}
std::vector <client_info> ParseFifo(std::vector<std::string> &v) {
std::vector <client_info> q;
int n = atoi(v.front().c_str());
v.erase(v.begin());
for (int i = 0; i < n; ++i)
{
q.push_back(ParseClientInfo(v));
}
return q;
}
void PackLock(std::vector<std::string> &v,lock_protocol::lockid_t lid, lock_t l) {
int l_id = lid;
v.push_back(itoa(l_id));
v.push_back(itoa((int)l.status));
PackClientInfo(v, l.current);
PackClientInfo(v, l.next);
PackFifo(v, l.waiting_queue);
}
void PackClientInfo(std::vector<std::string> &v, client_info cl) {
int l_id = cl.lockid;
v.push_back(itoa(l_id));
v.push_back(cl.client_address);
std::string k(itoa(cl.seq_number));
v.push_back(k);
}
void PackFifo(std::vector<std::string> &v, std::vector<client_info> f) {
v.push_back(itoa(f.size()));
for (std::vector<client_info>::iterator it = f.begin(); it != f.end(); ++it) {
PackClientInfo(v, *it);
}
}