Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Fix route comparison check #472

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ func (r *RouteRegistry) register(uri route.Uri, endpoint *route.Endpoint) route.

endpointAdded := pool.Put(endpoint)
// Overwrites the load balancing algorithm of a pool by that of a specified endpoint, if that is valid.
pool.SetPoolLoadBalancingAlgorithm(endpoint)
r.SetTimeOfLastUpdate(t)

return endpointAdded
Expand Down
36 changes: 36 additions & 0 deletions registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,42 @@ var _ = Describe("RouteRegistry", func() {
})
})

Context("when the endpoint has a modified loadbalancing algorithm", func() {
It("overwrites the loadbalacing algorithm of the pool", func() {
opts := route.EndpointOpts{
Host: "192.168.1.1",
Port: 8080,
Tags: map[string]string{
"runtime": "ruby18",
"framework": "sinatra",
},
LoadBalancingAlgorithm: config.LOAD_BALANCE_LC,
}

initialEndpoint := route.NewEndpoint(&opts)
r.Register("example.com/foo", initialEndpoint)

p1 := r.Lookup("example.com/foo")
Expect(p1).NotTo(BeNil())
Expect(p1.LoadBalancingAlgorithm).To(Equal(config.LOAD_BALANCE_LC))
p1.Each(func(endpoint *route.Endpoint) {
Expect(endpoint.LoadBalancingAlgorithm).To(Equal(config.LOAD_BALANCE_LC))
})

opts.LoadBalancingAlgorithm = config.LOAD_BALANCE_RR
updatedEndpoint := route.NewEndpoint(&opts)

r.Register("example.com/foo", updatedEndpoint)

p2 := r.Lookup("example.com/foo")
Expect(p2).NotTo(BeNil())
Expect(p2.LoadBalancingAlgorithm).To(Equal(config.LOAD_BALANCE_RR))
p2.Each(func(endpoint *route.Endpoint) {
Expect(endpoint.LoadBalancingAlgorithm).To(Equal(config.LOAD_BALANCE_RR))
})
})
})

Context("when the endpoint has a zero UpdatedAt timestamp", func() {
BeforeEach(func() {
fooEndpoint.UpdatedAt = time.Time{}
Expand Down
12 changes: 6 additions & 6 deletions route/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func (e *Endpoint) Equal(e2 *Endpoint) bool {
return e.ApplicationId == e2.ApplicationId &&
e.addr == e2.addr &&
e.Protocol == e2.Protocol &&
maps.Equal(e.Tags, e2.Tags) &&
e.ServerCertDomainSAN == e2.ServerCertDomainSAN &&
e.PrivateInstanceId == e2.PrivateInstanceId &&
e.StaleThreshold == e2.StaleThreshold &&
Expand All @@ -131,7 +130,9 @@ func (e *Endpoint) Equal(e2 *Endpoint) bool {
e.ModificationTag == e2.ModificationTag &&
e.IsolationSegment == e2.IsolationSegment &&
e.useTls == e2.useTls &&
e.UpdatedAt == e2.UpdatedAt
e.UpdatedAt.Equal(e2.UpdatedAt) &&
e.LoadBalancingAlgorithm == e2.LoadBalancingAlgorithm &&
maps.Equal(e.Tags, e2.Tags)

}

Expand Down Expand Up @@ -314,6 +315,7 @@ func (p *EndpointPool) Put(endpoint *Endpoint) PoolPutResult {

}
p.RouteSvcUrl = e.endpoint.RouteServiceUrl
p.setPoolLoadBalancingAlgorithm(e.endpoint)
e.updated = time.Now()
// set the update time of the pool
p.Update()
Expand Down Expand Up @@ -516,10 +518,8 @@ func (p *EndpointPool) MarshalJSON() ([]byte, error) {
return json.Marshal(endpoints)
}

// SetPoolLoadBalancingAlgorithm overwrites the load balancing algorithm of a pool by that of a specified endpoint, if that is valid.
func (p *EndpointPool) SetPoolLoadBalancingAlgorithm(endpoint *Endpoint) {
p.Lock()
defer p.Unlock()
// setPoolLoadBalancingAlgorithm overwrites the load balancing algorithm of a pool by that of a specified endpoint, if that is valid.
func (p *EndpointPool) setPoolLoadBalancingAlgorithm(endpoint *Endpoint) {
if len(endpoint.LoadBalancingAlgorithm) > 0 && endpoint.LoadBalancingAlgorithm != p.LoadBalancingAlgorithm {
if config.IsLoadBalancingAlgorithmValid(endpoint.LoadBalancingAlgorithm) {
p.LoadBalancingAlgorithm = endpoint.LoadBalancingAlgorithm
Expand Down
35 changes: 31 additions & 4 deletions route/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ var _ = Describe("EndpointPool", func() {
RouteServiceUrl: "url",
LoadBalancingAlgorithm: expectedLBAlgo,
})
pool.SetPoolLoadBalancingAlgorithm(endpoint)
pool.Put(endpoint)
Expect(pool.LoadBalancingAlgorithm).To(Equal(expectedLBAlgo))
Eventually(logger).Should(gbytes.Say(`setting-pool-load-balancing-algorithm-to-that-of-an-endpoint`))
})
Expand All @@ -300,7 +300,7 @@ var _ = Describe("EndpointPool", func() {
Host: "host-1", Port: 1234,
RouteServiceUrl: "url",
})
pool.SetPoolLoadBalancingAlgorithm(endpoint)
pool.Put(endpoint)
Expect(pool.LoadBalancingAlgorithm).To(Equal(expectedLBAlgo))
})

Expand All @@ -314,7 +314,7 @@ var _ = Describe("EndpointPool", func() {
Host: "host-1", Port: 1234,
RouteServiceUrl: "url",
})
pool.SetPoolLoadBalancingAlgorithm(endpoint)
pool.Put(endpoint)
Expect(pool.LoadBalancingAlgorithm).To(Equal(expectedLBAlgo))
})

Expand All @@ -329,12 +329,39 @@ var _ = Describe("EndpointPool", func() {
RouteServiceUrl: "url",
LoadBalancingAlgorithm: "invalid-lb-algo",
})
pool.SetPoolLoadBalancingAlgorithm(endpoint)
pool.Put(endpoint)
Expect(pool.LoadBalancingAlgorithm).To(Equal(expectedLBAlgo))
Eventually(logger).Should(gbytes.Say(`invalid-endpoint-load-balancing-algorithm-provided-keeping-pool-lb-algo`))
})
})

Context("Load balancing algorithm of a updated endpoint", func() {
It("is will overwrite the load balancing algorithm of the endpoint and pool", func() {
pool := route.NewPool(&route.PoolOpts{
Logger: logger.Logger,
LoadBalancingAlgorithm: config.LOAD_BALANCE_RR,
})

endpointOpts := route.EndpointOpts{
Host: "host-1",
Port: 1234,
RouteServiceUrl: "url",
LoadBalancingAlgorithm: config.LOAD_BALANCE_LC,
}

initalEndpoint := route.NewEndpoint(&endpointOpts)

pool.Put(initalEndpoint)
Expect(pool.LoadBalancingAlgorithm).To(Equal(config.LOAD_BALANCE_LC))

endpointOpts.LoadBalancingAlgorithm = config.LOAD_BALANCE_RR
updatedEndpoint := route.NewEndpoint(&endpointOpts)

pool.Put(updatedEndpoint)
Expect(pool.LoadBalancingAlgorithm).To(Equal(config.LOAD_BALANCE_RR))
})
})

Context("RouteServiceUrl", func() {
It("returns the route_service_url associated with the pool", func() {
endpoint := &route.Endpoint{}
Expand Down