Skip to content

Commit

Permalink
Merge pull request #780 from alexadhy/feature/min_hops_api
Browse files Browse the repository at this point in the history
Add min hops manipulation endpoint
  • Loading branch information
jdknives committed Jun 3, 2021
2 parents b883718 + b2d3432 commit be678f1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ before_install:
- nvm install 10.16

install:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.31.0
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.40.1
- make dep

before_script:
Expand Down
9 changes: 9 additions & 0 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type API interface {
UpdateAvailable(channel updater.Channel) (*updater.Version, error)
UpdateStatus() (string, error)
RuntimeLogs() (string, error)

SetMinHops(uint16) error
}

// HealthCheckable resource returns its health status as an integer
Expand Down Expand Up @@ -137,6 +139,7 @@ type Summary struct {
IsHypervisor bool `json:"is_hypervisor,omitempty"`
DmsgStats *dmsgtracker.DmsgClientSummary `json:"dmsg_stats"`
Online bool `json:"online"`
MinHops uint16 `json:"min_hops"`
}

// Summary implements API.
Expand Down Expand Up @@ -175,6 +178,7 @@ func (v *Visor) Summary() (*Summary, error) {
Health: health,
Uptime: uptime,
Routes: extraRoutes,
MinHops: v.conf.Routing.MinHops,
}

return summary, nil
Expand Down Expand Up @@ -734,3 +738,8 @@ func (v *Visor) RuntimeLogs() (string, error) {
builder.WriteString("]")
return builder.String(), nil
}

// SetMinHops sets min_hops routing config of visor
func (v *Visor) SetMinHops(in uint16) error {
return v.conf.UpdateMinHops(in)
}
23 changes: 23 additions & 0 deletions pkg/visor/hypervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func (hv *Hypervisor) makeMux() chi.Router {
r.Get("/visors/{pk}/update/available", hv.visorUpdateAvailable())
r.Get("/visors/{pk}/update/available/{channel}", hv.visorUpdateAvailable())
r.Get("/visors/{pk}/runtime-logs", hv.getRuntimeLogs())
r.Post("/visors/{pk}/min-hops", hv.postMinHops())
})
})

Expand Down Expand Up @@ -1296,6 +1297,28 @@ func (hv *Hypervisor) getRuntimeLogs() http.HandlerFunc {
})
}

func (hv *Hypervisor) postMinHops() http.HandlerFunc {
return hv.withCtx(hv.visorCtx, func(w http.ResponseWriter, r *http.Request, ctx *httpCtx) {
var reqBody struct {
MinHops uint16 `json:"min_hops"`
}

if err := httputil.ReadJSON(r, &reqBody); err != nil {
if err != io.EOF {
hv.log(r).Warnf("postMinHops request: %v", err)
}
httputil.WriteJSON(w, r, http.StatusBadRequest, usermanager.ErrMalformedRequest)
return
}

if err := ctx.API.SetMinHops(reqBody.MinHops); err != nil {
httputil.WriteJSON(w, r, http.StatusInternalServerError, err)
return
}
httputil.WriteJSON(w, r, http.StatusOK, struct{}{})
})
}

/*
<<< Helper functions >>>
*/
Expand Down
46 changes: 11 additions & 35 deletions pkg/visor/rpc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package visor

import (
"encoding/hex"
"errors"
"fmt"
"net/rpc"
Expand Down Expand Up @@ -147,42 +146,12 @@ func newTransportSummary(tm *transport.Manager, tp *transport.ManagedTransport,

// Summary provides an extra summary of the AppNode.
func (r *RPC) Summary(_ *struct{}, out *Summary) (err error) {
overview, err := r.visor.Overview()
if err != nil {
return fmt.Errorf("summary")
}

health, err := r.visor.Health()
if err != nil {
return fmt.Errorf("health")
}

uptime, err := r.visor.Uptime()
if err != nil {
return fmt.Errorf("uptime")
}

routes, err := r.visor.RoutingRules()
defer rpcutil.LogCall(r.log, "Summary", nil)(out, &err)
sum, err := r.visor.Summary()
if err != nil {
return fmt.Errorf("routes")
}

extraRoutes := make([]routingRuleResp, 0, len(routes))
for _, route := range routes {
extraRoutes = append(extraRoutes, routingRuleResp{
Key: route.KeyRouteID(),
Rule: hex.EncodeToString(route),
Summary: route.Summary(),
})
}

*out = Summary{
Overview: overview,
Health: health,
Uptime: uptime,
Routes: extraRoutes,
return err
}

*out = *sum
return nil
}

Expand Down Expand Up @@ -546,3 +515,10 @@ func (r *RPC) RuntimeLogs(_ *struct{}, logs *string) (err error) {
*logs, err = r.visor.RuntimeLogs()
return
}

// SetMinHops sets min_hops from visor's routing config
func (r *RPC) SetMinHops(n *uint16, _ *struct{}) (err error) {
defer rpcutil.LogCall(r.log, "SetMinHops", *n)
err = r.visor.SetMinHops(*n)
return
}
17 changes: 14 additions & 3 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,19 @@ func (rc *rpcClient) Update(config updater.UpdateConfig) (bool, error) {
return updated, err
}

// Update calls Update.
// RuntimeLogs calls RuntimeLogs.
func (rc *rpcClient) RuntimeLogs() (string, error) {
var logs string
err := rc.Call("RuntimeLogs", &struct{}{}, &logs)
return logs, err
}

// SetMinHops sets the min_hops from visor routing config
func (rc *rpcClient) SetMinHops(hops uint16) error {
err := rc.Call("SetMinHops", &hops, &struct{}{})
return err
}

// StatusMessage defines a status of visor update.
type StatusMessage struct {
Text string
Expand Down Expand Up @@ -424,7 +430,7 @@ func (rc *rpcClient) UpdateAvailable(channel updater.Channel) (*updater.Version,
return &version, err
}

// UpdateAvailable calls UpdateAvailable.
// UpdateStatus calls UpdateStatus
func (rc *rpcClient) UpdateStatus() (string, error) {
var result string
err := rc.Call("UpdateStatus", &struct{}{}, &result)
Expand Down Expand Up @@ -919,7 +925,12 @@ func (mc *mockRPCClient) UpdateStatus() (string, error) {
return "", nil
}

// UpdateStatus implements API.
// RuntimeLogs implements API.
func (mc *mockRPCClient) RuntimeLogs() (string, error) {
return "", nil
}

// SetMinHops implements API
func (mc *mockRPCClient) SetMinHops(n uint16) error {
return nil
}
9 changes: 9 additions & 0 deletions pkg/visor/visorconfig/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ func (v1 *V1) UpdateAppArg(launch *launcher.Launcher, appName, argName string, v
return v1.flush(v1)
}

// UpdateMinHops updates min_hops config
func (v1 *V1) UpdateMinHops(hops uint16) error {
v1.mu.Lock()
v1.Routing.MinHops = hops
v1.mu.Unlock()

return v1.flush(v1)
}

// updateStringArg updates the cli non-boolean flag of the specified app config and also within the launcher.
// It removes argName from app args if value is an empty string.
// The updated config gets flushed to file if there are any changes.
Expand Down

0 comments on commit be678f1

Please sign in to comment.