|
15 | 15 |
|
16 | 16 | import com.google.common.annotations.VisibleForTesting; |
17 | 17 | import com.google.common.collect.ImmutableList; |
| 18 | +import com.google.common.collect.ImmutableMap; |
18 | 19 | import com.google.common.collect.ImmutableSet; |
19 | 20 | import com.google.common.collect.Lists; |
20 | | -import com.google.common.collect.Maps; |
21 | 21 | import com.google.common.collect.Sets; |
22 | 22 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
23 | 23 | import com.google.devtools.build.lib.actions.Spawn; |
|
35 | 35 | import com.google.devtools.build.lib.util.AbruptExitException; |
36 | 36 | import com.google.devtools.build.lib.util.DetailedExitCode; |
37 | 37 | import com.google.devtools.common.options.OptionsBase; |
| 38 | +import java.util.HashMap; |
38 | 39 | import java.util.List; |
39 | 40 | import java.util.Map; |
40 | 41 | import java.util.concurrent.ExecutorService; |
@@ -69,40 +70,53 @@ public void beforeCommand(CommandEnvironment env) { |
69 | 70 | env.getEventBus().register(this); |
70 | 71 | } |
71 | 72 |
|
72 | | - private List<Map.Entry<String, List<String>>> getLocalStrategies( |
73 | | - DynamicExecutionOptions options) { |
| 73 | + @VisibleForTesting |
| 74 | + ImmutableMap<String, List<String>> getLocalStrategies(DynamicExecutionOptions options) |
| 75 | + throws AbruptExitException { |
74 | 76 | // Options that set "allowMultiple" to true ignore the default value, so we replicate that |
75 | 77 | // functionality here. Additionally, since we are still supporting --dynamic_worker_strategy, |
76 | 78 | // but will deprecate it soon, we add its functionality to --dynamic_local_strategy. This allows |
77 | 79 | // users to set --dynamic_local_strategy and not --dynamic_worker_strategy to stop defaulting to |
78 | | - // worker strategy. |
| 80 | + // worker strategy. For simplicity, we add the default strategy first, it may be overridden |
| 81 | + // later. |
| 82 | + // ImmutableMap.Builder fails on duplicates, so we use a regular map first to remove dups. |
| 83 | + Map<String, List<String>> localAndWorkerStrategies = new HashMap<>(); |
79 | 84 | // TODO(steinman): Deprecate --dynamic_worker_strategy and clean this up. |
80 | | - if (options.dynamicLocalStrategy == null || options.dynamicLocalStrategy.isEmpty()) { |
81 | | - String workerStrategy = |
82 | | - options.dynamicWorkerStrategy.isEmpty() ? "worker" : options.dynamicWorkerStrategy; |
83 | | - return ImmutableList.of( |
84 | | - Maps.immutableEntry("", ImmutableList.of(workerStrategy, "sandboxed"))); |
85 | | - } |
86 | | - |
87 | | - ImmutableList.Builder<Map.Entry<String, List<String>>> localAndWorkerStrategies = |
88 | | - ImmutableList.builder(); |
89 | | - for (Map.Entry<String, List<String>> entry : options.dynamicLocalStrategy) { |
90 | | - if ("".equals(entry.getKey())) { |
91 | | - List<String> newValue = Lists.newArrayList(options.dynamicWorkerStrategy); |
92 | | - newValue.addAll(entry.getValue()); |
93 | | - localAndWorkerStrategies.add(Maps.immutableEntry("", newValue)); |
94 | | - } else { |
95 | | - localAndWorkerStrategies.add(entry); |
| 85 | + List<String> defaultValue = Lists.newArrayList(); |
| 86 | + String workerStrategy = |
| 87 | + options.dynamicWorkerStrategy.isEmpty() ? "worker" : options.dynamicWorkerStrategy; |
| 88 | + defaultValue.addAll(ImmutableList.of(workerStrategy, "sandboxed")); |
| 89 | + throwIfContainsDynamic(defaultValue, "--dynamic_local_strategy"); |
| 90 | + localAndWorkerStrategies.put("", defaultValue); |
| 91 | + |
| 92 | + if (!options.dynamicLocalStrategy.isEmpty()) { |
| 93 | + for (Map.Entry<String, List<String>> entry : options.dynamicLocalStrategy) { |
| 94 | + if ("".equals(entry.getKey())) { |
| 95 | + List<String> newValue = Lists.newArrayList(); |
| 96 | + if (!options.dynamicWorkerStrategy.isEmpty()) { |
| 97 | + newValue.add(options.dynamicWorkerStrategy); |
| 98 | + } |
| 99 | + newValue.addAll(entry.getValue()); |
| 100 | + localAndWorkerStrategies.put("", newValue); |
| 101 | + } else { |
| 102 | + localAndWorkerStrategies.put(entry.getKey(), entry.getValue()); |
| 103 | + } |
| 104 | + throwIfContainsDynamic(entry.getValue(), "--dynamic_local_strategy"); |
96 | 105 | } |
97 | 106 | } |
98 | | - return localAndWorkerStrategies.build(); |
| 107 | + return ImmutableMap.copyOf(localAndWorkerStrategies); |
99 | 108 | } |
100 | 109 |
|
101 | | - private List<Map.Entry<String, List<String>>> getRemoteStrategies( |
102 | | - DynamicExecutionOptions options) { |
103 | | - return (options.dynamicRemoteStrategy == null || options.dynamicRemoteStrategy.isEmpty()) |
104 | | - ? ImmutableList.of(Maps.immutableEntry("", ImmutableList.of("remote"))) |
105 | | - : options.dynamicRemoteStrategy; |
| 110 | + private ImmutableMap<String, List<String>> getRemoteStrategies(DynamicExecutionOptions options) |
| 111 | + throws AbruptExitException { |
| 112 | + Map<String, List<String>> strategies = new HashMap<>(); // Needed to dedup |
| 113 | + for (Map.Entry<String, List<String>> e : options.dynamicRemoteStrategy) { |
| 114 | + throwIfContainsDynamic(e.getValue(), "--dynamic_remote_strategy"); |
| 115 | + strategies.put(e.getKey(), e.getValue()); |
| 116 | + } |
| 117 | + return options.dynamicRemoteStrategy.isEmpty() |
| 118 | + ? ImmutableMap.of("", ImmutableList.of("remote")) |
| 119 | + : ImmutableMap.copyOf(strategies); |
106 | 120 | } |
107 | 121 |
|
108 | 122 | @Override |
@@ -130,16 +144,8 @@ final void registerSpawnStrategies( |
130 | 144 | } |
131 | 145 | registryBuilder.registerStrategy(strategy, "dynamic", "dynamic_worker"); |
132 | 146 |
|
133 | | - for (Map.Entry<String, List<String>> mnemonicToStrategies : getLocalStrategies(options)) { |
134 | | - throwIfContainsDynamic(mnemonicToStrategies.getValue(), "--dynamic_local_strategy"); |
135 | | - registryBuilder.addDynamicLocalStrategiesByMnemonic( |
136 | | - mnemonicToStrategies.getKey(), mnemonicToStrategies.getValue()); |
137 | | - } |
138 | | - for (Map.Entry<String, List<String>> mnemonicToStrategies : getRemoteStrategies(options)) { |
139 | | - throwIfContainsDynamic(mnemonicToStrategies.getValue(), "--dynamic_remote_strategy"); |
140 | | - registryBuilder.addDynamicRemoteStrategiesByMnemonic( |
141 | | - mnemonicToStrategies.getKey(), mnemonicToStrategies.getValue()); |
142 | | - } |
| 147 | + registryBuilder.addDynamicLocalStrategies(getLocalStrategies(options)); |
| 148 | + registryBuilder.addDynamicRemoteStrategies(getRemoteStrategies(options)); |
143 | 149 | } |
144 | 150 |
|
145 | 151 | private void throwIfContainsDynamic(List<String> strategies, String flagName) |
|
0 commit comments