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

Set up file logging when shutting down the Jenkins test instance. #657

Merged
merged 5 commits into from
Sep 28, 2023
Merged
Changes from 2 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
41 changes: 35 additions & 6 deletions src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.Enumeration;
Expand Down Expand Up @@ -563,18 +564,46 @@ public static void _stopJenkins(Server server, List<LenientRunnable> tearDowns,
}
}
}

if (jenkins != null) {
jenkins.cleanUp();
try (var ignored = new TemporaryConsoleLogTweak("hudson.XmlFile", Level.FINEST)) {
if (jenkins != null) {
jenkins.cleanUp();
}
ExtensionList.clearLegacyInstances();
DescriptorExtensionList.clearLegacyInstances();
}
ExtensionList.clearLegacyInstances();
DescriptorExtensionList.clearLegacyInstances();

if (exception.getSuppressed().length > 0) {
throw exception;
}
}

private static final class TemporaryConsoleLogTweak implements AutoCloseable {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this temporary, and how will we ensure that it does not become permanent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not meant to be temporary, I renamed the class to clarify the intent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not meant to be temporary

What would its value be after we diagnose and fix the bug seen in https://github.com/jenkinsci/jenkins/pull/8511/checks?check_run_id=17000216956?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no more race condition, I would fine removing it, but the thing is with this kind of issue is that it always pop up at an unexpected time and you often don't have enough data to investigate. On the other hand its impact is very light (under normal condition this adds one stacktrace writing queue.xml) since you wouldn't expect much xmlfile write being donc between stopping the test Jenkins instance and the cleanup sequence.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with the merge of this PR if and only if it is reverted once its diagnostic value has been exhausted.

private final Handler handler;
private final Level priorHandlerLevel;
private final Logger logger;
private final Level priorLoggerLevel;

public TemporaryConsoleLogTweak(String loggerName, Level level) {
logger = Logger.getLogger(loggerName);
priorLoggerLevel = logger.getLevel();
logger.setLevel(level);
handler = Arrays.stream(Logger.getLogger("").getHandlers()).filter(ConsoleHandler.class::isInstance).findFirst().orElse(null);
if (handler != null) {
priorHandlerLevel = handler.getLevel();
handler.setLevel(level);
} else {
priorHandlerLevel = null;
}
}

@Override
public void close() {
logger.setLevel(priorLoggerLevel);
if (handler != null) {
handler.setLevel(priorHandlerLevel);
}
}
}

private static void jettyLevel(Level level) {
Logger.getLogger("org.eclipse.jetty").setLevel(level);
}
Expand Down