Add new method to SpawnStrategyRegistry to reset default strategies.

Part of the roll-forward of https://github.com/bazelbuild/bazel/commit/37aeabcd39fe326d1c4e55693d8d207f9f7ac6c4.

PiperOrigin-RevId: 303377571
diff --git a/src/main/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistry.java b/src/main/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistry.java
index a87d2bd..375b013 100644
--- a/src/main/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistry.java
+++ b/src/main/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistry.java
@@ -300,6 +300,12 @@
     SpawnStrategyRegistry.Builder setDefaultStrategies(List<String> defaultStrategies);
 
     /**
+     * Reset the default strategies (see {@link #setDefaultStrategies}) to the reverse of the order
+     * they were registered in.
+     */
+    SpawnStrategyRegistry.Builder resetDefaultStrategies();
+
+    /**
      * Sets the strategy names to use in the remote branch of dynamic execution for a given action
      * mnemonic.
      *
@@ -371,7 +377,6 @@
     private final HashMap<String, List<String>> mnemonicToRemoteIdentifiers = new HashMap<>();
     private final HashMap<String, List<String>> mnemonicToLocalIdentifiers = new HashMap<>();
     private boolean legacyFilterIterationOrder = false;
-    private boolean useRegistrationOrderForDefault = true;
     @Nullable private String remoteLocalFallbackStrategyIdentifier;
 
     /**
@@ -448,8 +453,21 @@
      */
     @Override
     public Builder setDefaultStrategies(List<String> defaultStrategies) {
+      // Ensure there are actual strategies and the contents are not empty.
+      Preconditions.checkArgument(!defaultStrategies.isEmpty());
+      Preconditions.checkArgument(
+          defaultStrategies.stream().anyMatch(strategy -> !"".equals(strategy)));
       this.explicitDefaultStrategies = ImmutableList.copyOf(defaultStrategies);
-      useRegistrationOrderForDefault = false;
+      return this;
+    }
+
+    /**
+     * Reset the default strategies (see {@link #setDefaultStrategies}) to the reverse of the order
+     * they were registered in.
+     */
+    @Override
+    public Builder resetDefaultStrategies() {
+      this.explicitDefaultStrategies = ImmutableList.of();
       return this;
     }
 
@@ -557,7 +575,8 @@
       }
 
       ImmutableList<? extends SpawnStrategy> defaultStrategies;
-      if (useRegistrationOrderForDefault) {
+      if (explicitDefaultStrategies.isEmpty()) {
+        // Use the strategies as registered, in reverse order.
         defaultStrategies = ImmutableList.copyOf(Lists.reverse(strategiesInRegistrationOrder));
       } else {
         defaultStrategies = toStrategies(explicitDefaultStrategies, "default strategies");
diff --git a/src/test/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistryTest.java b/src/test/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistryTest.java
index cac2458..4d3e087 100644
--- a/src/test/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/exec/SpawnStrategyRegistryTest.java
@@ -120,23 +120,6 @@
   }
 
   @Test
-  public void testFilterNoMatch() throws Exception {
-    NoopStrategy strategy = new NoopStrategy("");
-    SpawnStrategyRegistry strategyRegistry =
-        SpawnStrategyRegistry.builder()
-            .registerStrategy(strategy, "foo")
-            .addMnemonicFilter("mnem", ImmutableList.of("foo"))
-            .setDefaultStrategies(ImmutableList.of())
-            .build();
-
-    assertThat(
-            strategyRegistry.getStrategies(
-                createSpawnWithMnemonicAndDescription("other", ""),
-                SpawnStrategyRegistryTest::noopEventHandler))
-        .isEmpty();
-  }
-
-  @Test
   public void testDescriptionHasPrecedenceOverMnemonic() throws Exception {
     NoopStrategy strategy1 = new NoopStrategy("1");
     NoopStrategy strategy2 = new NoopStrategy("2");