Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes to improve debugging for JavaRuntimeViaHttpBase #81

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@
*/
public class JettyHttpProxy {
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
private static final String JETTY_LOG_CLASS = "org.eclipse.jetty.util.log.class";
private static final String JETTY_STDERRLOG = "org.eclipse.jetty.util.log.StdErrLog";
private static final long MAX_REQUEST_SIZE = 32 * 1024 * 1024;

/**
Expand All @@ -83,8 +81,6 @@ public class JettyHttpProxy {
*/
public static void startServer(ServletEngineAdapter.Config runtimeOptions) {
try {
System.setProperty(JETTY_LOG_CLASS, JETTY_STDERRLOG);

ForwardingHandler handler = new ForwardingHandler(runtimeOptions, System.getenv());
handler.init();
Server server = newServer(runtimeOptions, handler);
Expand Down
22 changes: 22 additions & 0 deletions runtime/test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,27 @@
<artifactId>appengine-utils</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<appengine.debug.port>${appengine.debug.port}</appengine.debug.port>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;

import com.google.apphosting.base.protos.api.RemoteApiPb;
import com.google.apphosting.testing.PortPicker;
Expand Down Expand Up @@ -63,6 +64,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -243,19 +245,22 @@ static <ApiServerT extends Closeable> RuntimeContext<ApiServerT> create(
.isTrue();
InetSocketAddress apiSocketAddress = new InetSocketAddress(apiPort);

ImmutableList<String> runtimeArgs =
ImmutableList.<String>builder()
.add(
JAVA_HOME.value() + "/bin/java",
ImmutableList.Builder<String> builder = ImmutableList.<String>builder();
builder.add(JAVA_HOME.value() + "/bin/java");
Integer debugPort = Integer.getInteger("appengine.debug.port");
if (debugPort != null) {
builder.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:" + debugPort);
}
builder.add(
"-Dcom.google.apphosting.runtime.jetty94.LEGACY_MODE=" + useJetty94LegacyMode(),
"-Dappengine.use.EE8=" + Boolean.getBoolean("appengine.use.EE8"),
"-Dappengine.use.EE10=" + Boolean.getBoolean("appengine.use.EE10"),
"-Duse.mavenjars=" + useMavenJars(),
"-cp",
useMavenJars()
? new File(runtimeDir, "jars/runtime-main.jar").getAbsolutePath()
: new File(runtimeDir, "runtime-main.jar").getAbsolutePath())
.addAll(optionalFlags())
: new File(runtimeDir, "runtime-main.jar").getAbsolutePath());
builder.addAll(optionalFlags())
.addAll(jvmFlagsFromEnvironment(config.environmentEntries()))
.add(
"com.google.apphosting.runtime.JavaRuntimeMainWithDefaults",
Expand All @@ -264,18 +269,15 @@ static <ApiServerT extends Closeable> RuntimeContext<ApiServerT> create(
"--trusted_host="
+ HostAndPort.fromParts(apiSocketAddress.getHostString(), apiPort),
runtimeDir.getAbsolutePath())
.addAll(config.launcherFlags())
.build();
.addAll(config.launcherFlags());
ImmutableList<String> runtimeArgs = builder.build();

Process runtimeProcess = launchRuntime(runtimeArgs, config.environmentEntries());
OutputPump outPump = new OutputPump(runtimeProcess.getInputStream(), "[stdout] ");
OutputPump errPump = new OutputPump(runtimeProcess.getErrorStream(), "[stderr] ");
new Thread(outPump).start();
new Thread(errPump).start();
// TODO(b/192665275):
// For some reason, a Maven build does not emit anymore this log, need to investigate.
// For now, just wait a bit so the server is started in tests.
Thread.sleep(SERVER_START_TIMEOUT_SECONDS * 100);
await().atMost(30, SECONDS).until(() -> isPortAvailable("localhost", jettyPort));

int timeoutMillis = 30_000;
RequestConfig requestConfig =
Expand All @@ -292,6 +294,16 @@ static <ApiServerT extends Closeable> RuntimeContext<ApiServerT> create(
runtimeProcess, httpApiServer, httpClient, jettyPort, outPump, errPump);
}

public static boolean isPortAvailable(String host, int port) {
try {
Socket socket = new Socket(host, port);
socket.close();
return true;
} catch (Exception e) {
return false;
}
}

private static List<String> jvmFlagsFromEnvironment(ImmutableMap<String, String> env) {
return Splitter.on(' ').omitEmptyStrings().splitToList(env.getOrDefault("GAE_JAVA_OPTS", ""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private RuntimeContext<DummyApiServer> runtimeContext() throws IOException, Inte

private static List<String> readOutput(InputStream inputStream) throws IOException {
try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) {
return output.lines().collect(Collectors.toList());
return output.lines().map(l -> l + "\n").collect(Collectors.toList());
}
}

Expand Down
Loading