1 // the caching lock server implementation
3 #include "lock_server.h"
8 lock_state::lock_state():
13 lock_state::lock_state(const lock_state & other) {
17 lock_state & lock_state::operator=(const lock_state & o) {
20 wanted_by = o.wanted_by;
21 old_requests = o.old_requests;
25 lock_state & lock_server::get_lock_state(lock_protocol::lockid_t lid) {
26 lock sl(lock_table_lock);
27 // this will create the lock if it doesn't already exist
28 return lock_table[lid];
31 lock_server::lock_server(rsm & r) : rsm_ (&r) {
32 thread(&lock_server::revoker, this).detach();
33 thread(&lock_server::retryer, this).detach();
34 r.set_state_transfer(this);
36 r.reg(lock_protocol::acquire, &lock_server::acquire, this);
37 r.reg(lock_protocol::release, &lock_server::release, this);
38 r.reg(lock_protocol::stat, &lock_server::stat, this);
41 void lock_server::revoker () {
43 lock_protocol::lockid_t lid;
44 revoke_fifo.deq(&lid);
45 LOG << "Revoking " << lid;
46 if (rsm_ && !rsm_->amiprimary())
49 lock_state & st = get_lock_state(lid);
59 //while (t-- && !proxy)
60 proxy = handle(held_by.first).safebind();
63 auto ret = (rlock_protocol::status)proxy->call(rlock_protocol::revoke, r, lid, held_by.second);
64 LOG << "Revoke returned " << ret;
69 void lock_server::retryer() {
71 lock_protocol::lockid_t lid;
73 if (rsm_ && !rsm_->amiprimary())
76 LOG << "Sending retry for " << lid;
77 lock_state & st = get_lock_state(lid);
81 if (st.wanted_by.empty())
83 front = st.wanted_by.front();
89 //while (t-- && !proxy)
90 proxy = handle(front.first).safebind();
93 auto ret = (rlock_protocol::status)proxy->call(rlock_protocol::retry, r, lid, front.second);
94 LOG << "Retry returned " << ret;
99 lock_protocol::status lock_server::acquire(int &, lock_protocol::lockid_t lid, const callback_t & id, lock_protocol::xid_t xid) {
100 LOG << "lid=" << lid << " client=" << id << "," << xid;
101 holder_t h = holder_t(id, xid);
102 lock_state & st = get_lock_state(lid);
105 // deal with duplicated requests
106 if (st.old_requests.count(id)) {
107 lock_protocol::xid_t old_xid = st.old_requests[id];
109 return lock_protocol::RPCERR;
110 else if (old_xid == xid) {
111 if (st.held && st.held_by == h) {
112 LOG << "Client " << id << " sent duplicate acquire xid=" << xid;
113 return lock_protocol::OK;
118 // grant the lock if it's available and I'm next in line
119 if (!st.held && (st.wanted_by.empty() || st.wanted_by.front() == h)) {
120 if (!st.wanted_by.empty())
121 st.wanted_by.pop_front();
122 st.old_requests[id] = xid;
126 LOG << "Lock " << lid << " held by " << h.first;
127 if (st.wanted_by.size())
128 revoke_fifo.enq(lid);
129 return lock_protocol::OK;
134 for (auto p : st.wanted_by) {
136 // make sure client is obeying serialization
137 if (p.second != xid) {
138 LOG << "Client " << id << " sent acquire xid=" << xid << " with in-progress xid=" << p.second;
139 return lock_protocol::RPCERR;
146 st.wanted_by.push_back(h);
148 LOG << "wanted_by=" << st.wanted_by;
150 // send revoke if we're first in line
151 if (st.wanted_by.front() == h)
152 revoke_fifo.enq(lid);
154 return lock_protocol::RETRY;
157 lock_protocol::status lock_server::release(int &, lock_protocol::lockid_t lid, const callback_t & id, lock_protocol::xid_t xid) {
158 LOG << "lid=" << lid << " client=" << id << "," << xid;
159 lock_state & st = get_lock_state(lid);
161 if (st.held && st.held_by == holder_t(id, xid)) {
163 LOG << "Lock " << lid << " not held";
165 if (st.wanted_by.size())
167 return lock_protocol::OK;
170 string lock_server::marshal_state() {
171 lock sl(lock_table_lock);
172 return marshall(nacquire, lock_table).content();
175 void lock_server::unmarshal_state(const string & state) {
176 lock sl(lock_table_lock);
177 unmarshall(state, false, nacquire, lock_table);
180 lock_protocol::status lock_server::stat(int & r, lock_protocol::lockid_t lid, const callback_t &) {
181 LOG << "stat request for " << lid;
184 return lock_protocol::OK;