-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial admin page for managing metastore admin nodes (#3743)
* Add initial admin page for managing metastore admin nodes * Improve field name * Close metastore client connections * Remove todo * Update comment * Move metastore admin ownership outside of metastore * Move admin methods to RaftNodeService * Run mockery, fix broken test * Make node management operation more robust to failures * Run make fmt * Use api specific request/response types * Add clarification in info tooltip
- Loading branch information
Showing
37 changed files
with
4,062 additions
and
768 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,241 @@ | ||
package admin | ||
|
||
import ( | ||
"context" | ||
"math" | ||
"net/http" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/grafana/dskit/services" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/grafana/pyroscope/pkg/experiment/metastore/discovery" | ||
"github.com/grafana/pyroscope/pkg/experiment/metastore/raftnode/raftnodepb" | ||
httputil "github.com/grafana/pyroscope/pkg/util/http" | ||
) | ||
|
||
type configChangeRequest struct { | ||
serverId string | ||
currentTerm uint64 | ||
} | ||
|
||
type formActionHandler func(http.ResponseWriter, *http.Request, configChangeRequest) error | ||
|
||
type Admin struct { | ||
service services.Service | ||
|
||
logger log.Logger | ||
|
||
servers []discovery.Server | ||
leaderClient raftnodepb.RaftNodeServiceClient | ||
|
||
actionHandlers map[string]formActionHandler | ||
} | ||
|
||
func (a *Admin) Service() services.Service { | ||
return a.service | ||
} | ||
|
||
type metastoreClient struct { | ||
raftnodepb.RaftNodeServiceClient | ||
conn *grpc.ClientConn | ||
} | ||
|
||
func New( | ||
client raftnodepb.RaftNodeServiceClient, | ||
logger log.Logger, | ||
metastoreAddress string, | ||
) (*Admin, error) { | ||
adm := &Admin{ | ||
leaderClient: client, | ||
logger: logger, | ||
actionHandlers: make(map[string]formActionHandler), | ||
} | ||
adm.addFormActionHandlers() | ||
adm.service = services.NewIdleService(adm.starting, adm.stopping) | ||
|
||
disc, err := discovery.NewDiscovery(logger, metastoreAddress, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
disc.Subscribe(adm) | ||
|
||
return adm, nil | ||
} | ||
|
||
func (a *Admin) starting(context.Context) error { return nil } | ||
func (a *Admin) stopping(error) error { return nil } | ||
|
||
func (a *Admin) Servers(servers []discovery.Server) { | ||
a.servers = servers | ||
slices.SortFunc(a.servers, func(a, b discovery.Server) int { | ||
return strings.Compare(string(a.Raft.ID), string(b.Raft.ID)) | ||
}) | ||
} | ||
|
||
func (a *Admin) NodeListHandler() http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodPost { | ||
for fieldName, handler := range a.actionHandlers { | ||
field := r.FormValue(fieldName) | ||
if field != "" { | ||
changeRequest := configChangeRequest{ | ||
serverId: field, | ||
} | ||
if currentTerm := r.FormValue("current-term"); currentTerm != "" { | ||
parsedTerm, err := strconv.ParseUint(currentTerm, 10, 64) | ||
if err == nil { | ||
changeRequest.currentTerm = parsedTerm | ||
} | ||
} | ||
if err := handler(w, r, changeRequest); err != nil { | ||
httputil.Error(w, err) | ||
return | ||
} | ||
w.Header().Set("Location", "#") | ||
w.WriteHeader(http.StatusFound) | ||
return | ||
} | ||
} | ||
} | ||
|
||
raftState, err := a.fetchRaftState(r.Context()) | ||
if err != nil { | ||
httputil.Error(w, err) | ||
return | ||
} | ||
|
||
err = pageTemplates.nodesTemplate.Execute(w, nodesPageContent{ | ||
DiscoveredServers: a.servers, | ||
Raft: raftState, | ||
Now: time.Now().UTC(), | ||
}) | ||
if err != nil { | ||
httputil.Error(w, err) | ||
} | ||
}) | ||
} | ||
|
||
func (a *Admin) fetchRaftState(ctx context.Context) (*raftNodeState, error) { | ||
observedLeaders := make(map[string]int) | ||
numRaftNodes := 0 | ||
nodes := make([]*metastoreNode, 0, len(a.servers)) | ||
|
||
for _, s := range a.servers { | ||
cl, err := newClient(s.ResolvedAddress) | ||
if err != nil { | ||
level.Warn(a.logger).Log("msg", "missing client for server", "server", s) | ||
continue | ||
} | ||
node := &metastoreNode{ | ||
DiscoveryServerId: string(s.Raft.ID), | ||
ResolvedAddress: s.ResolvedAddress, | ||
} | ||
nodes = append(nodes, node) | ||
|
||
res, err := cl.NodeInfo(ctx, &raftnodepb.NodeInfoRequest{}) | ||
_ = cl.conn.Close() | ||
|
||
if err != nil { | ||
level.Warn(a.logger).Log("msg", "error fetching node info", "server", s, "err", err) | ||
continue | ||
} | ||
nInfo := res.Node | ||
|
||
node.RaftServerId = nInfo.ServerId | ||
node.Member = nInfo.LeaderId != "" | ||
node.State = nInfo.State | ||
node.LeaderId = nInfo.LeaderId | ||
node.ConfigIndex = nInfo.ConfigurationIndex | ||
node.NumPeers = len(nInfo.Peers) | ||
node.CurrentTerm = nInfo.CurrentTerm | ||
node.LastIndex = nInfo.LastIndex | ||
node.CommitIndex = nInfo.CommitIndex | ||
node.AppliedIndex = nInfo.AppliedIndex | ||
node.BuildVersion = nInfo.BuildVersion | ||
node.BuildRevision = nInfo.BuildRevision | ||
node.Stats = make(map[string]string) | ||
for i, n := range nInfo.Stats.Name { | ||
node.Stats[n] = nInfo.Stats.Value[i] | ||
} | ||
|
||
if node.Member { | ||
numRaftNodes++ | ||
observedLeaders[node.LeaderId]++ | ||
} | ||
} | ||
|
||
currentTerm := findCurrentTerm(nodes) | ||
|
||
return &raftNodeState{ | ||
Nodes: nodes, | ||
ObservedLeaders: observedLeaders, | ||
CurrentTerm: currentTerm, | ||
NumNodes: numRaftNodes, | ||
}, nil | ||
} | ||
|
||
func findCurrentTerm(nodes []*metastoreNode) uint64 { | ||
terms := make(map[uint64]int) | ||
for _, node := range nodes { | ||
if node.Member { | ||
terms[node.CurrentTerm]++ | ||
} | ||
} | ||
// TODO aleks-p: in case of a mismatch in reported current terms, we bypass any validation | ||
term := uint64(math.MaxUint64) | ||
if len(terms) == 1 { | ||
for k := range terms { | ||
term = k | ||
} | ||
} | ||
return term | ||
} | ||
|
||
func newClient(address string) (*metastoreClient, error) { | ||
conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &metastoreClient{ | ||
RaftNodeServiceClient: raftnodepb.NewRaftNodeServiceClient(conn), | ||
conn: conn, | ||
}, nil | ||
} | ||
|
||
func (a *Admin) addFormActionHandlers() { | ||
a.actionHandlers["add"] = func(w http.ResponseWriter, r *http.Request, cr configChangeRequest) error { | ||
_, err := a.leaderClient.AddNode(r.Context(), &raftnodepb.AddNodeRequest{ | ||
ServerId: cr.serverId, | ||
CurrentTerm: cr.currentTerm, | ||
}) | ||
return err | ||
} | ||
a.actionHandlers["remove"] = func(w http.ResponseWriter, r *http.Request, cr configChangeRequest) error { | ||
_, err := a.leaderClient.RemoveNode(r.Context(), &raftnodepb.RemoveNodeRequest{ | ||
ServerId: cr.serverId, | ||
CurrentTerm: cr.currentTerm, | ||
}) | ||
return err | ||
} | ||
a.actionHandlers["promote"] = func(w http.ResponseWriter, r *http.Request, cr configChangeRequest) error { | ||
_, err := a.leaderClient.PromoteToLeader(r.Context(), &raftnodepb.PromoteToLeaderRequest{ | ||
ServerId: cr.serverId, | ||
CurrentTerm: cr.currentTerm, | ||
}) | ||
return err | ||
} | ||
a.actionHandlers["demote"] = func(w http.ResponseWriter, r *http.Request, cr configChangeRequest) error { | ||
_, err := a.leaderClient.DemoteLeader(r.Context(), &raftnodepb.DemoteLeaderRequest{ | ||
ServerId: cr.serverId, | ||
CurrentTerm: cr.currentTerm, | ||
}) | ||
return err | ||
} | ||
} |
Oops, something went wrong.