-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_server_cache.cc
210 lines (183 loc) · 5.48 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
// the caching lock server implementation
#include "slock.h"
#include "lock_server_cache.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
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_server_cache::lock_server_cache(class rsm *_rsm)
: rsm (_rsm)
{
pthread_t th;
int r = pthread_create(&th, NULL, &revokethread, (void *) this);
assert (r == 0);
r = pthread_create(&th, NULL, &retrythread, (void *) this);
assert (r == 0);
rsm->set_state_transfer(this);
}
void
lock_server_cache::revoker()
{
// This method should be a continuous loop, that sends revoke
// messages to lock holders whenever another client wants the
// same lock
while (true) {
auto lid = revoke_queue.consume();
if (not rsm->amiprimary())
continue;
pthread_mutex_lock(&cache_mutex);
auto lock_holder = cached_locks[lid].owning_client;
rpcc *cl = clients[lock_holder.clt];
pthread_mutex_unlock(&cache_mutex);
int r; assert(cl->call(rlock_protocol::revoke, lid, lock_holder.seq, r) == rlock_protocol::OK);
}
}
void
lock_server_cache::retryer()
{
// This method should be a continuous loop, waiting for locks
// to be released and then sending retry messages to those who
// are waiting for it.
while (true) {
auto lid = retry_queue.consume();
if (not rsm->amiprimary())
continue;
pthread_mutex_lock(&cache_mutex);
lock_info& lock = cached_locks[lid];
if (lock.waiting_clients.empty()) {
pthread_mutex_unlock(&cache_mutex);
continue;
}
auto client = lock.waiting_clients.front();
lock.waiting_clients.pop();
rpcc *cl = clients[client.clt];
pthread_mutex_unlock(&cache_mutex);
int r; assert(cl->call(rlock_protocol::retry, lid, client.seq, r) == rlock_protocol::OK);
}
}
lock_protocol::status
lock_server_cache::subscribe(int clt, std::string dst, int &) {
sockaddr_in dstsock;
make_sockaddr(dst.c_str(), &dstsock);
rpcc *cl = new rpcc(dstsock);
if (cl->bind() < 0) {
printf("lock_server subscribe: connection with the clt: %d failed!\n", clt);
}
pthread_mutex_lock(&cache_mutex);
clients[clt] = cl;
pthread_mutex_unlock(&cache_mutex);
return lock_protocol::OK;
}
lock_protocol::status
lock_server_cache::acquire(int clt, lock_protocol::lockid_t lid, unsigned int seq, int &) {
ScopedLock guard(&cache_mutex);
Client client(clt, seq);
lock_info& lock = cached_locks[lid];
if (lock.status == lock_info::LOCKED) {
lock.waiting_clients.push(client);
lock.status = lock_info::REVOKING;
revoke_queue.add(lid);
return lock_protocol::RETRY;
}
if (lock.status == lock_info::REVOKING) {
lock.waiting_clients.push(client);
return lock_protocol::RETRY;
}
if (lock.status == lock_info::FREE) {
lock.owning_client = client;
if (!lock.waiting_clients.empty()) {
lock.status = lock_info::REVOKING;
revoke_queue.add(lid);
return lock_protocol::OK;
}
lock.status = lock_info::LOCKED;
return lock_protocol::OK;
}
return lock_protocol::RPCERR;
}
lock_protocol::status
lock_server_cache::release(int clt, lock_protocol::lockid_t lid, unsigned int seq, int &) {
ScopedLock guard(&cache_mutex);
Client client(clt, seq);
lock_info& lock = cached_locks[lid];
lock.status = lock_info::FREE;
retry_queue.add(lid);
return lock_protocol::OK;
}
bool operator==(const Client &lhs, const Client &rhs) {
return lhs.clt == rhs.clt && lhs.seq == rhs.seq;
}
bool operator!=(const Client &lhs, const Client &rhs) {
return not(lhs == rhs);
}
std::string lock_server_cache::marshal_state() {
printf("lock_server_cache::marshall state - start !\n");
ScopedLock guard(&cache_mutex);
marshall rep;
rep << (long long unsigned int)cached_locks.size();
for (const auto &l : cached_locks)
rep << l.first << l.second;
printf("lock_server_cache::marshall state - end !\n");
return rep.str();
}
void lock_server_cache::unmarshal_state(std::string state) {
printf("lock_server_cache::marshall state - start!\n");
ScopedLock guard(&cache_mutex);
cached_locks.clear();
unmarshall rep(state);
long long unsigned int lock_size;
rep >> lock_size;
for (size_t i = 0; i < lock_size; i++) {
lock_protocol::lockid_t lid;
lock_info st;
rep >> lid >> st;
cached_locks[lid] = st;
}
printf("lock_server_cache::marshall state - end!\n");
}
marshall &operator<<(marshall &os, const Client &client) {
os << client.clt << client.seq;
return os;
}
unmarshall &operator>>(unmarshall &is, Client &client) {
is >> client.clt >> client.seq;
return is;
}
marshall &operator<<(marshall &os, const lock_info &lock) {
os << (char)lock.status << lock.owning_client << (long long unsigned int)lock.waiting_clients.size();
std::queue<Client> waiting_clients = lock.waiting_clients;
while (!waiting_clients.empty()) {
Client client = waiting_clients.front();
waiting_clients.pop();
os << client;
}
return os;
}
unmarshall &operator>>(unmarshall &is, lock_info &lock) {
char status;
long long unsigned int waiting_clients_size;
Client owning_client;
is >> status >> owning_client >> waiting_clients_size;
lock.status = (lock_info::Status)status;
lock.owning_client = owning_client;
for (size_t i = 0; i < waiting_clients_size; i++) {
Client client;
is >> client;
lock.waiting_clients.push(client);
}
return is;
}