Skip to content

Commit

Permalink
[opt](fe) Use table.readLock in TabletStatMgr for updating statis…
Browse files Browse the repository at this point in the history
…tics
  • Loading branch information
SWJTU-ZhangLei committed Aug 20, 2024
1 parent bba7c2c commit 029f727
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected void runAfterCatalogReady() {
Long tableRowsetCount = 0L;
Long tableSegmentCount = 0L;

if (!table.writeLockIfExist()) {
if (!table.readLockIfExist()) {
continue;
}
try {
Expand Down Expand Up @@ -203,13 +203,14 @@ 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, 0L,
tableReplicaCount, tableRowCount, tableRowsetCount, tableSegmentCount));
LOG.debug("finished to set row num for table: {} in database: {}",
table.getName(), db.getFullName());
} finally {
table.writeUnlock();
table.readUnlock();
}

newCloudTableStatsMap.put(Pair.of(dbId, table.getId()), new OlapTable.Statistics(db.getName(),
Expand Down
9 changes: 9 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,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 {
Expand Down Expand Up @@ -169,6 +163,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));
Expand All @@ -178,7 +173,7 @@ protected void runAfterCatalogReady() {
table.getName(), db.getFullName());
}
} finally {
table.writeUnlock();
table.readUnlock();
}
}
}
Expand Down

0 comments on commit 029f727

Please sign in to comment.