MCPcopy Create free account
hub / github.com/Tencent/phxqueue / AcquireLock

Method AcquireLock

phxqueue/lock/lockdb.cpp:130–212  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

128}
129
130comm::RetCode LockDb::AcquireLock(const proto::LockKeyInfo &lock_key_info,
131 const proto::LocalLockInfo &v, const bool sync) {
132 comm::LockDbBP::GetThreadInstance()->OnAcquireLock(sync);
133 proto::LocalLockInfo localv;
134 comm::RetCode ret{Get(lock_key_info.lock_key(), localv)};
135 if (comm::RetCode::RET_ERR_KEY_NOT_EXIST == ret) {
136 QLVerb("lock_key \"%s\" not exist", lock_key_info.lock_key().c_str());
137 // TODO:
138 //OssAttrInc(oss_attr_id_, 55u, 1u);
139 } else if (comm::RetCode::RET_OK != ret) {
140 QLErr("lock_key \"%s\" err %d", lock_key_info.lock_key().c_str(), ret);
141 // TODO:
142 //OssAttrInc(oss_attr_id_, 54u, 1u);
143
144 return ret;
145 }
146
147 if (comm::RetCode::RET_ERR_KEY_NOT_EXIST != ret && localv.version() != lock_key_info.version()) {
148 QLErr("lock_key \"%s\" local.ver %llu != req.ver %llu err",
149 lock_key_info.lock_key().c_str(),
150 localv.version(), lock_key_info.version());
151 // TODO:
152 //OssAttrInc(oss_attr_id_, 56u, 1u);
153
154 return comm::RetCode::RET_ERR_VERSION_NOT_EQUAL;
155 }
156
157 if (Type::MAP == type_) {
158 if (0 >= v.lease_time_ms()) { // release lock
159 map_.erase(lock_key_info.lock_key());
160 QLVerb("lock_key \"%s\" erase", lock_key_info.lock_key().c_str());
161
162 return comm::RetCode::RET_OK;
163 }
164
165 // TODO: not copy?
166 proto::LocalLockInfo v2;
167 v2.set_version(v.version());
168 v2.set_client_id(v.client_id());
169 v2.set_lease_time_ms(v.lease_time_ms());
170 v2.set_expire_time_ms(comm::utils::Time::GetSteadyClockMS() + v.lease_time_ms());
171 QLVerb("lock_key \"%s\" ver %llu client_id \"%s\" expire_time_ms %llu",
172 lock_key_info.lock_key().c_str(), v2.version(),
173 v2.client_id().c_str(), v2.expire_time_ms());
174
175 return Put(lock_key_info.lock_key(), v2);
176
177 } else if (Type::LEVELDB == type_) {
178 if (0 >= v.lease_time_ms()) { // release lock
179 leveldb::WriteOptions write_options;
180 write_options.sync = sync;
181 leveldb::Status status(leveldb_->Delete(write_options, lock_key_info.lock_key()));
182 if (status.IsNotFound()) {
183 QLVerb("lock_key \"%s\" not exist", lock_key_info.lock_key().c_str());
184
185 return comm::RetCode::RET_ERR_KEY_NOT_EXIST;
186 }
187 if (!status.ok()) {

Callers

nothing calls this directly

Calls 3

DeleteMethod · 0.80
ToStringMethod · 0.80
OnAcquireLockMethod · 0.45

Tested by

no test coverage detected