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())
	endpoint3 := routing_table.Endpoint{InstanceGuid: "ig-3", Host: "3.3.3.3", Port: 33, ContainerPort: 8080, Evacuating: false, ModificationTag: currentTag}

	evacuating1 := routing_table.Endpoint{InstanceGuid: "ig-1", Host: "1.1.1.1", Port: 11, ContainerPort: 8080, Evacuating: true, ModificationTag: currentTag}

	logGuid := "some-log-guid"

	BeforeEach(func() {
		table = routing_table.NewTable()
	})

	Describe("Swap", func() {
		Context("when a new routing key arrives", func() {
			Context("when the routing key has both routes and endpoints", func() {
				BeforeEach(func() {
					tempTable := routing_table.NewTempTable(
						routing_table.RoutesByRoutingKey{key: routing_table.Routes{Hostnames: []string{hostname1, hostname2}, LogGuid: logGuid}},
						routing_table.EndpointsByRoutingKey{key: {endpoint1, endpoint2}},
					)

					messagesToEmit = table.Swap(tempTable)
				})

				It("emits registrations for each pairing", func() {
					expected := routing_table.MessagesToEmit{
						RegistrationMessages: []routing_table.RegistryMessage{
							routing_table.RegistryMessageFor(endpoint1, routing_table.Routes{Hostnames: []string{hostname1, hostname2}, LogGuid: logGuid}),
							routing_table.RegistryMessageFor(endpoint2, routing_table.Routes{Hostnames: []string{hostname1, hostname2}, LogGuid: logGuid}),
						},
					}
					Expect(messagesToEmit).To(MatchMessagesToEmit(expected))
				})
			})
Esempio n. 3
0
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))
	}
}