-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlock_client_cache.cc
243 lines (223 loc) · 8.77 KB
/
lock_client_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
// RPC stubs for clients to talk to lock_server, and cache the locks
// see lock_client.cache.h for protocol details.
#include "lock_client_cache.h"
#include "rpc.h"
#include <sstream>
#include <iostream>
#include <stdio.h>
#include "tprintf.h"
static void *
releasethread(void *x)
{
lock_client_cache *cc = (lock_client_cache *) x;
cc->releaser();
return 0;
}
static void *
retrythread(void *x)
{
lock_client_cache *cc = (lock_client_cache *) x;
cc->retryer();
return 0;
}
lock_client_cache::lock_client_cache(std::string xdst,
class lock_release_user *_lu)
: lock_client(xdst), lu(_lu)
{
rpcs *rlsrpc = new rpcs(0);
rlsrpc->reg(rlock_protocol::revoke, this, &lock_client_cache::revoke_handler);
rlsrpc->reg(rlock_protocol::retry, this, &lock_client_cache::retry_handler);
const char *hname;
hname = "127.0.0.1";
std::ostringstream host;
host << hname << ":" << rlsrpc->port();
id = host.str();
rsmc = new rsm_client(xdst);
VERIFY(pthread_mutex_init(&client_cache_mutex,0) == 0);
VERIFY(pthread_cond_init(&client_cache_cv, NULL) == 0);
// Create retryer and releaser threads
pthread_t retryer_thread, releaser_thread;
VERIFY(pthread_mutex_init(&client_retry_mutex,0) == 0);
VERIFY(pthread_cond_init(&client_retry_cv, NULL) == 0);
VERIFY(pthread_mutex_init(&client_releaser_mutex,0) == 0);
VERIFY(pthread_cond_init(&client_releaser_cv, NULL) == 0);
if (pthread_create(&retryer_thread, NULL, &retrythread,(void *) this))
tprintf("Error in creating retryer thread\n");
if (pthread_create(&releaser_thread, NULL, &releasethread,(void *) this))
tprintf("Error in creating releaser thread\n");
// TODO: destroy these
}
lock_protocol::status
lock_client_cache::acquire(lock_protocol::lockid_t lid)
{
int ret = lock_protocol::OK, r;
lock_cache_value *lock_cache_obj;
//tprintf("In Client Acquire for lid:%llu,tid: %lu, id: %s\n", lid, pthread_self(), id.c_str());
lock_cache_obj = get_lock_obj(lid);
pthread_mutex_lock(&lock_cache_obj->client_lock_mutex);
while(true) {
if (lock_cache_obj->lock_cache_state == NONE) {
// send acquire rpc
tprintf("in None case,tid: %lu\n", pthread_self());
lock_cache_obj->lock_cache_state = ACQUIRING;
lock_cache_obj->doflush = false;
tprintf("Called server for Acquire, tid: %lu, id: %s\n", pthread_self(), id.c_str());
ret = rsmc->call(lock_protocol::acquire, id, lid, r);
tprintf("Responded back, tid:%lu\n", pthread_self());
if (ret == lock_protocol::OK) {
lock_cache_obj->lock_cache_state = LOCKED;
break;
}
else if (ret == lock_protocol::RETRY) {
// wait on condition to be notified by retry RPC
pthread_cond_wait(&lock_cache_obj->client_lock_cv,
&lock_cache_obj->client_lock_mutex);
tprintf("client_cache-acquire: out from condition\n");
if (FREE == lock_cache_obj->lock_cache_state) {
lock_cache_obj->lock_cache_state = LOCKED;
break;
}
else continue;
}
}
else if (lock_cache_obj->lock_cache_state == FREE) {
tprintf("in FREE case,tid: %lu\n", pthread_self());
lock_cache_obj->lock_cache_state = LOCKED;
break;
}
else {
pthread_cond_wait(&lock_cache_obj->client_lock_cv,
&lock_cache_obj->client_lock_mutex);
if (FREE == lock_cache_obj->lock_cache_state) {
lock_cache_obj->lock_cache_state = LOCKED;
break;
}
else continue;
}
}
lock_cache_obj->doflush = true;
pthread_mutex_unlock(&lock_cache_obj->client_lock_mutex);
return lock_protocol::OK;
}
lock_protocol::status
lock_client_cache::release(lock_protocol::lockid_t lid)
{
lock_cache_value *lock_cache_obj = get_lock_obj(lid);
pthread_mutex_lock(&lock_cache_obj->client_lock_mutex);
if (lock_cache_obj->lock_cache_state == RELEASING)
pthread_cond_signal(&lock_cache_obj->client_revoke_cv);
else if (lock_cache_obj->lock_cache_state == LOCKED) {
lock_cache_obj->lock_cache_state = FREE;
pthread_cond_signal(&lock_cache_obj->client_lock_cv);
}
else
tprintf("unknown state in release\n");
pthread_mutex_unlock(&lock_cache_obj->client_lock_mutex);
return lock_protocol::OK;
}
lock_client_cache::lock_cache_value*
lock_client_cache::get_lock_obj(lock_protocol::lockid_t lid)
{
lock_cache_value *lock_cache_obj;
pthread_mutex_lock(&client_cache_mutex);
if (clientcacheMap.count(lid) > 0) {
lock_cache_obj = clientcacheMap[lid];
//printf("found!!!!\n");
}
else {
lock_cache_obj = new lock_cache_value();
VERIFY(pthread_mutex_init(&lock_cache_obj->client_lock_mutex,0) == 0);
VERIFY(pthread_cond_init(&lock_cache_obj->client_lock_cv, NULL) == 0);
VERIFY(pthread_cond_init(&lock_cache_obj->client_revoke_cv, NULL) == 0);
clientcacheMap[lid] = lock_cache_obj;
//printf("allocating new\n");
}
pthread_mutex_unlock(&client_cache_mutex);
return lock_cache_obj;
}
void
lock_client_cache::retryer(void) {
int r;
int ret;
while(true) {
pthread_mutex_lock(&client_retry_mutex);
pthread_cond_wait(&client_retry_cv, &client_retry_mutex);
while (!retry_list.empty()) {
lock_protocol::lockid_t lid = retry_list.front();
retry_list.pop_front();
lock_cache_value *lock_cache_obj = get_lock_obj(lid);
tprintf("retryer: for lid:%llu\n", lid);
pthread_mutex_lock(&lock_cache_obj->client_lock_mutex);
ret = rsmc->call(lock_protocol::acquire, id, lid, r);
if (ret == lock_protocol::OK) {
lock_cache_obj->lock_cache_state = FREE;
pthread_cond_signal(&lock_cache_obj->client_lock_cv);
pthread_mutex_unlock(&lock_cache_obj->client_lock_mutex);
}
else
tprintf("retry fail, should never happen, ret:%d\n", ret);
}
pthread_mutex_unlock(&client_retry_mutex);
}
}
void
lock_client_cache::releaser(void) {
int r,ret;
// bool doflush=true;
while(true) {
pthread_mutex_lock(&client_releaser_mutex);
pthread_cond_wait(&client_releaser_cv, &client_releaser_mutex);
while (!revoke_list.empty()) {
lock_protocol::lockid_t lid = revoke_list.front();
revoke_list.pop_front();
lock_cache_value *lock_cache_obj = get_lock_obj(lid);
pthread_mutex_lock(&lock_cache_obj->client_lock_mutex);
tprintf("releaser: got lock checking\n");
if (lock_cache_obj->lock_cache_state == LOCKED) {
lock_cache_obj->lock_cache_state = RELEASING;
tprintf("releaser: waiting in releasing state\n");
pthread_cond_wait(&lock_cache_obj->client_revoke_cv,
&lock_cache_obj->client_lock_mutex);
}
// else if (lock_cache_obj->lock_cache_state == ACQUIRING) {
// tprintf("releaser: calling server release, id: ACQUIRING\n");
// doflush = false;
// }
tprintf("releaser: calling server release, id: %s\n", id.c_str());
if(lock_cache_obj->doflush) {
tprintf("releaser: state: %d\n", lock_cache_obj->lock_cache_state);
lu->dorelease(lid);
}
ret = rsmc->call(lock_protocol::release, id, lid, r);
lock_cache_obj->lock_cache_state = NONE;
pthread_cond_signal(&lock_cache_obj->client_lock_cv);
pthread_mutex_unlock(&lock_cache_obj->client_lock_mutex);
}
pthread_mutex_unlock(&client_releaser_mutex);
}
}
rlock_protocol::status
lock_client_cache::revoke_handler(lock_protocol::lockid_t lid,
int &)
{
int ret = rlock_protocol::OK;
tprintf("got revoke from server for lid:%llu\n", lid);
pthread_mutex_lock(&client_releaser_mutex);
revoke_list.push_back(lid);
pthread_cond_signal(&client_releaser_cv);
pthread_mutex_unlock(&client_releaser_mutex);
return ret;
}
rlock_protocol::status
lock_client_cache::retry_handler(lock_protocol::lockid_t lid,
int &)
{
int ret = rlock_protocol::OK;
// Push the lid to the retry list
tprintf("got retry from server for lid:%llu\n", lid);
pthread_mutex_lock(&client_retry_mutex);
retry_list.push_back(lid);
pthread_cond_signal(&client_retry_cv);
pthread_mutex_unlock(&client_retry_mutex);
return ret;
}