This is an automated email from the ASF dual-hosted git repository.

dsmiley pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/solr.git

commit a8dd307fe04ee294e8aeb4d0cd4d676f98f6fc18
Author: Boyi Dai <[email protected]>
AuthorDate: Wed Nov 11 20:02:25 2026 -0500

    SOLR-18562: Don't overwrite other aliases when fixing a time routed alias's 
start (#5191)
    
    Fixing a time routed alias's date math router.start when a document arrives 
no longer undoes changes to aliases the receiving node hadn't seen yet, and no 
longer moves an already fixed start
    
    (cherry picked from commit 2b0eb4962bafe98f9d808145bd140c85d46c215f)
---
 .../SOLR-18562-tra-start-keeps-other-aliases.yml   | 12 +++
 .../cloud/api/collections/TimeRoutedAlias.java     | 49 ++++++++---
 .../apache/solr/cloud/CreateRoutedAliasTest.java   | 95 ++++++++++++++++++++++
 .../collections/TimeRoutedAliasFixStartTest.java   | 70 ++++++++++++++++
 4 files changed, 213 insertions(+), 13 deletions(-)

diff --git a/changelog/unreleased/SOLR-18562-tra-start-keeps-other-aliases.yml 
b/changelog/unreleased/SOLR-18562-tra-start-keeps-other-aliases.yml
new file mode 100644
index 00000000000..ed3b2ea8369
--- /dev/null
+++ b/changelog/unreleased/SOLR-18562-tra-start-keeps-other-aliases.yml
@@ -0,0 +1,12 @@
+# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc
+
+title: >
+  Fixing a time routed alias's date math router.start when a document arrives 
no longer undoes
+  changes to aliases the receiving node hadn't seen yet, and no longer moves 
an already fixed start
+type: fixed
+authors:
+  - name: Boyi Dai
+    nick: SpookyJumpyBeans
+links:
+  - name: SOLR-18562
+    url: https://issues.apache.org/jira/browse/SOLR-18562
diff --git 
a/solr/core/src/java/org/apache/solr/cloud/api/collections/TimeRoutedAlias.java 
b/solr/core/src/java/org/apache/solr/cloud/api/collections/TimeRoutedAlias.java
index 4df16b611d2..ce2eed85e63 100644
--- 
a/solr/core/src/java/org/apache/solr/cloud/api/collections/TimeRoutedAlias.java
+++ 
b/solr/core/src/java/org/apache/solr/cloud/api/collections/TimeRoutedAlias.java
@@ -37,7 +37,6 @@ import java.time.temporal.ChronoUnit;
 import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -372,6 +371,30 @@ public class TimeRoutedAlias extends RoutedAlias {
     return nextCollTimestamp;
   }
 
+  /**
+   * Returns aliases with the router.start of an alias set to a fixed start, 
but only if it is still
+   * the date math the fixed start was computed from: nodes whose cached 
aliases are stale may
+   * compute it again after it was fixed. Builds on the given aliases, which 
are refreshed on a
+   * version conflict, so that other changes to aliases are kept.
+   *
+   * @return the aliases with the fixed start, or the given aliases if there 
is nothing to change
+   */
+  static Aliases fixStart(
+      Aliases aliases, String aliasName, String dateMathStart, String 
fixedStart) {
+    String currentStart = 
aliases.getCollectionAliasProperties(aliasName).get(ROUTER_START);
+    if (!dateMathStart.equals(currentStart)) {
+      if (currentStart == null) {
+        log.warn(
+            "Not fixing {} of alias [{}]: the alias no longer exists or has no 
{}",
+            ROUTER_START,
+            aliasName,
+            ROUTER_START);
+      }
+      return aliases;
+    }
+    return aliases.cloneWithCollectionAliasProperties(aliasName, ROUTER_START, 
fixedStart);
+  }
+
   @Override
   public void validateRouteValue(AddUpdateCommand cmd) throws SolrException {
 
@@ -406,19 +429,19 @@ public class TimeRoutedAlias extends RoutedAlias {
       startTime = DateMathParser.parseMath(new Date(), start).toInstant();
       SolrCore core = cmd.getReq().getCore();
       ZkStateReader zkStateReader = 
core.getCoreContainer().getZkController().zkStateReader;
-      Aliases aliases = zkStateReader.getAliases();
-      Map<String, String> props = new 
HashMap<>(aliases.getCollectionAliasProperties(aliasName));
+      final String dateMathStart = start;
       start = DateTimeFormatter.ISO_INSTANT.format(startTime);
-      props.put(ROUTER_START, start);
-
-      // This could race, but it only occurs when the alias is first used and 
the values produced
-      // should all be identical and who wins won't matter (baring cases of 
Date Math involving
-      // seconds, which is pretty far fetched). Putting this in a separate 
thread to ensure that any
-      // failed races don't cause documents to get rejected.
-      core.runAsync(
-          () ->
-              zkStateReader.aliasesManager.applyModificationAndExportToZk(
-                  (a) -> aliases.cloneWithCollectionAliasProperties(aliasName, 
props)));
+      final String fixedStart = start;
+
+      // Putting this in a separate thread to ensure that any failed races 
don't cause documents to
+      // get rejected. A time dimension of a dimensional routed alias has no 
alias name of its own
+      // (see DimensionalRoutedAlias), so its start can't be fixed here.
+      if (!aliasName.isEmpty()) {
+        core.runAsync(
+            () ->
+                zkStateReader.aliasesManager.applyModificationAndExportToZk(
+                    a -> fixStart(a, aliasName, dateMathStart, fixedStart)));
+      }
     }
     if (docTimestamp.isBefore(startTime)) {
       throw new SolrException(
diff --git 
a/solr/core/src/test/org/apache/solr/cloud/CreateRoutedAliasTest.java 
b/solr/core/src/test/org/apache/solr/cloud/CreateRoutedAliasTest.java
index 9184308b108..8475fd432db 100644
--- a/solr/core/src/test/org/apache/solr/cloud/CreateRoutedAliasTest.java
+++ b/solr/core/src/test/org/apache/solr/cloud/CreateRoutedAliasTest.java
@@ -20,6 +20,7 @@ package org.apache.solr.cloud;
 import static org.apache.solr.client.solrj.request.RoutedAliasTypes.TIME;
 
 import java.io.IOException;
+import java.lang.reflect.Field;
 import java.nio.charset.StandardCharsets;
 import java.time.Duration;
 import java.time.Instant;
@@ -28,19 +29,25 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.TimeZone;
+import java.util.concurrent.TimeUnit;
+import org.apache.lucene.util.SuppressForbidden;
 import org.apache.solr.SolrTestCaseJ4;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.impl.CloudSolrClient;
 import org.apache.solr.client.solrj.request.CollectionAdminRequest;
 import org.apache.solr.client.solrj.response.CollectionAdminResponse;
 import org.apache.solr.cloud.api.collections.TimeRoutedAlias;
+import org.apache.solr.common.SolrInputDocument;
 import org.apache.solr.common.cloud.Aliases;
 import org.apache.solr.common.cloud.CompositeIdRouter;
 import org.apache.solr.common.cloud.DocCollection;
 import org.apache.solr.common.cloud.ImplicitDocRouter;
 import org.apache.solr.common.cloud.Replica;
 import org.apache.solr.common.cloud.ZkStateReader;
+import org.apache.solr.common.util.TimeSource;
+import org.apache.solr.embedded.JettySolrRunner;
 import org.apache.solr.util.DateMathParser;
+import org.apache.solr.util.TimeOut;
 import org.eclipse.jetty.client.Request;
 import org.eclipse.jetty.client.StringRequestContent;
 import org.junit.After;
@@ -177,6 +184,94 @@ public class CreateRoutedAliasTest extends 
SolrCloudTestCase {
     assertEquals(createNode, meta.get("create-collection.createNodeSet"));
   }
 
+  /**
+   * Fixing a date math router.start when the first document arrives must not 
undo alias changes
+   * that the node handling the document hasn't seen yet.
+   */
+  @Test
+  @SuppressForbidden(
+      reason = "Reflection needed to give a node cached aliases that are 
behind ZooKeeper")
+  public void testFixingStartKeepsOtherAliases() throws Exception {
+    final String aliasName = getSaferTestName();
+    CollectionAdminRequest.createTimeRoutedAlias(
+            aliasName,
+            "NOW/DAY",
+            "+1DAY",
+            "evt_dt",
+            CollectionAdminRequest.createCollection("_unused_", "_default", 1, 
1))
+        .process(solrClient);
+    ZkStateReader clientReader = cluster.getZkStateReader();
+    clientReader.aliasesManager.update();
+    final String collectionName =
+        
clientReader.getAliases().getCollectionAliasListMap().get(aliasName).get(0);
+    cluster.waitForActiveCollection(collectionName, 1, 1);
+
+    // send the update to the node hosting the collection, so that node 
handles it itself
+    String nodeName =
+        clientReader
+            .getCollection(collectionName)
+            .replicaStream()
+            .findFirst()
+            .orElseThrow()
+            .getNodeName();
+    JettySolrRunner jetty =
+        cluster.getJettySolrRunners().stream()
+            .filter(j -> j.getNodeName().equals(nodeName))
+            .findFirst()
+            .orElseThrow();
+    ZkStateReader reader = 
jetty.getCoreContainer().getZkController().getZkStateReader();
+    int zkVersion = cluster.getZkClient().exists(ZkStateReader.ALIASES, 
null).getVersion();
+    new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME)
+        .waitFor(
+            "node did not catch up with aliases.json",
+            () -> reader.getAliases().getZNodeVersion() == zkVersion);
+    Aliases aliasesBeforeOtherAlias = reader.getAliases();
+
+    // another alias is created and a property is set on this one, and the 
node is put back to
+    // before it saw them, as if the document arrived before the node 
processed the watch events
+    final String otherAlias = aliasName + "_other";
+    CollectionAdminRequest.createAlias(otherAlias, 
collectionName).process(solrClient);
+    CollectionAdminRequest.setAliasProperty(aliasName)
+        .addProperty("foo", "bar")
+        .process(solrClient);
+    new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME)
+        .waitFor(
+            "node did not see the alias changes",
+            () ->
+                reader.getAliases().hasAlias(otherAlias)
+                    && "bar"
+                        .equals(
+                            reader
+                                .getAliases()
+                                .getCollectionAliasProperties(aliasName)
+                                .get("foo")));
+    Field aliasesField = 
ZkStateReader.AliasesManager.class.getDeclaredField("aliases");
+    aliasesField.setAccessible(true);
+    aliasesField.set(reader.aliasesManager, aliasesBeforeOtherAlias);
+    assertSame(
+        "the node's cached aliases were refreshed before the document was 
sent",
+        aliasesBeforeOtherAlias,
+        reader.getAliases());
+
+    SolrInputDocument doc = new SolrInputDocument("id", "1", "evt_dt", 
Instant.now().toString());
+    assertEquals(0, jetty.getSolrClient().add(collectionName, 
doc).getStatus());
+
+    // router.start is fixed asynchronously
+    new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME)
+        .waitFor(
+            "router.start was not fixed",
+            () ->
+                !"NOW/DAY"
+                    .equals(
+                        clientReader
+                            .getAliases()
+                            .getCollectionAliasProperties(aliasName)
+                            .get(TimeRoutedAlias.ROUTER_START)));
+    assertEquals(collectionName, 
clientReader.getAliases().getCollectionAliasMap().get(otherAlias));
+    assertEquals(
+        "bar", 
clientReader.getAliases().getCollectionAliasProperties(aliasName).get("foo"));
+  }
+
   @Test
   public void testV1() throws Exception {
     final String aliasName = getSaferTestName();
diff --git 
a/solr/core/src/test/org/apache/solr/cloud/api/collections/TimeRoutedAliasFixStartTest.java
 
b/solr/core/src/test/org/apache/solr/cloud/api/collections/TimeRoutedAliasFixStartTest.java
new file mode 100644
index 00000000000..fc035e97c9b
--- /dev/null
+++ 
b/solr/core/src/test/org/apache/solr/cloud/api/collections/TimeRoutedAliasFixStartTest.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF 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.apache.solr.cloud.api.collections;
+
+import static 
org.apache.solr.cloud.api.collections.TimeRoutedAlias.ROUTER_START;
+
+import org.apache.solr.SolrTestCase;
+import org.apache.solr.common.cloud.Aliases;
+import org.junit.Test;
+
+/** Tests {@link TimeRoutedAlias#fixStart}, which fixes a date math 
router.start to an instant. */
+public class TimeRoutedAliasFixStartTest extends SolrTestCase {
+
+  private static final String ALIAS = "tra";
+  private static final String DATE_MATH = "NOW/DAY";
+  private static final String FIXED = "2026-10-30T00:00:00Z";
+
+  private static Aliases aliasesWithStart(String start) {
+    return Aliases.EMPTY
+        .cloneWithCollectionAlias(ALIAS, "tra__TRA__2026-10-30")
+        .cloneWithCollectionAliasProperties(ALIAS, ROUTER_START, start);
+  }
+
+  @Test
+  public void testFixesDateMathStart() {
+    Aliases fixed = TimeRoutedAlias.fixStart(aliasesWithStart(DATE_MATH), 
ALIAS, DATE_MATH, FIXED);
+    assertEquals(FIXED, 
fixed.getCollectionAliasProperties(ALIAS).get(ROUTER_START));
+  }
+
+  @Test
+  public void testKeepsOtherAliasesAndProperties() {
+    Aliases aliases =
+        aliasesWithStart(DATE_MATH)
+            .cloneWithCollectionAlias("other", "collection1")
+            .cloneWithCollectionAliasProperties(ALIAS, "foo", "bar");
+    Aliases fixed = TimeRoutedAlias.fixStart(aliases, ALIAS, DATE_MATH, FIXED);
+    assertEquals("collection1", fixed.getCollectionAliasMap().get("other"));
+    assertEquals("bar", fixed.getCollectionAliasProperties(ALIAS).get("foo"));
+    assertEquals(FIXED, 
fixed.getCollectionAliasProperties(ALIAS).get(ROUTER_START));
+  }
+
+  @Test
+  public void testDoesNotChangeAnAlreadyFixedStart() {
+    // e.g. a node whose cached aliases were stale computed a later start
+    Aliases aliases = aliasesWithStart(FIXED);
+    assertSame(
+        aliases, TimeRoutedAlias.fixStart(aliases, ALIAS, DATE_MATH, 
"2026-10-31T00:00:00Z"));
+  }
+
+  @Test
+  public void testDoesNotRecreateADeletedAlias() {
+    Aliases aliases = Aliases.EMPTY.cloneWithCollectionAlias("other", 
"collection1");
+    assertSame(aliases, TimeRoutedAlias.fixStart(aliases, ALIAS, DATE_MATH, 
FIXED));
+  }
+}

Reply via email to