Skip to content

Commit

Permalink
Hide bwc build output on success (#42102)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rjernst authored May 16, 2019
1 parent 230ae18 commit bc0b0f5
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
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;

/**
Expand All @@ -19,35 +27,53 @@
@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) {
Expand Down
3 changes: 2 additions & 1 deletion distribution/bwc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bc0b0f5

Please sign in to comment.