Skip to content

Commit

Permalink
Merge pull request #2404 from openziti/xt-changes
Browse files Browse the repository at this point in the history
Refactor XT. Add concurrency test. Add inspect support. Fixes #2403
  • Loading branch information
plorenz authored Sep 16, 2024
2 parents e5ddb7c + ac83d93 commit 68d2c3c
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 224 deletions.
11 changes: 11 additions & 0 deletions common/inspect/terminator_inspections.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@

package inspect

type TerminatorCostDetails struct {
Terminators []*TerminatorCostDetail `json:"terminators"`
}

type TerminatorCostDetail struct {
TerminatorId string `json:"terminatorId"`
CircuitCount uint32 `json:"circuitCount"`
FailureCost uint32 `json:"failureCost"`
CurrentCost uint32 `json:"currentCost"`
}

type SdkTerminatorInspectResult struct {
Entries []*SdkTerminatorInspectDetail `json:"entries"`
Errors []string `json:"errors"`
Expand Down
11 changes: 11 additions & 0 deletions controller/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,17 @@ func (network *Network) Inspect(name string) (*string, error) {
}
resultStr := string(result)
return &resultStr, nil
} else if strings.HasPrefix(lc, "terminator-costs") {
state := inspect.TerminatorCostDetails{}
xt.GlobalCosts().IterCosts(func(terminatorId string, cost xt.Cost) {
state.Terminators = append(state.Terminators, cost.Inspect(terminatorId))
})
result, err := json.Marshal(state)
if err != nil {
return nil, fmt.Errorf("failed to marshall terminator cost state to json (%w)", err)
}
resultStr := string(result)
return &resultStr, nil
} else {
for _, inspectTarget := range network.inspectionTargets.Value() {
if handled, val, err := inspectTarget(lc); handled {
Expand Down
35 changes: 21 additions & 14 deletions controller/xt/costs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package xt

import (
"github.com/openziti/ziti/common/inspect"
"math"

cmap "github.com/orcaman/concurrent-map/v2"
Expand All @@ -30,7 +31,12 @@ const (
)

var globalCosts = &costs{
costMap: cmap.New[uint16](),
costMap: cmap.New[Cost](),
}

type Cost interface {
Get() uint16
Inspect(terminatorId string) *inspect.TerminatorCostDetail
}

func GlobalCosts() Costs {
Expand Down Expand Up @@ -126,32 +132,33 @@ func GetPrecedenceForName(name string) Precedence {
}

type costs struct {
costMap cmap.ConcurrentMap[string, uint16]
costMap cmap.ConcurrentMap[string, Cost]
}

func (self *costs) ClearCost(terminatorId string) {
self.costMap.Remove(terminatorId)
}

func (self *costs) SetDynamicCost(terminatorId string, cost uint16) {
self.costMap.Set(terminatorId, cost)
func (self *costs) SetDynamicCost(terminatorId string, c Cost) {
self.costMap.Set(terminatorId, c)
}

func (self *costs) UpdateDynamicCost(terminatorId string, updateF func(uint16) uint16) {
self.costMap.Upsert(terminatorId, 0, func(exist bool, valueInMap uint16, newValue uint16) uint16 {
if !exist {
return updateF(0)
}

return updateF(valueInMap)
})
func (self *costs) GetDynamicCost(terminatorId string) uint16 {
if cost, found := self.costMap.Get(terminatorId); found {
return cost.Get()
}
return 0
}

func (self *costs) GetDynamicCost(terminatorId string) uint16 {
func (self *costs) GetCost(terminatorId string) Cost {
if cost, found := self.costMap.Get(terminatorId); found {
return cost
}
return 0
return nil
}

func (self *costs) IterCosts(f func(string, Cost)) {
self.costMap.IterCb(f)
}

// In a list which is sorted by precedence, returns the terminators which have the
Expand Down
110 changes: 0 additions & 110 deletions controller/xt/failure.go

This file was deleted.

17 changes: 3 additions & 14 deletions controller/xt/xt.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,10 @@ type EventVisitor interface {
VisitCircuitRemoved(event TerminatorEvent)
}

type Stats interface {
GetCost() uint32
GetPrecedence() Precedence
}

type Costs interface {
ClearCost(terminatorId string)
SetDynamicCost(terminatorId string, weight uint16)
UpdateDynamicCost(terminatorId string, updateF func(uint16) uint16)
SetDynamicCost(terminatorId string, c Cost)
GetDynamicCost(terminatorId string) uint16
}

type FailureCosts interface {
Failure(terminatorId string) uint16
Success(terminatorId string) uint16
Clear(terminatorId string)
CreditOverTime(credit uint8, period time.Duration) *time.Ticker
GetCost(terminatorId string) Cost
IterCosts(func(terminatorId string, cost Cost))
}
Loading

0 comments on commit 68d2c3c

Please sign in to comment.