Sitecore SwitchOnRebuild Feature With SolrCloud

While rebuilding the index on your Sitecore CMS you may face scenarios where indexing data not populating in website search functionality, search stopped working, or slowdown. After completion of indexing again search functionality returns the results.

Here Sitecore introduces the very nice concept of SwitchOnRebuild. To understand it assume you have two cores for your Sitecore index active and passive. It means when you rebuild the Active index Sitecore will return the result using the Passive index and after rebuilding Sitecore will switch the Active index to serve the results. It’s a concept of switching of Sitecore index during the rebuild of the particular index to avoid downtime and search unavailability.

Recently when I was working on one migration project, when I deployed an application on the Azure Paas environment. I faced a similar type of issue while rebuilding the index it was throwing me the below error:

 Job started: Index_Update_IndexName=sitecore_web_index|#Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> SolrNet.Exceptions.SolrConnectionException: <?xml version="1.0" encoding="UTF-8"?>  
 <response>  
 <lst name="responseHeader">  
  <int name="status">400</int>  
  <int name="QTime">7</int>  
 </lst>  
 <lst name="error">  
  <lst name="metadata">  
   <str name="error-class">org.apache.solr.common.SolrException</str>  
   <str name="root-error-class">org.apache.solr.common.SolrException</str>  
  </lst>  
  <str name="msg">Not supported in SolrCloud</str>  
  <int name="code">400</int>  
 </lst>  
 </response>  
  ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.  
   at System.Net.HttpWebRequest.GetResponse()  
   at HttpWebAdapters.Adapters.HttpWebRequestAdapter.GetResponse()  
   at SolrNet.Impl.SolrConnection.GetResponse(IHttpWebRequest request)  
   at SolrNet.Impl.SolrConnection.Get(String relativeUrl, IEnumerable`1 parameters)  
   --- End of inner exception stack trace ---  
   at SolrNet.Impl.SolrConnection.Get(String relativeUrl, IEnumerable`1 parameters)  
   at SolrNet.Impl.LowLevelSolrServer.SendAndParseHeader(ISolrCommand cmd)  
   at Sitecore.ContentSearch.SolrProvider.SwitchOnRebuildSolrSearchIndex.SwapAfterRebuild()  
   at Sitecore.ContentSearch.SolrProvider.SwitchOnRebuildSolrSearchIndex.PerformRebuild(Boolean resetIndex, Boolean optimizeOnComplete, IndexingOptions indexingOptions, CancellationToken cancellationToken)  
   at Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.Rebuild(Boolean resetIndex, Boolean optimizeOnComplete)  
   --- End of inner exception stack trace ---  
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)  
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)  
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)  
   at Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj)  
   at Sitecore.Jobs.JobRunner.RunMethod(JobArgs args)  
   at (Object , Object )  
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)  
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists)  
   at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)  
   at Sitecore.Jobs.DefaultJob.DoExecute()  
   at Sitecore.Abstractions.BaseJob.ThreadEntry(Object state)  

The error indicates that I am running Solr in SolrCloud mode. The Solr SWAP operation (that is performed after rebuilding a SolrSearchIndex with the SwithOnRebuild feature enabled) is not supported in SolrCloud mode.

The SWAP command is designed to work with Solr Cores: SWAP and SolrCloud operate with collections and aliases.

To use the SwitchOnRebuild feature with SolrCloud you need to configure your search indexes.

You use the SwitchOnRebuildSolrSearchIndex class to rebuild and switch Solr indexes. This implementation uses Solr aliases instead of collection names.

The implementation in the SwitchOnRebuildSolrCloudSearchIndex class uses collection aliases: it uses the active alias for search and update operations and the rebuild alias for rebuild operations. When a rebuild operation finishes, the CREATEALIAS command swaps the collections of the aliases reference.

A SwitchOnRebuild index expects 4 parameters to be passed to the constructor:

  1. mainalias: The alias name for the primary index operations. The ContentSearch.Solr.EnforceAliasCreation setting controls whether the alias should be created automatically. It must have only one collection assigned to it.
  2. rebuildalias: The alias name for the rebuild index operations. The ContentSearch.Solr.EnforceAliasCreation setting controls whether the alias should be created automatically. It must have only one collection assigned to it.
  3. collection: The primary index collection name.
  4. rebuildcollection: The rebuild index collection name.

Solutions:

  1. Go to the App_Config\Include\Examples folder and copy the Sitecore.ContentSearch.SolrCloud.SwitchOnRebuild.config.example file. Remove the .example extension.
  2. Add it to your project and change the index name if it does not match your index name.
  3. By default ContentSearch.Solr.EnforceAliasCreation setting is false. If its true index aliases will be created on Solr during the index initialization process and mapped to the collections if they do not exist.
  4. Go to your Solr Instance and Add a new core for the rebuild collection.
  5. Now go to the Sitecore CMS and populate Solr managed schema using the control panel option.
  6. Populate your custom index.
  7. Go to the Indexing manager and rebuild the indexes.

You can verify your alias status in the SOLR cloud:

sitecore-switchonrebuild-feature-with-solrcloud-1

Now you can verify your SwitchOnRebuild configuration. Rebuild your index on Sitecore CM and verify the search functionality on the CD server and it will work fine without any functionality issues or downtime.

Reference: Switch Solr indexes

Happy Sitecoreing ðŸ˜Š

Comments

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Import CSV Data in Sitecore Using PowerShell: Part-3

Sitecore Technology MVP Journey 2022