-
Notifications
You must be signed in to change notification settings - Fork 55
Add store support for ProxySQL #129
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
718d4e4
Add store support for ProxySQL
timvaillancourt 335264f
Merge remote-tracking branch 'origin/master' into proxysql_v0
timvaillancourt 7292b19
Allow SHUNNED_REPLICATION_LAG status
timvaillancourt 1e9e2d7
Add README.md and update comments
timvaillancourt 3de1680
More README updates
timvaillancourt b071a52
Fix test on travis CI
timvaillancourt 1046923
Merge branch 'master' into proxysql_v0
timvaillancourt 42819f6
Add default addresses, user and password to MySQLConfigurationSettings
timvaillancourt fb48393
Merge branch 'master' into proxysql_v0
timvaillancourt 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
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
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 |
|---|---|---|
| @@ -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 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 |
|---|---|---|
| @@ -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 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 |
|---|---|---|
| @@ -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 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 |
|---|---|---|
| @@ -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.
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.