Hide bwc build output on success (#42102)

Previously we used LoggedExec for running the internal bwc builds.
However, this had bad performance implications as all the output was
buffered into memory, thus we changed back to normal Exec. This commit
adds a `spoolOutput` setting to LoggedExec which can be used for
commands with large amounts of output, and switches the bwc builds to
use this flag.
This commit is contained in:
Ryan Ernst 2019-05-16 09:48:57 -04:00
parent 6ffc6ea42e
commit 8681dd9cba
3 changed files with 118 additions and 24 deletions

View File

@ -0,0 +1,67 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.gradle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* An outputstream to a File that is lazily opened on the first write.
*/
class LazyFileOutputStream extends OutputStream {
private OutputStream delegate;
LazyFileOutputStream(File file) {
// use an initial dummy delegate to avoid doing a conditional on every write
this.delegate = new OutputStream() {
private void bootstrap() throws IOException {
file.getParentFile().mkdirs();
delegate = new FileOutputStream(file);
}
@Override
public void write(int b) throws IOException {
bootstrap();
delegate.write(b);
}
@Override
public void write(byte b[], int off, int len) throws IOException {
bootstrap();
delegate.write(b, off, len);
}
};
}
@Override
public void write(int b) throws IOException {
delegate.write(b);
}
@Override
public void write(byte b[], int off, int len) throws IOException {
delegate.write(b, off, len);
}
@Override
public void close() throws IOException {
delegate.close();
}
}

View File

@ -3,14 +3,22 @@ package org.elasticsearch.gradle;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.Exec;
import org.gradle.api.tasks.Internal;
import org.gradle.process.BaseExecSpec;
import org.gradle.process.ExecResult;
import org.gradle.process.ExecSpec;
import org.gradle.process.JavaExecSpec;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.function.Consumer;
import java.util.function.Function;
/**
@ -19,37 +27,55 @@ import java.util.function.Function;
@SuppressWarnings("unchecked")
public class LoggedExec extends Exec {
private Consumer<Logger> outputLogger;
public LoggedExec() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream error = new ByteArrayOutputStream();
if (getLogger().isInfoEnabled() == false) {
setStandardOutput(output);
setErrorOutput(error);
setIgnoreExitValue(true);
doLast((unused) -> {
if (getExecResult().getExitValue() != 0) {
try {
getLogger().error("Standard output:");
getLogger().error(output.toString("UTF-8"));
getLogger().error("Standard error:");
getLogger().error(error.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new GradleException("Failed to read exec output", e);
}
throw new GradleException(
String.format(
"Process '%s %s' finished with non-zero exit value %d",
getExecutable(),
getArgs(),
getExecResult().getExitValue()
)
);
setSpoolOutput(false);
doLast(task -> {
if (getExecResult().getExitValue() != 0) {
try {
getLogger().error("Output for " + getExecutable() + ":");
outputLogger.accept(getLogger());
} catch (Exception e) {
throw new GradleException("Failed to read exec output", e);
}
throw new GradleException(
String.format(
"Process '%s %s' finished with non-zero exit value %d",
getExecutable(),
getArgs(),
getExecResult().getExitValue()
)
);
}
);
});
}
}
@Internal
public void setSpoolOutput(boolean spoolOutput) {
final OutputStream out;
if (spoolOutput) {
File spoolFile = new File(getProject().getBuildDir() + "/buffered-output/" + this.getName());
out = new LazyFileOutputStream(spoolFile);
outputLogger = logger -> {
try {
Files.lines(spoolFile.toPath()).forEach(logger::error);
} catch (IOException e) {
throw new RuntimeException("could not log", e);
}
};
} else {
out = new ByteArrayOutputStream();
outputLogger = logger -> logger.error(((ByteArrayOutputStream) getStandardOutput()).toString(StandardCharsets.UTF_8));
}
setStandardOutput(out);
setErrorOutput(out);
}
public static ExecResult exec(Project project, Action<ExecSpec> action) {
return genericExec(project, project::exec, action);
}

View File

@ -121,8 +121,9 @@ bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInfo unreleased
}
Closure createRunBwcGradleTask = { name, extraConfig ->
return tasks.create(name: "$name", type: Exec) {
return tasks.create(name: "$name", type: LoggedExec) {
dependsOn checkoutBwcBranch, writeBuildMetadata
spoolOutput = true
workingDir = checkoutDir
doFirst {
// Execution time so that the checkouts are available