Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
milvus
milvus
提交
1bc01da8
M
milvus
项目概览
milvus
/
milvus
12 个月 前同步成功
通知
261
Star
22476
Fork
2472
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
milvus
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1bc01da8
编写于
4月 15, 2019
作者:
X
Xu Peng
提交者:
xj.lin
4月 16, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(cpp/db): add timer thread
Former-commit-id: e94c2675e8e4294620c94669fa1d3208af7fcc40
上级
3e8213ff
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
82 addition
and
11 deletion
+82
-11
cpp/src/db/db_impl.cpp
cpp/src/db/db_impl.cpp
+34
-4
cpp/src/db/db_impl.h
cpp/src/db/db_impl.h
+1
-0
cpp/src/db/memvectors.cpp
cpp/src/db/memvectors.cpp
+35
-4
cpp/src/db/memvectors.h
cpp/src/db/memvectors.h
+12
-3
未找到文件。
cpp/src/db/db_impl.cpp
浏览文件 @
1bc01da8
#include <assert.h>
#include <chrono>
#include "db_impl.h"
namespace
vecengine
{
...
...
@@ -9,8 +10,10 @@ DBImpl::DBImpl(const Options& options_, const std::string& name_)
_options
(
options_
),
_bg_work_finish_signal
(
_mutex
),
_bg_compaction_scheduled
(
false
),
_shutting_down
(
false
),
_pMeta
(
new
DBMetaImpl
(
*
(
_options
.
pMetaOptions
))),
_pMemMgr
(
new
MemManager
(
_pMeta
))
{
start_timer_task
(
Options
.
memory_sync_interval
);
}
Status
DBImpl
::
add_group
(
const
GroupOptions
&
options_
,
...
...
@@ -39,7 +42,27 @@ Status DBImpl::get_group_files(const std::string& group_id_,
Status
DBImpl
::
add_vectors
(
const
std
::
string
&
group_id_
,
size_t
n
,
const
float
*
vectors
,
IDNumbers
&
vector_ids_
)
{
return
_pMemMgr
->
add_vectors
(
group_id_
,
n
,
vectors
,
vector_ids_
);
Status
status
=
_pMemMgr
->
add_vectors
(
group_id_
,
n
,
vectors
,
vector_ids_
);
if
(
!
status
.
ok
())
{
return
status
;
}
}
void
DBImpl
::
start_timer_task
(
int
interval_
)
{
std
::
thread
bg_task
(
&
DBImpl
::
background_timer_task
,
this
,
interval_
);
bg_task
.
detach
();
}
void
DBImpl
::
background_timer_task
(
int
interval_
)
{
Status
status
;
while
(
true
)
{
if
(
!
_bg_error
.
ok
())
break
;
if
(
_shutting_down
.
load
(
std
::
memory_order_acquire
))
break
;
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
interval_
));
try_schedule_compaction
();
}
}
void
DBImpl
::
try_schedule_compaction
()
{
...
...
@@ -61,14 +84,21 @@ void DBImpl::background_call() {
if
(
!
_bg_error
.
ok
())
return
;
background_compaction
();
_bg_compaction_scheduled
=
false
;
_bg_work_finish_signal
.
notify_all
();
}
void
DBImpl
::
background_compaction
()
{
_pMemMgr
->
serialize
();
}
void
DBImpl
::
compact_memory
()
{
DBImpl
::~
DBImpl
()
{
std
::
lock_guard
<
std
::
mutex
>
_mutex
;
_shutting_down
.
store
(
true
,
std
::
memory_order_release
);
while
(
_bg_compaction_scheduled
)
{
_bg_work_finish_signal
.
wait
();
}
}
/*
...
...
cpp/src/db/db_impl.h
浏览文件 @
1bc01da8
...
...
@@ -49,6 +49,7 @@ private:
std
::
condition_variable
_bg_work_finish_signal
;
bool
_bg_compaction_scheduled
;
Status
_bg_error
;
std
::
atomic
<
bool
>
_shutting_down
;
std
::
shared_ptr
<
Meta
>
_pMeta
;
std
::
shared_ptr
<
MemManager
>
_pMemMgr
;
...
...
cpp/src/db/memvectors.cpp
浏览文件 @
1bc01da8
...
...
@@ -52,7 +52,7 @@ MemVectors::~MemVectors() {
* MemManager
*/
MemVectors
*
MemManager
::
get_mem_by_group
(
const
std
::
string
&
group_id_
)
{
VectorsPtr
MemManager
::
get_mem_by_group
(
const
std
::
string
&
group_id_
)
{
auto
memIt
=
_memMap
.
find
(
group_id_
);
if
memIt
!=
_memMap
.
end
()
{
return
&
(
memIt
->
second
);
...
...
@@ -63,15 +63,16 @@ MemVectors* MemManager::get_mem_by_group(const std::string& group_id_) {
if
(
!
status
.
ok
())
{
return
nullptr
;
}
_memMap
[
group_id
]
=
MemVectors
(
group_info
.
dimension
,
group_info
.
next_file_location
);
return
&
(
_memMap
[
group_id
]);
_memMap
[
group_id
]
=
std
::
shared_ptr
<
MemVectors
>
(
new
MemVectors
(
group_info
.
dimension
,
group_info
.
next_file_location
));
return
_memMap
[
group_id
];
}
Status
MemManager
::
add_vectors
(
const
std
::
string
&
group_id_
,
size_t
n_
,
const
float
*
vectors_
,
IDNumbers
&
vector_ids_
)
{
// PXU TODO
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
return
add_vectors_no_lock
(
group_id_
,
n_
,
vectors_
,
vector_ids_
);
}
...
...
@@ -86,5 +87,35 @@ Status MemManager::add_vectors_no_lock(const std::string& group_id_,
return
mem
->
add
(
n
,
vectors
,
vector_ids_
);
}
Status
MemManager
::
mark_memory_as_immutable
()
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
_mutex
);
for
(
auto
&
kv
:
_memMap
)
{
_immMems
.
push_back
(
kv
.
second
);
}
_memMap
.
clear
();
}
/* bool MemManager::need_serialize(double interval) { */
/* if (_immMems.size() > 0) { */
/* return false; */
/* } */
/* auto diff = std::difftime(std::time(nullptr), _last_compact_time); */
/* if (diff >= interval) { */
/* return true; */
/* } */
/* return false; */
/* } */
Status
MemManager
::
serialize
()
{
mark_memory_as_immutable
();
for
(
auto
&
mem
:
_immMems
)
{
mem
->
serialize
()
}
_immMems
.
clear
();
/* _last_compact_time = std::time(nullptr); */
}
}
// namespace vecengine
cpp/src/db/memvectors.h
浏览文件 @
1bc01da8
...
...
@@ -3,6 +3,7 @@
#include <map>
#include <string>
#include <ctime>
#include "id_generators.h"
#include "status.h"
...
...
@@ -42,20 +43,28 @@ class Meta;
class
MemManager
{
public:
MemManager
(
const
std
::
shared_ptr
<
Meta
>&
meta_
)
:
_pMeta
(
meta_
)
{}
typedef
std
::
shared_ptr
<
MemVectors
>
VectorsPtr
;
MemManager
(
const
std
::
shared_ptr
<
Meta
>&
meta_
)
:
_pMeta
(
meta_
),
_last_compact_time
(
std
::
time
(
nullptr
))
{}
MemVectors
*
get_mem_by_group
(
const
std
::
string
&
group_id_
);
VectorsPtr
get_mem_by_group
(
const
std
::
string
&
group_id_
);
Status
add_vectors
(
const
std
::
string
&
group_id_
,
size_t
n_
,
const
float
*
vectors_
,
IDNumbers
&
vector_ids_
);
Status
serialize
();
private:
Status
add_vectors_no_lock
(
const
std
::
string
&
group_id_
,
size_t
n_
,
const
float
*
vectors_
,
IDNumbers
&
vector_ids_
);
typedef
std
::
map
<
std
::
string
,
MemVectors
>
MemMap
;
typedef
std
::
map
<
std
::
string
,
VectorsPtr
>
MemMap
;
typedef
std
::
vector
<
VectorsPtr
>
ImmMemPool
;
MemMap
_memMap
;
ImmMemPool
_immMems
;
std
::
shared_ptr
<
Meta
>
_pMeta
;
std
::
time_t
_last_compact_time
;
std
::
mutex
_mutex
;
};
// MemManager
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录