Ribbon load balancer and strategies

Load Balancer

AbstractloadBalancer 

AbstractLoadBalancer is an abstract interface to achieve ILoadBalancer

1: enumeration class defines a packet service instance ServerGroup on the abstract class, it comprises the following three different types: 

 ALL: all service instances STATUS_UP: normal service instance STATUS_NOT_UP: examples out of service

2: implements a chooseServer () function, implemented interface chooseServer (Object key) by calling the function, wherein the key parameter is null, the key to omit the selection of a particular service instance when the condition is determined.

3: abstract defines two functions:

     getServerList (ServerGroup serverGroup): defines the different service instances to obtain the list of packet types according to    

     getLoadBalancerStats (): defines the method for acquiring LoadBalancerStats object, LoadBalancerStats object is used for each service instance attributes and statistics of the current memory load balancer. This information is very useful, we can use this information to observe the operation of the load balancer, but this information is an important basis for the development of load balancing strategy.

 

 

BaseloadBalancer

BaseLoadBalancer and Ribbon class is the base class that implements load balancer, the definition of a lot of content on the basis of the relevant load balancer in the class. 

 1: define and maintain a list of two instances of the Server object storage service. For storing a list of all service instances, a list of instance storage for normal service

 2: Define the objects used to store LoadBalancerStats load balancer for each service instance properties and statistics we mentioned before

 3: definition of the service instance to check whether the object IPing normal service, the default is null, the need to inject it at construction embodied in BaseLoadBalancer

4: Check the definition of the service instance operation execution policy object IpingStrategy, in BaseLoadBalancer the default static inner classes SerialPingStrategy defined in the class implementation. According to the source, we can see that the strategy employs a linear traversal ping service instances manner inspection. The strategy to achieve when IPing speed is not ideal, or Server list is too large, it may affect system performance, this time the need to implement the interface and override IPingStrategy pingServers (IPing ping, Server [] servers) function to extend the ping execution policy.

5: Define the load balancing processing rules IRule objects from BaseLoadBalancer in chooseServer (Object key) to realize source, we can know, the load balancer actual service instance select the tasks entrusted to the IRule instance choose functions to achieve while. here, the default initialization object that implements RoundRob river Rule for the Rule of work. RoundRobinRule achieve the most basic and commonly used linear load balancing rules.

6: Start the ping task: the default constructor BaseLoadBalancer, the task will be started directly for a regular check Server is healthy. The default task execution interval of 10 seconds. ILoadBalancer implements load balancer interface definition should have the following series of basic operations

7: addServers (List newServers): add a new instance of the list of services to the load balancer, which will implement the already maintains a list of all service instances al1ServerList and new incoming service instance are added to the list of newServers newList, followed by calls for new port st setServersList function for processing, when implemented in BaseLoadBalancer new list will be used to cover the old list

8: chooseServer (Object key): the selection of a particular service instance

9: markServerDown (Server server): mark a service instance suspended.

10: getReachableServers (): Gets an instance of a list of services available. Since BaseLoadBalancer alone maintain a list of examples of a normal service, it can be returned directly.

11: getAllServers (): Gets a list of all the service instance. Since BaseLoadBalancer alone maintains a list of examples of all the services, so it can be returned directly.

 

 

DynamicServerlistloadBalancer 

DynamicServerListLoadBalancer class inherits from BaseLoadBalancer class, which is an extension of the base load balancer. In the load balancer, the realization of the service instances dynamically updated list of capabilities in runtime; the same time, it also has a list of examples of filtering service

1: ServerList (ServerList implementation class, DomainExtractingServerList)

It defines two abstract methods: getInitialListOfServers used to get service instance initialization list, and the list of examples used to obtain the updated service getUpdatedListOfServers

2: ServerListUpdater: updater service, implementation class:

PollingServerListUpdater: dynamic service list update default policy, that is, the default DynamicServer giant stLoadBalancer load balancer is the realization of it, it updated the list of services by way of regular tasks. · EurekaNotificationServerListUpdater: This update can also serve DynamicServerListLoadBalancer load balancer, but its trigger mechanism and PollingServer giant stUpdater different, it requires the use of Eureka event listeners to drive the update service list.

3:ServerListFilter

Mainly used for filtering the list of instances of the service, by passing the list of examples of services, service instances returns a list of filtered according to some rules. Implementing Classes:

         1: AbstractServerListFilter: This is an abstract filter, here an important basis for the definition of the object when the filter needs LoadBalancerStats

         2: ZoneAffinityServerListFilter: The filter implementation examples of filtering services based on the "perception area (Zone Affinity)" way, that is to say, it (Zone) and the regions in which consumers according to their own instances in which the area served (zone) compared to filter out those examples are not the same at one area. It is achieved by Iterables.filter (servers, this.zoneAffinityPredicate.getServerOnlyPredicate ()), which is determined based on the service instance implemented by a consumer Zone ZoneAffinityPredicate compared with the service list of examples filtration. And after filtration, here and not immediately return filtered results, but to determine whether or not to enable the "perception area" function by shouldEnableZoneAffnity function. It uses the getZoneSnapshot LoadBalancerStats methods to obtain these basic indicators of regional example of filtered (including the number of instances, the number of breaker, the number of active requests, examples of the average load, etc.), determined according to the following series of several algorithms evaluation values ​​and compared (the default value below) with the threshold set, if a condition is met, do not enable the service list of examples of "regional awareness" filtering. This algorithm is the emergence of regional clusters failure, can still rely on other instances of normal service area provides a complete high availability protection. 

                      blackOutServerPercentage: fault percentage Example (breaker number / number of instances)> = 0.8

                      activeReqeustsPerServer: Examples average load> = 0.6

                      availableServers: number of available instances (the number of instances - Number breaker) <2

          3: DefaultNIWSServerListFilter: The filter is fully inherited from ZoneAffnityServerListF Liao ter, is the default NIWS (Netflix Internal Web Service) filter.

           4: ServerListSubsetSusetFilter: The filter also inherited from ZoneAffinityServerListFilter, it is very suitable for systems with large-scale server clusters (hundreds or more). It can generate a list of a subset of a "sensing area" result, while it is possible to determine whether the health service to the failure by the number of concurrent connections and communication service Comparative Example selectively removed from the list of the service instance that relatively less examples of health.

Implement the filter is divided into the following three steps:

                         1: Get the "regional awareness" of the filter results, as the service instance candidate list

                         2: Centralized excluding those relatively healthy enough examples (These examples will also be excluded from the candidate list to prevent the third step, when they elected) from the current consumer maintenance service instance is not shown in the sub-health standards are as follows.

                              a. concurrent connection service instance exceeds the value client configuration defaults to 0 'configuration parameters ..ServerListSubsetFilter.eliminat ionConnectionThresold.

                               b. number of failures exceeds the value of the service instance client configuration defaults to 0 'configuration parameters ..ServerListSubsetFilter.eliminat ionFailureThresold.

 c If the service instance by a line with any of the above rules reject, reject ratio is less than the percentage of the default configuration of the client, default o .1 C 10%), configuration parameters .. ServerListSubsetFilter.forceEliminatePercent, then to the remaining examples of healthy sort the list, then removed from the examples of the most unhealthy, until the percentage reaches eliminate configuration.

                     3: After completion of culling, the list has been reduced at least 10% C Default) service instance, and finally selected from the candidate list by way of a random number of instances added to the list, a subset of service instances to keep the original equal to the number, and the default number of instances of a subset of 20

 

 

 

ZoneAwareloadBalancer 

ZoneAwareLoadBalancer load balancer is an extension of the DynamicServerListLoadBalancer. In DynamicServerListLoadBalancer, we can see that it did not override the function select chooseServer specific service instances, it still uses the algorithm implemented in BaseLoadBalancer. Use RoundRobinRule rule, in a linear way to select polling service instances invoked, the algorithm is simple and there is no concept area (Zone), so it would all instances regarded as nodes in a Zone to look at, this will cycle of produce cases across the region (zone) access, due to the inter-regional produce higher latency, these examples mainly to prevent regional failure to achieve high availability for the purpose of example and not as a routine visit, so the deployment of multi-regional clearing there will be some performance problems under the conditions, and the load balancer such problems can be avoided

 

 

 

Load balancing strategy

  AbstractloadBalancerRule 

           Load balancing strategy abstract class that defines the load balancer ILoadBalancer objects in the abstract class, the object can select the service in specific implementation strategy, to get some information on load balancer maintained as a basis for allocation, and this design Some character method to achieve efficient strategy for a specific scene

  Random Rule 

          The strategy to achieve the instance of selecting a service from the service list of examples of random functions. Its specific implementation is as follows, see choose IRule interface (Object key) function implemented in the class entrusted to choose (ILoadBalancer lb, Object key), which adds a load balancer parameter object. From the specific implementation point of view, it will use the incoming load balancer to get a list of available instances and all instances of upList return a list of all st, and to get a random number by rand.nextint (serverCount) function, and the random number as an index to return a value upList specific examples. At the same time, the specific selection logic in a while (server == null) cycle, the selection logic and according to achieve, under normal circumstances should be selected each time you select a service instance, getting less service instance if there is an infinite loop , it is likely to be complicated by the existence Bug.

  RoundRobinRule

          The strategy to achieve the polling in a linear way to select each service instance functions. Its specific implementation is as follows, and the detailed structure is very similar RandomRule. In addition to different cyclic conditions, is that of obtaining a so-called logic different from the available list. From cycling conditions, we can see an increase of a count count variable that will accumulate after each cycle, that is, if the server has been selected less than 10 times, it will try to end, and print a warning information No available alive servers after 10 tries from load balancer: linear polling is implemented by Atomicinteger nextServerCyclicCounter object implementation, each call to a function implemented by incrementing instance selected incrementAndGetModulo.

  RetryRule

          The strategy to achieve the instance selection function with a retry mechanism. We can see from the following implementation, it also defines a IRule inside the object, using the default RoundRobinRule instance. In the method choose strategy is to achieve a defined internal policy repeated attempts, if the period can be selected to a specific service instance is returned, if it is not selected according to the end to try to set a time threshold (maxRetryMillis parameters defined + timestamp value choose method to begin), and when it exceeds the threshold value after the return null.

  WeightedResponseTimeRule

         The strategy is an extension of RoundRobinRule, an increase is calculated based on the operation of the example of the weight, and to select examples according to the weight in order to achieve better distribution effect, its implementation there are three core elements:

1: regular tasks

      . Schedule (new DynarnicServerWeightTask (), 0, serverWeightTaskTimerinterval) WeightedResponseTimeRule start at the time of initialization policy serverWeightTimer by a timed task, for the weight calculation for each service instance, the default 30 seconds to perform a task.

2: weight calculation

      In the source code, we can easily find in thousands of stored weights of objects List <Double> accumulatedWeights = new ArrayList (), the location of the List of moderate value at which each weight corresponds to a list of examples of services load balancer maintenance of all instances in the list in position. Examples of calculation of weight maintenance function by maintainWeights

Implementation of the function is divided into two steps: 

The statistics for each instance LoadBalancerStats recorded, cumulative average response time for all instances, the average response time to obtain a total totalResponseTime, this value will be used for subsequent calculations. ·

Examples of calculating the weight-by-list is maintained by the load balancer weight average response time calculation rule weightSoFar + totalResponseTime example, wherein weightSoFar initialized to zero (starting with the first), and each calculate a weight accumulated need for reference to weightSoFar The next time calculated using

 3: Instance Selection

       Generating a [0, the maximum weight value) random numbers in the interval. 

Traversing the weight list, compare the size and weight values ​​of random numbers, such as if the weight is greater than one thousand random number, took the index value of the current weight of the list to serve instance list for specific instances. This is the service instance mentioned in the previous section will be based on the principles of the weight range selected, and the principle of opening and closing the border according to the weight range stumbling law, the normal range for each (x, y) of the form, but the first instance and the last instance of why the difference will it make? Since the minimum value of the random number may be O, the lower limit is the first example of a closed interval, while the maximum value of the random number is less than the weight value takes the maximum weight, the upper limit is the last instance of an open interval

ZoneAvoidanceRule

        ZoneAvoidanceRule in the realization of the time and did not like AvailabilityFilteringRule as a function of rewrite choose to optimize, it fully complies with the filter main logic of the parent class: "first filter list, and then select polling." Wherein the filter is a list of conditions in our above-mentioned filter condition based ZoneAvoidancePredicate, AvailabilityPredicate combination filter condition CompositePredicate secondary filter conditions, from the source segment CompositePredicate, we can see that it defines a main filter condition AbstractServerPredicate delegate and put through a set of secondary condition list list fallbacks, it is a list of secondary filter can have a plurality of, and because it uses a list filter condition storage times so that the results achieved in obtaining the filter functions getEligibleServers sequentially executed, it processing logic is as follows:

1: Example list of the used primary filter condition and return to filter all instances filtered

2: filter list views sequentially using filters condition the results of the main filter filtering condition

3: After each filtration (including primary and secondary filter conditions the filter conditions), the following two conditions are necessary to determine, as long as there is no longer in line with a filter, the result will be returned for the current polling linear selection algorithm:

      1: Example Total> filtered = minimum number of instances filter (minimalFilteredServers, default 1)

      2: Examples ratio> filtered minimum percentage filter (minimalFilteredPercentage, the default is 0)

Published 50 original articles · won praise 2 · Views 2314

Guess you like

Origin blog.csdn.net/eafun_888/article/details/96567812