-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 RemoveTablet
during TabletExternallyReparented
causing connection issues
#16371
base: main
Are you sure you want to change the base?
Conversation
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
3d37750
to
9bbed96
Compare
…o tablets are marked as primary. Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
9bbed96
to
3cfaa01
Compare
This ensures that we can never end up in a situation where there is more than one healthy primary tablet for a given shard. Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #16371 +/- ##
==========================================
+ Coverage 68.69% 69.12% +0.42%
==========================================
Files 1547 1548 +1
Lines 198297 204912 +6615
==========================================
+ Hits 136228 141644 +5416
- Misses 62069 63268 +1199 ☔ View full report in Codecov by Sentry. |
Added the code that fixes this issue, and updated the PR body description accordingly. |
Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
|
||
<-resultChan | ||
<-resultChan | ||
|
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.
Don't you need to receive the results from all three tablets here?
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've not dug into why, but adding a third channel read causes the test to be stuck here. 🤷
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.
because Subscribe
returns a channel with a buffer of size 2 😅. Clearly, we were expecting that subscribers will process the results in a timely manner.
We could increase the channel buffer to 10, or the test can interleave receiving from the channel with the calls to AddTablet
like so
hc.AddTablet(firstTablet)
<-resultChan
hc.AddTablet(secondTablet)
<-resultChan
hc.AddTablet(thirdTablet)
<-resultChan
<-resultChan | ||
<-resultChan | ||
|
||
hc.RemoveTablet(thirdTablet) |
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.
Why does removing an unrelated tablet trigger the bug?
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.
Oh I see this
When RemoveTablet was called, the list of healthy tablets was recomputed, but the logic did not account for the fact that there can only be a single entry in the list of healthy primary tablets.
|
||
hc.RemoveTablet(thirdTablet) | ||
|
||
// tablet 1 is the primary now |
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.
This comment is wrong, it should be
// tablet 1 is the primary now | |
// `secondTablet` should be the primary now |
hc.recomputeHealthy(key) | ||
if tabletType == topodata.TabletType_PRIMARY { | ||
// If tablet type is primary, we should only have one tablet in the healthy list. | ||
hc.recomputeHealthyPrimary(key) |
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.
This is an interesting bug. We have safeguards in updateHealth
which make sure that there is exactly one element in healthy
for the PRIMARY tablet type, but the recomputation here had no such checks.
While the fix seems correct, what do you think about pushing it into recomputeHealthy
instead of creating a new function call here?
Right now this is the only call site where recomputeHealthy
is called for the PRIMARY tablet type, but it seems more compact and future proof to change recomputeHealthy
to maintain the invariant.
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.
While the fix seems correct, what do you think about pushing it into
recomputeHealthy
instead of creating a new function call here?
I feel like having two separate methods helps highlighting the difference between the recomputation logic for primaries vs any other replica type more clearly.
I was thinking that maybe having a recomputeHealthy
method that then calls out to either recomputeHealthyReplicas
and recomputeHealthyPrimaries
would probably be the best for clarity (and the go compiler should just inline all of these calls anyway, so not a performance issue either).
But then I noticed that pushing down the tablet type check into recomputeHealth
would require passing down the key
and tabletType
as arguments, which seems redundant, because key
contains the tablet type already, just as a string. Not passing down key
would require passing down other arguments to be able to build the key, and then things just start to become more messy.
As I mentioned in Slack, I'm very interested giving the healthcheck code a polishing pass separately from this fix, so I'd suggest we punt on this for now. 🙇♂️ What do you think?
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 can see that instead of passing the "key", you would now have to pass the target. When this was written, it just seemed convenient to use the key because it has already been computed. So we have
hc.recomputeHealthy(targetKey)
However, later on, we are doing a key computation for the second call
oldTargetKey := KeyFromTarget(prevTarget)
hc.recomputeHealthy(oldTargetKey)
It does not seem too invasive to change recomputeHealthy
to be
func (hc *HealthCheckImpl) recomputeHealthy(target *query.Target) {
key := KeyFromTarget(target)
if target.TabletType == ..._PRIMARY {
...
}
I do also see issues with the function name recomputeHealthyReplicas
. replica
is an overloaded term, and it could mean replicas in the sense of replicating from the primary, or it could mean the specific tablet type (as opposed to RDONLY and other tablet types). Refactoring it this way avoids that naming issue as well.
…municate the intent. Signed-off-by: Arthur Schreiber <arthurschreiber@github.com>
healthy, ok := hc.healthy[key] | ||
if ok && len(healthy) > 0 { | ||
hc.recomputeHealthy(key) | ||
if tabletType == topodata.TabletType_PRIMARY { |
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.
Thinking about this some more..
- we are deleting a tablet. if that tablet's type is not PRIMARY, we should not even "recompute the healthy primary"
- even if that tablet's type is PRIMARY, why should we iterate over the healthData for PRIMARY? we know that at any point there should only be one "healthy" primary. i.e
healthy[PRIMARY]
should have a size of either 0 or 1. If the tablet that we are deleting is THE current healthy primary, we should just sethealthy[PRIMARY]
to an empty slice. If it is not, we should do nothing.
It seems to me that the problem with the original implementation of this defer func was that it was calling recomputeHealthy
for PRIMARY type (which was never done before this defer func was added) instead of handling PRIMARY separately. This caused it to violate the assumption that there is at most one healthy
PRIMARY at any point in time.
for _, s := range hc.healthData[key] { | ||
if s.PrimaryTermStartTime >= highestPrimaryTermStartTime { | ||
highestPrimaryTermStartTime = s.PrimaryTermStartTime | ||
newestPrimary = s | ||
} | ||
} |
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.
Doing this may have the effect of "promoting" an old primary?
Description
This pull request fixes an issue in the health checking logic where an external primary failover in a keyspace/shard can cause two primaries to be marked as healthy. This can occur when a tablet is removed during an external reparenting operation.
This bug can lead to queries being sent to the old primary, or in case the demoted primary vttablet process is restarted, will lead to
vttablet: Connection closed
errors (which are mostly invisible because queries will be retried automatically on the "actual" primary).When
RemoveTablet
was called, the list of healthy tablets was recomputed, but the logic did not account for the fact that there can only be a single entry in the list of healthy primary tablets. This PR adds a specialized function to recompute the health of primary tablets, ensuring that we can never end up in a situation where there is more than one healthy primary tablet for a given shard.This new function recalculates the health of primary tablets by iterating over the health data for the given key and selecting the tablet with the highest
PrimaryTermStartTime
value.Related Issue(s)
Fixes: #16373
Checklist
Deployment Notes