Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] reduce lock granular of TabletStatMgr (backport #50668) #50707

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading