From c7b10200379b54a0cd99bd03d0a266514cf3fd70 Mon Sep 17 00:00:00 2001 From: Lei Zhang <27994433+SWJTU-ZhangLei@users.noreply.github.com> Date: Fri, 23 Aug 2024 12:20:40 +0800 Subject: [PATCH] [opt](fe) Use `table.readLock` in `TabletStatMgr` for updating statistics (#39612) --- .../main/java/org/apache/doris/catalog/Table.java | 9 +++++++++ .../java/org/apache/doris/catalog/TabletStatMgr.java | 12 +++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java index 862d6c1878e026..389be51d57af91 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java @@ -174,6 +174,15 @@ public void readLock() { } } + public boolean readLockIfExist() { + readLock(); + if (isDropped) { + readUnlock(); + return false; + } + return true; + } + public boolean tryReadLock(long timeout, TimeUnit unit) { try { boolean res = this.rwLock.readLock().tryLock(timeout, unit); diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/TabletStatMgr.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/TabletStatMgr.java index b9093dcd83828d..7224d66ad44e91 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/TabletStatMgr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/TabletStatMgr.java @@ -34,7 +34,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ForkJoinPool; -import java.util.concurrent.TimeUnit; /* * TabletStatMgr is for collecting tablet(replica) statistics from backends. @@ -114,13 +113,7 @@ protected void runAfterCatalogReady() { Long tableRowCount = 0L; - // Use try write lock to avoid such cases - // Time1: Thread1 hold read lock for 5min - // Time2: Thread2 want to add write lock, then it will be the first element in lock queue - // Time3: Thread3 want to add read lock, but it will not, because thread 2 want to add write lock - // In this case, thread 3 has to wait more than 5min, because it has to wait thread 2 to add - // write lock and release write lock and thread 2 has to wait thread 1 to release read lock - if (!table.tryWriteLockIfExist(3000, TimeUnit.MILLISECONDS)) { + if (!table.readLockIfExist()) { continue; } try { @@ -193,6 +186,7 @@ protected void runAfterCatalogReady() { } // end for indices } // end for partitions + // this is only one thread to update table statistics, readLock is enough olapTable.setStatistics(new OlapTable.Statistics(db.getName(), table.getName(), tableDataSize, tableTotalReplicaDataSize, tableRemoteDataSize, tableReplicaCount, tableRowCount, 0L, 0L)); @@ -202,7 +196,7 @@ protected void runAfterCatalogReady() { table.getName(), db.getFullName()); } } finally { - table.writeUnlock(); + table.readUnlock(); } } }