1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| package edu.algorithm.loadbalance;
import edu.algorithm.entity.Invocation; import edu.algorithm.entity.RpcStatus; import edu.algorithm.entity.Server; import org.apache.dubbo.common.constants.CommonConstants; import org.apache.dubbo.common.io.Bytes;
import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong;
public class ConsistentHashLoadBalance {
private final ConcurrentMap<String, ConsistentHashSelector<?>> selectors = new ConcurrentHashMap();
public ConsistentHashLoadBalance() { }
protected Server doSelect(List<Server> serverList,Invocation invocation) { String methodName = "edu.example.method"; String key = serverList.get(0).getIp() + "." + methodName;
int invokersHashCode = this.getCorrespondingHashCode(serverList); ConsistentHashSelector<?> selector = (ConsistentHashSelector)this.selectors.get(key); if (selector == null || selector.identityHashCode != invokersHashCode) { this.selectors.put(key, new ConsistentHashSelector(serverList, methodName, invokersHashCode)); selector = (ConsistentHashSelector)this.selectors.get(key); } return selector.select(invocation); }
public <T> int getCorrespondingHashCode(List<Server> serverList) { return serverList.hashCode(); }
private static final class ConsistentHashSelector<T> {
private final TreeMap<Long, Server> virtualInvokers = new TreeMap();
private final int replicaNumber = 160;
private final int identityHashCode;
private final int[] argumentIndex;
private Map<String, AtomicLong> serverRequestCountMap = new ConcurrentHashMap();
private AtomicLong totalRequestCount;
private int serverCount;
private static final double OVERLOAD_RATIO_THREAD = 1.5;
ConsistentHashSelector(List<Server> serverList, String methodName, int identityHashCode) { this.identityHashCode = identityHashCode;
String[] index = new String[]{"0"};
this.argumentIndex = new int[index.length];
for(int i = 0; i < index.length; ++i) { this.argumentIndex[i] = Integer.parseInt(index[i]); }
Iterator var14 = serverList.iterator(); while(var14.hasNext()) { Server server = (Server) var14.next(); String address = server.getIp() +":"+ server.getPort();
for(int i = 0; i < this.replicaNumber / 4; ++i) { byte[] digest = Bytes.getMD5(address + i);
for(int h = 0; h < 4; ++h) { long m = this.hash(digest, h); this.virtualInvokers.put(m, server); } } }
this.totalRequestCount = new AtomicLong(0L); this.serverCount = serverList.size(); this.serverRequestCountMap.clear(); }
public Server select(Invocation invocation) { String key = this.toKey(invocation.getArguments()); byte[] digest = Bytes.getMD5(key); return this.selectForKey(this.hash(digest, 0)); } private String toKey(Object[] args) { StringBuilder buf = new StringBuilder(); int[] var3 = this.argumentIndex; int var4 = var3.length;
for(int var5 = 0; var5 < var4; ++var5) { int i = var3[var5]; if (i >= 0 && i < args.length) { buf.append(args[i]); } }
return buf.toString(); }
private Server selectForKey(long hash) { Map.Entry<Long, Server> entry = this.virtualInvokers.ceilingEntry(hash); if (entry == null) { entry = this.virtualInvokers.firstEntry(); } String serverAddress = ((Server)entry.getValue()).getIp() +":"+ entry.getValue().getPort(); for(double overloadThread = (double)this.totalRequestCount.get() / (double)this.serverCount * 1.5; this.serverRequestCountMap.containsKey(serverAddress) && (double)((AtomicLong)this.serverRequestCountMap.get(serverAddress)).get() >= overloadThread; serverAddress = ((Server)entry.getValue()).getIp() +":"+ entry.getValue().getPort()) {
entry = this.getNextInvokerNode(this.virtualInvokers, entry); }
if (!this.serverRequestCountMap.containsKey(serverAddress)) { this.serverRequestCountMap.put(serverAddress, new AtomicLong(1L)); } else { ((AtomicLong)this.serverRequestCountMap.get(serverAddress)).incrementAndGet(); }
this.totalRequestCount.incrementAndGet(); return entry.getValue(); }
private Map.Entry<Long, Server> getNextInvokerNode(TreeMap<Long, Server> virtualInvokers, Map.Entry<Long, Server> entry) { Map.Entry<Long, Server> nextEntry = virtualInvokers.higherEntry(entry.getKey()); return nextEntry == null ? virtualInvokers.firstEntry() : nextEntry; }
private long hash(byte[] digest, int number) { return ((long)(digest[3 + number * 4] & 255) << 24 | (long)(digest[2 + number * 4] & 255) << 16 | (long)(digest[1 + number * 4] & 255) << 8 | (long)(digest[number * 4] & 255)) & 4294967295L; } }
public static void main(String[] args) { List<Server> serverList = new ArrayList<>(); serverList.add(new Server(1,"服务器1","8080","127.0.0.1",0,0,0,"")); serverList.add(new Server(2,"服务器2","8090","127.0.0.2",0,0,0,"")); serverList.add(new Server(3,"服务器3","8088","127.0.0.3",0,0,0,"")); serverList.add(new Server(4,"服务器4","8099","127.0.0.4",0,0,0,"")); serverList.add(new Server(5,"服务器5","8070","127.0.0.5",0,0,0,"")); serverList.add(new Server(6,"服务器6","8060","127.0.0.6",0,0,0,"")); serverList.add(new Server(7,"服务器7","8050","127.0.0.7",0,0,0,""));
ConsistentHashLoadBalance hashLoadBalance = new ConsistentHashLoadBalance(); Invocation invocation = new Invocation(); invocation.setArguments(new String[]{"loadbalance"}); ConcurrentMap<String, ConsistentHashSelector<?>> selectors = null; for (int i = 0; i < 10; i++) { invocation = new Invocation(); invocation.setArguments(new String[]{""+i}); System.out.println(hashLoadBalance.doSelect(serverList,invocation).toString()); selectors = hashLoadBalance.selectors; } System.out.println(selectors.toString()); } }
|