Skip to content

Commit

Permalink
Order the input-output map keys before processing its elements. This …
Browse files Browse the repository at this point in the history
…fixes non-determinism in how the protos get bundled into the BundledProtos_X archives.

PiperOrigin-RevId: 184009755
  • Loading branch information
sergiocampama authored and Copybara-Service committed Jan 31, 2018
1 parent 109dc14 commit 518f532
Showing 1 changed file with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
Expand Down Expand Up @@ -164,7 +165,7 @@ public ProtobufSupport(
public ProtobufSupport registerGenerationActions() {
int actionId = 0;

for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) {
for (ImmutableSet<Artifact> inputProtos : orderedInputOutputKeySet()) {
Iterable<Artifact> outputProtos = inputsToOutputsMap.get(inputProtos);
registerGenerationAction(outputProtos, inputProtos, getUniqueBundledProtosSuffix(actionId));
actionId++;
Expand Down Expand Up @@ -203,7 +204,7 @@ public ProtobufSupport registerCompilationActions()
int actionId = 0;
Iterable<PathFragment> userHeaderSearchPaths =
ImmutableList.of(getWorkspaceRelativeOutputDir());
for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) {
for (ImmutableSet<Artifact> inputProtos : orderedInputOutputKeySet()) {
ImmutableSet<Artifact> outputProtos = inputsToOutputsMap.get(inputProtos);

IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId);
Expand Down Expand Up @@ -243,7 +244,7 @@ public ProtobufSupport addFilesToBuild(NestedSetBuilder<Artifact> filesToBuild)
}

int actionId = 0;
for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) {
for (ImmutableSet<Artifact> inputProtos : orderedInputOutputKeySet()) {
ImmutableSet<Artifact> outputProtos = inputsToOutputsMap.get(inputProtos);
IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId);

Expand Down Expand Up @@ -275,7 +276,7 @@ public Optional<ObjcProvider> getObjcProvider() {
}

int actionId = 0;
for (ImmutableSet<Artifact> inputProtos : inputsToOutputsMap.keySet()) {
for (ImmutableSet<Artifact> inputProtos : orderedInputOutputKeySet()) {
ImmutableSet<Artifact> outputProtos = inputsToOutputsMap.get(inputProtos);
IntermediateArtifacts intermediateArtifacts = getUniqueIntermediateArtifacts(actionId);

Expand Down Expand Up @@ -372,6 +373,20 @@ private static ImmutableSetMultimap<ImmutableSet<Artifact>, Artifact> getInputsT
return inputsToOutputsMapBuilder.build();
}

/**
* Returns an ordered list of ImmutableSets<Artifact>s representing the keys to the inputs-outputs
* map. Using an ordered list ensures that for the same inputs, the keys are processed in the same
* order, and avoids non-determinism in the intermediate outputs.
*/
private List<ImmutableSet<Artifact>> orderedInputOutputKeySet() {
return new Ordering<ImmutableSet<Artifact>>() {
@Override
public int compare(ImmutableSet<Artifact> o1, ImmutableSet<Artifact> o2) {
return o1.hashCode() - o2.hashCode();
}
}.sortedCopy(inputsToOutputsMap.keySet());
}

private String getBundledProtosSuffix() {
return "_" + BUNDLED_PROTOS_IDENTIFIER;
}
Expand Down

0 comments on commit 518f532

Please sign in to comment.