Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
dfb07ae0
T
TDengine
项目概览
taosdata
/
TDengine
12 个月 前同步成功
通知
1180
Star
22014
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
dfb07ae0
编写于
7月 04, 2020
作者:
H
Haojun Liao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[td-225] refactor cache module
上级
74ea5ff4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
38 addition
and
35 deletion
+38
-35
src/util/inc/tcache.h
src/util/inc/tcache.h
+3
-2
src/util/src/tcache.c
src/util/src/tcache.c
+35
-33
未找到文件。
src/util/inc/tcache.h
浏览文件 @
dfb07ae0
...
...
@@ -65,7 +65,7 @@ typedef struct {
int64_t
totalSize
;
// total allocated buffer in this hash table, SCacheObj is not included.
int64_t
refreshTime
;
STrashElem
*
pTrash
;
c
onst
char
*
cacheN
ame
;
c
har
*
n
ame
;
// void * tmrCtrl;
// void * pTimer;
SCacheStatis
statistics
;
...
...
@@ -163,8 +163,9 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove);
/**
* move all data node into trash, clear node in trash can if it is not referenced by any clients
* @param handle
* @param _remove remove the data or not if refcount is greater than 0
*/
void
taosCacheEmpty
(
SCacheObj
*
pCacheObj
);
void
taosCacheEmpty
(
SCacheObj
*
pCacheObj
,
bool
_remove
);
/**
* release all allocated memory and destroy the cache object.
...
...
src/util/src/tcache.c
浏览文件 @
dfb07ae0
...
...
@@ -119,9 +119,8 @@ static FORCE_INLINE void taosCacheReleaseNode(SCacheObj *pCacheObj, SCacheDataNo
int32_t
size
=
pNode
->
size
;
taosHashRemove
(
pCacheObj
->
pHashTable
,
pNode
->
key
,
pNode
->
keySize
);
uDebug
(
"key:%p, %p is destroyed from cache, totalNum:%d totalSize:%"
PRId64
"bytes size:%dbytes, cacheName:%s"
,
pNode
->
key
,
pNode
->
data
,
(
int32_t
)
taosHashGetSize
(
pCacheObj
->
pHashTable
),
pCacheObj
->
totalSize
,
size
,
pCacheObj
->
cacheName
);
uDebug
(
"cache:%s, key:%p, %p is destroyed from cache, totalNum:%d totalSize:%"
PRId64
"bytes size:%dbytes"
,
pCacheObj
->
name
,
pNode
->
key
,
pNode
->
data
,
(
int32_t
)
taosHashGetSize
(
pCacheObj
->
pHashTable
),
pCacheObj
->
totalSize
,
size
);
if
(
pCacheObj
->
freeFp
)
pCacheObj
->
freeFp
(
pNode
->
data
);
free
(
pNode
);
}
...
...
@@ -226,7 +225,7 @@ static void doCleanupDataCache(SCacheObj *pCacheObj);
*/
static
void
*
taosCacheRefresh
(
void
*
handle
);
SCacheObj
*
taosCacheInit
WithCb
(
int32_t
keyType
,
int64_t
refreshTimeInSeconds
,
bool
extendLifespan
,
__cache_freeres_fn_t
fn
,
const
char
*
cacheName
)
{
SCacheObj
*
taosCacheInit
(
int32_t
keyType
,
int64_t
refreshTimeInSeconds
,
bool
extendLifespan
,
__cache_freeres_fn_t
fn
,
const
char
*
cacheName
)
{
if
(
refreshTimeInSeconds
<=
0
)
{
return
NULL
;
}
...
...
@@ -238,7 +237,7 @@ SCacheObj *taosCacheInitWithCb(int32_t keyType, int64_t refreshTimeInSeconds, bo
}
pCacheObj
->
pHashTable
=
taosHashInit
(
128
,
taosGetDefaultHashFunction
(
keyType
),
false
);
pCacheObj
->
cacheName
=
cacheName
;
pCacheObj
->
name
=
strdup
(
cacheName
)
;
if
(
pCacheObj
->
pHashTable
==
NULL
)
{
free
(
pCacheObj
);
uError
(
"failed to allocate memory, reason:%s"
,
strerror
(
errno
));
...
...
@@ -268,10 +267,6 @@ SCacheObj *taosCacheInitWithCb(int32_t keyType, int64_t refreshTimeInSeconds, bo
return
pCacheObj
;
}
SCacheObj
*
taosCacheInit
(
int32_t
keyType
,
int64_t
refreshTimeInSeconds
,
bool
extendLifespan
,
__cache_freeres_fn_t
fn
,
const
char
*
cacheName
)
{
return
taosCacheInitWithCb
(
keyType
,
refreshTimeInSeconds
,
extendLifespan
,
fn
,
cacheName
);
}
void
*
taosCachePut
(
SCacheObj
*
pCacheObj
,
const
void
*
key
,
size_t
keyLen
,
const
void
*
pData
,
size_t
dataSize
,
int
duration
)
{
SCacheDataNode
*
pNode
;
...
...
@@ -288,16 +283,16 @@ void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const v
if
(
NULL
!=
pNode
)
{
pCacheObj
->
totalSize
+=
pNode
->
size
;
uDebug
(
"key:%p, %p added into cache, added:%"
PRIu64
", expire:%"
PRIu64
", totalNum:%d totalSize:%"
PRId64
"bytes size:%"
PRId64
"bytes
, cacheName:%s
"
,
key
,
pNode
->
data
,
pNode
->
addedTime
,
(
pNode
->
lifespan
*
pNode
->
extendFactor
+
pNode
->
addedTime
),
(
int32_t
)
taosHashGetSize
(
pCacheObj
->
pHashTable
),
pCacheObj
->
totalSize
,
dataSize
,
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s,
key:%p, %p added into cache, added:%"
PRIu64
", expire:%"
PRIu64
", totalNum:%d totalSize:%"
PRId64
"bytes size:%"
PRId64
"bytes"
,
pCacheObj
->
name
,
key
,
pNode
->
data
,
pNode
->
addedTime
,
(
pNode
->
lifespan
*
pNode
->
extendFactor
+
pNode
->
addedTime
),
(
int32_t
)
taosHashGetSize
(
pCacheObj
->
pHashTable
),
pCacheObj
->
totalSize
,
dataSize
);
}
else
{
uError
(
"
key:%p, failed to added into cache, out of memory, cacheName:%s"
,
key
,
pCacheObj
->
cacheName
);
uError
(
"
cache:%s, key:%p, failed to added into cache, out of memory"
,
pCacheObj
->
name
,
key
);
}
}
else
{
// old data exists, update the node
pNode
=
taosUpdateCacheImpl
(
pCacheObj
,
pOld
,
key
,
keyLen
,
pData
,
dataSize
,
duration
*
1000L
);
uDebug
(
"
key:%p, %p exist in cache, updated, cacheName:%s"
,
key
,
pNode
->
data
,
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s, key:%p, %p exist in cache, updated"
,
pCacheObj
->
name
,
key
,
pNode
->
data
);
}
__cache_unlock
(
pCacheObj
);
...
...
@@ -332,10 +327,10 @@ void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen
if
(
ptNode
!=
NULL
)
{
atomic_add_fetch_32
(
&
pCacheObj
->
statistics
.
hitCount
,
1
);
uDebug
(
"
key:%p, %p is retrieved from cache, refcnt:%d, cacheName:%s"
,
key
,
(
*
ptNode
)
->
data
,
ref
,
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s, key:%p, %p is retrieved from cache, refcnt:%d"
,
pCacheObj
->
name
,
key
,
(
*
ptNode
)
->
data
,
ref
);
}
else
{
atomic_add_fetch_32
(
&
pCacheObj
->
statistics
.
missCount
,
1
);
uDebug
(
"
key:%p, not in cache, retrieved failed, cacheName:%s"
,
key
,
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s, key:%p, not in cache, retrieved failed"
,
pCacheObj
->
name
,
key
);
}
atomic_add_fetch_32
(
&
pCacheObj
->
statistics
.
totalAccess
,
1
);
...
...
@@ -360,11 +355,11 @@ void* taosCacheUpdateExpireTimeByName(SCacheObj *pCacheObj, void *key, size_t ke
if
(
ptNode
!=
NULL
)
{
atomic_add_fetch_32
(
&
pCacheObj
->
statistics
.
hitCount
,
1
);
uDebug
(
"
key:%p, %p expireTime is updated in cache, refcnt:%d, cacheName:%s"
,
key
,
(
*
ptNode
)
->
data
,
T_REF_VAL_GET
(
*
ptNode
),
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s, key:%p, %p expireTime is updated in cache, refcnt:%d"
,
pCacheObj
->
name
,
key
,
(
*
ptNode
)
->
data
,
T_REF_VAL_GET
(
*
ptNode
)
);
}
else
{
atomic_add_fetch_32
(
&
pCacheObj
->
statistics
.
missCount
,
1
);
uDebug
(
"
key:%p, not in cache, retrieved failed, cacheName:%s"
,
key
,
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s, key:%p, not in cache, retrieved failed"
,
pCacheObj
->
name
,
key
);
}
atomic_add_fetch_32
(
&
pCacheObj
->
statistics
.
totalAccess
,
1
);
...
...
@@ -383,7 +378,7 @@ void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data) {
}
int32_t
ref
=
T_REF_INC
(
ptNode
);
uDebug
(
"
%p acquired by data in cache, refcnt:%d, cacheName:%s"
,
ptNode
->
data
,
ref
,
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s, data: %p acquired by data in cache, refcnt:%d"
,
pCacheObj
->
name
,
ptNode
->
data
,
ref
);
// if the remained life span is less then the (*ptNode)->lifeSpan, add up one lifespan
if
(
pCacheObj
->
extendLifespan
)
{
...
...
@@ -391,7 +386,7 @@ void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data) {
if
((
now
-
ptNode
->
addedTime
)
<
ptNode
->
lifespan
*
ptNode
->
extendFactor
)
{
ptNode
->
extendFactor
+=
1
;
uDebug
(
"
%p extend life time to %"
PRId64
,
ptNode
->
data
,
uDebug
(
"
cache:%s, %p extend life time to %"
PRId64
,
pCacheObj
->
name
,
ptNode
->
data
,
ptNode
->
lifespan
*
ptNode
->
extendFactor
+
ptNode
->
addedTime
);
}
}
...
...
@@ -437,7 +432,7 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) {
*
data
=
NULL
;
int16_t
ref
=
T_REF_DEC
(
pNode
);
uDebug
(
"
key:%p, %p is released, refcnt:%d, cacheName:%s"
,
pNode
->
key
,
pNode
->
data
,
ref
,
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s, key:%p, %p is released, refcnt:%d"
,
pCacheObj
->
name
,
pNode
->
key
,
pNode
->
data
,
ref
);
if
(
_remove
&&
(
!
pNode
->
inTrashCan
))
{
__cache_wr_lock
(
pCacheObj
);
...
...
@@ -455,7 +450,7 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) {
}
}
void
taosCacheEmpty
(
SCacheObj
*
pCacheObj
)
{
void
taosCacheEmpty
(
SCacheObj
*
pCacheObj
,
bool
_remove
)
{
SHashMutableIterator
*
pIter
=
taosHashCreateIter
(
pCacheObj
->
pHashTable
);
__cache_wr_lock
(
pCacheObj
);
...
...
@@ -465,12 +460,16 @@ void taosCacheEmpty(SCacheObj *pCacheObj) {
}
SCacheDataNode
*
pNode
=
*
(
SCacheDataNode
**
)
taosHashIterGet
(
pIter
);
taosCacheMoveToTrash
(
pCacheObj
,
pNode
);
if
(
T_REF_VAL_GET
(
pNode
)
==
0
||
_remove
)
{
taosCacheReleaseNode
(
pCacheObj
,
pNode
);
}
else
{
taosCacheMoveToTrash
(
pCacheObj
,
pNode
);
}
}
__cache_unlock
(
pCacheObj
);
taosHashDestroyIter
(
pIter
);
taosTrashCanEmpty
(
pCacheObj
,
fals
e
);
taosTrashCanEmpty
(
pCacheObj
,
_remov
e
);
}
void
taosCacheCleanup
(
SCacheObj
*
pCacheObj
)
{
...
...
@@ -481,7 +480,7 @@ void taosCacheCleanup(SCacheObj *pCacheObj) {
pCacheObj
->
deleting
=
1
;
pthread_join
(
pCacheObj
->
refreshWorker
,
NULL
);
uInfo
(
"cache
Name:%p, will be cleanuped"
,
pCacheObj
->
cacheN
ame
);
uInfo
(
"cache
:%s will be cleaned up"
,
pCacheObj
->
n
ame
);
doCleanupDataCache
(
pCacheObj
);
}
...
...
@@ -601,22 +600,25 @@ void doCleanupDataCache(SCacheObj *pCacheObj) {
SHashMutableIterator
*
pIter
=
taosHashCreateIter
(
pCacheObj
->
pHashTable
);
while
(
taosHashIterNext
(
pIter
))
{
SCacheDataNode
*
pNode
=
*
(
SCacheDataNode
**
)
taosHashIterGet
(
pIter
);
// if (pNode->expiredTime <= expiredTime && T_REF_VAL_GET(pNode) <= 0) {
if
(
T_REF_VAL_GET
(
pNode
)
<=
0
)
{
int32_t
c
=
T_REF_VAL_GET
(
pNode
);
if
(
c
<=
0
)
{
taosCacheReleaseNode
(
pCacheObj
,
pNode
);
}
else
{
uDebug
(
"
key:%p, %p will not remove from cache, refcnt:%d, cacheName:%s"
,
pNode
->
key
,
pNode
->
data
,
T_REF_VAL_GET
(
pNode
),
pCacheObj
->
cacheName
);
uDebug
(
"
cache:%s key:%p, %p will not remove from cache, refcnt:%d"
,
pCacheObj
->
name
,
pNode
->
key
,
pNode
->
data
,
T_REF_VAL_GET
(
pNode
)
);
}
}
taosHashDestroyIter
(
pIter
);
taosHashCleanup
(
pCacheObj
->
pHashTable
);
// todo memory leak if there are object with refcount greater than 0 in hash table?
taosHashCleanup
(
pCacheObj
->
pHashTable
);
__cache_unlock
(
pCacheObj
);
taosTrashCanEmpty
(
pCacheObj
,
true
);
__cache_lock_destroy
(
pCacheObj
);
tfree
(
pCacheObj
->
name
);
memset
(
pCacheObj
,
0
,
sizeof
(
SCacheObj
));
free
(
pCacheObj
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录