You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sz...@apache.org on 2021/03/07 10:38:07 UTC

[ratis] branch master updated: RATIS-1147. TestMultiRaftGroup on MiniRaftClusterWithNetty is failing consistently. (#433)

This is an automated email from the ASF dual-hosted git repository.

szetszwo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis.git


The following commit(s) were added to refs/heads/master by this push:
     new f62698b  RATIS-1147. TestMultiRaftGroup on MiniRaftClusterWithNetty is failing consistently. (#433)
f62698b is described below

commit f62698b59c9e450e998f7e2647738efb112c2400
Author: Chris Nauroth <cn...@gmail.com>
AuthorDate: Sun Mar 7 02:38:00 2021 -0800

    RATIS-1147. TestMultiRaftGroup on MiniRaftClusterWithNetty is failing consistently. (#433)
---
 .../main/java/org/apache/ratis/util/NetUtils.java  | 20 +++++++++++++++-----
 .../ratis/logservice/util/LogServiceCluster.java   | 22 +++++++++-------------
 2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java b/ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java
index 10d2fb4..39d0b76 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/NetUtils.java
@@ -1,4 +1,4 @@
-/**
+/*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -116,8 +116,13 @@ public interface NetUtils {
     return addr;
   }
 
-  /** Creates {@code count} unique local addresses.  They may conflict with
-   * addresses created later, but not with one another. */
+  /**
+   * Creates {@code count} unique local addresses.  They may conflict with
+   * addresses created later, but not with one another.  Addresses are
+   * guaranteed to be bound to the loopback interface.
+   * @param count number of unique local addresses to create
+   * @return {@code count} number of unique local addresses
+   */
   static List<InetSocketAddress> createLocalServerAddress(int count) {
     List<InetSocketAddress> list = new ArrayList<>(count);
     List<ServerSocket> sockets = new ArrayList<>(count);
@@ -126,7 +131,7 @@ public interface NetUtils {
         ServerSocket s = new ServerSocket();
         sockets.add(s);
         s.setReuseAddress(true);
-        s.bind(null);
+        s.bind(new InetSocketAddress(InetAddress.getByName(null), 0), 1);
         list.add((InetSocketAddress) s.getLocalSocketAddress());
       }
     } catch (IOException e) {
@@ -137,10 +142,15 @@ public interface NetUtils {
     return list;
   }
 
+  /**
+   * Creates a unique local address.  Addresses are guaranteed to be bound to
+   * the loopback interface.
+   * @return unique local address
+   */
   static InetSocketAddress createLocalServerAddress() {
     try(ServerSocket s = new ServerSocket()) {
       s.setReuseAddress(true);
-      s.bind(null);
+      s.bind(new InetSocketAddress(InetAddress.getByName(null), 0), 1);
       return (InetSocketAddress) s.getLocalSocketAddress();
     } catch (IOException e) {
       throw new RuntimeException(e);
diff --git a/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/LogServiceCluster.java b/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/LogServiceCluster.java
index 3ed4f0f..4d44dc6 100644
--- a/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/LogServiceCluster.java
+++ b/ratis-logservice/src/test/java/org/apache/ratis/logservice/util/LogServiceCluster.java
@@ -25,8 +25,10 @@ import org.apache.ratis.logservice.api.LogStream;
 import org.apache.ratis.logservice.api.LogServiceClient;
 import org.apache.ratis.logservice.server.LogServer;
 import org.apache.ratis.logservice.server.MetadataServer;
+import org.apache.ratis.util.NetUtils;
 
 import java.io.IOException;
+import java.net.InetSocketAddress;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -50,7 +52,6 @@ public class LogServiceCluster implements AutoCloseable {
         List<LogServer> newWorkers = IntStream.range(0, numWorkers).parallel().mapToObj(i ->
                 LogServer.newBuilder()
                         .setHostName("localhost")
-                        .setPort(10000 + i)
                         .setMetaQuorum(meta)
                         .setWorkingDir(baseTestDir + "/workers/" + i)
                         .build()).collect(Collectors.toList());
@@ -82,19 +83,14 @@ public class LogServiceCluster implements AutoCloseable {
     public LogServiceCluster(int numServers) {
         // Have to construct the meta quorum by hand -- `getMetaIdentity()` requires
         // uses the masters to build the quorum (chicken and egg problem).
-        StringBuilder sb = new StringBuilder();
-        for (int i = 0; i < numServers; i++) {
-          if (sb.length() > 0) {
-            sb.append(",");
-          }
-          sb.append("localhost:").append(9000 + i);
-        }
-        String metaQuorum = sb.toString();
-        this.masters = IntStream.range(0, numServers).parallel().mapToObj(i ->
+        List<InetSocketAddress> addresses = NetUtils.createLocalServerAddress(numServers);
+        String metaQuorum = addresses.stream().map(address -> address.getHostString() + ':' + address.getPort())
+            .collect(Collectors.joining(","));
+        this.masters = addresses.stream().map(address ->
                 MetadataServer.newBuilder()
-                        .setHostName("localhost")
-                        .setPort(9000 + i)
-                        .setWorkingDir(baseTestDir + "/masters/" + i)
+                        .setHostName(address.getHostName())
+                        .setPort(address.getPort())
+                        .setWorkingDir(baseTestDir + "/masters/" + address.getPort())
                         .setMetaQuorum(metaQuorum)
                         .build())
                 .collect(Collectors.toList());