roundrobin.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package userspace
  14. import (
  15. "errors"
  16. "fmt"
  17. "net"
  18. "reflect"
  19. "strconv"
  20. "sync"
  21. "time"
  22. "github.com/golang/glog"
  23. "k8s.io/kubernetes/pkg/api"
  24. "k8s.io/kubernetes/pkg/proxy"
  25. "k8s.io/kubernetes/pkg/types"
  26. "k8s.io/kubernetes/pkg/util/slice"
  27. )
  28. var (
  29. ErrMissingServiceEntry = errors.New("missing service entry")
  30. ErrMissingEndpoints = errors.New("missing endpoints")
  31. )
  32. type affinityState struct {
  33. clientIP string
  34. //clientProtocol api.Protocol //not yet used
  35. //sessionCookie string //not yet used
  36. endpoint string
  37. lastUsed time.Time
  38. }
  39. type affinityPolicy struct {
  40. affinityType api.ServiceAffinity
  41. affinityMap map[string]*affinityState // map client IP -> affinity info
  42. ttlMinutes int
  43. }
  44. // LoadBalancerRR is a round-robin load balancer.
  45. type LoadBalancerRR struct {
  46. lock sync.RWMutex
  47. services map[proxy.ServicePortName]*balancerState
  48. }
  49. // Ensure this implements LoadBalancer.
  50. var _ LoadBalancer = &LoadBalancerRR{}
  51. type balancerState struct {
  52. endpoints []string // a list of "ip:port" style strings
  53. index int // current index into endpoints
  54. affinity affinityPolicy
  55. }
  56. func newAffinityPolicy(affinityType api.ServiceAffinity, ttlMinutes int) *affinityPolicy {
  57. return &affinityPolicy{
  58. affinityType: affinityType,
  59. affinityMap: make(map[string]*affinityState),
  60. ttlMinutes: ttlMinutes,
  61. }
  62. }
  63. // NewLoadBalancerRR returns a new LoadBalancerRR.
  64. func NewLoadBalancerRR() *LoadBalancerRR {
  65. return &LoadBalancerRR{
  66. services: map[proxy.ServicePortName]*balancerState{},
  67. }
  68. }
  69. func (lb *LoadBalancerRR) NewService(svcPort proxy.ServicePortName, affinityType api.ServiceAffinity, ttlMinutes int) error {
  70. glog.V(4).Infof("LoadBalancerRR NewService %q", svcPort)
  71. lb.lock.Lock()
  72. defer lb.lock.Unlock()
  73. lb.newServiceInternal(svcPort, affinityType, ttlMinutes)
  74. return nil
  75. }
  76. // This assumes that lb.lock is already held.
  77. func (lb *LoadBalancerRR) newServiceInternal(svcPort proxy.ServicePortName, affinityType api.ServiceAffinity, ttlMinutes int) *balancerState {
  78. if ttlMinutes == 0 {
  79. ttlMinutes = 180 //default to 3 hours if not specified. Should 0 be unlimited instead????
  80. }
  81. if _, exists := lb.services[svcPort]; !exists {
  82. lb.services[svcPort] = &balancerState{affinity: *newAffinityPolicy(affinityType, ttlMinutes)}
  83. glog.V(4).Infof("LoadBalancerRR service %q did not exist, created", svcPort)
  84. } else if affinityType != "" {
  85. lb.services[svcPort].affinity.affinityType = affinityType
  86. }
  87. return lb.services[svcPort]
  88. }
  89. func (lb *LoadBalancerRR) DeleteService(svcPort proxy.ServicePortName) {
  90. glog.V(4).Infof("LoadBalancerRR DeleteService %q", svcPort)
  91. lb.lock.Lock()
  92. defer lb.lock.Unlock()
  93. delete(lb.services, svcPort)
  94. }
  95. // return true if this service is using some form of session affinity.
  96. func isSessionAffinity(affinity *affinityPolicy) bool {
  97. // Should never be empty string, but checking for it to be safe.
  98. if affinity.affinityType == "" || affinity.affinityType == api.ServiceAffinityNone {
  99. return false
  100. }
  101. return true
  102. }
  103. // NextEndpoint returns a service endpoint.
  104. // The service endpoint is chosen using the round-robin algorithm.
  105. func (lb *LoadBalancerRR) NextEndpoint(svcPort proxy.ServicePortName, srcAddr net.Addr, sessionAffinityReset bool) (string, error) {
  106. // Coarse locking is simple. We can get more fine-grained if/when we
  107. // can prove it matters.
  108. lb.lock.Lock()
  109. defer lb.lock.Unlock()
  110. state, exists := lb.services[svcPort]
  111. if !exists || state == nil {
  112. return "", ErrMissingServiceEntry
  113. }
  114. if len(state.endpoints) == 0 {
  115. return "", ErrMissingEndpoints
  116. }
  117. glog.V(4).Infof("NextEndpoint for service %q, srcAddr=%v: endpoints: %+v", svcPort, srcAddr, state.endpoints)
  118. sessionAffinityEnabled := isSessionAffinity(&state.affinity)
  119. var ipaddr string
  120. if sessionAffinityEnabled {
  121. // Caution: don't shadow ipaddr
  122. var err error
  123. ipaddr, _, err = net.SplitHostPort(srcAddr.String())
  124. if err != nil {
  125. return "", fmt.Errorf("malformed source address %q: %v", srcAddr.String(), err)
  126. }
  127. if !sessionAffinityReset {
  128. sessionAffinity, exists := state.affinity.affinityMap[ipaddr]
  129. if exists && int(time.Now().Sub(sessionAffinity.lastUsed).Minutes()) < state.affinity.ttlMinutes {
  130. // Affinity wins.
  131. endpoint := sessionAffinity.endpoint
  132. sessionAffinity.lastUsed = time.Now()
  133. glog.V(4).Infof("NextEndpoint for service %q from IP %s with sessionAffinity %#v: %s", svcPort, ipaddr, sessionAffinity, endpoint)
  134. return endpoint, nil
  135. }
  136. }
  137. }
  138. // Take the next endpoint.
  139. endpoint := state.endpoints[state.index]
  140. state.index = (state.index + 1) % len(state.endpoints)
  141. if sessionAffinityEnabled {
  142. var affinity *affinityState
  143. affinity = state.affinity.affinityMap[ipaddr]
  144. if affinity == nil {
  145. affinity = new(affinityState) //&affinityState{ipaddr, "TCP", "", endpoint, time.Now()}
  146. state.affinity.affinityMap[ipaddr] = affinity
  147. }
  148. affinity.lastUsed = time.Now()
  149. affinity.endpoint = endpoint
  150. affinity.clientIP = ipaddr
  151. glog.V(4).Infof("Updated affinity key %s: %#v", ipaddr, state.affinity.affinityMap[ipaddr])
  152. }
  153. return endpoint, nil
  154. }
  155. type hostPortPair struct {
  156. host string
  157. port int
  158. }
  159. func isValidEndpoint(hpp *hostPortPair) bool {
  160. return hpp.host != "" && hpp.port > 0
  161. }
  162. func flattenValidEndpoints(endpoints []hostPortPair) []string {
  163. // Convert Endpoint objects into strings for easier use later. Ignore
  164. // the protocol field - we'll get that from the Service objects.
  165. var result []string
  166. for i := range endpoints {
  167. hpp := &endpoints[i]
  168. if isValidEndpoint(hpp) {
  169. result = append(result, net.JoinHostPort(hpp.host, strconv.Itoa(hpp.port)))
  170. }
  171. }
  172. return result
  173. }
  174. // Remove any session affinity records associated to a particular endpoint (for example when a pod goes down).
  175. func removeSessionAffinityByEndpoint(state *balancerState, svcPort proxy.ServicePortName, endpoint string) {
  176. for _, affinity := range state.affinity.affinityMap {
  177. if affinity.endpoint == endpoint {
  178. glog.V(4).Infof("Removing client: %s from affinityMap for service %q", affinity.endpoint, svcPort)
  179. delete(state.affinity.affinityMap, affinity.clientIP)
  180. }
  181. }
  182. }
  183. // Loop through the valid endpoints and then the endpoints associated with the Load Balancer.
  184. // Then remove any session affinity records that are not in both lists.
  185. // This assumes the lb.lock is held.
  186. func (lb *LoadBalancerRR) updateAffinityMap(svcPort proxy.ServicePortName, newEndpoints []string) {
  187. allEndpoints := map[string]int{}
  188. for _, newEndpoint := range newEndpoints {
  189. allEndpoints[newEndpoint] = 1
  190. }
  191. state, exists := lb.services[svcPort]
  192. if !exists {
  193. return
  194. }
  195. for _, existingEndpoint := range state.endpoints {
  196. allEndpoints[existingEndpoint] = allEndpoints[existingEndpoint] + 1
  197. }
  198. for mKey, mVal := range allEndpoints {
  199. if mVal == 1 {
  200. glog.V(2).Infof("Delete endpoint %s for service %q", mKey, svcPort)
  201. removeSessionAffinityByEndpoint(state, svcPort, mKey)
  202. }
  203. }
  204. }
  205. // OnEndpointsUpdate manages the registered service endpoints.
  206. // Registered endpoints are updated if found in the update set or
  207. // unregistered if missing from the update set.
  208. func (lb *LoadBalancerRR) OnEndpointsUpdate(allEndpoints []api.Endpoints) {
  209. registeredEndpoints := make(map[proxy.ServicePortName]bool)
  210. lb.lock.Lock()
  211. defer lb.lock.Unlock()
  212. // Update endpoints for services.
  213. for i := range allEndpoints {
  214. svcEndpoints := &allEndpoints[i]
  215. // We need to build a map of portname -> all ip:ports for that
  216. // portname. Explode Endpoints.Subsets[*] into this structure.
  217. portsToEndpoints := map[string][]hostPortPair{}
  218. for i := range svcEndpoints.Subsets {
  219. ss := &svcEndpoints.Subsets[i]
  220. for i := range ss.Ports {
  221. port := &ss.Ports[i]
  222. for i := range ss.Addresses {
  223. addr := &ss.Addresses[i]
  224. portsToEndpoints[port.Name] = append(portsToEndpoints[port.Name], hostPortPair{addr.IP, int(port.Port)})
  225. // Ignore the protocol field - we'll get that from the Service objects.
  226. }
  227. }
  228. }
  229. for portname := range portsToEndpoints {
  230. svcPort := proxy.ServicePortName{NamespacedName: types.NamespacedName{Namespace: svcEndpoints.Namespace, Name: svcEndpoints.Name}, Port: portname}
  231. state, exists := lb.services[svcPort]
  232. curEndpoints := []string{}
  233. if state != nil {
  234. curEndpoints = state.endpoints
  235. }
  236. newEndpoints := flattenValidEndpoints(portsToEndpoints[portname])
  237. if !exists || state == nil || len(curEndpoints) != len(newEndpoints) || !slicesEquiv(slice.CopyStrings(curEndpoints), newEndpoints) {
  238. glog.V(1).Infof("LoadBalancerRR: Setting endpoints for %s to %+v", svcPort, newEndpoints)
  239. lb.updateAffinityMap(svcPort, newEndpoints)
  240. // OnEndpointsUpdate can be called without NewService being called externally.
  241. // To be safe we will call it here. A new service will only be created
  242. // if one does not already exist. The affinity will be updated
  243. // later, once NewService is called.
  244. state = lb.newServiceInternal(svcPort, api.ServiceAffinity(""), 0)
  245. state.endpoints = slice.ShuffleStrings(newEndpoints)
  246. // Reset the round-robin index.
  247. state.index = 0
  248. }
  249. registeredEndpoints[svcPort] = true
  250. }
  251. }
  252. // Remove endpoints missing from the update.
  253. for k := range lb.services {
  254. if _, exists := registeredEndpoints[k]; !exists {
  255. glog.V(2).Infof("LoadBalancerRR: Removing endpoints for %s", k)
  256. // Reset but don't delete.
  257. state := lb.services[k]
  258. state.endpoints = []string{}
  259. state.index = 0
  260. state.affinity.affinityMap = map[string]*affinityState{}
  261. }
  262. }
  263. }
  264. // Tests whether two slices are equivalent. This sorts both slices in-place.
  265. func slicesEquiv(lhs, rhs []string) bool {
  266. if len(lhs) != len(rhs) {
  267. return false
  268. }
  269. if reflect.DeepEqual(slice.SortStrings(lhs), slice.SortStrings(rhs)) {
  270. return true
  271. }
  272. return false
  273. }
  274. func (lb *LoadBalancerRR) CleanupStaleStickySessions(svcPort proxy.ServicePortName) {
  275. lb.lock.Lock()
  276. defer lb.lock.Unlock()
  277. state, exists := lb.services[svcPort]
  278. if !exists {
  279. return
  280. }
  281. for ip, affinity := range state.affinity.affinityMap {
  282. if int(time.Now().Sub(affinity.lastUsed).Minutes()) >= state.affinity.ttlMinutes {
  283. glog.V(4).Infof("Removing client %s from affinityMap for service %q", affinity.clientIP, svcPort)
  284. delete(state.affinity.affinityMap, ip)
  285. }
  286. }
  287. }