From 7d27c3902afcb2bad9a3e083637c0ab7051aa133 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 25 Jan 2024 15:45:09 +0800 Subject: [PATCH 1/3] feat: avoid meta rwlock starvation --- include/os/osThread.h | 5 +++++ source/dnode/vnode/src/meta/metaOpen.c | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/os/osThread.h b/include/os/osThread.h index f0b79ac2c9..08575058ba 100644 --- a/include/os/osThread.h +++ b/include/os/osThread.h @@ -69,6 +69,11 @@ typedef pthread_key_t TdThreadKey; #define taosThreadCleanupPush pthread_cleanup_push #define taosThreadCleanupPop pthread_cleanup_pop +#if _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L +#define taosThreadRwlockAttrSetKindNP(A, B) pthread_rwlockattr_setkind_np(A, B) +#else +#define taosThreadRwlockAttrSetKindNP(A, B) ((void)0) +#endif #if defined(WINDOWS) && !defined(__USE_PTHREAD) #define TD_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER_FORBID diff --git a/source/dnode/vnode/src/meta/metaOpen.c b/source/dnode/vnode/src/meta/metaOpen.c index 8cab17c417..1f26c22167 100644 --- a/source/dnode/vnode/src/meta/metaOpen.c +++ b/source/dnode/vnode/src/meta/metaOpen.c @@ -27,7 +27,14 @@ static int taskIdxKeyCmpr(const void *pKey1, int kLen1, const void *pKey2, int k static int btimeIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); static int ncolIdxCmpr(const void *pKey1, int kLen1, const void *pKey2, int kLen2); -static int32_t metaInitLock(SMeta *pMeta) { return taosThreadRwlockInit(&pMeta->lock, NULL); } +static int32_t metaInitLock(SMeta *pMeta) { + TdThreadRwlockAttr attr; + taosThreadRwlockAttrInit(&attr); + taosThreadRwlockAttrSetKindNP(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); + taosThreadRwlockInit(&pMeta->lock, NULL); + taosThreadRwlockAttrDestroy(&attr); + return 0; +} static int32_t metaDestroyLock(SMeta *pMeta) { return taosThreadRwlockDestroy(&pMeta->lock); } static void metaCleanup(SMeta **ppMeta); From e8a8748dc9c11d67e2fff703d56c4a0e68ea0510 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 25 Jan 2024 15:48:45 +0800 Subject: [PATCH 2/3] more fix --- source/dnode/vnode/src/meta/metaOpen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/dnode/vnode/src/meta/metaOpen.c b/source/dnode/vnode/src/meta/metaOpen.c index 1f26c22167..1e434b7cd8 100644 --- a/source/dnode/vnode/src/meta/metaOpen.c +++ b/source/dnode/vnode/src/meta/metaOpen.c @@ -31,7 +31,7 @@ static int32_t metaInitLock(SMeta *pMeta) { TdThreadRwlockAttr attr; taosThreadRwlockAttrInit(&attr); taosThreadRwlockAttrSetKindNP(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); - taosThreadRwlockInit(&pMeta->lock, NULL); + taosThreadRwlockInit(&pMeta->lock, &attr); taosThreadRwlockAttrDestroy(&attr); return 0; } From b03b111c2a9a5c99fd1d84c14c54e9fefb30b5c9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Fri, 26 Jan 2024 14:38:58 +0800 Subject: [PATCH 3/3] make it compilable on windows and macos --- include/os/osThread.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/os/osThread.h b/include/os/osThread.h index 08575058ba..4ef4550419 100644 --- a/include/os/osThread.h +++ b/include/os/osThread.h @@ -69,11 +69,19 @@ typedef pthread_key_t TdThreadKey; #define taosThreadCleanupPush pthread_cleanup_push #define taosThreadCleanupPop pthread_cleanup_pop +#if !defined(WINDOWS) +#if defined(_TD_DARWIN_64) // MACOS +#define taosThreadRwlockAttrSetKindNP(A, B) ((void)0) +#else // LINUX #if _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200809L #define taosThreadRwlockAttrSetKindNP(A, B) pthread_rwlockattr_setkind_np(A, B) #else #define taosThreadRwlockAttrSetKindNP(A, B) ((void)0) #endif +#endif +#else // WINDOWS +#define taosThreadRwlockAttrSetKindNP(A, B) ((void)0) +#endif #if defined(WINDOWS) && !defined(__USE_PTHREAD) #define TD_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER_FORBID