mirror of
https://github.com/openziti/ziti.git
synced 2026-10-03 12:10:30 +00:00
8d11af3d97
* When routers change (connect/disconnect), we re-check the mesh. We are passing in the routers to the channel and they can build up, which is unnecessary. We only need to signal once for all the routers that change since the last time we checked, that the mesh needs to be checked. * We pass link changes through the run method, which is unnecessary since we pass it to a new goroutine. This inefficiency should be fixed. * We're evaluating all faulted links for rerouting, even if they were still pending. Only reroute connected links. * We're using time.After in each for loop, which can accumulate timers. Use a single Ticker instead.
170 lines
4.3 KiB
Go
170 lines
4.3 KiB
Go
/*
|
|
Copyright NetFoundry Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package network
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/openziti/ziti/controller/change"
|
|
"github.com/openziti/ziti/controller/models"
|
|
"github.com/openziti/ziti/controller/xt_smartrouting"
|
|
|
|
"github.com/openziti/transport/v2"
|
|
"github.com/openziti/transport/v2/tcp"
|
|
"github.com/openziti/ziti/controller/db"
|
|
)
|
|
|
|
func newTestEntityHelper(ctx *db.TestContext, network *Network) *testEntityHelper {
|
|
addr := "tcp:0.0.0.0:0"
|
|
transportAddr, err := tcp.AddressParser{}.Parse(addr)
|
|
ctx.NoError(err)
|
|
|
|
return &testEntityHelper{
|
|
ctx: ctx,
|
|
network: network,
|
|
transportAddr: transportAddr,
|
|
}
|
|
}
|
|
|
|
type testEntityHelper struct {
|
|
ctx *db.TestContext
|
|
network *Network
|
|
routerIdx int
|
|
serviceIdx int
|
|
terminatorIdx int
|
|
transportAddr transport.Address
|
|
}
|
|
|
|
func (self *testEntityHelper) addTestRouter() *Router {
|
|
router := newRouterForTest(fmt.Sprintf("router-%03d", self.routerIdx), "", self.transportAddr, nil, 0, false)
|
|
self.network.Routers.markConnected(router)
|
|
self.ctx.NoError(self.network.Routers.Create(router, change.New()))
|
|
self.routerIdx++
|
|
return router
|
|
}
|
|
|
|
func (self *testEntityHelper) addTestTerminator(serviceName string, routerName string, instanceId string, isSystem bool) *Terminator {
|
|
id := fmt.Sprintf("terminator-#%d", self.terminatorIdx)
|
|
term := &Terminator{
|
|
BaseEntity: models.BaseEntity{
|
|
Id: id,
|
|
IsSystem: isSystem,
|
|
},
|
|
Service: serviceName,
|
|
Router: routerName,
|
|
InstanceId: instanceId,
|
|
Address: "ToDo",
|
|
}
|
|
self.ctx.NoError(self.network.Terminators.Create(term, change.New()))
|
|
self.terminatorIdx++
|
|
return term
|
|
}
|
|
|
|
func (self *testEntityHelper) addTestService(serviceName string) *Service {
|
|
id := fmt.Sprintf("service-#%d", self.serviceIdx)
|
|
svc := &Service{
|
|
BaseEntity: models.BaseEntity{Id: id},
|
|
Name: serviceName,
|
|
TerminatorStrategy: xt_smartrouting.Name,
|
|
}
|
|
self.serviceIdx++
|
|
self.ctx.NoError(self.network.Services.Create(svc, change.New()))
|
|
return svc
|
|
}
|
|
|
|
func (self *testEntityHelper) discardControllerEvents() {
|
|
for {
|
|
select {
|
|
case <-self.network.assembleAndCleanC:
|
|
default:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// these debug methods can be used to dump routing evaluation steps to a file for easier analysis
|
|
|
|
//func initDebug(path string) {
|
|
// f, err := os.Create(path)
|
|
// if err != nil {
|
|
// panic(err)
|
|
// }
|
|
// dbg = &debugger{f: f}
|
|
//}
|
|
//
|
|
//func stopDebug() {
|
|
// if err := dbg.f.Close(); err != nil {
|
|
// panic(err)
|
|
// }
|
|
//}
|
|
//
|
|
//var dbg *debugger
|
|
//
|
|
//type debugger struct {
|
|
// f *os.File
|
|
// err error
|
|
//}
|
|
//
|
|
//func debugf(v string, args ...interface{}) {
|
|
// if dbg.err == nil {
|
|
// _, dbg.err = fmt.Fprintf(dbg.f, v, args...)
|
|
// }
|
|
//}
|
|
//
|
|
//func debugDumpDistance(dist map[*Router]int64) {
|
|
// var keys []*Router
|
|
// for k := range dist {
|
|
// keys = append(keys, k)
|
|
// }
|
|
// sort.Slice(keys, func(i, j int) bool {
|
|
// return keys[i].Id < keys[j].Id
|
|
// })
|
|
// for _, k := range keys {
|
|
// debugf(" ->%v = %v\n", k.Id, dist[k])
|
|
// }
|
|
//}
|
|
//
|
|
//func debugPath(p *Path) {
|
|
// nodes := p.Nodes
|
|
// debugf("[r/%v]", nodes[0].Id)
|
|
// if len(p.Links) > 0 {
|
|
// nodes = nodes[1:]
|
|
// for _, link := range p.Links {
|
|
// debugf(" -> [l/%v cost=%v] -> [r/%v]", link.Id, link.Cost, nodes[0].Id)
|
|
// }
|
|
// }
|
|
// debugf("\n")
|
|
//}
|
|
//
|
|
//func debugNetwork(n *Network) {
|
|
// routers := n.AllConnectedRouters()
|
|
// sort.Slice(routers, func(i, j int) bool {
|
|
// return routers[i].Id < routers[j].Id
|
|
// })
|
|
//
|
|
// for rIdx, router := range routers {
|
|
// debugf("%v router: %v\n", rIdx, router.Id)
|
|
// links := router.routerLinks.GetLinks()
|
|
// sort.Slice(links, func(i, j int) bool {
|
|
// return links[i].Id < links[j].Id
|
|
// })
|
|
// for lIdx, link := range links {
|
|
// debugf(" %v link %v for (%v -> %v) c: %v sc: %v sl:%v dl: %v\n",
|
|
// lIdx, link.Id, link.Src.Id, link.Dst.Id, link.GetCost(), link.StaticCost, link.SrcLatency, link.DstLatency)
|
|
// }
|
|
// }
|
|
//}
|