Skip to content

Commit

Permalink
Merge pull request #2227 from openziti/sort-by-online-routers
Browse files Browse the repository at this point in the history
Sort connected routers first for session ers. Increase er limit to 25. Fixes #2046
  • Loading branch information
plorenz authored Jul 13, 2024
2 parents d849fb9 + 1388ec2 commit 7a9e3cc
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 32 deletions.
2 changes: 1 addition & 1 deletion controller/internal/routes/service_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (r *ServiceRouter) listClientEdgeRouters(ae *env.AppEnv, rc *response.Reque
func getServiceEdgeRouters(ae *env.AppEnv, rc *response.RequestContext, serviceId string) (*rest_model.ServiceEdgeRouters, error) {
edgeRouters := &rest_model.ServiceEdgeRouters{}

edgeRoutersForSvc, err := ae.Managers.EdgeRouter.ListForIdentityAndService(rc.Identity.Id, serviceId, nil)
edgeRoutersForSvc, err := ae.Managers.EdgeRouter.ListForIdentityAndService(rc.Identity.Id, serviceId)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion controller/internal/routes/session_api_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func MapSessionsToRestEntities(ae *env.AppEnv, rc *response.RequestContext, sess
func getSessionEdgeRouters(ae *env.AppEnv, ns *model.Session) ([]*rest_model.SessionEdgeRouter, error) {
var edgeRouters []*rest_model.SessionEdgeRouter

edgeRoutersForSession, err := ae.Managers.EdgeRouter.ListForIdentityAndService(ns.IdentityId, ns.ServiceId, nil)
edgeRoutersForSession, err := ae.Managers.EdgeRouter.ListForIdentityAndService(ns.IdentityId, ns.ServiceId)
if err != nil {
return nil, err
}
Expand Down
58 changes: 35 additions & 23 deletions controller/model/edge_router_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,21 @@ package model
import (
"encoding/json"
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/storage/boltz"
"github.com/openziti/ziti/common/cert"
"github.com/openziti/ziti/common/eid"
"github.com/openziti/ziti/common/pb/cmd_pb"
"github.com/openziti/ziti/common/pb/edge_cmd_pb"
"github.com/openziti/ziti/controller/apierror"
"github.com/openziti/ziti/controller/change"
"github.com/openziti/ziti/controller/command"
"github.com/openziti/ziti/controller/fields"
"google.golang.org/protobuf/proto"
"strconv"

"github.com/michaelquigley/pfxlog"
"github.com/openziti/storage/boltz"
"github.com/openziti/ziti/controller/apierror"
"github.com/openziti/ziti/controller/db"
"github.com/openziti/ziti/controller/fields"
"github.com/openziti/ziti/controller/models"
"github.com/pkg/errors"
"go.etcd.io/bbolt"
"google.golang.org/protobuf/proto"
)

func NewEdgeRouterManager(env Env) *EdgeRouterManager {
Expand All @@ -54,6 +52,24 @@ func NewEdgeRouterManager(env Env) *EdgeRouterManager {

manager.impl = manager

isConnectedSymbol := boltz.NewBoolFuncSymbol(env.GetStores().EdgeRouter, "connected", func(id string) bool {
return env.GetManagers().Router.IsConnected(id)
})

isOnlineSymbol := boltz.NewBoolFuncSymbol(env.GetStores().EdgeRouter, "online", func(id string) bool {
return env.IsEdgeRouterOnline(id)
})

if store, ok := env.GetStores().EdgeRouter.(boltz.ConfigurableStore); ok {
store.AddEntitySymbol(isConnectedSymbol)
store.MakeSymbolPublic(isConnectedSymbol.GetName())

store.AddEntitySymbol(isOnlineSymbol)
store.MakeSymbolPublic(isOnlineSymbol.GetName())
} else {
panic("edge-router store is not boltz.ConfigurableStore")
}

RegisterCommand(env, &CreateEdgeRouterCmd{}, &edge_cmd_pb.CreateEdgeRouterCmd{})
RegisterUpdateDecoder[*EdgeRouter](env, manager)
RegisterDeleteDecoder(env, manager)
Expand Down Expand Up @@ -188,11 +204,11 @@ func (self *EdgeRouterManager) Query(query string) (*EdgeRouterListResult, error
return result, nil
}

func (self *EdgeRouterManager) ListForIdentityAndService(identityId, serviceId string, limit *int) (*EdgeRouterListResult, error) {
func (self *EdgeRouterManager) ListForIdentityAndService(identityId, serviceId string) (*EdgeRouterListResult, error) {
var list *EdgeRouterListResult
var err error
if txErr := self.env.GetDb().View(func(tx *bbolt.Tx) error {
list, err = self.ListForIdentityAndServiceWithTx(tx, identityId, serviceId, limit)
list, err = self.ListForIdentityAndServiceWithTx(tx, identityId, serviceId)
return nil
}); txErr != nil {
return nil, txErr
Expand All @@ -201,20 +217,6 @@ func (self *EdgeRouterManager) ListForIdentityAndService(identityId, serviceId s
return list, err
}

func (self *EdgeRouterManager) ListForIdentityAndServiceWithTx(tx *bbolt.Tx, identityId, serviceId string, limit *int) (*EdgeRouterListResult, error) {
query := fmt.Sprintf(`anyOf(identities) = "%v" and anyOf(services) = "%v"`, identityId, serviceId)

if limit != nil {
query += " limit " + strconv.Itoa(*limit)
}

result := &EdgeRouterListResult{manager: self}
if err := self.ListWithTx(tx, query, result.collect); err != nil {
return nil, err
}
return result, nil
}

func (self *EdgeRouterManager) IsAccessToEdgeRouterAllowed(identityId, serviceId, edgeRouterId string) (bool, error) {
var result bool
err := self.GetDb().View(func(tx *bbolt.Tx) error {
Expand All @@ -232,6 +234,16 @@ func (self *EdgeRouterManager) IsAccessToEdgeRouterAllowed(identityId, serviceId
return result, nil
}

func (self *EdgeRouterManager) ListForIdentityAndServiceWithTx(tx *bbolt.Tx, identityId, serviceId string) (*EdgeRouterListResult, error) {
query := fmt.Sprintf(`anyOf(identities) = "%v" and anyOf(services) = "%v" SORT BY connected DESC LIMIT 25`, identityId, serviceId)

result := &EdgeRouterListResult{manager: self}
if err := self.ListWithTx(tx, query, result.collect); err != nil {
return nil, err
}
return result, nil
}

func (self *EdgeRouterManager) IsSharedEdgeRouterPresent(identityId, serviceId string) (bool, error) {
var result bool
err := self.GetDb().View(func(tx *bbolt.Tx) error {
Expand Down
2 changes: 1 addition & 1 deletion controller/model/edge_router_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (ctx *TestContext) testGetEdgeRoutersForServiceAndIdentity(*testing.T) {
func (ctx *TestContext) isEdgeRouterAccessible(edgeRouterId, identityId, serviceId string) bool {
found := false
err := ctx.GetDb().View(func(tx *bbolt.Tx) error {
result, err := ctx.managers.EdgeRouter.ListForIdentityAndServiceWithTx(tx, identityId, serviceId, nil)
result, err := ctx.managers.EdgeRouter.ListForIdentityAndServiceWithTx(tx, identityId, serviceId)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions controller/model/link_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"
)

// TODO: Add CreateDate
type Link struct {
SrcLatency int64
DstLatency int64
Expand Down
11 changes: 11 additions & 0 deletions controller/model/router_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ func newRouterManager(env Env) *RouterManager {

RegisterManagerDecoder[*Router](env, result)

isConnectedSymbol := boltz.NewBoolFuncSymbol(routerStore, "connected", func(id string) bool {
return result.connected.Has(id)
})

if store, ok := routerStore.(boltz.ConfigurableStore); ok {
store.AddEntitySymbol(isConnectedSymbol)
store.MakeSymbolPublic(isConnectedSymbol.GetName())
} else {
panic("router store is not boltz.ConfigurableStore")
}

return result
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/openziti/runzmd v1.0.49
github.com/openziti/sdk-golang v0.23.38
github.com/openziti/secretstream v0.1.21
github.com/openziti/storage v0.2.45
github.com/openziti/storage v0.2.46-0.20240712192941-cc29b37bac57
github.com/openziti/transport/v2 v2.0.138
github.com/openziti/x509-claims v1.0.3
github.com/openziti/xweb/v2 v2.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,8 @@ github.com/openziti/sdk-golang v0.23.38 h1:CqA+/YN5rBO5csjQef9Qs5pfrSxk5CqT7e1n8
github.com/openziti/sdk-golang v0.23.38/go.mod h1:ZRHgiWVOUpTk9LW2BuvIv8sQP3jPtBctGn9E1s5XoCQ=
github.com/openziti/secretstream v0.1.21 h1:r4xN8/CzSEvxZFFYGSztrlhMtIvk3B+SQcq2zgZ4Tb4=
github.com/openziti/secretstream v0.1.21/go.mod h1:1lfAnS8gBHsKZiPbRRK1sularbAsqizN6tWUEuZSfo0=
github.com/openziti/storage v0.2.45 h1:rCvQzWmGSmx6Ir2zUFO8H48klONGwZQfwFrfrEJuq9k=
github.com/openziti/storage v0.2.45/go.mod h1:v6KddQOjkQmKHnC7hIgFtcm4tdA8l8DbxYHQOIbRWa8=
github.com/openziti/storage v0.2.46-0.20240712192941-cc29b37bac57 h1:f5Y/XFVo9YqddlYjiRok/Wkqoxk422x29WdmEyVpIIk=
github.com/openziti/storage v0.2.46-0.20240712192941-cc29b37bac57/go.mod h1:v6KddQOjkQmKHnC7hIgFtcm4tdA8l8DbxYHQOIbRWa8=
github.com/openziti/transport/v2 v2.0.138 h1:F7TUv34BZ6x2BetYLtYbxSU/G15B+vkGRU4uPKwvRvU=
github.com/openziti/transport/v2 v2.0.138/go.mod h1:v0PN1dhFP48HeUUeBq9n/Ql2u5ln8EOtPBA3KkzD2GI=
github.com/openziti/x509-claims v1.0.3 h1:HNdQ8Nf1agB3lBs1gahcO6zfkeS4S5xoQ2/PkY4HRX0=
Expand Down
2 changes: 1 addition & 1 deletion zititest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/openziti/foundation/v2 v2.0.47
github.com/openziti/identity v1.0.81
github.com/openziti/sdk-golang v0.23.38
github.com/openziti/storage v0.2.45
github.com/openziti/storage v0.2.46-0.20240712192941-cc29b37bac57
github.com/openziti/transport/v2 v2.0.138
github.com/openziti/ziti v0.28.3
github.com/orcaman/concurrent-map/v2 v2.0.1
Expand Down
4 changes: 2 additions & 2 deletions zititest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ github.com/openziti/sdk-golang v0.23.38 h1:CqA+/YN5rBO5csjQef9Qs5pfrSxk5CqT7e1n8
github.com/openziti/sdk-golang v0.23.38/go.mod h1:ZRHgiWVOUpTk9LW2BuvIv8sQP3jPtBctGn9E1s5XoCQ=
github.com/openziti/secretstream v0.1.21 h1:r4xN8/CzSEvxZFFYGSztrlhMtIvk3B+SQcq2zgZ4Tb4=
github.com/openziti/secretstream v0.1.21/go.mod h1:1lfAnS8gBHsKZiPbRRK1sularbAsqizN6tWUEuZSfo0=
github.com/openziti/storage v0.2.45 h1:rCvQzWmGSmx6Ir2zUFO8H48klONGwZQfwFrfrEJuq9k=
github.com/openziti/storage v0.2.45/go.mod h1:v6KddQOjkQmKHnC7hIgFtcm4tdA8l8DbxYHQOIbRWa8=
github.com/openziti/storage v0.2.46-0.20240712192941-cc29b37bac57 h1:f5Y/XFVo9YqddlYjiRok/Wkqoxk422x29WdmEyVpIIk=
github.com/openziti/storage v0.2.46-0.20240712192941-cc29b37bac57/go.mod h1:v6KddQOjkQmKHnC7hIgFtcm4tdA8l8DbxYHQOIbRWa8=
github.com/openziti/transport/v2 v2.0.138 h1:F7TUv34BZ6x2BetYLtYbxSU/G15B+vkGRU4uPKwvRvU=
github.com/openziti/transport/v2 v2.0.138/go.mod h1:v0PN1dhFP48HeUUeBq9n/Ql2u5ln8EOtPBA3KkzD2GI=
github.com/openziti/x509-claims v1.0.3 h1:HNdQ8Nf1agB3lBs1gahcO6zfkeS4S5xoQ2/PkY4HRX0=
Expand Down

0 comments on commit 7a9e3cc

Please sign in to comment.