ready = make(chan struct{}) actualLRPRoutingInfo1 := &routing_table.ActualLRPRoutingInfo{ ActualLRP: actualLRPGroup1.Instance, Evacuating: false, } actualLRPRoutingInfo2 := &routing_table.ActualLRPRoutingInfo{ ActualLRP: actualLRPGroup2.Instance, Evacuating: false, } tempTable := routing_table.NewTempTable( routing_table.RoutesByRoutingKeyFromSchedulingInfos([]*models.DesiredLRPSchedulingInfo{schedulingInfo1, schedulingInfo2}), routing_table.EndpointsByRoutingKeyFromActuals([]*routing_table.ActualLRPRoutingInfo{ actualLRPRoutingInfo1, actualLRPRoutingInfo2, }), ) table := routing_table.NewTable() table.Swap(tempTable) watcherProcess = watcher.NewWatcher(bbsClient, clock, table, emitter, syncEvents, logger) bbsClient.DesiredLRPSchedulingInfosStub = func(f models.DesiredLRPFilter) ([]*models.DesiredLRPSchedulingInfo, error) { defer GinkgoRecover() ready <- struct{}{} Eventually(ready).Should(Receive()) return []*models.DesiredLRPSchedulingInfo{schedulingInfo1, schedulingInfo2}, nil
Describe("EndpointsByRoutingKeyFromActuals", func() { It("should build a map of endpoints, ignoring those without ports", func() { endpoints := routing_table.EndpointsByRoutingKeyFromActuals([]*routing_table.ActualLRPRoutingInfo{ { ActualLRP: &models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey("abc", 0, "domain"), ActualLRPNetInfo: models.NewActualLRPNetInfo("1.1.1.1", models.NewPortMapping(11, 44), models.NewPortMapping(66, 99)), }, }, { ActualLRP: &models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey("abc", 1, "domain"), ActualLRPNetInfo: models.NewActualLRPNetInfo("2.2.2.2", models.NewPortMapping(22, 44), models.NewPortMapping(88, 99)), }, }, { ActualLRP: &models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey("def", 0, "domain"), ActualLRPNetInfo: models.NewActualLRPNetInfo("3.3.3.3", models.NewPortMapping(33, 55)), }, }, { ActualLRP: &models.ActualLRP{ ActualLRPKey: models.NewActualLRPKey("def", 1, "domain"), ActualLRPNetInfo: models.NewActualLRPNetInfo("4.4.4.4", nil), }, }, }) Expect(endpoints).To(HaveLen(3)) Expect(endpoints[routing_table.RoutingKey{ProcessGuid: "abc", ContainerPort: 44}]).To(HaveLen(2))
func (watcher *Watcher) sync(logger lager.Logger, syncEndChan chan syncEndEvent) { endEvent := syncEndEvent{logger: logger} defer func() { syncEndChan <- endEvent }() before := watcher.clock.Now() var runningActualLRPs []*routing_table.ActualLRPRoutingInfo var getActualLRPsErr error var schedulingInfos []*models.DesiredLRPSchedulingInfo var getSchedulingInfosErr error wg := sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() logger.Debug("getting-actual-lrps") actualLRPGroups, err := watcher.bbsClient.ActualLRPGroups(models.ActualLRPFilter{}) if err != nil { logger.Error("failed-getting-actual-lrps", err) getActualLRPsErr = err return } logger.Debug("succeeded-getting-actual-lrps", lager.Data{"num-actual-responses": len(actualLRPGroups)}) runningActualLRPs = make([]*routing_table.ActualLRPRoutingInfo, 0, len(actualLRPGroups)) for _, actualLRPGroup := range actualLRPGroups { actualLRP, evacuating := actualLRPGroup.Resolve() if actualLRP.State == models.ActualLRPStateRunning { runningActualLRPs = append(runningActualLRPs, &routing_table.ActualLRPRoutingInfo{ ActualLRP: actualLRP, Evacuating: evacuating, }) } } }() wg.Add(1) go func() { var err error defer wg.Done() logger.Debug("getting-scheduling-infos") schedulingInfos, err = watcher.bbsClient.DesiredLRPSchedulingInfos(models.DesiredLRPFilter{}) if err != nil { logger.Error("failed-getting-desired-lrps", err) getSchedulingInfosErr = err return } logger.Debug("succeeded-getting-scheduling-infos", lager.Data{"num-desired-responses": len(schedulingInfos)}) }() wg.Wait() if getActualLRPsErr != nil || getSchedulingInfosErr != nil { return } newTable := routing_table.NewTempTable( routing_table.RoutesByRoutingKeyFromSchedulingInfos(schedulingInfos), routing_table.EndpointsByRoutingKeyFromActuals(runningActualLRPs), ) endEvent.table = newTable endEvent.callback = func(table routing_table.RoutingTable) { after := watcher.clock.Now() routeSyncDuration.Send(after.Sub(before)) } }