Skip to content

Commit

Permalink
[Enhancement] reduce lock granular of TabletStatMgr (backport #50668) (
Browse files Browse the repository at this point in the history
…#50707)

Co-authored-by: Murphy <[email protected]>
  • Loading branch information
mergify[bot] and murphyatwork authored Sep 5, 2024
1 parent 4640a00 commit acdf2d3
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions fe/fe-core/src/main/java/com/starrocks/catalog/TabletStatMgr.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.starrocks.catalog.MaterializedIndex.IndexExtState;
import com.starrocks.common.ClientPool;
import com.starrocks.common.Config;
Expand Down Expand Up @@ -102,25 +103,28 @@ protected void runAfterCatalogReady() {
if (db == null) {
continue;
}
db.writeLock();
try {
for (Table table : db.getTables()) {
long totalRowCount = 0L;
if (!table.isNativeTableOrMaterializedView()) {
continue;
}
for (Table table : db.getTables()) {
long totalRowCount = 0L;
if (!table.isNativeTableOrMaterializedView()) {
continue;
}

// NOTE: calculate the row first with read lock, then update the stats with write lock
db.readLock();
Map<Long, Long> indexRowCountMap = Maps.newHashMap();
try {
OlapTable olapTable = (OlapTable) table;
for (Partition partition : olapTable.getAllPartitions()) {
for (PhysicalPartition physicalPartition : partition.getSubPartitions()) {
long version = physicalPartition.getVisibleVersion();
for (MaterializedIndex index : physicalPartition.getMaterializedIndices(
IndexExtState.VISIBLE)) {
long indexRowCount = 0L;
// NOTE: can take a rather long time to iterate lots of tablets
for (Tablet tablet : index.getTablets()) {
indexRowCount += tablet.getRowCount(version);
} // end for tablets
index.setRowCount(indexRowCount);
indexRowCountMap.put(index.getId(), indexRowCount);
if (!olapTable.isTempPartition(partition.getId())) {
totalRowCount += indexRowCount;
}
Expand All @@ -129,10 +133,29 @@ protected void runAfterCatalogReady() {
} // end for partitions
LOG.debug("finished to set row num for table: {} in database: {}",
table.getName(), db.getFullName());
} finally {
db.readUnlock();
}

// update
db.writeLock();
try {
OlapTable olapTable = (OlapTable) table;
for (Partition partition : olapTable.getAllPartitions()) {
for (PhysicalPartition physicalPartition : partition.getSubPartitions()) {
for (MaterializedIndex index :
physicalPartition.getMaterializedIndices(IndexExtState.VISIBLE)) {
Long indexRowCount = indexRowCountMap.get(index.getId());
if (indexRowCount != null) {
index.setRowCount(indexRowCount);
}
}
}
}
adjustStatUpdateRows(table.getId(), totalRowCount);
} finally {
db.writeUnlock();
}
} finally {
db.writeUnlock();
}
}
LOG.info("finished to update index row num of all databases. cost: {} ms",
Expand All @@ -144,7 +167,8 @@ private void updateLocalTabletStat() {
if (!RunMode.isSharedNothingMode()) {
return;
}
ImmutableMap<Long, Backend> backends = GlobalStateMgr.getCurrentSystemInfo().getIdToBackend();
ImmutableMap<Long, Backend> backends =
GlobalStateMgr.getCurrentState().getNodeMgr().getClusterInfo().getIdToBackend();

long start = System.currentTimeMillis();
for (Backend backend : backends.values()) {
Expand Down Expand Up @@ -357,7 +381,8 @@ private void sendTasks() {
LOG.debug("Sent tablet stat collection task to node {} for partition {} of version {}. tablet count={}",
node.getHost(), debugName(), version, entry.getValue().size());
} catch (Throwable e) {
LOG.warn("Fail to send tablet stat task to host {} for partition {}: {}", node.getHost(), debugName(),
LOG.warn("Fail to send tablet stat task to host {} for partition {}: {}", node.getHost(),
debugName(),
e.getMessage());
}
}
Expand Down

0 comments on commit acdf2d3

Please sign in to comment.