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

fix: fix issues with multi-statement requests failing to validate #3952

Merged

Conversation

big-andy-coates
Copy link
Contributor

Description

fixes: #3363

Issue was that the prepare call is throwing if the meta-store doesn't contain the sources in a statement.

Given a request contains a later statement that depends on an earlier statement, e.g.

CREATE STREAM FOO (...) WITH (...);
CREATE STREAM BAR AS SELECT *

The KSQL correctly validates the statements, because it updates the sandboxed copy of the meta-store as it goes.

However, when it comes to executing the statement it once against prepares the statement. Only this time its using the true meta-store and that is only updated by the command runner thread. This can mean that the first statement is correctly written to the command topic, but if the next statement is prepare'd before the command runner thread executes the statement and updates the meta-store, then the prepare call on the rest-request thead will fail with an error that FOO does not exist.

The server has the ability to wait for a previous command to be executed by the command runner thread before executing the next. However, this isn't done for distributed statements, i.e. those written to the command topic as its not meant to be required. It is done for statements such as SHOW STREAMS, which need to have any previous command executed as it may be creating a stream!

This is fixed in master already. The fix was to not have prepare validate that the source exists. This is the correct fix. However, this would be a big change for 5.4.x. Instead, this commit makes two changes:

  1. Move the check to wait for previous commands to before the prepare call.
  2. All statement types should wait for previous commands to finish. (Because we check before prepare we don't know the statement type).

This fixes the race condition at the cost of more synchronisation. However, this synchronisation will only impact multi-statement requests. The CLI generally issues single line requests, so no impact. C3 issues multi-line requests, i.e. the entire script in one go. So this will slow down responses to C3 slightly, but will stop intermittent fails in C3 if there are interdependent statements.

Pros:

  • interdependent multi-statement requests, as issued by C3 and run script work.
    Cons:
  • multi-statement requests will run slightly slower.

Testing done

usual + manual.

Reviewer checklist

  • Ensure docs are updated if necessary. (eg. if a user visible feature is being added or changed).
  • Ensure relevant issues are linked (description should include text like "Fixes #")

fixes: confluentinc#3363

Issue was that the `prepare` call is throwing if the meta-store doesn't contain the sources in a statement.

Given a request contains a later statement that depends on an earlier statement, e.g.

```sql
CREATE STREAM FOO (...) WITH (...);
CREATE STREAM BAR AS SELECT *
```

The KSQL correctly validates the statements, because it updates the sandboxed copy of the meta-store as it goes.

However, when it comes to executing the statement it once against `prepares` the statement. Only this time its using the true meta-store and that is only updated by the command runner thread.  This can mean that the first statement is correctly written to the command topic, but if the next statement is `prepare`'d before the command runner thread executes the statement and updates the meta-store, then the `prepare` call on the rest-request thead will fail with an error that `FOO` does not exist.

The server has the ability to wait for a previous command to be executed by the command runner thread before executing the next. However, this isn't done for _distributed_ statements, i.e. those written to the command topic as its not meant to be required.  It is done for statements such as `SHOW STREAMS`, which need to have any previous command executed as it may be creating a stream!

This is fixed in master already. The fix was to _not_ have `prepare` validate that the source exists.  This is the correct fix. However, this would be a big change for 5.4.x.  Instead, this commit makes two changes:

1. Move the check to wait for previous commands to _before_ the `prepare` call.
2. All statement types should wait for previous commands to finish. (Because we check _before_ `prepare` we don't _know_ the statement type).

This fixes the race condition at the cost of more synchronisation. However, this synchronisation will only impact multi-statement requests. The CLI generally issues single line requests, so no impact. C3 issues multi-line requests, i.e. the entire script in one go.  So this will slow down responses to C3 slightly, but will stop intermittent fails in C3 if there are interdependent statements.
@big-andy-coates big-andy-coates requested a review from a team as a code owner November 22, 2019 12:31
Copy link
Contributor

@vcrfxia vcrfxia left a comment

Choose a reason for hiding this comment

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

LGTM -- thanks @big-andy-coates !

@vcrfxia vcrfxia requested a review from a team November 22, 2019 16:09
Copy link
Contributor

@agavra agavra left a comment

Choose a reason for hiding this comment

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

LGTM - I'm wondering how bad the synchronization cost actually is. Is it perceptible to a human?

@big-andy-coates big-andy-coates changed the title fix: fix issues with multi-statement requests failing to validate fix: fix issues with multi-statement requests failing to validate (MINOR) Nov 22, 2019
@big-andy-coates big-andy-coates changed the title fix: fix issues with multi-statement requests failing to validate (MINOR) fix: fix issues with multi-statement requests failing to validate Nov 22, 2019
@agavra agavra requested a review from a team November 22, 2019 17:37
@big-andy-coates big-andy-coates merged commit 3e7169b into confluentinc:5.4.x Nov 23, 2019
@big-andy-coates big-andy-coates deleted the mutli-statement-request branch November 23, 2019 07:14
big-andy-coates added a commit that referenced this pull request Nov 23, 2019
Deliberately not merging the fixes for #3952 into master, as this issue is already resolved in master
@big-andy-coates
Copy link
Contributor Author

Merged into 5.3.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants