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-19694][table] Support Upsert ChangelogMode for ScanTableSource #13721

Merged
merged 3 commits into from Oct 27, 2020

Conversation

wuchong
Copy link
Member

@wuchong wuchong commented Oct 21, 2020

What is the purpose of the change

Support [UPDATE_AFTER, DELETE] ChangelogMode which indicates the source will emit only UPDATE_AFTER and DELETE messages during runtime (e.g. an upsert source). The planner will add the a materialization operator when the ChangelogMode of the source is [UPDATE_AFTER, DELETE].

The materialization operator will materialize the upsert stream and generate changelog stream with full change messages. In the physical operator, we will use state to know whether the key is the first time to be seen. The operator will produce INSERT rows, or additionally generate UPDATE_BEFORE rows for the previous image, or produce DELETE rows with all columns filled with values.

Brief change log

  • Support in planner: Generate a StreamExecUpsertMaterialize when the scan is a upsert source.
  • Update metadata handlers: Update ColumnUniqueness, ModifiedMonotonicity, UniqueKeys metadatas and fix some minor bugs in FlinkRelMdModifiedMonotonicity.
  • Support in runtime: Reuse the implementation of deduplicate function, add a new process path for changelog input stream.

Verifying this change

  • Added many tests for plan and runtime to cover different operations on upsert source.

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

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

Documentation

  • Does this pull request introduce a new feature? (yes / no)
  • If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)

@wuchong
Copy link
Member Author

wuchong commented Oct 21, 2020

Hi @leonardBang , could you help to review this?
Appreciate if @godfreyhe can help to review the plan and metadata part.

@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 a62fac3 (Wed Oct 21 10:28:42 UTC 2020)

Warnings:

  • No documentation files were touched! Remember to keep the Flink docs up to date!

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 21, 2020

CI report:

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

@wuchong
Copy link
Member Author

wuchong commented Oct 23, 2020

I have rebased the branch to resolve conflicts. Appreciate if you can have a look @leonardBang .

Copy link
Contributor

@leonardBang leonardBang left a comment

Choose a reason for hiding this comment

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

Thanks @wuchong for the contribution, I left some comments

import scala.collection.JavaConversions._

/**
* Stream physical RelNode which materializes an upsert stream where where each data record
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Stream physical RelNode which materializes an upsert stream where where each data record
* Stream physical RelNode which materializes an upsert stream where each record

false, // inputInsertOnly
rowSerializer,
// disable state ttl, the upsert materialize should keep all state to have data integrity
// we can enable state ttl if this is really needed in some cases
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this conflicts with global TTL setting? I understand the motivation, but it may confuse user.

|)
""".stripMargin)
thrown.expect(classOf[UnsupportedOperationException])
thrown.expectMessage(
Copy link
Contributor

Choose a reason for hiding this comment

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

The test failed, please update the exception message.

@@ -307,7 +307,7 @@ Join(joinType=[LeftSemiJoin], where=[$f0], select=[a, b, c], leftInputSpec=[NoUn
: +- LegacyTableSourceScan(table=[[default_catalog, default_database, l, source: [TestTableSource(a, b, c)]]], fields=[a, b, c])
+- Exchange(distribution=[single])
+- Calc(select=[IS NOT NULL(m) AS $f0])
+- GroupAggregate(select=[MIN(i) AS m])
+- GroupAggregate(select=[MIN_RETRACT(i) AS m])
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the function change for existed test?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a bug in previous FlinkRelMdModifiedMonotonicity, the need retraction inference was not correct.

Copy link
Contributor

@godfreyhe godfreyhe left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @wuchong , I left a few comments

if (!schema.getPrimaryKey().isPresent()) {
throw new TableException(
String.format(
"Table '%s' produces a changelog stream contains UPDATE_AFTER no UPDATE_BEFORE, " +
Copy link
Contributor

Choose a reason for hiding this comment

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

which contains only UPDATE_AFTER, no UPDATE_BEFORE. ?

.replace(FlinkConventions.STREAM_PHYSICAL)
val newInput: RelNode = RelOptRule.convert(newScan, requiredTraitSet)

new StreamExecUpsertMaterialize(
Copy link
Contributor

Choose a reason for hiding this comment

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

If the primary key fields are not required, we should add a Calc here to reduce output fields.

Copy link
Member Author

Choose a reason for hiding this comment

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

Why we have to add a Calc here? The transformed tree has the same output row type with the original Scan node. If the primary key fields are never used in the following nodes, there should already be a Calc after the Scan node. So I think we shouldn't add a Calc.

: +- Calc(select=[amount, currency, PROCTIME() AS proctime], changelogMode=[I])
: +- TableSourceScan(table=[[default_catalog, default_database, orders]], fields=[amount, currency], changelogMode=[I])
+- Exchange(distribution=[hash[currency]], changelogMode=[I,UB,UA,D])
+- UpsertMaterialize(key=[currency], changelogMode=[I,UB,UA,D])
Copy link
Contributor

Choose a reason for hiding this comment

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

does UpsertMaterialize produce UB message ?

Copy link
Member Author

Choose a reason for hiding this comment

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

It depends on whether the following nodes requires UB messages.
In this case, the temporal join currently always root requires UB, therefore UpsertMaterialize has to emit `UB.

mq: RelMetadataQuery,
columns: ImmutableBitSet,
ignoreNulls: Boolean): JBoolean = {
columns != null && util.Arrays.equals(columns.toArray, rel.uniqueKeys)
Copy link
Contributor

Choose a reason for hiding this comment

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

is the values of rel.uniqueKeys in order ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point!

@wuchong
Copy link
Member Author

wuchong commented Oct 26, 2020

After discussing with @leonardBang , I renamed UpsertMaterialize into ChangelogNormalize. Because this can be more general purpose and can be used for cdc source to drop duplications in the future.

@wuchong
Copy link
Member Author

wuchong commented Oct 27, 2020

Hi @godfreyhe , I have added the plan test as we discussed offline, and I did find a bug. Appreciate if you can have another look .

Copy link
Contributor

@leonardBang leonardBang left a comment

Choose a reason for hiding this comment

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

Thanks @wuchong for the update, LGTM +1

@wuchong
Copy link
Member Author

wuchong commented Oct 27, 2020

Build is passed in my own Azure build: https://dev.azure.com/imjark/Flink/_build/results?buildId=308&view=results

Copy link
Contributor

@godfreyhe godfreyhe left a comment

Choose a reason for hiding this comment

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

LGTM overall, I left a few minor comments

}

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

Choose a reason for hiding this comment

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

rename to testGetUniqueKeysOnStreamExecChangelogNormalize

@@ -691,6 +691,18 @@ class FlinkRelMdHandlerTestBase {
(calcOfFirstRow, calcOfLastRow)
}

protected lazy val streamUpsertMaterialize = {
Copy link
Contributor

Choose a reason for hiding this comment

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

rename to streamChangelogNormalize

}

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

Choose a reason for hiding this comment

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

rename to testGetRelMonotonicityOnChangelogNormalize

@@ -275,6 +275,15 @@ class FlinkRelMdColumnUniquenessTest extends FlinkRelMdHandlerTestBase {
assertFalse(mq.areColumnsUnique(streamDeduplicateLastRow, ImmutableBitSet.of(0, 1, 2)))
}

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

Choose a reason for hiding this comment

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

rename to testAreColumnsUniqueCountOnStreamExecChangelogNormalize

@@ -1140,48 +1140,4 @@ class JoinITCase(state: StateBackendMode) extends StreamingWithStateTestBase(sta
val expected = Seq("Hi,Hallo", "Hello,Hallo Welt", "Hello world,Hallo Welt")
assertEquals(expected.sorted, sink.getRetractResults.sorted)
}

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

Choose a reason for hiding this comment

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

why this test is removed ?

Copy link
Member Author

Choose a reason for hiding this comment

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

This has been moved to ChangelogSourceITCase#testRegularJoin to reuse the tests.

@wuchong
Copy link
Member Author

wuchong commented Oct 27, 2020

Thanks for the reviewing @godfreyhe . I have rebased and squashed the commits and hope I have addressed all the comments.

Will merge this pull request once build passed.

@wuchong wuchong merged commit 436a4c2 into apache:master Oct 27, 2020
wuchong added a commit that referenced this pull request Oct 27, 2020
wuchong added a commit that referenced this pull request Oct 27, 2020
…w introduced StreamExecUpsertMaterialize node

This closes #13721
@wuchong wuchong deleted the upsert-mode branch October 27, 2020 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants