| 15 | import Zeze.Util.OutObject; |
| 16 | |
| 17 | public class ProxyServer extends Service { |
| 18 | public static final String eProxyServerName = "Zeze.Raft.ProxyServer"; |
| 19 | private final int rpcTimeout; |
| 20 | |
| 21 | public ProxyServer(Config config, int rpcTimeout) { |
| 22 | super(eProxyServerName, config); |
| 23 | this.rpcTimeout = rpcTimeout; |
| 24 | getSocketOptions().setOutputBufferMaxSize(100 * 1024 * 1024); |
| 25 | getSocketOptions().setInputBufferMaxProtocolSize(100 * 1024 * 1024); |
| 26 | RegisterProtocols(); |
| 27 | } |
| 28 | |
| 29 | public ProxyServer(Application zeze, int rpcTimeout) { |
| 30 | super(eProxyServerName, zeze); |
| 31 | this.rpcTimeout = rpcTimeout; |
| 32 | getSocketOptions().setOutputBufferMaxSize(100 * 1024 * 1024); |
| 33 | getSocketOptions().setInputBufferMaxProtocolSize(100 * 1024 * 1024); |
| 34 | RegisterProtocols(); |
| 35 | } |
| 36 | |
| 37 | private void RegisterProtocols() { |
| 38 | AddFactoryHandle(ProxyRequest.TypeId_, new ProtocolFactoryHandle<>( |
| 39 | ProxyRequest::new, |
| 40 | this::ProcessProxyRequest, |
| 41 | TransactionLevel.None |
| 42 | )); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * 把代理请求派发到指定的raft中执行。 |
| 47 | * |
| 48 | * @param r ProxyRequest |
| 49 | */ |
| 50 | private long ProcessProxyRequest(ProxyRequest r) throws Exception { |
| 51 | var raft = rafts.get(r.Argument.getRaftName()); |
| 52 | if (null == raft) { |
| 53 | logger.warn("ProxyRequest: not found raftName={}, available={}", r.Argument.getRaftName(), rafts.keySet()); |
| 54 | return Procedure.ProviderNotExist; |
| 55 | } |
| 56 | var server = raft.getServer(); |
| 57 | var outFactoryHandle = new OutObject<ProtocolFactoryHandle<?>>(); |
| 58 | var p = Protocol.decode(server::findProtocolFactoryHandle, ByteBuffer.Wrap(r.Argument.getRpcBinary()), outFactoryHandle); |
| 59 | if (null == p) |
| 60 | return Procedure.NotImplement; |
| 61 | var raftRpc = (RaftRpc<?, ?>)p; |
| 62 | raftRpc.setProxyRequest(r); |
| 63 | |
| 64 | // 下面的流程从Raft.Server.dispatchProtocol的部分代码拷贝出,请参考原来的地方,进行比较。。 |
| 65 | if (raft.isWorkingLeader()) { |
| 66 | if (raftRpc.getUnique().getRequestId() <= 0) { |
| 67 | p.SendResultCode(Procedure.ErrorRequestId); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | server.dispatchRaftRequest(p, |
| 72 | () -> server.processRequest(p, outFactoryHandle.value), |
| 73 | p.getClass().getName(), |
| 74 | () -> p.SendResultCode(Procedure.RaftRetry), |
nothing calls this directly
no outgoing calls
no test coverage detected