1

我正在尝试为分布式系统项目实施 Raft 共识算法。

我需要一些非常快速的方法来了解服务器 A 是否可以从服务器 B 访问并且 A 的分布式系统是否已启动。换句话说,可能会发生 B 可以访问 A 但 A 的云系统尚未启动的情况。所以我认为这InetAddress.getByName(ip).isReachable(timeout);还不够。

由于每个服务器的stub都被重命名为服务器的名字,所以我想先获取服务器的注册表,然后检查是否存在与服务器同名的stub:如果没有,则跳到下一个服务器,否则执行lookup(这可能需要很长时间)。这是代码的一部分:

try {
    System.out.println("Getting "+clusterElement.getId()+"'s registry");
    Registry registry = LocateRegistry.getRegistry(clusterElement.getAddress());
    System.out.println("Checking contains:");
    if(!Arrays.asList(registry.list()).contains(clusterElement.getId())) {
        System.out.println("Server "+clusterElement.getId()+" not bound (maybe down?)!");
        continue;
    }
    System.out.println("Looking up "+clusterElement.getId()+"'s stub");
    ServerInterface stub = (ServerInterface) registry.lookup(clusterElement.getId());
    System.out.println("Asking vote to "+clusterElement.getId());
    //here methods are called on stub (exploiting costum SocketFactory)
} catch (NoSuchObjectException | java.rmi.ConnectException | java.rmi.ConnectIOException e){
    System.err.println("Candidate "+serverRMI.id+" cannot request vote to "+clusterElement.getId()+" because not reachable");
} catch (UnmarshalException e) {
    System.err.println("Candidate " + serverRMI.id + " timeout requesting vote to " + clusterElement.getId());
} catch (RemoteException e) {
    e.printStackTrace();
} catch (NotBoundException e) {
   System.out.println("Candidate "+serverRMI.id+" NotBound "+clusterElement.getId());
}

现在的问题是服务器卡在了contains()线路上,因为消息Checking contains是打印的,而Looking up...没有打印。

为什么会发生这种情况?有什么方法可以加快这个过程?这个算法充满了超时,所以任何建议都会非常感激!

更新: 在尝试了所有可能的关于 RMI 超时的 VM 属性后,例如: -Dsun.rmi.transport.tcp.responseTimeout=1 -Dsun.rmi.transport.proxy.connectTimeout=1 -Dsun.rmi.transport.tcp.handshakeTimeout=1 我根本没有看到任何区别,即使在每次 RMI 操作时都应该抛出异常(因为每个超时都设置为 1 毫秒!)。

我发现这个问题的唯一解决方案是使用这个RMISocketFactory重新实现:

final int timeoutMillis = 100;            
RMISocketFactory.setSocketFactory( new RMISocketFactory()
            {
                public Socket createSocket( String host, int port )
                        throws IOException
                {
                    Socket socket = new Socket();
                    socket.setSoTimeout(timeoutMillis);
                    socket.connect(new InetSocketAddress(host, port), timeoutMillis);
                    return socket;
                }

                public ServerSocket createServerSocket( int port )
                        throws IOException
                {
                    return new ServerSocket( port );
                }
            } );
4

1 回答 1

0

它卡在Registry.list().它最终会超时。

您最好在lookup()没有此先前步骤的情况下直接调用,这不会增加任何价值,并调查从 RMI 主页链接的两个属性页面中提到的所有超时选项。

于 2015-03-31T09:08:18.217 回答