lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g..@apache.org
Subject [lucene-solr] 03/05: SOLR-12348: move start collection creation logic to TimeRoutedAlias
Date Mon, 28 Jan 2019 23:07:38 GMT
This is an automated email from the ASF dual-hosted git repository.

gus pushed a commit to branch solr-13131
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git

commit 5c90eede4913173f68d02edd7a54d48f2ef4d22c
Author: Moshe <moshebla@mail.com>
AuthorDate: Mon Jan 28 02:06:47 2019 +0200

    SOLR-12348: move start collection creation logic to TimeRoutedAlias
---
 .../cloud/api/collections/CategoryRoutedAlias.java |  6 +++
 .../solr/cloud/api/collections/CreateAliasCmd.java | 48 +++++++++-------------
 .../solr/cloud/api/collections/RoutedAlias.java    | 11 +++++
 .../cloud/api/collections/TimeRoutedAlias.java     | 20 +++++++++
 4 files changed, 56 insertions(+), 29 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/CategoryRoutedAlias.java
b/solr/core/src/java/org/apache/solr/cloud/api/collections/CategoryRoutedAlias.java
index bc565f03..3ac09c6 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/CategoryRoutedAlias.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/CategoryRoutedAlias.java
@@ -20,6 +20,7 @@ package org.apache.solr.cloud.api.collections;
 import java.time.Instant;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 
 import org.apache.solr.cloud.ZkController;
 import org.apache.solr.common.cloud.Aliases;
@@ -59,4 +60,9 @@ public class CategoryRoutedAlias implements RoutedAlias {
   public String createCollectionsIfRequired(AddUpdateCommand cmd) {
     return null;
   }
+
+  @Override
+  public String computeInitialCollectionName(String collection) {
+    return null;
+  }
 }
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateAliasCmd.java
b/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateAliasCmd.java
index 61931d9..dd8edc1 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateAliasCmd.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/CreateAliasCmd.java
@@ -17,17 +17,13 @@
  */
 package org.apache.solr.cloud.api.collections;
 
-import java.time.Instant;
-import java.time.temporal.ChronoUnit;
 import java.util.Collections;
-import java.util.Date;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
-import java.util.TimeZone;
 import java.util.stream.Collectors;
 
 import org.apache.solr.common.SolrException;
@@ -37,7 +33,6 @@ import org.apache.solr.common.cloud.ZkStateReader;
 import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.common.util.StrUtils;
-import org.apache.solr.util.DateMathParser;
 
 import static org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST;
 
@@ -46,7 +41,7 @@ public class CreateAliasCmd extends AliasCmd {
   private final OverseerCollectionMessageHandler ocmh;
 
   private static boolean anyRoutingParams(ZkNodeProps message) {
-    return message.keySet().stream().anyMatch(k -> k.startsWith(TimeRoutedAlias.ROUTER_PREFIX));
+    return message.keySet().stream().anyMatch(k -> k.startsWith(RoutedAlias.ROUTER_PREFIX));
   }
 
   public CreateAliasCmd(OverseerCollectionMessageHandler ocmh) {
@@ -113,35 +108,30 @@ public class CreateAliasCmd extends AliasCmd {
         .filter(entry -> TimeRoutedAlias.PARAM_IS_PROP.test(entry.getKey()))
         .forEach(entry -> aliasProperties.put(entry.getKey(), (String) entry.getValue()));
// way easier than .collect
 
-    TimeRoutedAlias timeRoutedAlias = new TimeRoutedAlias(aliasName, aliasProperties); //
validates as well
-
-    String start = message.getStr(TimeRoutedAlias.ROUTER_START);
-    Instant startTime = parseStart(start, timeRoutedAlias.getTimeZone());
-
-    String initialCollectionName = TimeRoutedAlias.formatCollectionNameFromInstant(aliasName,
startTime);
-
-    // Create the collection
-    createCollectionAndWait(state, aliasName, aliasProperties, initialCollectionName, ocmh);
-    validateAllCollectionsExistAndNoDups(Collections.singletonList(initialCollectionName),
zkStateReader);
+    RoutedAlias timeRoutedAlias = new TimeRoutedAlias(aliasName, aliasProperties); // validates
as well
+
+    String start = message.getStr(RoutedAlias.ROUTER_START);
+    if (start != null) {
+      String initialCollectionName = timeRoutedAlias.computeInitialCollectionName(start);
+      if (initialCollectionName != null) {
+        ensureCollection(aliasName, zkStateReader, state, aliasProperties, initialCollectionName);
+        // Create/update the alias
+        zkStateReader.aliasesManager.applyModificationAndExportToZk(aliases -> aliases
+            .cloneWithCollectionAlias(aliasName, initialCollectionName)
+            .cloneWithCollectionAliasProperties(aliasName, aliasProperties));
+        return;
+      }
+    }
 
     // Create/update the alias
     zkStateReader.aliasesManager.applyModificationAndExportToZk(aliases -> aliases
-        .cloneWithCollectionAlias(aliasName, initialCollectionName)
         .cloneWithCollectionAliasProperties(aliasName, aliasProperties));
   }
 
-  private Instant parseStart(String str, TimeZone zone) {
-    Instant start = DateMathParser.parseMath(new Date(), str, zone).toInstant();
-    checkMilis(start);
-    return start;
-  }
-
-  private void checkMilis(Instant date) {
-    if (!date.truncatedTo(ChronoUnit.SECONDS).equals(date)) {
-      throw new SolrException(BAD_REQUEST,
-          "Date or date math for start time includes milliseconds, which is not supported.
" +
-              "(Hint: 'NOW' used without rounding always has this problem)");
-    }
+  private void ensureCollection(String aliasName, ZkStateReader zkStateReader, ClusterState
state, Map<String, String> aliasProperties, String initialCollectionName) throws Exception
{
+    // Create the collection
+    createCollectionAndWait(state, aliasName, aliasProperties, initialCollectionName, ocmh);
+    validateAllCollectionsExistAndNoDups(Collections.singletonList(initialCollectionName),
zkStateReader);
   }
 
   private void validateAllCollectionsExistAndNoDups(List<String> collectionList, ZkStateReader
zkStateReader) {
diff --git a/solr/core/src/java/org/apache/solr/cloud/api/collections/RoutedAlias.java b/solr/core/src/java/org/apache/solr/cloud/api/collections/RoutedAlias.java
index dd7240f..b004d79 100644
--- a/solr/core/src/java/org/apache/solr/cloud/api/collections/RoutedAlias.java
+++ b/solr/core/src/java/org/apache/solr/cloud/api/collections/RoutedAlias.java
@@ -20,7 +20,9 @@ package org.apache.solr.cloud.api.collections;
 import java.time.Instant;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
+import com.google.common.collect.Sets;
 import org.apache.solr.cloud.ZkController;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.cloud.Aliases;
@@ -32,7 +34,9 @@ public interface RoutedAlias {
   String ROUTER_TYPE_NAME = ROUTER_PREFIX + "name";
   String ROUTER_FIELD = ROUTER_PREFIX + "field";
   String ROUTER_AUTO_DELETE_AGE = ROUTER_PREFIX + "autoDeleteAge";
+  String ROUTER_START = ROUTER_PREFIX + "start";
   String CREATE_COLLECTION_PREFIX = "create-collection.";
+  Set<String> MINIMAL_REQUIRED_PARAMS = Sets.newHashSet(ROUTER_TYPE_NAME, ROUTER_FIELD);
   String ROUTED_ALIAS_NAME_CORE_PROP = "routedAliasName"; // core prop
 
   static SolrException newAliasMustExistException(String aliasName) {
@@ -47,6 +51,13 @@ public interface RoutedAlias {
    */
   boolean updateParsedCollectionAliases(ZkController zkController);
 
+  /**
+   *
+   * @param startParam the start parameter passed to create alias cmd
+   * @return optional string of initial collection name
+   */
+  String computeInitialCollectionName(String startParam);
+
   String getAliasName();
 
   /**
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 a4b29ea..007e2ce 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
@@ -24,6 +24,7 @@ import java.time.ZoneOffset;
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatterBuilder;
 import java.time.temporal.ChronoField;
+import java.time.temporal.ChronoUnit;
 import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -119,6 +120,11 @@ public class TimeRoutedAlias implements RoutedAlias {
       .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
       .toFormatter(Locale.ROOT).withZone(ZoneOffset.UTC); // deliberate -- collection names
disregard TZ
 
+  @Override
+  public String computeInitialCollectionName(String dateStr) {
+    return formatCollectionNameFromInstant(aliasName, parseStringAsInstant(dateStr, timeZone));
+  }
+
   public static Instant parseInstantFromCollectionName(String aliasName, String collection)
{
     final String dateTimePart = collection.substring(aliasName.length() + 1);
     return DATE_TIME_FORMATTER.parse(dateTimePart, Instant::from);
@@ -135,6 +141,20 @@ public class TimeRoutedAlias implements RoutedAlias {
     return aliasName + "_" + nextCollName;
   }
 
+  Instant parseStringAsInstant(String str, TimeZone zone) {
+    Instant start = DateMathParser.parseMath(new Date(), str, zone).toInstant();
+    checkMilis(start);
+    return start;
+  }
+
+  private void checkMilis(Instant date) {
+    if (!date.truncatedTo(ChronoUnit.SECONDS).equals(date)) {
+      throw new SolrException(BAD_REQUEST,
+          "Date or date math for start time includes milliseconds, which is not supported.
" +
+              "(Hint: 'NOW' used without rounding always has this problem)");
+    }
+  }
+
 
   @Override
   public boolean updateParsedCollectionAliases(ZkController zkController) {


Mime
View raw message