patsonluk commented on code in PR #2438:
URL: https://github.com/apache/solr/pull/2438#discussion_r1633899859


##########
solr/core/src/java/org/apache/solr/servlet/CoordinatorHttpSolrCall.java:
##########
@@ -88,198 +81,70 @@ public static SolrCore getCore(
     String syntheticCoreName = 
factory.collectionVsCoreNameMapping.get(collectionName);
     if (syntheticCoreName != null) {
       SolrCore syntheticCore = solrCall.cores.getCore(syntheticCoreName);
-      setMDCLoggingContext(collectionName);
+      setMdcLoggingContext(collectionName);
       return syntheticCore;
     } else {
+      // first time loading this collection
       ZkStateReader zkStateReader = 
solrCall.cores.getZkController().getZkStateReader();
       ClusterState clusterState = zkStateReader.getClusterState();
       DocCollection coll = clusterState.getCollectionOrNull(collectionName, 
true);
-      SolrCore core = null;
-      if (coll != null) {
-        String confName = coll.getConfigName();
-        String syntheticCollectionName = getSyntheticCollectionName(confName);
 
-        DocCollection syntheticColl = 
clusterState.getCollectionOrNull(syntheticCollectionName);
-        synchronized (CoordinatorHttpSolrCall.class) {
-          if (syntheticColl == null) {
-            // no synthetic collection for this config, let's create one
-            if (log.isInfoEnabled()) {
-              log.info(
-                  "synthetic collection: {} does not exist, creating.. ", 
syntheticCollectionName);
-            }
-
-            SolrException createException = null;
-            try {
-              createColl(syntheticCollectionName, solrCall.cores, confName);
-            } catch (SolrException exception) {
-              // concurrent requests could have created the collection hence 
causing collection
-              // exists
-              // exception
-              createException = exception;
-            } finally {
-              syntheticColl =
-                  
zkStateReader.getClusterState().getCollectionOrNull(syntheticCollectionName);
-            }
-
-            // then indeed the collection was not created properly, either by 
this or other
-            // concurrent
-            // requests
-            if (syntheticColl == null) {
-              if (createException != null) {
-                throw createException; // rethrow the exception since such 
collection was not
-                // created
-              } else {
-                throw new SolrException(
-                    SolrException.ErrorCode.SERVER_ERROR,
-                    "Could not locate synthetic collection ["
-                        + syntheticCollectionName
-                        + "] after creation!");
-              }
-            }
-          }
-
-          // get docCollection again to ensure we get the fresh state
-          syntheticColl =
-              
zkStateReader.getClusterState().getCollectionOrNull(syntheticCollectionName);
-          List<Replica> nodeNameSyntheticReplicas =
-              
syntheticColl.getReplicas(solrCall.cores.getZkController().getNodeName());
-          if (nodeNameSyntheticReplicas == null || 
nodeNameSyntheticReplicas.isEmpty()) {
-            // this node does not have a replica. add one
-            if (log.isInfoEnabled()) {
-              log.info(
-                  "this node does not have a replica of the synthetic 
collection: {} , adding replica ",
-                  syntheticCollectionName);
-            }
+      if (coll == null) { // querying on a non-existent collection, it could 
have been removed
+        log.info(
+            "Cannot find collection {} to proxy call to, it could have been 
deleted",
+            collectionName);
+        return null;
+      }
 
-            addReplica(syntheticCollectionName, solrCall.cores);
-          }
+      String confName = coll.getConfigName();
+      syntheticCoreName = getSyntheticCoreName(confName);
+
+      SolrCore syntheticCore;
+      synchronized (CoordinatorHttpSolrCall.class) {
+        CoreContainer coreContainer = solrCall.cores;
+        syntheticCore = coreContainer.getCore(syntheticCoreName);
+        if (syntheticCore == null) {
+          // first time loading this config set
+          log.info("Loading synthetic core for config set {}", confName);
+          syntheticCore =
+              SyntheticSolrCore.createAndRegisterCore(
+                  coreContainer, syntheticCoreName, coll.getConfigName());
+          // getting the core should open it
+          syntheticCore.open();
+        }
 
-          // still have to ensure that it's active, otherwise 
super.getCoreByCollection
-          // will return null and then CoordinatorHttpSolrCall will call 
getCore again
-          // hence creating a calling loop
-          try {
-            zkStateReader.waitForState(
-                syntheticCollectionName,
-                10,
-                TimeUnit.SECONDS,
-                docCollection -> {
-                  List<Replica> replicas =
-                      
docCollection.getReplicas(solrCall.cores.getZkController().getNodeName());
-                  if (replicas == null || replicas.isEmpty()) {
+        factory.collectionVsCoreNameMapping.put(collectionName, 
syntheticCore.getName());
+
+        // for the watcher, only remove on collection deletion (ie collection 
== null), since
+        // watch from coordinator is collection specific
+        coreContainer
+            .getZkController()
+            .getZkStateReader()
+            .registerDocCollectionWatcher(
+                collectionName,
+                collection -> {
+                  if (collection == null) {
+                    factory.collectionVsCoreNameMapping.remove(collectionName);
+                    return true;
+                  } else {
                     return false;
                   }
-                  for (Replica nodeNameSyntheticReplica : replicas) {
-                    if (nodeNameSyntheticReplica.getState() == 
Replica.State.ACTIVE) {
-                      return true;
-                    }
-                  }
-                  return false;
                 });
-          } catch (Exception e) {
-            throw new SolrException(
-                SolrException.ErrorCode.SERVER_ERROR,
-                "Failed to wait for active replica for synthetic collection ["
-                    + syntheticCollectionName
-                    + "]",
-                e);
-          }
-        }
-
-        core = solrCall.getCoreByCollection(syntheticCollectionName, 
isPreferLeader);
-        if (core != null) {
-          factory.collectionVsCoreNameMapping.put(collectionName, 
core.getName());
-          // for the watcher, only remove on collection deletion (ie 
collection == null), since
-          // watch from coordinator is collection specific
-          solrCall
-              .cores
-              .getZkController()
-              .getZkStateReader()
-              .registerDocCollectionWatcher(
-                  collectionName,
-                  collection -> {
-                    if (collection == null) {
-                      
factory.collectionVsCoreNameMapping.remove(collectionName);
-                      return true;
-                    } else {
-                      return false;
-                    }
-                  });
-          if (log.isDebugEnabled()) {
-            log.debug("coordinator node, returns synthetic core: {}", 
core.getName());
-          }
-        }
-        setMDCLoggingContext(collectionName);
-        return core;
       }
-      return null;
+      setMdcLoggingContext(collectionName);
+      if (log.isDebugEnabled()) {
+        log.debug("coordinator node, returns synthetic core: {}", 
syntheticCore.getName());
+      }
+      return syntheticCore;
     }
   }
 
   public static String getSyntheticCollectionName(String configName) {
     return SYNTHETIC_COLL_PREFIX + configName;
   }
 
-  /**
-   * Overrides the MDC context as the core set was synthetic core, which does 
not reflect the
-   * collection being operated on
-   */
-  private static void setMDCLoggingContext(String collectionName) {
-    MDCLoggingContext.setCollection(collectionName);
-
-    // below is irrelevant for call to coordinator
-    MDCLoggingContext.setCoreName(null);
-    MDCLoggingContext.setShard(null);
-    MDCLoggingContext.setCoreName(null);
-  }
-
-  private static void addReplica(String syntheticCollectionName, CoreContainer 
cores) {
-    SolrQueryResponse rsp = new SolrQueryResponse();
-    try {
-      CollectionAdminRequest.AddReplica addReplicaRequest =
-          CollectionAdminRequest.addReplicaToShard(syntheticCollectionName, 
"shard1")
-              // we are fixing the name, so that no two replicas are created 
in the same node
-              .setNode(cores.getZkController().getNodeName());
-      addReplicaRequest.setWaitForFinalState(true);
-      cores
-          .getCollectionsHandler()
-          .handleRequestBody(new LocalSolrQueryRequest(null, 
addReplicaRequest.getParams()), rsp);
-      if (rsp.getValues().get("success") == null) {
-        throw new SolrException(
-            SolrException.ErrorCode.SERVER_ERROR,
-            "Could not auto-create collection: " + 
Utils.toJSONString(rsp.getValues()));
-      }
-    } catch (Exception e) {
-      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
-    }
-  }
-
-  private static void createColl(
-      String syntheticCollectionName, CoreContainer cores, String confName) {
-    SolrQueryResponse rsp = new SolrQueryResponse();
-    try {
-      CollectionAdminRequest.Create collCreationRequest =
-          CollectionAdminRequest.createCollection(syntheticCollectionName, 
confName, 1, 1)
-              .setCreateNodeSet(cores.getZkController().getNodeName());
-      collCreationRequest.setWaitForFinalState(true);
-      SolrParams params = collCreationRequest.getParams();
-      if (log.isInfoEnabled()) {
-        log.info("sending collection admin command : {}", 
Utils.toJSONString(params));
-      }
-      cores.getCollectionsHandler().handleRequestBody(new 
LocalSolrQueryRequest(null, params), rsp);
-      if (rsp.getValues().get("success") == null) {
-        throw new SolrException(
-            SolrException.ErrorCode.SERVER_ERROR,
-            "Could not create :"
-                + syntheticCollectionName
-                + " collection: "
-                + Utils.toJSONString(rsp.getValues()));
-      }
-    } catch (SolrException e) {
-      throw e;
-
-    } catch (Exception e) {
-      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
-    }
+  public static String getSyntheticCoreName(String configName) {

Review Comment:
   Sure! Using `getSyntheticCollectionNameFromConfig` and 
`getSyntheticCoreNameFromConfig` instead :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to