From 6d63f8a1ad33490b97e60955277c4ebca40a4f48 Mon Sep 17 00:00:00 2001 From: michajlo Date: Wed, 17 Jul 2019 07:55:43 -0700 Subject: [PATCH] Add (inactive) code to compress large GenQuery results in memory on the fly Dynamically switches to gzipping GenQuery results >1M, trading CPU for memory. Hard coded to off for now while I sort out how to connect this to a flag. PiperOrigin-RevId: 258568242 --- .../build/lib/rules/genquery/GenQuery.java | 56 +++-- .../rules/genquery/GenQueryOutputStream.java | 210 ++++++++++++++++++ .../genquery/GenQueryOutputStreamTest.java | 141 ++++++++++++ 3 files changed, 392 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/google/devtools/build/lib/rules/genquery/GenQueryOutputStream.java create mode 100644 src/test/java/com/google/devtools/build/lib/rules/genquery/GenQueryOutputStreamTest.java diff --git a/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java b/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java index 118e783acf7444..190aa3930c61a0 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java +++ b/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java @@ -36,7 +36,7 @@ import com.google.devtools.build.lib.analysis.Runfiles; import com.google.devtools.build.lib.analysis.RunfilesProvider; import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction; -import com.google.devtools.build.lib.analysis.actions.ByteStringDeterministicWriter; +import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction.DeterministicWriter; import com.google.devtools.build.lib.analysis.config.CoreOptions; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.PackageIdentifier; @@ -73,6 +73,7 @@ import com.google.devtools.build.lib.query2.query.output.QueryOptions; import com.google.devtools.build.lib.query2.query.output.QueryOptions.OrderOutput; import com.google.devtools.build.lib.query2.query.output.QueryOutputUtils; +import com.google.devtools.build.lib.rules.genquery.GenQueryOutputStream.GenQueryResult; import com.google.devtools.build.lib.runtime.KeepGoingOption; import com.google.devtools.build.lib.skyframe.PackageValue; import com.google.devtools.build.lib.skyframe.TargetPatternValue; @@ -92,6 +93,7 @@ import com.google.devtools.common.options.OptionsParsingException; import com.google.protobuf.ByteString; import java.io.IOException; +import java.io.OutputStream; import java.nio.channels.ClosedByInterruptException; import java.util.Collection; import java.util.HashSet; @@ -117,6 +119,7 @@ public ConfiguredTarget create(RuleContext ruleContext) // The query string final String query = ruleContext.attributes().get("expression", Type.STRING); + @SuppressWarnings("unchecked") OptionsParser optionsParser = OptionsParser.builder() .optionsClasses(QueryOptions.class, KeepGoingOption.class) @@ -169,7 +172,7 @@ public ConfiguredTarget create(RuleContext ruleContext) // force relative_locations to true so it has a deterministic output across machines. queryOptions.relativeLocations = true; - ByteString result; + GenQueryResult result; try (SilentCloseable c = Profiler.instance().profile("GenQuery.executeQuery/" + ruleContext.getLabel())) { result = @@ -193,11 +196,14 @@ public ConfiguredTarget create(RuleContext ruleContext) NestedSet filesToBuild = NestedSetBuilder.create(Order.STABLE_ORDER, outputArtifact); return new RuleConfiguredTargetBuilder(ruleContext) .setFilesToBuild(filesToBuild) - .add(RunfilesProvider.class, RunfilesProvider.simple( - new Runfiles.Builder( - ruleContext.getWorkspaceName(), - ruleContext.getConfiguration().legacyExternalRunfiles()) - .addTransitiveArtifacts(filesToBuild).build())) + .addProvider( + RunfilesProvider.class, + RunfilesProvider.simple( + new Runfiles.Builder( + ruleContext.getWorkspaceName(), + ruleContext.getConfiguration().legacyExternalRunfiles()) + .addTransitiveArtifacts(filesToBuild) + .build())) .build(); } @@ -273,7 +279,7 @@ private ExtendedEventHandler getEventHandler(RuleContext ruleContext) { } @Nullable - private ByteString executeQuery( + private GenQueryResult executeQuery( RuleContext ruleContext, QueryOptions queryOptions, Collection