Skip to content

Commit

Permalink
Add lock guard (#865)
Browse files Browse the repository at this point in the history
* Use shared_ptr for Read/WriteLockGuard
  • Loading branch information
Yang Ce authored and bluebore committed Mar 3, 2017
1 parent ad878bb commit bd60cf8
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ ifdef FUSE_LL_PATH
BIN += bfs_ll_mount
endif
TESTS = namespace_test block_mapping_test location_provider_test logdb_test \
file_lock_manager_test chunkserver_impl_test file_cache_test \
block_manager_test data_block_test
file_lock_manager_test file_lock_test chunkserver_impl_test \
file_cache_test block_manager_test data_block_test
TEST_OBJS = src/nameserver/test/namespace_test.o \
src/nameserver/test/block_mapping_test.o \
src/nameserver/test/logdb_test.o \
Expand All @@ -91,6 +91,7 @@ TEST_OBJS = src/nameserver/test/namespace_test.o \
src/nameserver/test/raft_test.o \
src/nameserver/test/nameserver_impl_test.o \
src/nameserver/test/file_lock_manager_test.o \
src/nameserver/test/file_lock_test.o \
src/chunkserver/test/file_cache_test.o \
src/chunkserver/test/chunkserver_impl_test.o \
src/chunkserver/test/block_manager_test.o \
Expand Down Expand Up @@ -152,6 +153,11 @@ file_lock_manager_test: src/nameserver/test/file_lock_manager_test.o \
src/nameserver/file_lock_manager.o
$(CXX) $^ $(OBJS) -o $@ $(LDFLAGS)

file_lock_test: src/nameserver/test/file_lock_test.o \
src/nameserver/file_lock.o \
src/nameserver/file_lock_manager.o
$(CXX) $^ $(OBJS) -o $@ $(LDFLAGS)

chunkserver_impl_test: src/chunkserver/test/chunkserver_impl_test.o \
src/chunkserver/chunkserver_impl.o src/chunkserver/data_block.o src/chunkserver/block_manager.o \
src/chunkserver/counter_manager.o src/chunkserver/file_cache.o src/chunkserver/disk.o \
Expand Down
67 changes: 67 additions & 0 deletions src/nameserver/file_lock.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

#include "nameserver/file_lock.h"

#include <common/logging.h>

namespace baidu {
namespace bfs {

FileLockManager* WriteLock::file_lock_manager_ = NULL;
FileLockManager* ReadLock::file_lock_manager_ = NULL;

WriteLock::WriteLock(const std::string& file_path) {
file_path_.push_back(file_path);
file_lock_manager_->WriteLock(file_path);
}

WriteLock::WriteLock(const std::string& file_path_a,
const std::string& file_path_b) {
int r = strcmp(file_path_a.c_str(), file_path_b.c_str());
if (r == 0) {
file_path_.push_back(file_path_a);
file_lock_manager_->WriteLock(file_path_a);
} else if (r < 0) {
file_path_.push_back(file_path_a);
file_path_.push_back(file_path_b);
file_lock_manager_->WriteLock(file_path_a);
file_lock_manager_->WriteLock(file_path_b);
} else {
file_path_.push_back(file_path_b);
file_path_.push_back(file_path_a);
file_lock_manager_->WriteLock(file_path_b);
file_lock_manager_->WriteLock(file_path_a);
}
}

WriteLock::~WriteLock() {
if (file_path_.size() == 1) {
file_lock_manager_->Unlock(file_path_[0]);
} else {
file_lock_manager_->Unlock(file_path_[1]);
file_lock_manager_->Unlock(file_path_[0]);
}
}

void WriteLock::SetFileLockManager(FileLockManager* file_lock_manager) {
file_lock_manager_ = file_lock_manager;
}

ReadLock::ReadLock(const std::string& file_path) {
file_path_ = file_path;
file_lock_manager_->ReadLock(file_path);
}

ReadLock::~ReadLock() {
file_lock_manager_->Unlock(file_path_);
}

void ReadLock::SetFileLockManager(FileLockManager* file_lock_manager) {
file_lock_manager_ = file_lock_manager;
}

} // namespace bfs
} // namespace baidu
55 changes: 55 additions & 0 deletions src/nameserver/file_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

#ifndef BFS_FILE_LOCK_H_
#define BFS_FILE_LOCK_H_

#include "nameserver/file_lock_manager.h"

#include <vector>
#include <memory>

namespace baidu {
namespace bfs {

class FileLockManager;

class Lock {
public:
virtual ~Lock() {}
};

class WriteLock : public Lock {
public:
WriteLock(const std::string& file_path);
WriteLock(const std::string& file_path_a,
const std::string& file_path_b);
~WriteLock();
static void SetFileLockManager(FileLockManager* file_lock_manager);
private:
// will be initialized in NameServerImpl's constructor
static FileLockManager* file_lock_manager_;
std::vector<std::string> file_path_;
};

class ReadLock : public Lock {
public:
ReadLock(const std::string& file_path);
~ReadLock();
static void SetFileLockManager(FileLockManager* file_lock_manager);
private:
// will be initialized in NameServerImpl's constructor
static FileLockManager* file_lock_manager_;
std::string file_path_;
};

typedef std::shared_ptr<Lock> FileLockGuard;

} // namespace bfs
} // namespace baidu

#endif //BFS_FILE_LOCK_H_

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
5 changes: 5 additions & 0 deletions src/nameserver/nameserver_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "nameserver/sync.h"
#include "nameserver/chunkserver_manager.h"
#include "nameserver/namespace.h"
#include "nameserver/file_lock_manager.h"
#include "nameserver/file_lock.h"

#include "proto/status_code.pb.h"

Expand Down Expand Up @@ -63,6 +65,9 @@ NameServerImpl::NameServerImpl(Sync* sync) : readonly_(true),
heartbeat_thread_pool_ = new common::ThreadPool(FLAGS_nameserver_heartbeat_thread_num);
chunkserver_manager_ = new ChunkServerManager(work_thread_pool_, block_mapping_manager_);
namespace_ = new NameSpace(false);
file_lock_manager_ = new FileLockManager;
WriteLock::SetFileLockManager(file_lock_manager_);
ReadLock::SetFileLockManager(file_lock_manager_);
if (sync_) {
SyncCallbacks callbacks(std::bind(&NameSpace::TailLog, namespace_,
std::placeholders::_1),
Expand Down
2 changes: 2 additions & 0 deletions src/nameserver/nameserver_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class NameSpace;
class ChunkServerManager;
class BlockMappingManager;
class Sync;
class FileLockManager;

enum RecoverMode {
kStopRecover = 0,
Expand Down Expand Up @@ -178,6 +179,7 @@ class NameServerImpl : public NameServer {
int64_t start_time_;
/// Namespace
NameSpace* namespace_;
FileLockManager* file_lock_manager_;
/// ha
Sync* sync_;
bool is_leader_;
Expand Down
50 changes: 50 additions & 0 deletions src/nameserver/test/file_lock_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

#define private public

#include <nameserver/file_lock.h>

#include <gtest/gtest.h>

namespace baidu {
namespace bfs {

class FileLockTest : public ::testing::Test {
public:
FileLockTest() {}
protected:
};
FileLockManager file_lock_manager;

void SetFileLockManager() {
WriteLock::file_lock_manager_ = &file_lock_manager;
ReadLock::file_lock_manager_ = &file_lock_manager;
}

TEST_F(FileLockTest, WriteLockForOneFile) {
FileLockGuard guard1(new WriteLock("/home/dir1/file1"));
FileLockGuard guard2(new WriteLock("/home/dir1/file2"));
}

TEST_F(FileLockTest, WriteLockForTwoFile) {
FileLockGuard guard2(new WriteLock("/home/dir1/file2", "/home/dir1/file1"));
}

TEST_F(FileLockTest, ReadLock) {
FileLockGuard guard1(new ReadLock("/home/dir1/file1"));
FileLockGuard guard2(new ReadLock("/home/dir1/file2"));
}

} // namespace bfs
} // namespace baidu

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
baidu::bfs::SetFileLockManager();
baidu::common::SetLogLevel(2);
return RUN_ALL_TESTS();
}

0 comments on commit bd60cf8

Please sign in to comment.