open-consul/agent/consul/status_endpoint.go

86 lines
2.4 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2013-12-09 23:29:20 +00:00
package consul
2017-03-07 21:58:06 +00:00
import (
"fmt"
"strconv"
"github.com/hashicorp/consul/agent/structs"
2017-03-07 21:58:06 +00:00
)
2013-12-09 23:29:20 +00:00
// Status endpoint is used to check on server status
type Status struct {
server *Server
}
// Ping is used to just check for connectivity
func (s *Status) Ping(args EmptyReadRequest, reply *struct{}) error {
2013-12-09 23:29:20 +00:00
return nil
}
2013-12-10 23:16:41 +00:00
// Leader is used to get the address of the leader
func (s *Status) Leader(args *structs.DCSpecificRequest, reply *string) error {
// not using the regular forward function as it does a bunch of stuff we
// dont want like verifying consistency etc. We just want to enable DC
// forwarding
if args.Datacenter != "" && args.Datacenter != s.server.config.Datacenter {
return s.server.forwardDC("Status.Leader", args.Datacenter, args, reply)
}
leader := string(s.server.raft.Leader())
2015-05-08 18:35:12 +00:00
if leader != "" {
*reply = leader
2013-12-10 23:16:41 +00:00
} else {
*reply = ""
}
return nil
}
// Peers is used to get all the Raft peers
func (s *Status) Peers(args *structs.DCSpecificRequest, reply *[]string) error {
// not using the regular forward function as it does a bunch of stuff we
// dont want like verifying consistency etc. We just want to enable DC
// forwarding
if args.Datacenter != "" && args.Datacenter != s.server.config.Datacenter {
return s.server.forwardDC("Status.Peers", args.Datacenter, args, reply)
}
future := s.server.raft.GetConfiguration()
if err := future.Error(); err != nil {
return err
}
for _, server := range future.Configuration().Servers {
*reply = append(*reply, string(server.Address))
}
return nil
}
2017-03-07 21:58:06 +00:00
// EmptyReadRequest implements the interface used by middleware.RequestRecorder
// to communicate properties of requests.
type EmptyReadRequest struct{}
func (e EmptyReadRequest) IsRead() bool {
return true
}
// RaftStats is used by Autopilot to query the raft stats of the local server.
func (s *Status) RaftStats(args EmptyReadRequest, reply *structs.RaftStats) error {
2017-03-07 21:58:06 +00:00
stats := s.server.raft.Stats()
var err error
reply.LastContact = stats["last_contact"]
reply.LastIndex, err = strconv.ParseUint(stats["last_log_index"], 10, 64)
if err != nil {
return fmt.Errorf("error parsing server's last_log_index value: %w", err)
2017-03-07 21:58:06 +00:00
}
reply.LastTerm, err = strconv.ParseUint(stats["last_log_term"], 10, 64)
if err != nil {
return fmt.Errorf("error parsing server's last_log_term value: %w", err)
2017-03-07 21:58:06 +00:00
}
return nil
}