2017-12-18 21:16:23 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-01 18:27:10 +00:00
|
|
|
"net"
|
2017-12-18 21:16:23 +00:00
|
|
|
"sync"
|
|
|
|
|
2018-09-15 23:23:13 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2022-09-01 18:27:10 +00:00
|
|
|
"github.com/hashicorp/raft"
|
|
|
|
autopilot "github.com/hashicorp/raft-autopilot"
|
2018-09-15 23:23:13 +00:00
|
|
|
|
2018-01-12 21:58:44 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pool"
|
2022-09-01 18:27:10 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-12-18 21:16:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StatsFetcher has two functions for autopilot. First, lets us fetch all the
|
|
|
|
// stats in parallel so we are taking a sample as close to the same time as
|
|
|
|
// possible, since we are comparing time-sensitive info for the health check.
|
|
|
|
// Second, it bounds the time so that one slow RPC can't hold up the health
|
|
|
|
// check loop; as a side effect of how it implements this, it also limits to
|
|
|
|
// a single in-flight RPC to any given server, so goroutines don't accumulate
|
|
|
|
// as we run the health check fairly frequently.
|
|
|
|
type StatsFetcher struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
logger log.Logger
|
2018-01-12 21:58:44 +00:00
|
|
|
pool *pool.ConnPool
|
2017-12-18 21:16:23 +00:00
|
|
|
region string
|
2022-09-01 18:27:10 +00:00
|
|
|
inflight map[raft.ServerID]struct{}
|
2017-12-18 21:16:23 +00:00
|
|
|
inflightLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStatsFetcher returns a stats fetcher.
|
2018-09-15 23:23:13 +00:00
|
|
|
func NewStatsFetcher(logger log.Logger, pool *pool.ConnPool, region string) *StatsFetcher {
|
2017-12-18 21:16:23 +00:00
|
|
|
return &StatsFetcher{
|
2018-09-15 23:23:13 +00:00
|
|
|
logger: logger.Named("stats_fetcher"),
|
2017-12-18 21:16:23 +00:00
|
|
|
pool: pool,
|
|
|
|
region: region,
|
2022-09-01 18:27:10 +00:00
|
|
|
inflight: make(map[raft.ServerID]struct{}),
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch does the RPC to fetch the server stats from a single server. We don't
|
|
|
|
// cancel this when the context is canceled because we only want one in-flight
|
|
|
|
// RPC to each server, so we let it finish and then clean up the in-flight
|
|
|
|
// tracking.
|
2022-09-01 18:27:10 +00:00
|
|
|
func (f *StatsFetcher) fetch(server *autopilot.Server, replyCh chan *autopilot.ServerStats) {
|
2023-01-26 20:05:51 +00:00
|
|
|
var args structs.GenericRequest
|
2022-09-01 18:27:10 +00:00
|
|
|
var reply structs.RaftStats
|
|
|
|
|
|
|
|
// defer some cleanup to notify everything else that the fetching is no longer occurring
|
|
|
|
// this is easier than trying to make the conditionals line up just right.
|
|
|
|
defer func() {
|
|
|
|
f.inflightLock.Lock()
|
|
|
|
delete(f.inflight, server.ID)
|
|
|
|
f.inflightLock.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", string(server.Address))
|
2017-12-18 21:16:23 +00:00
|
|
|
if err != nil {
|
2022-09-01 18:27:10 +00:00
|
|
|
f.logger.Warn("error resolving TCP address for server",
|
|
|
|
"address", server.Address,
|
|
|
|
"error", err)
|
|
|
|
return
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-01 18:27:10 +00:00
|
|
|
err = f.pool.RPC(f.region, addr, "Status.RaftStats", &args, &reply)
|
|
|
|
if err != nil {
|
|
|
|
f.logger.Warn("error getting server health", "server", server.Name, "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
replyCh <- reply.ToAutopilotServerStats()
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch will attempt to query all the servers in parallel.
|
2022-09-01 18:27:10 +00:00
|
|
|
func (f *StatsFetcher) Fetch(ctx context.Context, servers map[raft.ServerID]*autopilot.Server) map[raft.ServerID]*autopilot.ServerStats {
|
2017-12-18 21:16:23 +00:00
|
|
|
type workItem struct {
|
2022-09-01 18:27:10 +00:00
|
|
|
server *autopilot.Server
|
2017-12-18 21:16:23 +00:00
|
|
|
replyCh chan *autopilot.ServerStats
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip any servers that have inflight requests.
|
|
|
|
var work []*workItem
|
|
|
|
f.inflightLock.Lock()
|
2022-09-01 18:27:10 +00:00
|
|
|
for id, server := range servers {
|
|
|
|
if _, ok := f.inflight[id]; ok {
|
2018-09-15 23:23:13 +00:00
|
|
|
f.logger.Warn("failed retrieving server health; last request still outstanding", "server", server.Name)
|
2017-12-18 21:16:23 +00:00
|
|
|
} else {
|
|
|
|
workItem := &workItem{
|
|
|
|
server: server,
|
|
|
|
replyCh: make(chan *autopilot.ServerStats, 1),
|
|
|
|
}
|
|
|
|
work = append(work, workItem)
|
2022-09-01 18:27:10 +00:00
|
|
|
f.inflight[id] = struct{}{}
|
2017-12-18 21:16:23 +00:00
|
|
|
go f.fetch(workItem.server, workItem.replyCh)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.inflightLock.Unlock()
|
|
|
|
|
|
|
|
// Now wait for the results to come in, or for the context to be
|
|
|
|
// canceled.
|
2022-09-01 18:27:10 +00:00
|
|
|
replies := make(map[raft.ServerID]*autopilot.ServerStats)
|
2017-12-18 21:16:23 +00:00
|
|
|
for _, workItem := range work {
|
2022-09-01 18:27:10 +00:00
|
|
|
// Drain the reply first if there is one.
|
|
|
|
select {
|
|
|
|
case reply := <-workItem.replyCh:
|
|
|
|
replies[workItem.server.ID] = reply
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
select {
|
|
|
|
case reply := <-workItem.replyCh:
|
|
|
|
replies[workItem.server.ID] = reply
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
2022-09-01 18:27:10 +00:00
|
|
|
f.logger.Warn("failed retrieving server health",
|
|
|
|
"server", workItem.server.Name, "error", ctx.Err())
|
|
|
|
|
|
|
|
f.inflightLock.Lock()
|
|
|
|
delete(f.inflight, workItem.server.ID)
|
|
|
|
f.inflightLock.Unlock()
|
|
|
|
|
2017-12-18 21:16:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return replies
|
|
|
|
}
|