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] Add some logs to help online businesses quickly find the queries that cause FE oom #51528

Merged
merged 1 commit into from
Sep 29, 2024

Conversation

before-Sunrise
Copy link
Contributor

Why I'm doing:

What I'm doing:

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

Signed-off-by: before-Sunrise <[email protected]>
Copy link

sonarcloud bot commented Sep 29, 2024

}

}

/**
* We query meta to get request's data location
* extra result info will pass to backend ScanNode
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
A potential integer overflow when calculating totalScanRangeBytes, which could lead to incorrect memory checks and potential OOM issues.

You can modify the code like this:

@@ -188,6 +189,11 @@ public class OlapScanNode extends ScanNode {
 
     private VectorSearchOptions vectorSearchOptions = new VectorSearchOptions();
 
+    private boolean calculatedScanRange = false;
+
+    private long totalScanRangeBytes = 0;
+
+    // Constructs node to scan given data files of table 'tbl'.
     // Constructs node to scan given data files of table 'tbl'.
     public OlapScanNode(PlanNodeId id, TupleDescriptor desc, String planNodeName) {
         super(id, desc, planNodeName);
@@ -641,6 +647,11 @@ public void addScanRangeLocations(Partition partition,
             scanRangeLocations.setScan_range(scanRange);
 
             bucketSeq2locations.put(tabletId2BucketSeq.get(tabletId), scanRangeLocations);
+            if (!calculatedScanRange && scanRangeLocations != null) {
+                long scanRangeSize = SizeEstimator.estimate(scanRangeLocations);
+                checkIfScanRangeNumSafe(scanRangeSize);
+                calculatedScanRange = true;
+            }
 
             result.add(scanRangeLocations);
         }
@@ -731,6 +742,32 @@ private void computeTabletInfo() throws UserException {
         }
     }
 
+    public void checkIfScanRangeNumSafe(long scanRangeSize) {
+        long totalPartitionNum = 0;
+        long totalTabletsNum = 0;
+        for (long partitionId : selectedPartitionIds) {
+            final Partition partition = olapTable.getPartition(partitionId);
+            List<PhysicalPartition> physicalPartitions = (List<PhysicalPartition>) partition.getSubPartitions();
+            totalPartitionNum += physicalPartitions.size();
+            for (PhysicalPartition physicalPartition : physicalPartitions) {
+                final MaterializedIndex selectedTable = physicalPartition.getIndex(selectedIndexId);
+                totalTabletsNum += selectedTable.getTablets().size();
+            }
+        }
+
+        totalScanRangeBytes = Math.multiplyExact(scanRangeSize, totalTabletsNum); // Prevent overflow
+        if (totalScanRangeBytes > 1024 * 1024) {
+            Runtime runtime = Runtime.getRuntime();
+            long freeMemory = runtime.freeMemory();
+            if (totalScanRangeBytes > freeMemory / 2) {
+                LOG.warn(
+                        "Try to allocate too many scan ranges for table {}, which may cause FE OOM, Partition Num:{}, tablet Num:{}, Scan Range Total Bytes:{}",
+                        olapTable.getName(), totalPartitionNum, totalTabletsNum, totalScanRangeBytes);
+            }
+        }
+
+    }
+
     /**
      * We query meta to get request's data location
      * extra result info will pass to backend ScanNode

@before-Sunrise before-Sunrise changed the title [Enhancement] Add some logs to help online businesses quickly find the queries that FE cause oom [Enhancement] Add some logs to help online businesses quickly find the queries that cause FE oom Sep 29, 2024
Copy link

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[FE Incremental Coverage Report]

pass : 41 / 51 (80.39%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/StatementPlanner.java 1 3 33.33% [155, 156]
🔵 com/starrocks/http/rest/ExecuteSqlAction.java 1 2 50.00% [134]
🔵 com/starrocks/qe/ConnectProcessor.java 1 2 50.00% [311]
🔵 com/starrocks/qe/ConnectContext.java 5 7 71.43% [940, 941]
🔵 com/starrocks/statistic/StatisticsCollectJob.java 4 5 80.00% [153]
🔵 com/starrocks/statistic/StatisticExecutor.java 5 6 83.33% [434]
🔵 com/starrocks/planner/OlapScanNode.java 23 25 92.00% [763, 765]
🔵 com/starrocks/common/Config.java 1 1 100.00% []

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@satanson satanson enabled auto-merge (squash) September 29, 2024 05:58
@satanson satanson merged commit 5dd17dc into StarRocks:main Sep 29, 2024
58 of 59 checks passed
Copy link

@Mergifyio backport branch-3.3

Copy link

@Mergifyio backport branch-3.2

Copy link
Contributor

mergify bot commented Sep 29, 2024

backport branch-3.3

✅ Backports have been created

Copy link
Contributor

mergify bot commented Sep 29, 2024

backport branch-3.2

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 29, 2024
…e queries that cause FE oom (#51528)

Signed-off-by: before-Sunrise <[email protected]>
(cherry picked from commit 5dd17dc)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/common/Config.java
#	fe/fe-core/src/main/java/com/starrocks/planner/OlapScanNode.java
mergify bot pushed a commit that referenced this pull request Sep 29, 2024
…e queries that cause FE oom (#51528)

Signed-off-by: before-Sunrise <[email protected]>
(cherry picked from commit 5dd17dc)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/common/Config.java
#	fe/fe-core/src/main/java/com/starrocks/planner/OlapScanNode.java
before-Sunrise added a commit to before-Sunrise/starrocks that referenced this pull request Sep 29, 2024
satanson pushed a commit that referenced this pull request Sep 30, 2024
…e queries that cause FE oom (backport #51528) (#51534)

Signed-off-by: before-Sunrise <[email protected]>
mergify bot pushed a commit that referenced this pull request Sep 30, 2024
…e queries that cause FE oom (backport #51528) (#51534)

Signed-off-by: before-Sunrise <[email protected]>
(cherry picked from commit 7f42a15)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/common/Config.java
#	fe/fe-core/src/main/java/com/starrocks/planner/OlapScanNode.java
satanson pushed a commit that referenced this pull request Sep 30, 2024
…e queries that cause FE oom (backport #51528) (#51547)

Signed-off-by: before-Sunrise <[email protected]>
renzhimin7 pushed a commit to renzhimin7/starrocks that referenced this pull request Nov 7, 2024
…e queries that cause FE oom (StarRocks#51528)

Signed-off-by: before-Sunrise <[email protected]>
Signed-off-by: zhiminr.ren <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants