From 9dc93c5c9372f1501006e2d3a3a7affecd65fb8e Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Wed, 5 Jan 2022 09:49:19 -0500 Subject: [PATCH] fix: align ip version preference for the wrapped emulator (#1052) * fix: align ip version preference for the wrapped emulator When the emulator wrapper is started with -Djava.net.preferIPv6Addresses=true on a machine that defaults to ipv4, the golang emulator and the java client will pick different ip stacks and not be able to connect. This change will use java to resolve the localhost's ip address and push it down to the golang emulator * remove debug * whitespace --- .../cloud/bigtable/emulator/v2/Emulator.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/google-cloud-bigtable-emulator/src/main/java/com/google/cloud/bigtable/emulator/v2/Emulator.java b/google-cloud-bigtable-emulator/src/main/java/com/google/cloud/bigtable/emulator/v2/Emulator.java index 4ddc3620e..b43322831 100644 --- a/google-cloud-bigtable-emulator/src/main/java/com/google/cloud/bigtable/emulator/v2/Emulator.java +++ b/google-cloud-bigtable-emulator/src/main/java/com/google/cloud/bigtable/emulator/v2/Emulator.java @@ -25,10 +25,13 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; +import java.net.UnknownHostException; import java.nio.file.Path; import java.util.Locale; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.logging.Level; @@ -98,10 +101,24 @@ public synchronized void start() throws IOException, TimeoutException, Interrupt } this.port = getAvailablePort(); + // Try to align the localhost address across java & golang emulator + // This should fix issues on systems that default to ipv4 but the jvm is started with + // -Djava.net.preferIPv6Addresses=true + Optional localhostAddress = Optional.empty(); + try { + localhostAddress = Optional.of(InetAddress.getByName(null).getHostAddress()); + } catch (UnknownHostException e) { + } + // Workaround https://bugs.openjdk.java.net/browse/JDK-8068370 for (int attemptsLeft = 3; process == null; attemptsLeft--) { try { - process = Runtime.getRuntime().exec(String.format("%s -port %d", executable, port)); + String cmd = executable.toString(); + if (localhostAddress.isPresent()) { + cmd += String.format(" -host [%s]", localhostAddress.get()); + } + cmd += String.format(" -port %d", port); + process = Runtime.getRuntime().exec(cmd); } catch (IOException e) { if (attemptsLeft > 0) { Thread.sleep(1000);