Skip to content

Commit

Permalink
Remove acquire distributed lock.
Browse files Browse the repository at this point in the history
Signed-off-by: vegetableysm <[email protected]>
  • Loading branch information
vegetableysm committed Aug 7, 2024
1 parent b0c51a8 commit 4de2cf1
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 106 deletions.
21 changes: 1 addition & 20 deletions modules/llm-cache/ds/vineyard_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ Status VineyardFile::Make(std::shared_ptr<VineyardFile>& file,
RPCClient& rpc_client, Client& ipc_client,
std::string path) {
std::string origin_path = std::regex_replace(path, std::regex("/+"), "\\/");
VineyardFileLock lock(rpc_client, ipc_client, path);
RETURN_ON_ERROR(lock.TryLock());
ObjectID file_id;
ObjectMeta meta;
ObjectMeta object_meta;
Expand Down Expand Up @@ -158,17 +156,9 @@ Status VineyardFile::BatchedMake(
Client& ipc_client, const std::vector<std::string>& paths) {
std::vector<std::string> origin_paths;
std::vector<ObjectID> file_ids;
std::vector<std::shared_ptr<VineyardFileLock>> locks;

for (auto const& path : paths) {
std::shared_ptr<VineyardFileLock> lock =
std::make_shared<VineyardFileLock>(rpc_client, ipc_client, path);
if (lock->TryLock().ok()) {
origin_paths.push_back(std::regex_replace(path, std::regex("/+"), "\\/"));
locks.push_back(lock);
} else {
break;
}
origin_paths.push_back(std::regex_replace(path, std::regex("/+"), "\\/"));
}

std::vector<ObjectMeta> file_metas;
Expand Down Expand Up @@ -219,9 +209,6 @@ Status VineyardFileBuilder::Make(std::shared_ptr<VineyardFileBuilder>& builder,
std::string actural_path;
std::string origin_path = std::regex_replace(path, std::regex("/+"), "\\/");
builder = std::make_shared<VineyardFileBuilder>(origin_path);
builder->lock =
std::make_unique<VineyardFileLock>(rpc_client, ipc_client, path);
RETURN_ON_ERROR(builder->lock->TryLock());
ObjectID id;
if (ipc_client.Connected()) {
if (ipc_client.GetName(origin_path, id).ok()) {
Expand Down Expand Up @@ -273,7 +260,6 @@ std::shared_ptr<Object> VineyardFileBuilder::SealAndPersist(
Status status = rpc_client.PutName(vineyardFile->id_, path_);
}

lock.reset();
return vineyardFile;
}

Expand Down Expand Up @@ -314,11 +300,6 @@ std::vector<std::shared_ptr<Object>> VineyardFileBuilder::BatchedSealAndPersist(
rpc_client.CreateMetaData(vineyard_file->meta_, vineyard_file->id_));
rpc_client.Persist(vineyard_file->id_);
Status status = rpc_client.PutName(vineyard_file->id_, builders[i]->path_);

builders[i]->lock.reset();
}
for (size_t i = blob_metas.size(); i < builders.size(); i++) {
builders[i]->lock.reset();
}

return vineyard_file_objects;
Expand Down
77 changes: 0 additions & 77 deletions modules/llm-cache/ds/vineyard_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,82 +32,6 @@ namespace vineyard {

class VineyardFileBuilder;

class VineyardFileLock {
public:
explicit VineyardFileLock(RPCClient& rpc_client, Client& ipc_client,
std::string path)
: path_(path), rpc_client_(rpc_client), ipc_client_(ipc_client) {}

~VineyardFileLock() { Unlock(); }

Status TryLock() {
if (ipc_client_.Connected()) {
return TryLockWithIPCClient();
} else {
return TryLockWithRPCClient();
}
}

private:
Status Unlock() {
if (ipc_client_.Connected()) {
return UnlockWithIPCClient();
} else {
return UnlockWithRPCClient();
}
}

Status UnlockWithRPCClient() {
if (!lock_path_.empty()) {
// unlock
bool result = false;
do {
rpc_client_.TryReleaseLock(lock_path_, result);
} while (!result);
}
return Status::OK();
}

Status UnlockWithIPCClient() {
if (!lock_path_.empty()) {
// unlock
bool result = false;
do {
ipc_client_.TryReleaseLock(lock_path_, result);
} while (!result);
}
return Status::OK();
}

Status TryLockWithRPCClient() {
bool result = false;
std::string origin_path =
std::regex_replace(path_, std::regex("/+"), "\\/");
rpc_client_.TryAcquireLock(origin_path, result, lock_path_);
if (!result) {
return Status::Invalid("Failed to acquire lock for file: " + path_);
}
return Status::OK();
}

Status TryLockWithIPCClient() {
bool result = false;
std::string origin_path =
std::regex_replace(path_, std::regex("/+"), "\\/");
ipc_client_.TryAcquireLock(origin_path, result, lock_path_);
if (!result) {
return Status::Invalid("Failed to acquire lock for file: " + path_);
}
return Status::OK();
}

private:
std::string path_;
std::string lock_path_;
RPCClient& rpc_client_;
Client& ipc_client_;
};

class VineyardFile : public vineyard::Registered<VineyardFile> {
public:
VineyardFile() = default;
Expand Down Expand Up @@ -178,7 +102,6 @@ class VineyardFileBuilder {
std::shared_ptr<RemoteBlobWriter> remote_writer_;
std::unique_ptr<BlobWriter> writer_;
std::string path_;
std::unique_ptr<VineyardFileLock> lock;
};

} // namespace vineyard
Expand Down
10 changes: 1 addition & 9 deletions modules/llm-cache/storage/vineyard_file_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Status VineyardFileStorage::BatchedOpen(
std::vector<std::shared_ptr<VineyardFile>> vineyardFileList;
RETURN_ON_ERROR(VineyardFile::BatchedMake(vineyardFileList, rpc_client_,
ipc_client_, pathList));
for (size_t i = 0; i < pathList.size(); i++) {
for (size_t i = 0; i < vineyardFileList.size(); i++) {
std::shared_ptr<VineyardFileDescriptor> lfd =
std::make_shared<VineyardFileDescriptor>();
lfd->path = pathList[i];
Expand Down Expand Up @@ -191,18 +191,12 @@ bool VineyardFileStorage::IsFileExist(const std::string& path) {
Status VineyardFileStorage::Delete(std::string path) {
std::string origin_path = std::regex_replace(path, std::regex("/+"), "\\/");
std::string lock_path;
bool result = false;
VineyardFileLock lock(rpc_client_, ipc_client_, origin_path);
RETURN_ON_ERROR(lock.TryLock());
ObjectID file_id;
Status status = Status::OK();
if (rpc_client_.GetName(origin_path, file_id, false).ok()) {
status = rpc_client_.DelData(std::vector<ObjectID>{file_id}, true, true);
status = rpc_client_.DropName(origin_path);
}
do {
rpc_client_.TryReleaseLock(lock_path, result);
} while (!result);
return status;
}

Expand Down Expand Up @@ -235,8 +229,6 @@ Status VineyardFileStorage::TouchFile(const std::string& path) {
ObjectMeta meta;
std::string lock_path;
std::string origin_path = std::regex_replace(path, std::regex("/+"), "\\/");
VineyardFileLock lock(rpc_client_, ipc_client_, origin_path);
RETURN_ON_ERROR(lock.TryLock());
RETURN_ON_ERROR(rpc_client_.GetName(origin_path, file_id, false));
RETURN_ON_ERROR(rpc_client_.GetMetaData(file_id, meta, false));
meta.AddKeyValue(
Expand Down
1 change: 1 addition & 0 deletions modules/llm-cache/storage/vineyard_file_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.

#include "client/client.h"
#include "client/rpc_client.h"
#include "common/util/logging.h"
#include "llm-cache/ds/vineyard_file.h"
#include "llm-cache/storage/file_storage.h"

Expand Down

0 comments on commit 4de2cf1

Please sign in to comment.