rewrite cache code

Former-commit-id: 00247bcda3df3d0e29bc4a809f7894ce92470e0e
This commit is contained in:
groot 2019-04-24 11:52:13 +08:00
parent 06ab815c7b
commit 97d4738bc6
10 changed files with 149 additions and 51 deletions

View File

@ -35,4 +35,5 @@ log_config:
filename: "/tmp/vecwise/logs/vecwise_engine-%datetime{%h:%m}-fatal.log"
cache_config:
cache_capacity: 16 # unit: GB
cpu_cache_capacity: 16 # unit: GB
gpu_cache_capacity: 2 # unit: GB

View File

@ -43,6 +43,12 @@ public:
int64_t usage() const { return usage_; }
int64_t capacity() const { return capacity_; }
void set_capacity(int64_t capacity) {
if(capacity > 0) {
capacity_ = capacity;
free_memory();
}
}
size_t size() const;
bool exists(const std::string& key);

View File

@ -11,8 +11,10 @@ namespace vecwise {
namespace cache {
CacheMgr::CacheMgr() {
//TODO: loada config to initialize cache
cache_ = std::make_shared<Cache>(16, 1UL<<32);
}
CacheMgr::~CacheMgr() {
}
uint64_t CacheMgr::ItemCount() const {
@ -87,6 +89,13 @@ int64_t CacheMgr::CacheCapacity() const {
return cache_->capacity();
}
void CacheMgr::SetCapacity(int64_t capacity) {
if(cache_ == nullptr) {
return;
}
cache_->set_capacity(capacity);
}
}
}
}

View File

@ -14,32 +14,30 @@ namespace cache {
class CacheMgr {
public:
static CacheMgr& GetInstance() {
static CacheMgr mgr;
return mgr;
}
virtual uint64_t ItemCount() const;
uint64_t ItemCount() const;
virtual bool ItemExists(const std::string& key);
bool ItemExists(const std::string& key);
virtual DataObjPtr GetItem(const std::string& key);
DataObjPtr GetItem(const std::string& key);
virtual void InsertItem(const std::string& key, const DataObjPtr& data);
void InsertItem(const std::string& key, const DataObjPtr& data);
virtual void EraseItem(const std::string& key);
void EraseItem(const std::string& key);
virtual void PrintInfo();
void PrintInfo();
virtual void ClearCache();
void ClearCache();
virtual int64_t CacheUsage() const;
virtual int64_t CacheCapacity() const;
int64_t CacheUsage() const;
int64_t CacheCapacity() const;
virtual void SetCapacity(int64_t capacity);
private:
protected:
CacheMgr();
virtual ~CacheMgr();
private:
protected:
CachePtr cache_;
};

22
cpp/src/cache/CpuCacheMgr.cpp vendored Normal file
View File

@ -0,0 +1,22 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "CpuCacheMgr.h"
#include "server/ServerConfig.h"
namespace zilliz {
namespace vecwise {
namespace cache {
CpuCacheMgr::CpuCacheMgr() {
server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE);
int64_t cap = config.GetInt64Value(server::CONFIG_CPU_CACHE_CAPACITY, 16);
cache_ = std::make_shared<Cache>(cap, 1UL<<32);
}
}
}
}

27
cpp/src/cache/CpuCacheMgr.h vendored Normal file
View File

@ -0,0 +1,27 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "CacheMgr.h"
namespace zilliz {
namespace vecwise {
namespace cache {
class CpuCacheMgr : public CacheMgr {
private:
CpuCacheMgr();
public:
static CacheMgr* GetInstance() {
static CpuCacheMgr s_mgr;
return &s_mgr;
}
};
}
}
}

26
cpp/src/cache/GpuCacheMgr.cpp vendored Normal file
View File

@ -0,0 +1,26 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "GpuCacheMgr.h"
#include "server/ServerConfig.h"
namespace zilliz {
namespace vecwise {
namespace cache {
GpuCacheMgr::GpuCacheMgr() {
server::ConfigNode& config = server::ServerConfig::GetInstance().GetConfig(server::CONFIG_CACHE);
int64_t cap = config.GetInt64Value(server::CONFIG_GPU_CACHE_CAPACITY, 1);
cache_ = std::make_shared<Cache>(cap, 1UL<<32);
}
void GpuCacheMgr::InsertItem(const std::string& key, const DataObjPtr& data) {
//TODO: copy data to gpu
}
}
}
}

28
cpp/src/cache/GpuCacheMgr.h vendored Normal file
View File

@ -0,0 +1,28 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "CacheMgr.h"
namespace zilliz {
namespace vecwise {
namespace cache {
class GpuCacheMgr : public CacheMgr {
private:
GpuCacheMgr();
public:
static CacheMgr* GetInstance() {
static GpuCacheMgr s_mgr;
return &s_mgr;
}
void InsertItem(const std::string& key, const DataObjPtr& data) override;
};
}
}
}

View File

@ -25,7 +25,8 @@ static const std::string CONFIG_SERVER_DB_PATH = "db_path";
static const std::string CONFIG_LOG = "log_config";
static const std::string CONFIG_CACHE = "cache_config";
static const std::string CONFIG_CACHE_CAPACITY = "cache_capacity";
static const std::string CONFIG_CPU_CACHE_CAPACITY = "cpu_cache_capacity";
static const std::string CONFIG_GPU_CACHE_CAPACITY = "gpu_cache_capacity";
class ServerConfig {
public:

View File

@ -4,38 +4,18 @@
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
#include "cache/CacheMgr.h"
#include "cache/CpuCacheMgr.h"
#include "cache/GpuCacheMgr.h"
using namespace zilliz::vecwise;
//namespace {
//cache::DataObjPtr MakeData(int64_t size) {
// auto data_ptr = std::shared_ptr<char>(new char[size], std::default_delete<char[]>());
// return std::make_shared<cache::DataObj>(data_ptr, size);
//}
//
//#define MAKE_1GB_DATA MakeData(1*1024*1024*1024)
//#define MAKE_100MB_DATA MakeData(100*1024*1024)
//#define MAKE_1MB_DATA MakeData(1*1024*1024)
//}
//
//TEST(CacheTest, CACHE_TEST) {
// cache::CacheMgr cache_mgr = cache::CacheMgr::GetInstance();
//
// for(int i = 0; i < 10; i++) {
// cache_mgr.InsertItem(std::to_string(i), MAKE_100MB_DATA);
// }
//
// ASSERT_EQ(cache_mgr.ItemCount(), 10);
//
// std::string key = "test_data";
// cache_mgr.InsertItem(key, MAKE_100MB_DATA);
// cache::DataObjPtr data = cache_mgr.GetItem(key);
// ASSERT_TRUE(data != nullptr);
// ASSERT_TRUE(cache_mgr.ItemExists(key));
// ASSERT_EQ(data->size(), 100*1024*1024);
//
// cache_mgr.EraseItem(key);
// data = cache_mgr.GetItem(key);
// ASSERT_TRUE(data == nullptr);
//}
TEST(CacheTest, CACHE_TEST) {
cache::CacheMgr* cpu_mgr = cache::CpuCacheMgr::GetInstance();
cache::CacheMgr* gpu_mgr = cache::GpuCacheMgr::GetInstance();
cpu_mgr->SetCapacity(15);
ASSERT_EQ(cpu_mgr->CacheCapacity(), 15);
gpu_mgr->SetCapacity(2);
ASSERT_EQ(gpu_mgr->CacheCapacity(), 2);
}