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 service URL for MySQL backend #165

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type ConfigurationSettings struct {
Environment string
Domain string
ShareDomain string
ServiceURL string
RaftBind string
RaftDataDir string
DefaultRaftPort int // if a RaftNodes entry does not specify port, use this one
Expand Down Expand Up @@ -158,6 +159,9 @@ func (settings *ConfigurationSettings) postReadAdjustments() error {
if submatch := envVariableRegexp.FindStringSubmatch(settings.ShareDomain); len(submatch) > 1 {
settings.ShareDomain = os.Getenv(submatch[1])
}
if submatch := envVariableRegexp.FindStringSubmatch(settings.ServiceURL); len(submatch) > 1 {
settings.ServiceURL = os.Getenv(submatch[1])
}
if settings.RaftDataDir == "" && settings.BackendMySQLHost == "" {
return fmt.Errorf("Either RaftDataDir or BackendMySQLHost must be set")
}
Expand Down
29 changes: 21 additions & 8 deletions pkg/group/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
CREATE TABLE service_election (
domain varchar(32) NOT NULL,
share_domain varchar(32) NOT NULL,
service_url varchar(64) NOT NULL DEFAULT '',
service_id varchar(128) NOT NULL,
last_seen_active timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (domain),
Expand Down Expand Up @@ -61,6 +62,7 @@ type MySQLBackend struct {
domain string
shareDomain string
serviceId string
serviceUrl string
leaderState int64
healthState int64
throttler *throttle.Throttler
Expand Down Expand Up @@ -94,12 +96,14 @@ func NewMySQLBackend(throttler *throttle.Throttler) (*MySQLBackend, error) {
domain = fmt.Sprintf("%s:%s", config.Settings().DataCenter, config.Settings().Environment)
}
shareDomain := config.Settings().ShareDomain
serviceUrl := config.Settings().ServiceURL
serviceId := fmt.Sprintf("%s:%d", hostname, config.Settings().ListenPort)
backend := &MySQLBackend{
db: db,
domain: domain,
shareDomain: shareDomain,
serviceId: serviceId,
serviceUrl: serviceUrl,
throttler: throttler,
}
go backend.continuousOperations()
Expand Down Expand Up @@ -261,28 +265,29 @@ func (backend *MySQLBackend) GetHealthyDomainServices() (services []string, err
func (backend *MySQLBackend) AttemptLeadership() error {
query := `
insert ignore into service_election (
domain, share_domain, service_id, last_seen_active
domain, share_domain, service_id, service_url, last_seen_active
) values (
?, ?, ?, now()
?, ?, ?, ?, now()
) on duplicate key update
service_id = if(last_seen_active < now() - interval ? second, values(service_id), service_id),
service_url = if(service_id = values(service_id), values(service_url), service_url),
share_domain = if(service_id = values(service_id), values(share_domain), share_domain),
last_seen_active = if(service_id = values(service_id), values(last_seen_active), last_seen_active)
`
args := sqlutils.Args(backend.domain, backend.shareDomain, backend.serviceId, electionExpireSeconds)
args := sqlutils.Args(backend.domain, backend.shareDomain, backend.serviceId, backend.serviceUrl, electionExpireSeconds)
_, err := sqlutils.ExecNoPrepare(backend.db, query, args...)
return err
}

func (backend *MySQLBackend) ForceLeadership() error {
query := `
replace into service_election (
domain, service_id, last_seen_active
domain, service_id, service_url, last_seen_active
) values (
?, ?, now()
?, ?, ?, now()
)
`
args := sqlutils.Args(backend.domain, backend.serviceId)
args := sqlutils.Args(backend.domain, backend.serviceId, backend.serviceUrl)
_, err := sqlutils.ExecNoPrepare(backend.db, query, args...)
return err
}
Expand Down Expand Up @@ -325,7 +330,8 @@ func (backend *MySQLBackend) GetSharedDomainServices() (services map[string]stri
query := `
select
domain,
service_id
service_id,
service_url
from
service_election
where
Expand All @@ -335,7 +341,14 @@ func (backend *MySQLBackend) GetSharedDomainServices() (services map[string]stri
`
args := sqlutils.Args(backend.shareDomain, electionExpireSeconds, backend.serviceId)
err = sqlutils.QueryRowsMap(backend.db, query, func(m sqlutils.RowMap) error {
services[m.GetString("domain")] = m.GetString("service_id")
serviceUrl := m.GetString("service_url")
domain := m.GetString("domain")
if serviceUrl != "" {
services[domain] = serviceUrl
} else {
// fall back to using service_id
services[domain] = m.GetString("service_id")
}
return nil
}, args...)

Expand Down
6 changes: 3 additions & 3 deletions pkg/throttle/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package throttle
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
Expand All @@ -23,6 +22,7 @@ import (

"github.com/bradfitz/gomemcache/memcache"
metrics "github.com/rcrowley/go-metrics"
"io"
)

const leaderCheckInterval = 1 * time.Second
Expand Down Expand Up @@ -673,15 +673,15 @@ func (throttler *Throttler) collectShareDomainMetricHealth() error {
aggregatedMetricHealth := make(base.MetricHealthMap)
for _, service := range services {
err := func() error {
uri := fmt.Sprintf("http://%s/metrics-health", service)
uri := fmt.Sprintf("https://%s/metrics-health", service)

resp, err := throttler.httpClient.Get(uri)
if err != nil {
return err
}

defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
Loading