Skip to content

Commit 9dc93c5

Browse files
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
1 parent 1ee5773 commit 9dc93c5

File tree

1 file changed

+18
-1
lines changed
  • google-cloud-bigtable-emulator/src/main/java/com/google/cloud/bigtable/emulator/v2

1 file changed

+18
-1
lines changed

google-cloud-bigtable-emulator/src/main/java/com/google/cloud/bigtable/emulator/v2/Emulator.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
import java.io.IOException;
2626
import java.io.InputStream;
2727
import java.io.InputStreamReader;
28+
import java.net.InetAddress;
2829
import java.net.ServerSocket;
2930
import java.net.Socket;
31+
import java.net.UnknownHostException;
3032
import java.nio.file.Path;
3133
import java.util.Locale;
34+
import java.util.Optional;
3235
import java.util.concurrent.TimeUnit;
3336
import java.util.concurrent.TimeoutException;
3437
import java.util.logging.Level;
@@ -98,10 +101,24 @@ public synchronized void start() throws IOException, TimeoutException, Interrupt
98101
}
99102
this.port = getAvailablePort();
100103

104+
// Try to align the localhost address across java & golang emulator
105+
// This should fix issues on systems that default to ipv4 but the jvm is started with
106+
// -Djava.net.preferIPv6Addresses=true
107+
Optional<String> localhostAddress = Optional.empty();
108+
try {
109+
localhostAddress = Optional.of(InetAddress.getByName(null).getHostAddress());
110+
} catch (UnknownHostException e) {
111+
}
112+
101113
// Workaround https://bugs.openjdk.java.net/browse/JDK-8068370
102114
for (int attemptsLeft = 3; process == null; attemptsLeft--) {
103115
try {
104-
process = Runtime.getRuntime().exec(String.format("%s -port %d", executable, port));
116+
String cmd = executable.toString();
117+
if (localhostAddress.isPresent()) {
118+
cmd += String.format(" -host [%s]", localhostAddress.get());
119+
}
120+
cmd += String.format(" -port %d", port);
121+
process = Runtime.getRuntime().exec(cmd);
105122
} catch (IOException e) {
106123
if (attemptsLeft > 0) {
107124
Thread.sleep(1000);

0 commit comments

Comments
 (0)