提交 54361633 编写于 作者: G groot

refine code


Former-commit-id: eae3ec0b3f87dbecf1e44d079d4a4a46794f9b9f
上级 31e0303d
...@@ -25,6 +25,10 @@ namespace engine { ...@@ -25,6 +25,10 @@ namespace engine {
namespace { namespace {
static constexpr uint64_t METRIC_ACTION_INTERVAL = 1;
static constexpr uint64_t COMPACT_ACTION_INTERVAL = 1;
static constexpr uint64_t INDEX_ACTION_INTERVAL = 1;
void CollectInsertMetrics(double total_time, size_t n, bool succeed) { void CollectInsertMetrics(double total_time, size_t n, bool succeed) {
double avg_time = total_time / n; double avg_time = total_time / n;
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
...@@ -130,7 +134,7 @@ DBImpl::DBImpl(const Options& options) ...@@ -130,7 +134,7 @@ DBImpl::DBImpl(const Options& options)
pMemMgr_(new MemManager(pMeta_, options_)), pMemMgr_(new MemManager(pMeta_, options_)),
compact_thread_pool_(1, 1), compact_thread_pool_(1, 1),
index_thread_pool_(1, 1) { index_thread_pool_(1, 1) {
StartTimerTasks(options_.memory_sync_interval); StartTimerTasks();
} }
Status DBImpl::CreateTable(meta::TableSchema& table_schema) { Status DBImpl::CreateTable(meta::TableSchema& table_schema) {
...@@ -399,12 +403,11 @@ Status DBImpl::QueryAsync(const std::string& table_id, const meta::TableFilesSch ...@@ -399,12 +403,11 @@ Status DBImpl::QueryAsync(const std::string& table_id, const meta::TableFilesSch
return Status::OK(); return Status::OK();
} }
void DBImpl::StartTimerTasks(int interval) { void DBImpl::StartTimerTasks() {
bg_timer_thread_ = std::thread(&DBImpl::BackgroundTimerTask, this, interval); bg_timer_thread_ = std::thread(&DBImpl::BackgroundTimerTask, this);
} }
void DBImpl::BackgroundTimerTask() {
void DBImpl::BackgroundTimerTask(int interval) {
Status status; Status status;
server::SystemInfo::GetInstance().Init(); server::SystemInfo::GetInstance().Init();
while (true) { while (true) {
...@@ -419,27 +422,42 @@ void DBImpl::BackgroundTimerTask(int interval) { ...@@ -419,27 +422,42 @@ void DBImpl::BackgroundTimerTask(int interval) {
break; break;
} }
std::this_thread::sleep_for(std::chrono::seconds(interval)); std::this_thread::sleep_for(std::chrono::seconds(1));
server::Metrics::GetInstance().KeepingAliveCounterIncrement(interval);
int64_t cache_usage = cache::CpuCacheMgr::GetInstance()->CacheUsage();
int64_t cache_total = cache::CpuCacheMgr::GetInstance()->CacheCapacity();
server::Metrics::GetInstance().CacheUsageGaugeSet(cache_usage*100/cache_total);
uint64_t size;
Size(size);
server::Metrics::GetInstance().DataFileSizeGaugeSet(size);
server::Metrics::GetInstance().CPUUsagePercentSet();
server::Metrics::GetInstance().RAMUsagePercentSet();
server::Metrics::GetInstance().GPUPercentGaugeSet();
server::Metrics::GetInstance().GPUMemoryUsageGaugeSet();
server::Metrics::GetInstance().OctetsSet();
StartMetricTask();
StartCompactionTask(); StartCompactionTask();
StartBuildIndexTask(); StartBuildIndexTask();
} }
} }
void DBImpl::StartMetricTask() {
static uint64_t metric_clock_tick = 0;
metric_clock_tick++;
if(metric_clock_tick%METRIC_ACTION_INTERVAL != 0) {
return;
}
server::Metrics::GetInstance().KeepingAliveCounterIncrement(METRIC_ACTION_INTERVAL);
int64_t cache_usage = cache::CpuCacheMgr::GetInstance()->CacheUsage();
int64_t cache_total = cache::CpuCacheMgr::GetInstance()->CacheCapacity();
server::Metrics::GetInstance().CacheUsageGaugeSet(cache_usage*100/cache_total);
uint64_t size;
Size(size);
server::Metrics::GetInstance().DataFileSizeGaugeSet(size);
server::Metrics::GetInstance().CPUUsagePercentSet();
server::Metrics::GetInstance().RAMUsagePercentSet();
server::Metrics::GetInstance().GPUPercentGaugeSet();
server::Metrics::GetInstance().GPUMemoryUsageGaugeSet();
server::Metrics::GetInstance().OctetsSet();
}
void DBImpl::StartCompactionTask() { void DBImpl::StartCompactionTask() {
static uint64_t compact_clock_tick = 0;
compact_clock_tick++;
if(compact_clock_tick%COMPACT_ACTION_INTERVAL != 0) {
return;
}
//serialize memory data //serialize memory data
std::vector<std::string> temp_table_ids; std::vector<std::string> temp_table_ids;
pMemMgr_->Serialize(temp_table_ids); pMemMgr_->Serialize(temp_table_ids);
...@@ -556,6 +574,12 @@ void DBImpl::BackgroundCompaction(std::set<std::string> table_ids) { ...@@ -556,6 +574,12 @@ void DBImpl::BackgroundCompaction(std::set<std::string> table_ids) {
} }
void DBImpl::StartBuildIndexTask() { void DBImpl::StartBuildIndexTask() {
static uint64_t index_clock_tick = 0;
index_clock_tick++;
if(index_clock_tick%INDEX_ACTION_INTERVAL != 0) {
return;
}
//build index has been finished? //build index has been finished?
if(!index_thread_results_.empty()) { if(!index_thread_results_.empty()) {
std::chrono::milliseconds span(10); std::chrono::milliseconds span(10);
...@@ -581,29 +605,36 @@ Status DBImpl::BuildIndex(const meta::TableFileSchema& file) { ...@@ -581,29 +605,36 @@ Status DBImpl::BuildIndex(const meta::TableFileSchema& file) {
} }
ExecutionEnginePtr to_index = EngineFactory::Build(file.dimension_, file.location_, (EngineType)file.engine_type_); ExecutionEnginePtr to_index = EngineFactory::Build(file.dimension_, file.location_, (EngineType)file.engine_type_);
if(to_index == nullptr) {
return Status::Error("Invalid engine type");
}
to_index->Load(); try {
auto start_time = METRICS_NOW_TIME; to_index->Load();
auto index = to_index->BuildIndex(table_file.location_); auto start_time = METRICS_NOW_TIME;
auto end_time = METRICS_NOW_TIME; auto index = to_index->BuildIndex(table_file.location_);
auto total_time = METRICS_MICROSECONDS(start_time, end_time); auto end_time = METRICS_NOW_TIME;
server::Metrics::GetInstance().BuildIndexDurationSecondsHistogramObserve(total_time); auto total_time = METRICS_MICROSECONDS(start_time, end_time);
server::Metrics::GetInstance().BuildIndexDurationSecondsHistogramObserve(total_time);
table_file.file_type_ = meta::TableFileSchema::INDEX; table_file.file_type_ = meta::TableFileSchema::INDEX;
table_file.size_ = index->Size(); table_file.size_ = index->Size();
auto to_remove = file; auto to_remove = file;
to_remove.file_type_ = meta::TableFileSchema::TO_DELETE; to_remove.file_type_ = meta::TableFileSchema::TO_DELETE;
meta::TableFilesSchema update_files = {to_remove, table_file}; meta::TableFilesSchema update_files = {to_remove, table_file};
pMeta_->UpdateTableFiles(update_files); pMeta_->UpdateTableFiles(update_files);
LOG(DEBUG) << "New index file " << table_file.file_id_ << " of size " LOG(DEBUG) << "New index file " << table_file.file_id_ << " of size "
<< index->PhysicalSize()/(1024*1024) << " M" << index->PhysicalSize()/(1024*1024) << " M"
<< " from file " << to_remove.file_id_; << " from file " << to_remove.file_id_;
index->Cache(); index->Cache();
pMeta_->Archive();
} catch (std::exception& ex) {
return Status::Error("Build index encounter exception", ex.what());
}
return Status::OK(); return Status::OK();
} }
......
...@@ -70,8 +70,10 @@ private: ...@@ -70,8 +70,10 @@ private:
const meta::DatesT& dates, QueryResults& results); const meta::DatesT& dates, QueryResults& results);
void StartTimerTasks(int interval); void StartTimerTasks();
void BackgroundTimerTask(int interval); void BackgroundTimerTask();
void StartMetricTask();
void StartCompactionTask(); void StartCompactionTask();
Status MergeFiles(const std::string& table_id, Status MergeFiles(const std::string& table_id,
......
...@@ -186,10 +186,18 @@ Status DBMetaImpl::CreateTable(TableSchema &table_schema) { ...@@ -186,10 +186,18 @@ Status DBMetaImpl::CreateTable(TableSchema &table_schema) {
try { try {
MetricCollector metric; MetricCollector metric;
server::Metrics::GetInstance().MetaAccessTotalIncrement();
if (table_schema.table_id_ == "") { if (table_schema.table_id_ == "") {
NextTableId(table_schema.table_id_); NextTableId(table_schema.table_id_);
} else {
auto table = ConnectorPtr->select(columns(&TableSchema::state_),
where(c(&TableSchema::table_id_) == table_schema.table_id_));
if (table.size() == 1) {
std::string msg = (TableSchema::TO_DELETE == std::get<0>(table[0])) ?
"Table already exists" : "Table already exists and it is in delete state, please wait a second";
return Status::Error(msg);
}
} }
table_schema.files_cnt_ = 0; table_schema.files_cnt_ = 0;
table_schema.id_ = -1; table_schema.id_ = -1;
table_schema.created_on_ = utils::GetMicroSecTimeStamp(); table_schema.created_on_ = utils::GetMicroSecTimeStamp();
...@@ -207,8 +215,8 @@ Status DBMetaImpl::CreateTable(TableSchema &table_schema) { ...@@ -207,8 +215,8 @@ Status DBMetaImpl::CreateTable(TableSchema &table_schema) {
auto ret = boost::filesystem::create_directories(table_path); auto ret = boost::filesystem::create_directories(table_path);
if (!ret) { if (!ret) {
ENGINE_LOG_ERROR << "Create directory " << table_path << " Error"; ENGINE_LOG_ERROR << "Create directory " << table_path << " Error";
return Status::Error("Failed to create table path");
} }
assert(ret);
} }
} catch (std::exception &e) { } catch (std::exception &e) {
...@@ -733,10 +741,12 @@ Status DBMetaImpl::Size(uint64_t &result) { ...@@ -733,10 +741,12 @@ Status DBMetaImpl::Size(uint64_t &result) {
} }
Status DBMetaImpl::DiscardFiles(long to_discard_size) { Status DBMetaImpl::DiscardFiles(long to_discard_size) {
LOG(DEBUG) << "About to discard size=" << to_discard_size;
if (to_discard_size <= 0) { if (to_discard_size <= 0) {
return Status::OK(); return Status::OK();
} }
LOG(DEBUG) << "About to discard size=" << to_discard_size;
try { try {
auto selected = ConnectorPtr->select(columns(&TableFileSchema::id_, auto selected = ConnectorPtr->select(columns(&TableFileSchema::id_,
&TableFileSchema::size_), &TableFileSchema::size_),
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "SearchScheduler.h" #include "SearchScheduler.h"
#include "IndexLoaderQueue.h" #include "IndexLoaderQueue.h"
#include "SearchTaskQueue.h" #include "SearchTask.h"
#include "utils/Log.h" #include "utils/Log.h"
#include "utils/TimeRecorder.h" #include "utils/TimeRecorder.h"
#include "metrics/Metrics.h" #include "metrics/Metrics.h"
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "SearchContext.h" #include "SearchContext.h"
#include "IndexLoaderQueue.h" #include "IndexLoaderQueue.h"
#include "SearchTaskQueue.h" #include "SearchTask.h"
namespace zilliz { namespace zilliz {
namespace milvus { namespace milvus {
...@@ -35,6 +35,8 @@ private: ...@@ -35,6 +35,8 @@ private:
std::shared_ptr<std::thread> search_thread_; std::shared_ptr<std::thread> search_thread_;
IndexLoaderQueue index_load_queue_; IndexLoaderQueue index_load_queue_;
using SearchTaskQueue = server::BlockingQueue<SearchTaskPtr>;
SearchTaskQueue search_queue_; SearchTaskQueue search_queue_;
bool stopped_ = true; bool stopped_ = true;
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* Unauthorized copying of this file, via any medium is strictly prohibited. * Unauthorized copying of this file, via any medium is strictly prohibited.
* Proprietary and confidential. * Proprietary and confidential.
******************************************************************************/ ******************************************************************************/
#include "SearchTaskQueue.h" #include "SearchTask.h"
#include "utils/Log.h" #include "utils/Log.h"
#include "utils/TimeRecorder.h" #include "utils/TimeRecorder.h"
......
...@@ -27,7 +27,6 @@ public: ...@@ -27,7 +27,6 @@ public:
}; };
using SearchTaskPtr = std::shared_ptr<SearchTask>; using SearchTaskPtr = std::shared_ptr<SearchTask>;
using SearchTaskQueue = server::BlockingQueue<SearchTaskPtr>;
} }
......
...@@ -221,7 +221,7 @@ ServerError CreateTableTask::OnExecute() { ...@@ -221,7 +221,7 @@ ServerError CreateTableTask::OnExecute() {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DescribeTableTask::DescribeTableTask(const std::string &table_name, thrift::TableSchema &schema) DescribeTableTask::DescribeTableTask(const std::string &table_name, thrift::TableSchema &schema)
: BaseTask(PING_TASK_GROUP), : BaseTask(DDL_DML_TASK_GROUP),
table_name_(table_name), table_name_(table_name),
schema_(schema) { schema_(schema) {
schema_.table_name = table_name_; schema_.table_name = table_name_;
...@@ -329,7 +329,7 @@ ServerError DeleteTableTask::OnExecute() { ...@@ -329,7 +329,7 @@ ServerError DeleteTableTask::OnExecute() {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ShowTablesTask::ShowTablesTask(std::vector<std::string>& tables) ShowTablesTask::ShowTablesTask(std::vector<std::string>& tables)
: BaseTask(DQL_TASK_GROUP), : BaseTask(DDL_DML_TASK_GROUP),
tables_(tables) { tables_(tables) {
} }
...@@ -451,13 +451,13 @@ SearchVectorTask::SearchVectorTask(const std::string &table_name, ...@@ -451,13 +451,13 @@ SearchVectorTask::SearchVectorTask(const std::string &table_name,
const std::vector<thrift::Range> &query_range_array, const std::vector<thrift::Range> &query_range_array,
const int64_t top_k, const int64_t top_k,
std::vector<thrift::TopKQueryResult> &result_array) std::vector<thrift::TopKQueryResult> &result_array)
: BaseTask(DQL_TASK_GROUP), : BaseTask(DQL_TASK_GROUP),
table_name_(table_name), table_name_(table_name),
file_id_array_(file_id_array), file_id_array_(file_id_array),
record_array_(query_record_array), record_array_(query_record_array),
range_array_(query_range_array), range_array_(query_range_array),
top_k_(top_k), top_k_(top_k),
result_array_(result_array) { result_array_(result_array) {
} }
...@@ -575,7 +575,7 @@ ServerError SearchVectorTask::OnExecute() { ...@@ -575,7 +575,7 @@ ServerError SearchVectorTask::OnExecute() {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GetTableRowCountTask::GetTableRowCountTask(const std::string& table_name, int64_t& row_count) GetTableRowCountTask::GetTableRowCountTask(const std::string& table_name, int64_t& row_count)
: BaseTask(DQL_TASK_GROUP), : BaseTask(DDL_DML_TASK_GROUP),
table_name_(table_name), table_name_(table_name),
row_count_(row_count) { row_count_(row_count) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册