(NetClient client, RaftConfig raftConf)
| 345 | } |
| 346 | |
| 347 | private void init(NetClient client, RaftConfig raftConf) throws Exception { |
| 348 | if (raftConf == null) |
| 349 | raftConf = RaftConfig.load(); |
| 350 | |
| 351 | raftConfig = raftConf; |
| 352 | this.client = client; |
| 353 | |
| 354 | if (this.client.getConfig().acceptorCount() != 0) |
| 355 | throw new IllegalStateException("Acceptor Found!"); |
| 356 | if (this.client.getConfig().connectorCount() != 0) |
| 357 | throw new IllegalStateException("Connector Found!"); |
| 358 | |
| 359 | if (proxyAgent == null) { |
| 360 | // 没有启用代理,按原始raft方式建立连接器。 |
| 361 | for (var node : raftConfig.getNodes().values()) |
| 362 | this.client.getConfig().addConnector(new Connector(node.getHost(), node.getPort())); |
| 363 | } else { |
| 364 | proxyAgent.addAgent(this); |
| 365 | leader = proxyAgent.getLeader(raftConfig.getNodes().values().iterator().next()); |
| 366 | logger.info("proxy first leader {} {}_{}", |
| 367 | leader.getName(), leader.getConnector().getHostNameOrAddress(), leader.getConnector().getPort()); |
| 368 | } |
| 369 | |
| 370 | this.client.AddFactoryHandle(LeaderIs.TypeId_, new Service.ProtocolFactoryHandle<>( |
| 371 | LeaderIs::new, this::processLeaderIs, TransactionLevel.Serializable, DispatchMode.Critical)); |
| 372 | |
| 373 | this.client.AddFactoryHandle(GetLeader.TypeId_, new Service.ProtocolFactoryHandle<>( |
| 374 | GetLeader::new, null, TransactionLevel.None, DispatchMode.Normal)); |
| 375 | this.client.AddFactoryHandle(StartServerConnector.TypeId_, new Service.ProtocolFactoryHandle<>( |
| 376 | StartServerConnector::new, null, TransactionLevel.None, DispatchMode.Normal)); |
| 377 | this.client.AddFactoryHandle(StopServerConnector.TypeId_, new Service.ProtocolFactoryHandle<>( |
| 378 | StopServerConnector::new, null, TransactionLevel.None, DispatchMode.Normal)); |
| 379 | // ugly |
| 380 | resendTask = Task.scheduleUnsafe(1000, 1000, this::resend); |
| 381 | } |
| 382 | |
| 383 | private Connector getRandomConnector(Connector except) { |
| 384 | var notMe = new ArrayList<Connector>(client.getConfig().connectorCount()); |
no test coverage detected