-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock_server_cache.cc
185 lines (166 loc) · 6.45 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
// the caching lock server implementation
#include "lock_server_cache.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "lang/verify.h"
#include "handle.h"
#include "tprintf.h"
lock_server_cache::lock_server_cache()
{
init();
}
void
lock_server_cache::init()
{
lock_state_map.clear();
VERIFY(pthread_mutex_init(&lock_mutex, 0) == 0);
}
lock_protocol::status
lock_server_cache::acquire(lock_protocol::lockid_t lid, std::string id,
int &r)
{
bool revoke = false;
std::string revoke_client;
lock_protocol::status ret = lock_protocol::OK;
std::map<lock_protocol::lockid_t, slock_status>::iterator iter;
{
ScopedLock ml(&lock_mutex);
server_acquire_count++;
tprintf("%d id %s lid %d\n", server_acquire_count, id.c_str(), (int)lid);
iter = lock_state_map.find(lid);
if (iter == lock_state_map.end()) {
iter = lock_state_map.insert(std::make_pair(lid, slock_status())).first;
VERIFY (iter != lock_state_map.end());
}
switch (iter->second.slstatus) {
case FREE: {
iter->second.slstatus = LOCKED;
iter->second.id = id;
tprintf(" acquire lid %d id %s\n", (int)lid, id.c_str());
}
break;
case LOCKED: {
if (id != iter->second.id) {
iter->second.slstatus = WAITING;
revoke = true;
revoke_client = iter->second.id;
iter->second.wait_clients.insert(id);//唯一性
ret = lock_protocol::RETRY;
tprintf(" acquire begin wait lid %d id %s, owner %s, size %d\n", (int)lid, id.c_str(), iter->second.id.c_str(), (int)iter->second.wait_clients.size());
}
}
break;
case WAITING: {
if (id != iter->second.id) {
iter->second.wait_clients.insert(id);//唯一性
ret = lock_protocol::RETRY;
tprintf(" acquire wait queue lid %d id %s, size %d\n", (int)lid, id.c_str(), (int)iter->second.wait_clients.size());
}
}
break;
case RETRYING: {
if (iter->second.wait_clients.count(id)) {
iter->second.wait_clients.erase(id);
iter->second.id = id;
iter->second.slstatus = LOCKED;
if (iter->second.wait_clients.size()) {
iter->second.slstatus = WAITING;
revoke = true;
revoke_client = id;
tprintf(" acquire 1 lid %d id %s, size %d\n", (int)lid, id.c_str(), (int)iter->second.wait_clients.size());
} else {
tprintf(" acquire 2 lid %d id %s\n", (int)lid, id.c_str());
}
} else {
iter->second.wait_clients.insert(id);
ret = lock_protocol::RETRY;
tprintf(" acquire wait queue lid %d id %s, size %d\n", (int)lid, id.c_str(), (int)iter->second.wait_clients.size());
}
}
break;
default:
break;
}
}
if (revoke) {
int cnt = 0;
rlock_protocol::status ret = 1;
while (ret != 0 && cnt < 7) {
cnt++;
ret = handle(revoke_client).safebind()->call(rlock_protocol::revoke, lid, r);
tprintf("%s revoke %s ret : %d\n", id.c_str(), revoke_client.c_str(), ret);
}
}
return ret;
}
lock_protocol::status
lock_server_cache::release(lock_protocol::lockid_t lid, std::string id,
int &r)
{
bool retry = false;
std::string retry_client;
lock_protocol::status ret = lock_protocol::OK;
std::map<lock_protocol::lockid_t, slock_status>::iterator iter;
{
ScopedLock ml(&lock_mutex);
server_acquire_count++;
tprintf("%d id %s lid %d\n", server_acquire_count, id.c_str(), (int)lid);
iter = lock_state_map.find(lid);
if (iter == lock_state_map.end()) {
iter = lock_state_map.insert(std::make_pair(lid, slock_status())).first;
VERIFY (iter != lock_state_map.end());
}
switch (iter->second.slstatus) {
case FREE: {
tprintf("ERROR: id %s release a lid %d when it's state is FREE\n", id.c_str(), (int)lid);//这个说明client的状态跟server的不同步
ret = lock_protocol::IOERR;
}
break;
case LOCKED: {
if (id == iter->second.id) {
iter->second.slstatus = FREE;
iter->second.id = "";
tprintf(" release lid %d id %s\n", (int)lid, id.c_str());
}
}
break;
case WAITING: {
if (id == iter->second.id) {
iter->second.slstatus = RETRYING;
iter->second.id = "";
retry = true;
retry_client = *iter->second.wait_clients.begin();
tprintf(" release retry lid %d id %s retry id %s , size %d\n", (int)lid, id.c_str(), retry_client.c_str(), (int)iter->second.wait_clients.size());
}
}
break;
case RETRYING: {
tprintf("ERROR: id %s release a lid %d when it's state is RETRYING\n", id.c_str(), (int)lid);
ret = lock_protocol::IOERR;
}
break;
default:
break;
}
}
if (retry) {
int cnt = 0;
rlock_protocol::status ret = 1;
while (ret != 0 && cnt < 7) {
cnt++;
ret = handle(retry_client).safebind()->call(rlock_protocol::retry, lid, r);
tprintf("%s retry %s ret : %d\n", id.c_str(), retry_client.c_str(), ret);
}
}
return ret;
}
lock_protocol::status
lock_server_cache::stat(lock_protocol::lockid_t lid, std::string id,
int &r)
{
tprintf("stat request\n");
r = nacquire;
return lock_protocol::OK;
}