This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Give vitess a chance to enforce connection timeouts if we've been waiting for a row for longer #801
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
afd72ba
Enforce the read timeout on row iterator reads
bebe841
Added test for row timeout
6f8dd72
Review feedback and some simplified logic for the row read select
6659d13
RowTimeout: Use a single NewTimer instead of After for better memory …
a37b443
Restore default timeout and readTimeout values
c11202d
Fixes from review
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import ( | |
sqle "github.com/src-d/go-mysql-server" | ||
"github.com/src-d/go-mysql-server/auth" | ||
"github.com/src-d/go-mysql-server/sql" | ||
errors "gopkg.in/src-d/go-errors.v1" | ||
"gopkg.in/src-d/go-errors.v1" | ||
|
||
"github.com/sirupsen/logrus" | ||
"vitess.io/vitess/go/mysql" | ||
|
@@ -21,25 +21,29 @@ import ( | |
|
||
var regKillCmd = regexp.MustCompile(`^kill (?:(query|connection) )?(\d+)$`) | ||
|
||
var errConnectionNotFound = errors.NewKind("Connection not found: %c") | ||
var errConnectionNotFound = errors.NewKind("connection not found: %c") | ||
// ErrRowTimeout will be returned if the wait for the row is longer than the connection timeout | ||
var ErrRowTimeout = errors.NewKind("row read wait bigger than connection timeout") | ||
|
||
// TODO parametrize | ||
const rowsBatch = 100 | ||
|
||
// Handler is a connection handler for a SQLe engine. | ||
type Handler struct { | ||
mu sync.Mutex | ||
e *sqle.Engine | ||
sm *SessionManager | ||
c map[uint32]*mysql.Conn | ||
mu sync.Mutex | ||
e *sqle.Engine | ||
sm *SessionManager | ||
c map[uint32]*mysql.Conn | ||
readTimeout time.Duration | ||
} | ||
|
||
// NewHandler creates a new Handler given a SQLe engine. | ||
func NewHandler(e *sqle.Engine, sm *SessionManager) *Handler { | ||
func NewHandler(e *sqle.Engine, sm *SessionManager, rt time.Duration) *Handler { | ||
return &Handler{ | ||
e: e, | ||
sm: sm, | ||
c: make(map[uint32]*mysql.Conn), | ||
e: e, | ||
sm: sm, | ||
c: make(map[uint32]*mysql.Conn), | ||
readTimeout: rt, | ||
} | ||
} | ||
|
||
|
@@ -103,6 +107,42 @@ func (h *Handler) ComQuery( | |
|
||
var r *sqltypes.Result | ||
var proccesedAtLeastOneBatch bool | ||
|
||
rowchan := make(chan sql.Row) | ||
errchan := make(chan error) | ||
quit := make(chan struct{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [optional] You could put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to make it explicit so the code reader can see where we are actively signaling the goroutine. |
||
|
||
// This goroutine will be select{}ed giving a chance to Vitess to call the | ||
// handler.CloseConnection callback and enforcing the timeout if configured | ||
go func() { | ||
for { | ||
select { | ||
case <-quit: | ||
return | ||
default: | ||
} | ||
row, err := rows.Next() | ||
if err != nil { | ||
errchan <- err | ||
return | ||
} | ||
rowchan <- row | ||
} | ||
}() | ||
|
||
// Default waitTime is one 1 minute if there is not timeout configured, in which case | ||
// it will loop to iterate again unless the socket died by the OS timeout or other problems. | ||
// If there is a timeout, it will be enforced to ensure that Vitess has a chance to | ||
// call Handler.CloseConnection() | ||
waitTime := 1 * time.Minute | ||
|
||
if h.readTimeout > 0 { | ||
waitTime = h.readTimeout | ||
} | ||
timer := time.NewTimer(waitTime) | ||
defer timer.Stop() | ||
|
||
rowLoop: | ||
for { | ||
if r == nil { | ||
r = &sqltypes.Result{Fields: schemaToFields(schema)} | ||
|
@@ -115,26 +155,32 @@ func (h *Handler) ComQuery( | |
|
||
r = nil | ||
proccesedAtLeastOneBatch = true | ||
|
||
continue | ||
} | ||
|
||
row, err := rows.Next() | ||
if err != nil { | ||
select { | ||
case err = <-errchan: | ||
if err == io.EOF { | ||
break | ||
break rowLoop | ||
} | ||
|
||
return err | ||
} | ||
case row := <-rowchan: | ||
outputRow, err := rowToSQL(schema, row) | ||
if err != nil { | ||
close(quit) | ||
return err | ||
juanjux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
outputRow, err := rowToSQL(schema, row) | ||
if err != nil { | ||
return err | ||
r.Rows = append(r.Rows, outputRow) | ||
r.RowsAffected++ | ||
case <-timer.C: | ||
if h.readTimeout != 0 { | ||
juanjux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Return so Vitess can call the CloseConnection callback | ||
close(quit) | ||
return ErrRowTimeout.New() | ||
} | ||
} | ||
|
||
r.Rows = append(r.Rows, outputRow) | ||
r.RowsAffected++ | ||
timer.Reset(waitTime) | ||
} | ||
|
||
if err := rows.Close(); err != nil { | ||
|
@@ -149,6 +195,7 @@ func (h *Handler) ComQuery( | |
return nil | ||
} | ||
|
||
close(quit) | ||
return callback(r) | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.