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

add rejectReadOnly option (to fix AWS Aurora failover) #604

Merged
merged 6 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ Zhenye Xie <xiezhenye at gmail.com>

Barracuda Networks, Inc.
Google Inc.
Keybase Inc.
Stripe Inc.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,29 @@ Default: 0

I/O read timeout. The value must be a decimal number with a unit suffix (*"ms"*, *"s"*, *"m"*, *"h"*), such as *"30s"*, *"0.5m"* or *"1m30s"*.

##### `rejectReadOnly`

```
Type: bool
Valid Values: true, false
Default: false
```


RejectreadOnly causes mysql driver to reject read-only connections. This is
Copy link
Member

Choose a reason for hiding this comment

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

s/mysql driver/the driver/

Please keep the explanation general, i.e. independent from AWS Aurora, which can however be mentioned as an example. For example explain that failover instances might be read-only and that this option purges them from the connection pool. Please also don't mention internals like "returning a driver.ErrBadConnection".

specifically for AWS Aurora: During a failover, there seems to be a race
condition on Aurora, where we get connected to the [old master before
failover], i.e. the [new read-only slave after failover].

Note that this should be a fairly rare case, as automatic failover normally
happens when master is down, and the race condition shouldn't happen unless it
comes back up online as soon as the failover is kicked off. But it's pretty
easy to reproduce using a manual failover. In case this happens, we should
reconnect to the Aurora cluster by returning a driver.ErrBadConnection.

tl;dr: Set this if you are using Aurora.


##### `strict`

```
Expand Down
18 changes: 18 additions & 0 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Config struct {
InterpolateParams bool // Interpolate placeholders into query string
MultiStatements bool // Allow multiple statements in one query
ParseTime bool // Parse time values to time.Time
RejectReadOnly bool // Reject read-only connections; see README.md
Copy link
Member

Choose a reason for hiding this comment

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

Remove the "; see README.md", all of these params are explained in the README.md

Strict bool // Return warnings as errors
}

Expand Down Expand Up @@ -195,6 +196,15 @@ func (cfg *Config) FormatDSN() string {
buf.WriteString(cfg.ReadTimeout.String())
}

if cfg.RejectReadOnly {
if hasParam {
buf.WriteString("&rejectReadOnly=true")
} else {
hasParam = true
buf.WriteString("?rejectReadOnly=true")
}
}

if cfg.Strict {
if hasParam {
buf.WriteString("&strict=true")
Expand Down Expand Up @@ -472,6 +482,14 @@ func parseDSNParams(cfg *Config, params string) (err error) {
return
}

// Reject read-only connections
case "rejectReadOnly":
var isBool bool
cfg.RejectReadOnly, isBool = readBool(value)
if !isBool {
return errors.New("invalid bool value: " + value)
}

// Strict mode
case "strict":
var isBool bool
Expand Down
15 changes: 15 additions & 0 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,21 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error {
// Error Number [16 bit uint]
errno := binary.LittleEndian.Uint16(data[1:3])

// 1792: ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION
if errno == 1792 && mc.cfg.RejectReadOnly {
// Oops; we are connected to a read-only connection, and won't be able
// to issue any write statements. Since RejectReadOnly is configured,
// we throw away this connection hoping this one would have write
// permission. This is specifically for an AWS Aurora behavior during
// failover. See README.md for more.
//
// We explicitly close the connection before returning
// driver.ErrBadConn to ensure that `database/sql` purges this
// connection and initiates a new one for next statement next time.
mc.Close()
return driver.ErrBadConn
}

pos := 3

// SQL State [optional: # + 5bytes string]
Expand Down