Skip to content

Commit 7c44bfe

Browse files
committed
* Minor improvements and refactoring
1 parent 2d02a51 commit 7c44bfe

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

src/code.cloudfoundry.org/gorouter/route/hash_based.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ func (h *HashBased) Next(attempt int) *Endpoint {
5858
return endpoint
5959
}
6060

61+
if len(h.pool.endpoints) == 0 {
62+
h.logger.Warn("hash-based-routing-pool-empty", slog.String("host", h.pool.host))
63+
return nil
64+
}
65+
66+
endpoint = h.getSingleEndpoint()
67+
if endpoint != nil {
68+
h.lastEndpoint = endpoint
69+
return endpoint
70+
}
71+
6172
if h.pool.HashLookupTable == nil {
6273
h.logger.Error("hash-based-routing-failed", slog.String("host", h.pool.host), log.ErrAttr(errors.New("Lookup table is empty")))
6374
return nil
@@ -89,11 +100,6 @@ func (h *HashBased) Next(attempt int) *Endpoint {
89100
}
90101

91102
func (h *HashBased) findEndpoint(index uint64, attempt int) *Endpoint {
92-
maxIterations := len(h.pool.endpoints)
93-
if maxIterations == 0 {
94-
return nil
95-
}
96-
97103
// Ensure we don't exceed the lookup table size
98104
lookupTableSize := h.pool.HashLookupTable.GetLookupTableSize()
99105

@@ -128,10 +134,9 @@ func (h *HashBased) findEndpoint(index uint64, attempt int) *Endpoint {
128134

129135
lastEndpointPrivateId = id
130136

131-
e := endpointElem.endpoint
132-
if h.pool.HashRoutingProperties.BalanceFactor <= 0 || !h.isOverloaded(e) {
137+
if h.pool.HashRoutingProperties.BalanceFactor <= 0 || !h.isImbalancedOrOverloaded(endpointElem) {
133138
h.lastLookupTableIndex = currentIndex
134-
return e
139+
return endpointElem.endpoint
135140
}
136141

137142
currentIndex = (currentIndex + 1) % lookupTableSize
@@ -141,11 +146,12 @@ func (h *HashBased) findEndpoint(index uint64, attempt int) *Endpoint {
141146
return nil
142147
}
143148

144-
func (h *HashBased) isOverloaded(e *Endpoint) bool {
145-
avgLoad := h.CalculateAverageLoad()
149+
func (h *HashBased) isImbalancedOrOverloaded(e *endpointElem) bool {
150+
endpoint := e.endpoint
151+
avgNumberOfConnections := h.CalculateAverageNumberOfConnections()
146152
balanceFactor := h.pool.HashRoutingProperties.BalanceFactor
147-
if float64(e.Stats.NumberConnections.Count())/avgLoad > balanceFactor {
148-
h.logger.Info("hash-based-routing-endpoint-overloaded", slog.String("host", h.pool.host), slog.String("endpoint-id", e.PrivateInstanceId), slog.Int64("endpoint-connections", e.Stats.NumberConnections.Count()), slog.Float64("average-load", avgLoad))
153+
if avgNumberOfConnections == 0 || float64(endpoint.Stats.NumberConnections.Count())/avgNumberOfConnections > balanceFactor || e.isOverloaded() {
154+
h.logger.Debug("hash-based-routing-endpoint-overloaded", slog.String("host", h.pool.host), slog.String("endpoint-id", endpoint.PrivateInstanceId), slog.Int64("endpoint-connections", endpoint.Stats.NumberConnections.Count()), slog.Float64("average-load", avgNumberOfConnections))
149155
return true
150156
}
151157
return false
@@ -203,17 +209,29 @@ func (h *HashBased) PostRequest(e *Endpoint) {
203209
e.Stats.NumberConnections.Decrement()
204210
}
205211

206-
func (h *HashBased) CalculateAverageLoad() float64 {
212+
func (h *HashBased) CalculateAverageNumberOfConnections() float64 {
207213
if len(h.pool.endpoints) == 0 {
208214
return 0
209215
}
210216

211-
var currentInFlightRequestCount int64
217+
var totalConnections int64
212218
for _, endpointElem := range h.pool.endpoints {
213219
endpointElem.RLock()
214-
currentInFlightRequestCount += endpointElem.endpoint.Stats.NumberConnections.Count()
220+
totalConnections += endpointElem.endpoint.Stats.NumberConnections.Count()
215221
endpointElem.RUnlock()
216222
}
217223

218-
return float64(currentInFlightRequestCount) / float64(len(h.pool.endpoints))
224+
return float64(totalConnections) / float64(len(h.pool.endpoints))
225+
}
226+
227+
func (h *HashBased) getSingleEndpoint() *Endpoint {
228+
if len(h.pool.endpoints) == 1 {
229+
e := h.pool.endpoints[0]
230+
if e.isOverloaded() {
231+
return nil
232+
}
233+
234+
return e.endpoint
235+
}
236+
return nil
219237
}

src/code.cloudfoundry.org/gorouter/route/hash_based_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ var _ = Describe("HashBased", func() {
257257
Expect(endpoint.Stats.NumberConnections.Count()).To(Equal(initialCount - 1))
258258
})
259259
})
260-
Describe("CalculateAverageLoad", func() {
260+
Describe("CalculateAverageNumberOfConnections", func() {
261261
var iter *route.HashBased
262262
var endpoints []*route.Endpoint
263263

@@ -267,7 +267,7 @@ var _ = Describe("HashBased", func() {
267267

268268
Context("when there are no endpoints", func() {
269269
It("returns 0", func() {
270-
Expect(iter.CalculateAverageLoad()).To(Equal(float64(0)))
270+
Expect(iter.CalculateAverageNumberOfConnections()).To(Equal(float64(0)))
271271
})
272272
})
273273

@@ -277,7 +277,7 @@ var _ = Describe("HashBased", func() {
277277
pool.Put(route.NewEndpoint(&route.EndpointOpts{Host: "2.2.3.4", Port: 5678, LoadBalancingAlgorithm: "hash", PrivateInstanceId: "ID2"}))
278278
})
279279
It("returns 0", func() {
280-
Expect(iter.CalculateAverageLoad()).To(Equal(float64(0)))
280+
Expect(iter.CalculateAverageNumberOfConnections()).To(Equal(float64(0)))
281281
})
282282
})
283283

@@ -303,7 +303,7 @@ var _ = Describe("HashBased", func() {
303303
})
304304
It("returns the correct average", func() {
305305
// in general 12 in flight requests
306-
Expect(iter.CalculateAverageLoad()).To(Equal(float64(4)))
306+
Expect(iter.CalculateAverageNumberOfConnections()).To(Equal(float64(4)))
307307
})
308308
})
309309

@@ -321,7 +321,7 @@ var _ = Describe("HashBased", func() {
321321
}
322322
})
323323
It("returns the correct average", func() {
324-
Expect(iter.CalculateAverageLoad()).To(Equal(float64(5)))
324+
Expect(iter.CalculateAverageNumberOfConnections()).To(Equal(float64(5)))
325325
})
326326
})
327327
})

0 commit comments

Comments
 (0)