Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
12187a38
T
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
Star
22018
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看板
未验证
提交
12187a38
编写于
4月 19, 2022
作者:
H
Haojun Liao
提交者:
GitHub
4月 19, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9101 from haoyifan/tcache_cleanup_branch
Tcache cleanup branch
上级
8d2f7e63
fbfa7a09
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
31 addition
and
23 deletion
+31
-23
src/util/src/tcache.c
src/util/src/tcache.c
+31
-23
未找到文件。
src/util/src/tcache.c
浏览文件 @
12187a38
...
@@ -85,7 +85,7 @@ static void doInitRefreshThread(void) {
...
@@ -85,7 +85,7 @@ static void doInitRefreshThread(void) {
pthread_attr_destroy
(
&
thattr
);
pthread_attr_destroy
(
&
thattr
);
}
}
pthread_t
doRegisterCacheObj
(
SCacheObj
*
pCacheObj
)
{
static
pthread_t
doRegisterCacheObj
(
SCacheObj
*
pCacheObj
)
{
pthread_once
(
&
cacheThreadInit
,
doInitRefreshThread
);
pthread_once
(
&
cacheThreadInit
,
doInitRefreshThread
);
pthread_mutex_lock
(
&
guard
);
pthread_mutex_lock
(
&
guard
);
...
@@ -184,36 +184,44 @@ static FORCE_INLINE void doDestroyTrashcanElem(SCacheObj* pCacheObj, STrashElem
...
@@ -184,36 +184,44 @@ static FORCE_INLINE void doDestroyTrashcanElem(SCacheObj* pCacheObj, STrashElem
}
}
SCacheObj
*
taosCacheInit
(
int32_t
keyType
,
int64_t
refreshTimeInSeconds
,
bool
extendLifespan
,
__cache_free_fn_t
fn
,
const
char
*
cacheName
)
{
SCacheObj
*
taosCacheInit
(
int32_t
keyType
,
int64_t
refreshTimeInSeconds
,
bool
extendLifespan
,
__cache_free_fn_t
fn
,
const
char
*
cacheName
)
{
const
int32_t
SLEEP_DURATION
=
500
;
//500 ms
const
int32_t
SLEEP_DURATION
_IN_MS
=
500
;
if
(
refreshTimeInSeconds
<=
0
)
{
if
(
refreshTimeInSeconds
<=
0
)
{
return
NULL
;
return
NULL
;
}
}
SCacheObj
*
pCacheObj
=
(
SCacheObj
*
)
calloc
(
1
,
sizeof
(
SCacheObj
));
SCacheObj
*
pCacheObj
=
(
SCacheObj
*
)
calloc
(
1
,
sizeof
(
SCacheObj
));
if
(
pCacheObj
==
NULL
)
{
if
(
pCacheObj
==
NULL
)
{
uError
(
"failed to allocate memory, reason:%s"
,
strerror
(
errno
));
uError
(
"failed to allocate memory, reason:%s"
,
strerror
(
errno
));
return
NULL
;
return
NULL
;
}
}
pCacheObj
->
pHashTable
=
taosHashInit
(
4096
,
taosGetDefaultHashFunction
(
keyType
),
false
,
HASH_ENTRY_LOCK
);
pCacheObj
->
pHashTable
=
taosHashInit
(
4096
,
taosGetDefaultHashFunction
(
keyType
),
false
,
HASH_ENTRY_LOCK
);
pCacheObj
->
name
=
strdup
(
cacheName
);
if
(
pCacheObj
->
pHashTable
==
NULL
)
{
if
(
pCacheObj
->
pHashTable
==
NULL
)
{
free
(
pCacheObj
);
free
(
pCacheObj
);
uError
(
"failed to allocate memory, reason:%s"
,
strerror
(
errno
));
uError
(
"failed to allocate memory, reason:%s"
,
strerror
(
errno
));
return
NULL
;
return
NULL
;
}
}
pCacheObj
->
name
=
strdup
(
cacheName
);
if
(
pCacheObj
->
name
==
NULL
)
{
taosHashCleanup
(
pCacheObj
->
pHashTable
);
free
(
pCacheObj
);
uError
(
"failed to allocate memory, reason:%s"
,
strerror
(
errno
));
return
NULL
;
}
// set free cache node callback function
// set free cache node callback function
pCacheObj
->
freeFp
=
fn
;
pCacheObj
->
freeFp
=
fn
;
pCacheObj
->
refreshTime
=
refreshTimeInSeconds
*
1000
;
pCacheObj
->
refreshTime
=
refreshTimeInSeconds
*
1000
;
pCacheObj
->
checkTick
=
pCacheObj
->
refreshTime
/
SLEEP_DURATION
;
pCacheObj
->
checkTick
=
pCacheObj
->
refreshTime
/
SLEEP_DURATION
_IN_MS
;
pCacheObj
->
extendLifespan
=
extendLifespan
;
// the TTL after the last access
pCacheObj
->
extendLifespan
=
extendLifespan
;
// the TTL after the last access
if
(
__cache_lock_init
(
pCacheObj
)
!=
0
)
{
if
(
__cache_lock_init
(
pCacheObj
)
!=
0
)
{
taosHashCleanup
(
pCacheObj
->
pHashTable
);
taosHashCleanup
(
pCacheObj
->
pHashTable
);
free
(
pCacheObj
->
name
);
free
(
pCacheObj
);
free
(
pCacheObj
);
uError
(
"failed to init lock, reason:%s"
,
strerror
(
errno
));
uError
(
"failed to init lock, reason:%s"
,
strerror
(
errno
));
return
NULL
;
return
NULL
;
}
}
...
@@ -323,10 +331,10 @@ void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen
...
@@ -323,10 +331,10 @@ void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen
void
*
taosCacheAcquireByData
(
SCacheObj
*
pCacheObj
,
void
*
data
)
{
void
*
taosCacheAcquireByData
(
SCacheObj
*
pCacheObj
,
void
*
data
)
{
if
(
pCacheObj
==
NULL
||
data
==
NULL
)
return
NULL
;
if
(
pCacheObj
==
NULL
||
data
==
NULL
)
return
NULL
;
size_t
offset
=
offsetof
(
SCacheDataNode
,
data
);
size_t
offset
=
offsetof
(
SCacheDataNode
,
data
);
SCacheDataNode
*
ptNode
=
(
SCacheDataNode
*
)((
char
*
)
data
-
offset
);
SCacheDataNode
*
ptNode
=
(
SCacheDataNode
*
)((
char
*
)
data
-
offset
);
if
(
ptNode
->
signature
!=
(
uint64_t
)
ptNode
)
{
if
(
ptNode
->
signature
!=
(
uint64_t
)
ptNode
)
{
uError
(
"cache:%s, key: %p the data from cache is invalid"
,
pCacheObj
->
name
,
ptNode
);
uError
(
"cache:%s, key: %p the data from cache is invalid"
,
pCacheObj
->
name
,
ptNode
);
return
NULL
;
return
NULL
;
...
@@ -342,22 +350,22 @@ void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data) {
...
@@ -342,22 +350,22 @@ void *taosCacheAcquireByData(SCacheObj *pCacheObj, void *data) {
void
*
taosCacheTransfer
(
SCacheObj
*
pCacheObj
,
void
**
data
)
{
void
*
taosCacheTransfer
(
SCacheObj
*
pCacheObj
,
void
**
data
)
{
if
(
pCacheObj
==
NULL
||
data
==
NULL
||
(
*
data
)
==
NULL
)
return
NULL
;
if
(
pCacheObj
==
NULL
||
data
==
NULL
||
(
*
data
)
==
NULL
)
return
NULL
;
size_t
offset
=
offsetof
(
SCacheDataNode
,
data
);
size_t
offset
=
offsetof
(
SCacheDataNode
,
data
);
SCacheDataNode
*
ptNode
=
(
SCacheDataNode
*
)((
char
*
)(
*
data
)
-
offset
);
SCacheDataNode
*
ptNode
=
(
SCacheDataNode
*
)((
char
*
)(
*
data
)
-
offset
);
if
(
ptNode
->
signature
!=
(
uint64_t
)
ptNode
)
{
if
(
ptNode
->
signature
!=
(
uint64_t
)
ptNode
)
{
uError
(
"cache:%s, key: %p the data from cache is invalid"
,
pCacheObj
->
name
,
ptNode
);
uError
(
"cache:%s, key: %p the data from cache is invalid"
,
pCacheObj
->
name
,
ptNode
);
return
NULL
;
return
NULL
;
}
}
assert
(
T_REF_VAL_GET
(
ptNode
)
>=
1
);
assert
(
T_REF_VAL_GET
(
ptNode
)
>=
1
);
char
*
d
=
*
data
;
char
*
d
=
*
data
;
// clear its reference to old area
// clear its reference to old area
*
data
=
NULL
;
*
data
=
NULL
;
return
d
;
return
d
;
}
}
...
@@ -377,7 +385,7 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) {
...
@@ -377,7 +385,7 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) {
// It happens when there is only one object in the cache, and two threads which has referenced this object
// It happens when there is only one object in the cache, and two threads which has referenced this object
// start to free the it simultaneously [TD-1569].
// start to free the it simultaneously [TD-1569].
size_t
offset
=
offsetof
(
SCacheDataNode
,
data
);
size_t
offset
=
offsetof
(
SCacheDataNode
,
data
);
SCacheDataNode
*
pNode
=
(
SCacheDataNode
*
)((
char
*
)(
*
data
)
-
offset
);
SCacheDataNode
*
pNode
=
(
SCacheDataNode
*
)((
char
*
)(
*
data
)
-
offset
);
if
(
pNode
->
signature
!=
(
uint64_t
)
pNode
)
{
if
(
pNode
->
signature
!=
(
uint64_t
)
pNode
)
{
uError
(
"cache:%s, %p, release invalid cache data"
,
pCacheObj
->
name
,
pNode
);
uError
(
"cache:%s, %p, release invalid cache data"
,
pCacheObj
->
name
,
pNode
);
...
@@ -542,8 +550,8 @@ void taosCacheCleanup(SCacheObj *pCacheObj) {
...
@@ -542,8 +550,8 @@ void taosCacheCleanup(SCacheObj *pCacheObj) {
// wait for the refresh thread quit before destroying the cache object.
// wait for the refresh thread quit before destroying the cache object.
// But in the dll, the child thread will be killed before atexit takes effect.
// But in the dll, the child thread will be killed before atexit takes effect.
while
(
atomic_load_8
(
&
pCacheObj
->
deleting
)
!=
0
)
{
while
(
atomic_load_8
(
&
pCacheObj
->
deleting
)
!=
0
)
{
if
(
refreshWorkerNormalStopped
)
break
;
if
(
refreshWorkerNormalStopped
)
break
;
if
(
refreshWorkerUnexpectedStopped
)
return
;
if
(
refreshWorkerUnexpectedStopped
)
return
;
taosMsleep
(
50
);
taosMsleep
(
50
);
}
}
...
@@ -649,7 +657,7 @@ void doCleanupDataCache(SCacheObj *pCacheObj) {
...
@@ -649,7 +657,7 @@ void doCleanupDataCache(SCacheObj *pCacheObj) {
taosTrashcanEmpty
(
pCacheObj
,
true
);
taosTrashcanEmpty
(
pCacheObj
,
true
);
__cache_lock_destroy
(
pCacheObj
);
__cache_lock_destroy
(
pCacheObj
);
tfree
(
pCacheObj
->
name
);
tfree
(
pCacheObj
->
name
);
memset
(
pCacheObj
,
0
,
sizeof
(
SCacheObj
));
memset
(
pCacheObj
,
0
,
sizeof
(
SCacheObj
));
free
(
pCacheObj
);
free
(
pCacheObj
);
...
@@ -694,12 +702,12 @@ void* taosCacheTimedRefresh(void *handle) {
...
@@ -694,12 +702,12 @@ void* taosCacheTimedRefresh(void *handle) {
setThreadName
(
"cacheRefresh"
);
setThreadName
(
"cacheRefresh"
);
const
int32_t
SLEEP_DURATION
=
500
;
//500 ms
const
int32_t
SLEEP_DURATION
_IN_MS
=
500
;
int64_t
count
=
0
;
int64_t
count
=
0
;
atexit
(
taosCacheRefreshWorkerUnexpectedStopped
);
atexit
(
taosCacheRefreshWorkerUnexpectedStopped
);
while
(
1
)
{
while
(
1
)
{
taosMsleep
(
SLEEP_DURATION
);
taosMsleep
(
SLEEP_DURATION
_IN_MS
);
if
(
stopRefreshWorker
)
{
if
(
stopRefreshWorker
)
{
goto
_end
;
goto
_end
;
}
}
...
@@ -778,4 +786,4 @@ void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void* param1)
...
@@ -778,4 +786,4 @@ void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void* param1)
void
taosStopCacheRefreshWorker
(
void
)
{
void
taosStopCacheRefreshWorker
(
void
)
{
stopRefreshWorker
=
true
;
stopRefreshWorker
=
true
;
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录