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

[FLINK-24704][table] Fix exception when the input record loses monotonicity on the sort key field of UpdatableTopNFunction #17605

Merged
merged 5 commits into from
Nov 5, 2021

Conversation

lincoln-lil
Copy link
Contributor

What is the purpose of the change

Fix the exception when the input record loses monotonicity on the sort key field of UpdatableTopNFunction

Verifying this change

This change is already covered by RankHarnessTest

Does this pull request potentially affect one of the following parts:

- Dependencies (does it add or upgrade a dependency): (no)
- The public API, i.e., is any changed class annotated with @Public(Evolving): (no)
- The serializers: (no )
- The runtime per-record code paths (performance sensitive): (no)
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
- The S3 file system connector: (no)

Documentation

  • Does this pull request introduce a new feature? (no)

…nicity on the sort key field of UpdatableTopNFunction
@flinkbot
Copy link
Collaborator

Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
to review your pull request. We will use this comment to track the progress of the review.

Automated Checks

Last check on commit 10ca9cf (Fri Oct 29 12:11:53 UTC 2021)

Warnings:

  • No documentation files were touched! Remember to keep the Flink docs up to date!
  • This pull request references an unassigned Jira ticket. According to the code contribution guide, tickets need to be assigned before starting with the implementation work.

Mention the bot in a comment to re-run the automated checks.

Review Progress

  • ❓ 1. The [description] looks good.
  • ❓ 2. There is [consensus] that the contribution should go into to Flink.
  • ❓ 3. Needs [attention] from.
  • ❓ 4. The change fits into the overall [architecture].
  • ❓ 5. Overall code [quality] is good.

Please see the Pull Request Review Guide for a full explanation of the review process.


The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required Bot commands
The @flinkbot bot supports the following commands:

  • @flinkbot approve description to approve one or more aspects (aspects: description, consensus, architecture and quality)
  • @flinkbot approve all to approve all aspects
  • @flinkbot approve-until architecture to approve everything until architecture
  • @flinkbot attention @username1 [@username2 ..] to require somebody's attention
  • @flinkbot disapprove architecture to remove an approval you gave earlier

@flinkbot
Copy link
Collaborator

flinkbot commented Oct 29, 2021

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

@lincoln-lil
Copy link
Contributor Author

lincoln-lil commented Oct 29, 2021

Some tests failed, I'll fix it.

@lincoln-lil
Copy link
Contributor Author

@flinkbot run azure

Copy link
Contributor

@JingsongLi JingsongLi left a comment

Choose a reason for hiding this comment

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

Thanks @lincoln-lil for your contribution, left some comments.

private final boolean lenient = true;

// data converter for logging only.
private final DataStructureConverter rowConverter;
Copy link
Contributor

Choose a reason for hiding this comment

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

This will break compatibility. We can just introduce two transient fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! transient is better.

testHarness.processElement(binaryRecord(INSERT, "f", 1: JInt, 70: JInt))

testHarness.processElement(binaryRecord(UPDATE_AFTER, "b", 1: JInt, 10: JInt))

Copy link
Contributor

Choose a reason for hiding this comment

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

Just left one line is OK

currentRow = rowKeyMap.get(rowKey).row;
if (oldRank <= currentRank) {
if (currentRank == oldRank) {
checkArgument(0 == sortKeyComparator.compare(curSortKey, oldSortKey));
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this true? It might be that the oldSortKey is unique and after the change it does not exist anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a wrong check here, and it's unnecessary after think it over, so I'll remove it.

@lincoln-lil
Copy link
Contributor Author

@flinkbot run azure

Comment on lines 375 to 385
if (oldRank <= currentRank) {
if (currentRank == oldRank) {
collectUpdateBefore(out, oldRow.row, oldRank);
} else {
collectUpdateBefore(out, prevRow, currentRank);
collectUpdateAfter(out, prevRow, currentRank - 1);
if (currentRank == newRank) {
collectUpdateAfter(out, newRow, currentRank);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What if newSortKey > oldSortKey but oldRank == currentRank? The update after message will be lost. Please also add a test about this after fixing it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems that a more proper solution to this is not to send any message if oldRank == currentRank.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! will add case to cover it.

Comment on lines 367 to 389
while (iterator.hasNext() && currentRank <= newRank) {
Map.Entry<RowData, Collection<RowData>> entry = iterator.next();
Collection<RowData> rowKeys = entry.getValue();
Iterator<RowData> rowKeyIter = rowKeys.iterator();
while (rowKeyIter.hasNext()) {
RowData rowKey = rowKeyIter.next();
currentRank += 1;
currentRow = rowKeyMap.get(rowKey).row;
if (oldRank <= currentRank) {
if (currentRank == oldRank) {
collectUpdateBefore(out, oldRow.row, oldRank);
} else {
collectUpdateBefore(out, prevRow, currentRank);
collectUpdateAfter(out, prevRow, currentRank - 1);
if (currentRank == newRank) {
collectUpdateAfter(out, newRow, currentRank);
}
}
}
prevRow = currentRow;
}
}
}
Copy link
Contributor

@tsreaper tsreaper Nov 4, 2021

Choose a reason for hiding this comment

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

This piece of algorithm seems awkward to me. Consider modifying it to:

while (iterator.hasNext() && currentRank < newRank) {
    // ...
    while (rowKeyIter.hasNext()) {
        // ...
        if (oldRank <= currentRank) {
            collectUpdateBefore(out, currentRow, currentRank + 1);
            collectUpdateAfter(out, currentRow, currentRank);
        }
    }
}
collectUpdateBefore(out, oldRow.row, oldRank);
collectUpdateAfter(out, newRow, newRank);

so that there is no prevRow thingy. It is misleading to see a prevRow and a currentRank sending within the same message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool! It's more simpler and readable. A small change is to emit the UB of old row before the following changes.

@@ -220,4 +221,74 @@ class RankHarnessTest(mode: StateBackendMode) extends HarnessTestBase(mode) {
assertor.assertOutputEqualsSorted("result mismatch", expectedOutput, result)
testHarness.close()
}

@Test
def testUpdateRankWithRowNumber(): Unit = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add more test scenarios.

  1. Calculate top 5 but there are more than 5 candidates.
  2. Sort key drops but ranking does not change.
  3. Sort key drops but does not drop to the last ranking.
  4. Calculate top 5, 7 candidates, previous rank 3 drops to rank 6 (but it is still "rank 5").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your review, I'll update it.

Copy link
Contributor

@tsreaper tsreaper left a comment

Choose a reason for hiding this comment

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

Looks good to me. Let's wait for the CI to pass.

Copy link
Contributor

@JingsongLi JingsongLi left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@JingsongLi JingsongLi merged commit 33aaabe into apache:master Nov 5, 2021
lincoln-lil added a commit to lincoln-lil/flink that referenced this pull request Nov 9, 2021
…s monotonicity on the sort key field of UpdatableTopNFunction

This closes apache#17605
JingsongLi pushed a commit that referenced this pull request Nov 10, 2021
…s monotonicity on the sort key field of UpdatableTopNFunction

This closes #17605
niklassemmler pushed a commit to niklassemmler/flink that referenced this pull request Feb 3, 2022
…s monotonicity on the sort key field of UpdatableTopNFunction

This closes apache#17605
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants