KLALB大更新

新负载均衡算法
This commit is contained in:
Administrator
2023-07-02 13:20:11 +08:00
parent f743b23e53
commit 585b7c5fe6
59 changed files with 1681 additions and 553 deletions
@@ -11,18 +11,24 @@ import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.Timer;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.function.Supplier;
import org.kne.cloud.network.mport.MultipurposeSocketAddress;
import org.kne.cloud.network.mport.NetworkService;
import org.kne.cloud.network.mport.ProxyProfileEntry;
import org.kne.cloud.network.mport.ThreadTool;
import javassist.ClassPool;
@@ -33,6 +39,29 @@ import javassist.bytecode.CodeAttribute;
import javassist.bytecode.CodeIterator;
public class KLALBController {
private LineManager lineManager;
private Supplier<String> selflineTableSupplier=()->{return null;};
public Supplier<String> getSelflineTableSupplier() {
return selflineTableSupplier;
}
public void setSelflineTableSupplier(Supplier<String> selflineTableSupplier) {
this.selflineTableSupplier = selflineTableSupplier;
}
public LineManager getLineManager() {
if(lineManager==null)
lineManager=createLineManager();
return lineManager;
}
protected LineManager createLineManager() {
return new LineManager(this);
}
private Inet6Address self;
@@ -70,6 +99,7 @@ public class KLALBController {
}
public void addRemoteSocket(KLALBRemoteSocket krs) {
krs.setController(this);
CountDownLatch cdl = new CountDownLatch(1);
krs.setPacketReceiver((rec) -> {
try {
@@ -139,6 +169,14 @@ public class KLALBController {
VADDRPacket var = (VADDRPacket) rec;
setLine(var.getVaddr(), krs);
cdl.countDown();
}else if(rec instanceof LINESPacket) {
LINESPacket lpt=(LINESPacket) rec;
String s=lpt.getLines();
Scanner scn=new Scanner(s);
while(scn.hasNext()) {
String sn=scn.nextLine();
getLineManager().addHostPort(new MultipurposeSocketAddress(sn));
}
}
} catch (NoRouteToHostException e) {
e.printStackTrace();
@@ -146,8 +184,12 @@ public class KLALBController {
});
krs.setCloseListener((x) -> {
removeLine(krs);
cdl.countDown();
});
krs.sendPacket(new VADDRPacket(self), 65537);
String selflineTable=selflineTableSupplier.get();
if(selflineTable!=null)
krs.sendPacket(new LINESPacket(selflineTable), 65537);
try {
cdl.await();
} catch (InterruptedException e) {
@@ -155,14 +197,14 @@ public class KLALBController {
}
}
public KLALBController() {
this.self = KLALBUtils.uuidToIP(UUID.randomUUID());
}
public KLALBController(Inet6Address self) {
this.self = self;
}
public KLALBController() {
this.self=KLALBUtils.uuidToIP(UUID.randomUUID());
}
private Map<Integer, KLALBVirtualSocketImpl> bindmap = new ConcurrentHashMap<>();
protected Map<Integer, KLALBVirtualSocketImpl> getBindmap() {
@@ -225,7 +267,8 @@ public class KLALBController {
int count0 = Math.min(count, l.size());
List<KLALBRemoteSocket> l2 = (List<KLALBRemoteSocket>) ((ArrayList<KLALBRemoteSocket>) l).clone();
LineDecitionComparator ldc = new LineDecitionComparator(l2, priority);
l2.sort(ldc);
Collections.sort(l2,ldc);
//System.out.println(l2);
for (Iterator<KLALBRemoteSocket> iterator = l2.iterator(); iterator.hasNext();) {
KLALBRemoteSocket krst = (KLALBRemoteSocket) iterator.next();
krst.sendPacket(packet, priority);
@@ -236,7 +279,16 @@ public class KLALBController {
}
}
protected void removeFromSend(Inet6Address addr,KLALBPacket klalbPacket) {
synchronized (routes) {
List<KLALBRemoteSocket> l = routes.get(addr);
if (l != null && !l.isEmpty()) {
l.forEach((x)->{
x.remoeFromSendQueue(klalbPacket);
});
}
}
}
protected boolean checkIsBind(KLALBVirtualSocketImpl klalbVirtualSocketImpl) {
return bindmap.containsValue(klalbVirtualSocketImpl);
}
@@ -245,4 +297,37 @@ private Timer t=new Timer("数据包发送计时器", true);
return t;
}
public void registerToProxyTypeAs(String proxyname) {
MultipurposeSocketAddress.getSocketFactoryRegister().put(proxyname, new KLALBVirtualSocketFactory(this));
MultipurposeSocketAddress.getServerSocketFactoryRegister().put(proxyname, new KLALBVirtualServerSocketFactory(this));
ProxyProfileEntry.getRegister().put(proxyname, new KLALBNetworkService());
}
private class KLALBNetworkService implements NetworkService{
@Override
public void listen(MultipurposeSocketAddress msa) {
// TODO 自动生成的方法存根
}
@Override
public void unlisten(MultipurposeSocketAddress msc) {
// TODO 自动生成的方法存根
}
@Override
public void connect(MultipurposeSocketAddress msa) {
getLineManager().addHostPort(msa);
}
@Override
public void unconnect(MultipurposeSocketAddress msc) {
getLineManager().removeHostPort(msc);
}
}
}