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

VStreamer: fix deadlock when there are a lot of vschema changes at the same time as binlog events #11325

Merged

Conversation

rohit-nayak-ps
Copy link
Contributor

@rohit-nayak-ps rohit-nayak-ps commented Sep 22, 2022

Description

This fixes the bug reported in #11169

Pre-conditions for bug:

  1. There is a vstreamer client that is subscribed to, but not reading events fast enough, from a Replica
  2. There are a lot of VSchema changes at this time
  3. A PRS happens at this time, switching this Replica to a Primary

Bug cause:

  1. In vstreamer.SetVSchema() the new vschema is written to a single message buffered channel for each active vstreamer. This is read by the vstreamer event processor, holding the vstreamer engine mutex.
  2. In the vstreamer event processor there are two parallel case statements: one reads from the vschema channel and other reads the binlog events. The binlog events are streamed to the receiver. If the receiver is not reading them then the send will block. This means that the other case which polls the vschema change channel will not get called. This results in the vstreamer engine lock being held until all events are streamed
  3. The PRS results in the vttablet state manager trying to shut down the vstreamer engine for which it needs the vstreamer engine mutex. This can cause the PRS to get blocked.

This PR drains the vschema channel so that only the latest vschema update is sent. So it no longer holds the vstreamer lock for an extended time allowing the PRS to proceed.

This PR also incorporates the changes made in #11268 which also solves the same issue in a different way: the vschema update no longer holds a lock (since it uses atomic.int32 to signal whether the vstreamer engine is open, rather than the mutex). #11268 also adds checks for cancelled contexts.

Related Issue(s)

fixes #11169

Checklist

  • "Backport me!" label has been added if this change should be backported
  • Tests were added or are not required
  • Documentation was added or is not required

@vitess-bot
Copy link
Contributor

vitess-bot bot commented Sep 22, 2022

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • If this is a change that users need to know about, please apply the release notes (needs details) label so that merging is blocked unless the summary release notes document is included.
  • If a new flag is being introduced, review whether it is really needed. The flag names should be clear and intuitive (as far as possible), and the flag's help should be descriptive. Additionally, flag names should use dashes (-) as word separators rather than underscores (_).
  • If a workflow is added or modified, each items in Jobs should be named in order to mark it as required. If the workflow should be required, the GitHub Admin should be notified.

Bug fixes

  • There should be at least one unit or end-to-end test.
  • The Pull Request description should either include a link to an issue that describes the bug OR an actual description of the bug and how to reproduce, along with a description of the fix.

Non-trivial changes

  • There should be some code comments as to why things are implemented the way they are.

New/Existing features

  • Should be documented, either by modifying the existing documentation or creating new documentation.
  • New features should have a link to a feature request issue or an RFC that documents the use cases, corner cases and test cases.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • vtctl command output order should be stable and awk-able.

The VStreamer engine is somewhat unusual in two ways:
  1. It is open and running on replica tablets rather than only
     running on primary tablets.
  2. It has no controllers so the main engine mutex is widely shared.

Because of this, when a tablet has open vstreams (direct binary log
streams) performing work and a state transition starts, it can
deadlock with the tabletmanager's state lock when checking if
the engine is open or not.

Signed-off-by: Matt Lord <[email protected]>
@rohit-nayak-ps rohit-nayak-ps marked this pull request as ready for review September 27, 2022 10:48
@rohit-nayak-ps rohit-nayak-ps changed the title VStreamer: fix deadlock when there are a lot of vschema changes as well as db events VStreamer: fix deadlock when there are a lot of vschema changes at the same time as binlog events Sep 27, 2022
Copy link
Contributor

@mattlord mattlord 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! I had minor comments/suggestions/questions, so will unblock you and let you make the final call on those things.

go/test/endtoend/vreplication/vschema_load_test.go Outdated Show resolved Hide resolved
go/test/endtoend/vreplication/vschema_load_test.go Outdated Show resolved Hide resolved
go/test/endtoend/vreplication/vschema_load_test.go Outdated Show resolved Hide resolved

flags := &vtgatepb.VStreamFlags{}

ctx2, cancel := context.WithTimeout(ctx, 2*time.Minute)
Copy link
Contributor

Choose a reason for hiding this comment

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

This timeout is equal to the extendedTimeout var, right? Might as well use that IMO so that we can easily change timeouts in various places.

go/test/endtoend/vreplication/vschema_load_test.go Outdated Show resolved Hide resolved
go/test/endtoend/vreplication/vschema_load_test.go Outdated Show resolved Hide resolved
"Args": ["vitess.io/vitess/go/test/endtoend/vreplication", "-run", "TestVSchemaChangesUnderLoad"],
"Command": [],
"Manual": false,
"Shard": "vreplication_cellalias",
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably rename the shard, e.g. vreplication_cellalias_vschema or something.

Signed-off-by: Rohit Nayak <[email protected]>
DeathBorn pushed a commit to vinted/vitess that referenced this pull request Jan 26, 2023
VStreamer: fix deadlock when there are a lot of vschema changes at the same time as binlog events (vitessio#11325)

* Don't block on vschema channel in case of heavy vschema changes and vstream load

Signed-off-by: Rohit Nayak <[email protected]>

* Prevent VStreamer engine deadlocks during state transitions

The VStreamer engine is somewhat unusual in two ways:
  1. It is open and running on replica tablets rather than only
     running on primary tablets.
  2. It has no controllers so the main engine mutex is widely shared.

Because of this, when a tablet has open vstreams (direct binary log
streams) performing work and a state transition starts, it can
deadlock with the tabletmanager's state lock when checking if
the engine is open or not.

Signed-off-by: Matt Lord <[email protected]>

* Address review comments

Signed-off-by: Rohit Nayak <[email protected]>

Signed-off-by: Rohit Nayak <[email protected]>
Signed-off-by: Matt Lord <[email protected]>
Co-authored-by: Matt Lord <[email protected]>
Signed-off-by: Vilius Okockis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: a vstream client can block a tablet ChangeType from replica => primary
2 participants