-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add store support for ProxySQL (#129)
* Add store support for ProxySQL
- Loading branch information
1 parent
1016c68
commit de4f804
Showing
34 changed files
with
2,791 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package config | ||
|
||
import "fmt" | ||
|
||
// | ||
// ProxySQL-specific configuration | ||
// | ||
|
||
const ProxySQLDefaultDatabase = "stats" | ||
|
||
type ProxySQLConfigurationSettings struct { | ||
Addresses []string | ||
User string | ||
Password string | ||
HostgroupID uint | ||
IgnoreServerTTLSecs uint | ||
} | ||
|
||
func (settings ProxySQLConfigurationSettings) AddressToDSN(address string) string { | ||
return fmt.Sprintf("mysql://%s:*****@%s/%s", settings.User, address, ProxySQLDefaultDatabase) | ||
} | ||
|
||
func (settings *ProxySQLConfigurationSettings) IsEmpty() bool { | ||
if len(settings.Addresses) == 0 { | ||
return true | ||
} | ||
if settings.User == "" || settings.Password == "" { | ||
return true | ||
} | ||
if settings.HostgroupID < 1 { | ||
return true | ||
} | ||
return false | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
test "github.com/outbrain/golib/tests" | ||
) | ||
|
||
func TestProxySQLAddressToDSN(t *testing.T) { | ||
{ | ||
c := &ProxySQLConfigurationSettings{User: "freno"} | ||
test.S(t).ExpectEquals(c.AddressToDSN("proxysql-123abcd.test:6032"), "mysql://freno:*****@proxysql-123abcd.test:6032/"+ProxySQLDefaultDatabase) | ||
} | ||
} | ||
|
||
func TestProxySQLIsEmpty(t *testing.T) { | ||
{ | ||
c := &ProxySQLConfigurationSettings{} | ||
isEmpty := c.IsEmpty() | ||
test.S(t).ExpectTrue(isEmpty) | ||
} | ||
{ | ||
c := &ProxySQLConfigurationSettings{Addresses: []string{"localhost:6032"}} | ||
isEmpty := c.IsEmpty() | ||
test.S(t).ExpectTrue(isEmpty) | ||
} | ||
{ | ||
c := &ProxySQLConfigurationSettings{Addresses: []string{"localhost:6032"}, User: "freno"} | ||
isEmpty := c.IsEmpty() | ||
test.S(t).ExpectTrue(isEmpty) | ||
} | ||
{ | ||
c := &ProxySQLConfigurationSettings{Addresses: []string{"localhost:6032"}, User: "freno", Password: "freno"} | ||
isEmpty := c.IsEmpty() | ||
test.S(t).ExpectTrue(isEmpty) | ||
} | ||
{ | ||
c := &ProxySQLConfigurationSettings{Addresses: []string{"localhost:6032"}, User: "freno", Password: "freno", HostgroupID: 20} | ||
isEmpty := c.IsEmpty() | ||
test.S(t).ExpectFalse(isEmpty) | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# ProxySQL | ||
|
||
This package implements freno store support for [ProxySQL](https://proxysql.com/) | ||
|
||
## Logic | ||
|
||
Freno will probe servers found in the `stats.stats_mysql_connection_pool` ProxySQL admin table that have either status: | ||
1. `ONLINE` - connect, ping and replication checks pass | ||
1. `SHUNNED_REPLICATION_LAG` - connect and ping checks pass, but replication is lagging | ||
|
||
All other statuses are considered unhealthy and therefore are ignored by freno, eg: | ||
1. `SHUNNED` - proxysql connot connect and/or ping a backend | ||
1. `OFFLINE_SOFT` - a server that is draining, usually for maintenance, etc | ||
1. `OFFLINE_HARD` - a server that is completely offline | ||
|
||
## Requirements | ||
1. The ProxySQL `--no-monitor` flag is not set | ||
1. The [ProxySQL monitor module](https://github.com/sysown/proxysql/wiki/Monitor-Module) is enabled, eg: [`mysql-monitor_enabled`](https://github.com/sysown/proxysql/wiki/Global-variables#mysql-monitor_enabled) is `true` | ||
1. The `max_replication_lag` column is defined for backend servers in [the `mysql_servers` admin table](https://github.com/sysown/proxysql/wiki/Main-(runtime)#mysql_servers) | ||
- This ensures servers with lag do not receive reads but are still probed by freno | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package proxysql | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"sort" | ||
"time" | ||
|
||
"github.com/github/freno/pkg/config" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/outbrain/golib/log" | ||
"github.com/patrickmn/go-cache" | ||
) | ||
|
||
const ignoreServerCacheCleanupTTL = time.Duration(500) * time.Millisecond | ||
|
||
// MySQLConnectionPoolServer represents a row in the stats_mysql_connection_pool table | ||
type MySQLConnectionPoolServer struct { | ||
Host string | ||
Port int32 | ||
Status string | ||
} | ||
|
||
// Address returns a string of the hostname/port of a server | ||
func (ms *MySQLConnectionPoolServer) Address() string { | ||
return fmt.Sprintf("%s:%d", ms.Host, ms.Port) | ||
} | ||
|
||
// Client is the ProxySQL admin client | ||
type Client struct { | ||
dbs map[string]*sql.DB | ||
defaultIgnoreServerTTL time.Duration | ||
ignoreServerCache *cache.Cache | ||
} | ||
|
||
// NewClient returns a new ProxySQL admin client | ||
func NewClient(defaultIgnoreServerTTL time.Duration) *Client { | ||
return &Client{ | ||
dbs: make(map[string]*sql.DB, 0), | ||
defaultIgnoreServerTTL: defaultIgnoreServerTTL, | ||
ignoreServerCache: cache.New(cache.NoExpiration, ignoreServerCacheCleanupTTL), | ||
} | ||
} | ||
|
||
// GetDB returns a configured ProxySQL admin connection | ||
func (c *Client) GetDB(settings config.ProxySQLConfigurationSettings) (*sql.DB, string, error) { | ||
addrs := settings.Addresses | ||
sort.Strings(addrs) | ||
|
||
var lastErr error | ||
for _, addr := range addrs { | ||
if db, found := c.dbs[addr]; found { | ||
return db, addr, nil | ||
} | ||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?interpolateParams=true&timeout=500ms", | ||
settings.User, settings.Password, addr, config.ProxySQLDefaultDatabase, | ||
)) | ||
if err != nil { | ||
lastErr = err | ||
continue | ||
} | ||
if err = db.Ping(); err != nil { | ||
lastErr = err | ||
continue | ||
} | ||
log.Debugf("connected to ProxySQL at %s", settings.AddressToDSN(addr)) | ||
c.dbs[addr] = db | ||
return c.dbs[addr], addr, nil | ||
} | ||
if lastErr != nil { | ||
return nil, "", lastErr | ||
} | ||
return nil, "", errors.New("failed to get connection") | ||
} | ||
|
||
// CloseDB closes a ProxySQL admin connection based on an address string | ||
func (c *Client) CloseDB(addr string) { | ||
if db, found := c.dbs[addr]; found { | ||
db.Close() | ||
delete(c.dbs, addr) | ||
} | ||
} | ||
|
||
// GetServers returns a list of MySQLConnectionPoolServers with 'ONLINE' or 'SHUNNED_REPLICATION_LAG' status, based on hostgroup ID | ||
func (c *Client) GetServers(db *sql.DB, settings config.ProxySQLConfigurationSettings) (servers []*MySQLConnectionPoolServer, err error) { | ||
ignoreServerTTL := c.defaultIgnoreServerTTL | ||
if settings.IgnoreServerTTLSecs > 0 { | ||
ignoreServerTTL = time.Duration(settings.IgnoreServerTTLSecs) * time.Second | ||
} | ||
|
||
rows, err := db.Query(fmt.Sprintf(`SELECT srv_host, srv_port, status FROM stats_mysql_connection_pool WHERE hostgroup=%d`, settings.HostgroupID)) | ||
if err != nil { | ||
return servers, err | ||
} | ||
defer rows.Close() | ||
|
||
var server *MySQLConnectionPoolServer | ||
for rows.Next() { | ||
server = &MySQLConnectionPoolServer{} | ||
if err = rows.Scan(&server.Host, &server.Port, &server.Status); err != nil { | ||
return nil, err | ||
} | ||
|
||
switch server.Status { | ||
case "ONLINE": | ||
if _, ignore := c.ignoreServerCache.Get(server.Address()); ignore { | ||
log.Debugf("found %q in the proxysql ignore-server cache, ignoring ONLINE state for %s", server.Address(), ignoreServerTTL) | ||
continue | ||
} | ||
servers = append(servers, server) | ||
case "SHUNNED_REPLICATION_LAG": | ||
defer c.ignoreServerCache.Delete(server.Address()) | ||
servers = append(servers, server) | ||
default: | ||
c.ignoreServerCache.Set(server.Address(), true, ignoreServerTTL) | ||
} | ||
} | ||
|
||
return servers, rows.Err() | ||
} |
Oops, something went wrong.