-
Notifications
You must be signed in to change notification settings - Fork 13.9k
[FLINK-26334][datastream] Fix getWindowStartWithOffset in TimeWindow.java #18982
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
Conversation
| return timestamp - (timestamp - offset + windowSize) % windowSize; | ||
| return timestamp | ||
| - (timestamp - offset) % windowSize | ||
| - (windowSize & (timestamp - offset) >> 63); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(windowSize & (timestamp - offset) >> 63 hard to understand, better to add more comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing our code, we made a mistake. What we want to change is org.apache.flink.streaming.api.windowing.windows.TimeWindow, not org.apache.flink.table.runtime.operators.window.TimeWindow
In the new commit, we find the class we want to change and modify the unit test.
Please ignore this change.
Thanks.
zjuwangg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your efforts, we should add test to verify the change. eg. in TimeWindowUtilTest or some other places
|
@zjuwangg |
realdengziqi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code has been changed and tests have been added
| return timestamp - (timestamp - offset + windowSize) % windowSize; | ||
| return timestamp | ||
| - (timestamp - offset) % windowSize | ||
| - (windowSize & (timestamp - offset) >> 63); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing our code, we made a mistake. What we want to change is org.apache.flink.streaming.api.windowing.windows.TimeWindow, not org.apache.flink.table.runtime.operators.window.TimeWindow
In the new commit, we find the class we want to change and modify the unit test.
Please ignore this change.
Thanks.
| Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-7, offset, 7), -7); | ||
| Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-6, offset, 7), -7); | ||
| Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-1, offset, 7), -7); | ||
| // --- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // --- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, we have removed this line of comments in the new commit a2fd873
zjuwangg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your efforts, the fix and and test looks good to me. But as your guys pointed, if i understand right, the method getWindowStartWithOffset in org.apache.flink.table.runtime.operators.window.TimeWindow would also have the same issue. I would suggest fix it here or open a new jira ticket.
| Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-3, offset, 7), -4); | ||
| Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-2, offset, 7), -4); | ||
| Assert.assertEquals(TimeWindow.getWindowStartWithOffset(-1, offset, 7), -4); | ||
| // --- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this line was also removed in the new commit a2fd873
zjuwangg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. @MartijnVisser will u help have a look and merge it if it's ok?
|
@zjuwangg Unfortunately this is not my expertise, so I can't review and merge it. |
|
@fapaul Hi. will u help have a look and merge ir if it's ok? thanks |
fapaul
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the great work. Overall the change looks good me but I left some inline comments.
Please also squash the commits together.
...ava/src/test/java/org/apache/flink/streaming/runtime/operators/windowing/TimeWindowTest.java
Outdated
Show resolved
Hide resolved
...treaming-java/src/main/java/org/apache/flink/streaming/api/windowing/windows/TimeWindow.java
Outdated
Show resolved
Hide resolved
| */ | ||
| public static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) { | ||
| return timestamp - (timestamp - offset + windowSize) % windowSize; | ||
| long remainder = (timestamp - offset) % windowSize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you haven't introduced this method but what do you think about removing this method in favor of the other TimeWindow#getWindowStartWithOffset. I am afraid that it is easy to miss that we have the same method twice in the code base.
I am also fine to do this as followup if you think it is too much for this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reviewing this code. I guess it involves reorganizing the code, which maybe too much for this pr. If necessary, we are willing to start a new jira and pr to deal with this problem. And looking forward the next pleasure cooperation with you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes no worries this can also be done outside of the PR.
74f16ec to
3559fb4
Compare
|
@realdengziqi it would be good to resolve the conflicts first and rebase with the latest master branch. In the Flink community we always use rebase over merge. Can you also create backport PRs for the release-1.14 and release-1.15 branch? |
… org.apache.flink.table.runtime.operators.window.TimeWindow and org.apache.flink.streaming.api.windowing.windows.TimeWindow . Added test cases in unit test TimeWindowTest. Co-authored-by: Lin WanNi <linwanniderz@foxmail.com> Co-authored-by: Guo YuanFang <1650213825@qq.com>
4b4e1a5 to
484a656
Compare
|
@fapaul Thanks for the guidance, we rebased it to the latest master branch of the community and force pushed it recently. We also created backport PRs for the release1.14 branch and the release1.15 branch. And, these PRs have successfully passed the azure build. |
Co-authored-by: Lin WanNi linwanniderz@foxmail.com
Co-authored-by: Guo YuanFang 1650213825@qq.com
What is the purpose of the change
https://issues.apache.org/jira/browse/FLINK-26334
The goal of this PR is to fix the bug that: the element couldn't be assigned to the correct window-start, if it's timestamp - offset + windowSize < 0.
This bug located at org.apache.flink.streaming.api.windowing.windows.TimeWindow .
This problem will be triggered by the negative timestamp, and is caused by the calculation method of remainder in the JAVA compiler.
Specifically, when we try to calculate the window-start of an incoming element, if timestamp - offset + windowSize < 0, based on the current calculation formula for window-start, the element will be right shifted to the next window, which has a start time larger than the timestamp of current element, seems violated the assignment principle for elements on window.
This problem can be fixed by modifying the calculation formula inside the getWindowStartWithOffset() method as below:
After this modify, for the element who has negative timestamp, we can still get the correct window-start. Like the below graph showing:

The getWindowStartWithOffset() method in other package
Considered the common usage of this method, we checked out the other getWindowStartWithOffset() method in the project, found one in the org.apache.flink.table.runtime.operators.window.grouping.WindowsGrouping
Turn out this method correctly handled the negative timestamp situation. Below is the source code.
Brief change log
Verifying this change
This change is already covered by existing tests, such as the tests in the flink-streaming-java [mvn clean verify]
Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation