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

server: return error on ping when in graceful shutdown #58008

Merged
merged 4 commits into from
Dec 14, 2024

Conversation

dveeden
Copy link
Contributor

@dveeden dveeden commented Dec 5, 2024

Issue Number: ref #58007

Problem Summary:

What changed and how does it work?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

TiDB now returns errors for COM_PING commands when in shutdown

From ProxySQL:

mysql-5.5.30> select * from monitor.mysql_server_ping_log order by time_start_us desc limit 15;
+-----------+------+------------------+----------------------+--------------------------------------------------+
| hostname  | port | time_start_us    | ping_success_time_us | ping_error                                       |
+-----------+------+------------------+----------------------+--------------------------------------------------+
| 127.0.0.1 | 4000 | 1733385305164128 | 0                    | ping refused, because unhealthy (shutting down?) |
| 127.0.0.1 | 4000 | 1733385295163107 | 452                  | NULL                                             |
| 127.0.0.1 | 4000 | 1733385285162817 | 66                   | NULL                                             |
| 127.0.0.1 | 4000 | 1733385275162347 | 0                    | Can't connect to server on '127.0.0.1' (115)     |

@dveeden dveeden requested a review from bb7133 December 5, 2024 07:55
@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 5, 2024
Copy link

tiprow bot commented Dec 5, 2024

Hi @dveeden. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@dveeden
Copy link
Contributor Author

dveeden commented Dec 5, 2024

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Dec 5, 2024
Copy link

codecov bot commented Dec 5, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 73.7072%. Comparing base (30069c5) to head (fc0b12c).
Report is 50 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #58008        +/-   ##
================================================
+ Coverage   73.1633%   73.7072%   +0.5438%     
================================================
  Files          1674       1678         +4     
  Lines        461261     471346     +10085     
================================================
+ Hits         337474     347416      +9942     
+ Misses       103041     102976        -65     
- Partials      20746      20954       +208     
Flag Coverage Δ
integration 44.1301% <75.0000%> (?)
unit 72.3521% <75.0000%> (+0.0624%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.6910% <ø> (ø)
parser ∅ <ø> (∅)
br 45.3576% <ø> (-0.6498%) ⬇️

@ti-chi-bot ti-chi-bot bot added approved needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 5, 2024
Copy link
Contributor

@mjonss mjonss left a comment

Choose a reason for hiding this comment

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

I think this is good solution for triggering clients to connect to another TiDB node.

Please update the failing tests, like TestDispatch :)

Maybe a dedicated error as well?

@dveeden dveeden requested a review from djshow832 December 7, 2024 08:23
@dveeden
Copy link
Contributor Author

dveeden commented Dec 7, 2024

Small demo on how this works with Go:

Setup a server with this PR and graceful wait set to 30s.

./bin/tidb-server -config <(echo -en "graceful-wait-before-shutdown=30")

Run the demo

go run main.go > /tmp/demo.log

Now press ^C to stop the server

Now let's look at the results:

$ grep -E '^(p|w) 5' /tmp/demo.log 
p 5: OK 2024-12-07 09:25:25.655509027 +0100 CET m=+0.000590851
w 5: OK: 2024-12-07 09:25:25.663644
p 5: OK 2024-12-07 09:25:28.664281291 +0100 CET m=+3.009363116
w 5: OK: 2024-12-07 09:25:28.665180
p 5: OK 2024-12-07 09:25:31.66737386 +0100 CET m=+6.012455713
w 5: OK: 2024-12-07 09:25:31.668165
p 5: ERROR Error 1105 (HY000): ping refused, because unhealthy (shutting down?)
w 5: OK: 2024-12-07 09:25:34.673028
w 5: OK: 2024-12-07 09:25:37.675960
w 5: OK: 2024-12-07 09:25:40.678945
w 5: OK: 2024-12-07 09:25:43.681615
w 5: OK: 2024-12-07 09:25:46.684629
w 5: OK: 2024-12-07 09:25:49.686894
w 5: OK: 2024-12-07 09:25:52.689721
w 5: OK: 2024-12-07 09:25:55.692857
w 5: OK: 2024-12-07 09:25:58.696796
w 5: OK: 2024-12-07 09:26:01.699940
w 5: OK: 2024-12-07 09:26:04.702768
w 5: OK: 2024-12-07 09:26:07.704676
w 5: OK: 2024-12-07 09:26:10.709099
w 5: OK: 2024-12-07 09:26:13.710915
w 5: ERROR invalid connection

As you can see the lines starting with p 5 that indicate the ping goroutine nr 5 show the ping refused, because unhealthy (shutting down?) error and then the goroutine stops.

The lines with w 5 for worker 5 show that ongoing transactions stay alive.

This shows that anything that does a COM_PING like a pool healthcheck or a proxy healthcheck will notice that the server shouldn't be used anymore, but ongoing work is allowed to finish until the timeout is hit.

The demo code:

package main

import (
	"context"
	"database/sql"
	"fmt"
	"sync"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

var wg sync.WaitGroup

func dbpinger(db *sql.DB, nr int) error {
	defer wg.Done()
	for {
		fmt.Printf("p %d: OK %s\n", nr, time.Now())
		err := db.Ping()
		if err != nil {
			fmt.Printf("p %d: ERROR %v\n", nr, err)
			return err
		}
		time.Sleep(time.Second * 3)
	}
}

func dbworker(db *sql.DB, nr int) error {
	defer wg.Done()
	var ts []uint8
	ctx := context.Background()
	tx, err := db.BeginTx(ctx, nil)
	if err != nil {
		fmt.Printf("w %d: ERROR %v\n", nr, err)
		return err
	}
	defer tx.Rollback()

	for {
		err := tx.QueryRowContext(ctx, "SELECT NOW(6)").Scan(&ts)
		if err != nil {
			fmt.Printf("w %d: ERROR %v\n", nr, err)
			return err
		}
		fmt.Printf("w %d: OK: %s\n", nr, ts)
		time.Sleep(time.Second * 3)
	}
}

func main() {
	db, err := sql.Open("mysql", "root@tcp(127.0.0.1:4000)/test")
	if err != nil {
		panic(err)
	}

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go dbpinger(db, i)
	}

	for i := 0; i < 10; i++ {
		wg.Add(1)
		go dbworker(db, i)
	}

	wg.Wait()
}

@ti-chi-bot ti-chi-bot bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 7, 2024
@dveeden dveeden requested a review from mjonss December 7, 2024 08:38
@dveeden
Copy link
Contributor Author

dveeden commented Dec 7, 2024

/retest

@djshow832
Copy link
Contributor

djshow832 commented Dec 14, 2024

I'm afraid some clients may rely on COM_PING to decide whether the connection is created successfully, which will cause connection failure. However, I don't know such client drivers or proxies, and I can't enumerate all the drivers and proxies, so I choose to approve this PR.

Copy link

ti-chi-bot bot commented Dec 14, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: djshow832, lance6716

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [djshow832,lance6716]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Dec 14, 2024
Copy link

ti-chi-bot bot commented Dec 14, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-12-05 09:15:38.255312732 +0000 UTC m=+1319125.874967248: ☑️ agreed by lance6716.
  • 2024-12-14 02:07:03.15708184 +0000 UTC m=+663413.245884383: ☑️ agreed by djshow832.

@ti-chi-bot ti-chi-bot bot merged commit 8c7f784 into pingcap:master Dec 14, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm ok-to-test Indicates a PR is ready to be tested. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants