[
https://issues.apache.org/jira/browse/FLINK-38349?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Huny updated FLINK-38349:
-------------------------
Description:
*Description*
In the method
{{{}org.apache.flink.runtime.operators.sort.SpillingThread#mergeChannelList{}}},
the following code may lead to incorrect results:
{{final double scale = Math.ceil(Math.log(channelIDs.size()) /
Math.log(this.maxFanIn)) - 1;}}
*Steps to Reproduce*
# Configure {{{}maxFanIn = 18{}}}.
# Run with {{channelIDs.size() = 7055}} (which is greater than or equal to
{{{}18^3{}}}).
# The calculation results in:
** {{numStart = 7055}}
** {{numEnd = 5832}} ({{{}18^3{}}})
** {{numToMerge = 0}}
** {{channelsToMergePerStep = 0}}
# Later processing logic performs division by zero and throws an exception.
*Expected Behavior*
The calculation of {{scale}} should never result in {{{}channelsToMergePerStep
= 0{}}}.
No divide-by-zero exception should occur.
*Actual Behavior*
When {{channelIDs.size()}} is greater than or equal to some power of
{{{}maxFanIn{}}}, the calculation becomes inaccurate due to floating-point
precision.
This results in {{{}numToMerge = 0{}}}, which propagates to
{{channelsToMergePerStep = 0}} and eventually causes a {*}divide-by-zero
exception{*}.
*Problem Pattern*
* If {{{}channelIDs.size() >= maxFanIn^3{}}}, problematic values of
{{maxFanIn}} include:
{{5, 6, 18, 25, 36, 47, 66, 75, 80, 86, 131, 143, 148 ...}}
* If {{{}channelIDs.size() >= maxFanIn^5{}}}, problematic values of
{{maxFanIn}} include:
{{7, 19, 20, 45, 49, 50, 58, 65, 67 ...}}
*Environment*
* Flink version: (all)
was:
org.apache.flink.runtime.operators.sort.SpillingThread#mergeChannelList 方法中,代码
final double scale = Math.ceil(Math.log(channelIDs.size()) /
Math.log(this.maxFanIn)) - 1; 进行计算得到的结果是不准确的。
例如:
如果 maxFanIn = 5 且 channelIDs =125 ,则
Math.log(channelIDs.size()) / Math.log(this.maxFanIn) = 3.0000000000000004,所以
scale = 3 。
numStart = 125, numEnd = 125(maxFanIn 的 sacle 次方),这会导致 numToMerge 为 0 。从而最终使变量
channelsToMergePerStep 为 0。 在之后的处理逻辑中出现 /by zero 异常。
总得来说出现这样的问题有很多可能,比如
如果channelIDs 大于等于 maxFanIn 的三次方的情况,则会出现问题的 maxFanIn 有
5, 6, 18, 25, 36, 47, 66, 75, 80, 86, 131, 143, 148 ....
如果channelIDs 大于等于 maxFanIn 的五次方的情况,会出现问题的 maxFanIn 有
7, 19, 20, 45, 49, 50, 58, 65, 67 ...
而我的环境报错,分析发现 maxFanIn 为 18 ,而 channelIDs 为7055, 它大于等于了 18 的三次方,触发了这个BUG。
Summary: Incorrect calculation of scale in
SpillingThread#mergeChannelList may cause divide-by-zero exception (was: Bugs
caused by floating-point operations)
> Incorrect calculation of scale in SpillingThread#mergeChannelList may cause
> divide-by-zero exception
> ----------------------------------------------------------------------------------------------------
>
> Key: FLINK-38349
> URL: https://issues.apache.org/jira/browse/FLINK-38349
> Project: Flink
> Issue Type: Bug
> Components: Runtime / Task
> Affects Versions: 1.13.0, 1.19.3, 1.20.2
> Environment: 所有环境
> Reporter: Huny
> Priority: Major
> Attachments: image-2025-09-12-09-05-54-870.png,
> image-2025-09-12-09-06-10-159.png
>
>
> *Description*
> In the method
> {{{}org.apache.flink.runtime.operators.sort.SpillingThread#mergeChannelList{}}},
> the following code may lead to incorrect results:
> {{final double scale = Math.ceil(Math.log(channelIDs.size()) /
> Math.log(this.maxFanIn)) - 1;}}
> *Steps to Reproduce*
> # Configure {{{}maxFanIn = 18{}}}.
> # Run with {{channelIDs.size() = 7055}} (which is greater than or equal to
> {{{}18^3{}}}).
> # The calculation results in:
> ** {{numStart = 7055}}
> ** {{numEnd = 5832}} ({{{}18^3{}}})
> ** {{numToMerge = 0}}
> ** {{channelsToMergePerStep = 0}}
> # Later processing logic performs division by zero and throws an exception.
> *Expected Behavior*
> The calculation of {{scale}} should never result in
> {{{}channelsToMergePerStep = 0{}}}.
> No divide-by-zero exception should occur.
> *Actual Behavior*
> When {{channelIDs.size()}} is greater than or equal to some power of
> {{{}maxFanIn{}}}, the calculation becomes inaccurate due to floating-point
> precision.
> This results in {{{}numToMerge = 0{}}}, which propagates to
> {{channelsToMergePerStep = 0}} and eventually causes a {*}divide-by-zero
> exception{*}.
> *Problem Pattern*
> * If {{{}channelIDs.size() >= maxFanIn^3{}}}, problematic values of
> {{maxFanIn}} include:
> {{5, 6, 18, 25, 36, 47, 66, 75, 80, 86, 131, 143, 148 ...}}
> * If {{{}channelIDs.size() >= maxFanIn^5{}}}, problematic values of
> {{maxFanIn}} include:
> {{7, 19, 20, 45, 49, 50, 58, 65, 67 ...}}
> *Environment*
> * Flink version: (all)
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)