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

Flakes: Address TestServerStats flakiness #16991

Merged
merged 3 commits into from
Oct 18, 2024

Conversation

mattlord
Copy link
Contributor

@mattlord mattlord commented Oct 17, 2024

Description

We have seen the TestServerStats unit test fail pretty regularly, in particular in the unit_race workflow. Example failure:

--- FAIL: TestServerStats (0.02s)
    server_test.go:265: listening on address '127.0.0.1' port 41297
    server_test.go:1363: Running mysql command: /usr/bin/mysql [-v -v -v -e error -h 127.0.0.1 -P 41297 -u user1 -ppassword1]
    server_test.go:670: 
        	Error Trace:	/opt/actions-runner/_work/vitess-private/vitess-private/go/mysql/server_test.go:670
        	Error:      	Not equal: 
        	            	expected: int(0)
        	            	actual  : int64(1)
        	Test:       	TestServerStats
        	Messages:   	connCount

I could not repeat the failure locally (despite trying many different methods), but in walking through the code it was clear that we had a potential race here. The race is this:

  • The test starts a mysql server component (to handle the mysql protocol and incoming client connections) and uses mysql cli invocations to connect and execute queries against this internal server
  • The server component accepts this incoming connection in Accept
    • And it starts a goroutine to handle this connection, after it's been accepted, in the handle function:

      vitess/go/mysql/server.go

      Lines 327 to 359 in dc692fa

      // Accept runs an accept loop until the listener is closed.
      func (l *Listener) Accept() {
      ctx := context.Background()
      for {
      conn, err := l.listener.Accept()
      if err != nil {
      // Close() was probably called.
      connRefuse.Add(1)
      return
      }
      acceptTime := time.Now()
      connectionID := l.connectionID
      l.connectionID++
      connCount.Add(1)
      connAccept.Add(1)
      go func() {
      if l.PreHandleFunc != nil {
      conn, err = l.PreHandleFunc(ctx, conn, connectionID)
      if err != nil {
      log.Errorf("mysql_server pre hook: %s", err)
      return
      }
      }
      l.handle(conn, connectionID, acceptTime)
      }()
      }
      }
  • The test immediately checked some stats that are changed in that handle function running in the goroutine, most notably connCount which is decremented in a defer:

    vitess/go/mysql/server.go

    Lines 390 to 391 in dc692fa

    // Adjust the count of open connections
    defer connCount.Add(-1)

So in this PR we wait for the expected value to eliminate this race.

Related Issue(s)

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Copy link
Contributor

vitess-bot bot commented Oct 17, 2024

Review Checklist

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

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Oct 17, 2024
@github-actions github-actions bot added this to the v22.0.0 milestone Oct 17, 2024
@mattlord mattlord removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Oct 17, 2024
@mattlord mattlord requested review from frouioui and dbussink October 17, 2024 21:13
Signed-off-by: Matt Lord <[email protected]>
Signed-off-by: Matt Lord <[email protected]>
Copy link

codecov bot commented Oct 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 67.02%. Comparing base (da49d08) to head (364af21).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #16991      +/-   ##
==========================================
- Coverage   69.35%   67.02%   -2.33%     
==========================================
  Files        1571     1571              
  Lines      204183   251677   +47494     
==========================================
+ Hits       141601   168686   +27085     
- Misses      62582    82991   +20409     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@GuptaManan100 GuptaManan100 left a comment

Choose a reason for hiding this comment

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

TIL assert.Eventually exists! I was up until now always writing a for loop with a Ticker 😿!

@mattlord mattlord merged commit 3360865 into vitessio:main Oct 18, 2024
98 checks passed
@mattlord mattlord deleted the flakes_server_stats branch October 18, 2024 14:13
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.

4 participants