国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java實現權重隨機算法詳解

瀏覽:7日期:2022-08-09 08:34:02
目錄應用場景本文目標算法詳解權重比例Java 實現參考應用場景

客戶端負載均衡,例如 Nacos 提供的客戶端負載均衡就是使用了該算法游戲抽獎(普通道具的權重很高,稀有道具的權重很低)

本文目標

Java 實現權重隨機算法

算法詳解

比如我們現在有三臺 Server,權重分別為1,3,2。現在想對三臺 Server 做負載均衡

Server1 Server2 Server3 weight weight weight 1 3 2權重比例

我們算出每臺 Server 的權重比例,權重比例 = 自己的權重 / 總權重

server1 server2 server3 weight weight weight 1 3 2 radio radio radio 1/6 3/6 2/6

根據權重比例計算覆蓋區域

server1 server2 server3 ^ ^^ |---------||---------|---------|---------||---------|---------|| 0 1/6 4/6 6/6 ^ ^ ^ 0.16666667 0.66666667 1.0

根據權重負載均衡

如步驟2所示,每個 server 都有自己的范圍,把每一個格子作為單位來看的話

server1 (0,1] server2 (1,4] server3 (4,6]

使用隨機數函數,取 (0,6] 之間的隨機數,根據隨機數落在哪個范圍決定如何選擇。例如隨機數為 2,處于 (1,4] 范圍,那么就選擇 server2。

思路大概就是這樣,落實到代碼上,用一個數組 [0.16666667, 0.66666667, 1] 來表示這三個 server 的覆蓋范圍,使用 ThreadLocalRandom 或者 Random 獲取 [0,1) 內的隨機數。然后使用二分查找法快速定位隨機數處于哪個區間

Java 實現

代碼基本上與 com.alibaba.nacos.client.naming.utils.Chooser 一致,在可讀性方面做了下優化。

import java.util.*;import java.util.concurrent.ThreadLocalRandom;import java.util.concurrent.atomic.AtomicInteger;public class WeightRandom<T> { private final List<T> items = new ArrayList<>(); private double[] weights; public WeightRandom(List<ItemWithWeight<T>> itemsWithWeight) {this.calWeights(itemsWithWeight); } /** * 計算權重,初始化或者重新定義權重時使用 * */ public void calWeights(List<ItemWithWeight<T>> itemsWithWeight) {items.clear();// 計算權重總和double originWeightSum = 0;for (ItemWithWeight<T> itemWithWeight : itemsWithWeight) { double weight = itemWithWeight.getWeight(); if (weight <= 0) {continue; } items.add(itemWithWeight.getItem()); if (Double.isInfinite(weight)) {weight = 10000.0D; } if (Double.isNaN(weight)) {weight = 1.0D; } originWeightSum += weight;}// 計算每個item的實際權重比例double[] actualWeightRatios = new double[items.size()];int index = 0;for (ItemWithWeight<T> itemWithWeight : itemsWithWeight) { double weight = itemWithWeight.getWeight(); if (weight <= 0) {continue; } actualWeightRatios[index++] = weight / originWeightSum;}// 計算每個item的權重范圍// 權重范圍起始位置weights = new double[items.size()];double weightRangeStartPos = 0;for (int i = 0; i < index; i++) { weights[i] = weightRangeStartPos + actualWeightRatios[i]; weightRangeStartPos += actualWeightRatios[i];} } /** * 基于權重隨機算法選擇 * */ public T choose() {double random = ThreadLocalRandom.current().nextDouble();int index = Arrays.binarySearch(weights, random);if (index < 0) { index = -index - 1;} else { return items.get(index);}if (index < weights.length && random < weights[index]) { return items.get(index);}// 通常不會走到這里,為了保證能得到正確的返回,這里隨便返回一個return items.get(0); } public static class ItemWithWeight<T> {T item;double weight;public ItemWithWeight() {}public ItemWithWeight(T item, double weight) { this.item = item; this.weight = weight;}public T getItem() { return item;}public void setItem(T item) { this.item = item;}public double getWeight() { return weight;}public void setWeight(double weight) { this.weight = weight;} } public static void main(String[] args) {// for testint sampleCount = 1_000_000;ItemWithWeight<String> server1 = new ItemWithWeight<>('server1', 1.0);ItemWithWeight<String> server2 = new ItemWithWeight<>('server2', 3.0);ItemWithWeight<String> server3 = new ItemWithWeight<>('server3', 2.0);WeightRandom<String> weightRandom = new WeightRandom<>(Arrays.asList(server1, server2, server3));// 統計 (這里用 AtomicInteger 僅僅是因為寫起來比較方便,這是一個單線程測試)Map<String, AtomicInteger> statistics = new HashMap<>();for (int i = 0; i < sampleCount; i++) { statistics .computeIfAbsent(weightRandom.choose(), (k) -> new AtomicInteger()) .incrementAndGet();}statistics.forEach((k, v) -> { double hit = (double) v.get() / sampleCount; System.out.println(k + ', hit:' + hit);}); }}

這里重點說一下 Arrays.binarySearch(weights, random),這個 API 我之前沒有用過導致我在讀 Nacos 源碼時,對這塊的操作十分費解

來看一下 java API 文檔對該方法返回值的解釋

Returns:index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

解釋下,首先該方法的作用是通過指定的 key 搜索數組。(前提條件是要保證數組的順序是從小到大排序過的)

如果數組中包含該 key,則返回對應的索引 如果不包含該 key,則返回該 key 的 (-(insertion point)-1)

insertion point(插入點):該 key 應該在數組的哪個位置。舉個例子,數組 [1,3,5],我的搜索 key 為 2,按照順序排的話 2 應該在數組的 index = 1 的位置,所以此時 insertion point = 1。

(這里 jdk 將能查到 key 和 查不到 key 兩種情況做了區分。為了將未找到的情況全部返回負數,所以做了 (-(insertion point)-1) 這樣的操作)

看到這,我們就懂了,insertion point 就是我們需要的,現在我們用小學數學來推導一下如何計算 insertion point

// 小學數學推導一下 insertion point 如何計算returnValue = (- (insertionPoint) - 1)insertionPoint = (- (returnValue + 1) )// 所以就有了上邊代碼中的if (index < 0) { index = -index - 1;}參考

https://github.com/alibaba/nacos/blob/develop/client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java

到此這篇關于Java實現權重隨機算法詳解的文章就介紹到這了,更多相關Java 權重隨機內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 美国一级毛片片aa久久综合 | 日韩一级在线播放免费观看 | 香港经典a毛片免费观看看 香港经典a毛片免费观看爽爽影院 | 亚洲天堂免费看 | 悠悠影院欧美日韩国产 | xxx免费视频 | 欧美精品午夜久久久伊人 | 欧美日韩一级片在线观看 | 久久亚洲精品永久网站 | 欧美日本亚洲国产一区二区 | 国产精品视频久 | 在线欧美精品一区二区三区 | 中文字幕一区二区小泽玛利亚 | 香港激情三级做爰小说 | 一级看片免费视频 | 日韩国产精品99久久久久久 | 一区二区三区在线视频观看 | 91精品全国免费观看 | 怡红院免费的全部视频国产a | 写真片福利视频在线播放 | 久久九九热视频 | 美女又黄又免费视频 | 日本三级毛片 | 欧美精品亚洲 | 国产一区在线看 | 国产日韩欧美在线一二三四 | 欧美三级美国一级 | 美女被躁免费视频软件 | 手机免费看a | 67194在线午夜亚洲 | 成人观看视频又黄又免费 | 精品久久久久久国产91 | 4438全国最大成人网视频 | 欧美视频自拍偷拍 | 久久精品视频一区二区三区 | 久久久久久久国产高清 | 亚洲免费在线视频播放 | 99精品网站 | 日韩久久精品 | 色毛片| 美女张开腿让我桶 |