Skip to main content

Got RAM?

I remember my first experience with .Net Framework and C# after years of MFC/COM/DCOM torture with lovely C++. Whoa, I don't have to track every reference and resource allocation? That GC thing rules! No more memory leaks! Right...

Let's say, you're tasked with integrating Sitecore Commerce 9 and some PIM. It totally makes sense to use the Sitecore Commerce catalog system, so your integration would imply importing catalog structure and contents from PIM. While API capabilities of PIMs may vary, pretty much every type of PIM supports file exports of catalog. The native Sitecore Commerce catalog import/export is still in its infancy (no support for partial, incremental, or selective operations, not to mention lack of converters into its pretty unorthodox format, as well as inability to import product assets), so your only option is to build a custom catalog import plugin. The code for creating catalogs, categories and sellable items is relatively well-known and somewhat documented, so there should not be any surprises. You create a minion that watches for new PIM exports and consumes them. Everything is very straightforward. You create pipelines for updating categories, products, variants, and corresponding relationships. In order to perform operations with entities, you need instances of commerce commands. IoC takes care of that for you with something like this:

public ImportCategoriesBlock(CreateCategoryCommand createCategoryCommand)
{
this.createCategoryCommand = createCategoryCommand;
}

OK, now you can iterate through the new categories and use this command to create them in catalog. For example:

foreach (var category in categories)
{
var createdCategory = await this.createCategoryCommand.Process(context.CommerceContext, catalogName, category.CategoryName, category.DisplayName, category.Description);
}

Easy! And it works just fine in tests. However, when you decide it to run against your full catalog with dozens of thousands categories, the Commerce Engine suddenly starts eating up gigabytes of your precious RAM. How could that be even happening? Moreover, memory gets eaten much faster if you use several commerce commands within the same pipeline block - for example, CreateCategoryCommand, AssociateCategoryToParentCommand, EditCategoryCommand, DeleteCategoryCommand, and DeleteRelationshipCommand for the full category synchronization package.

All commands in Sitecore Commerce 9 derive from the Sitecore.Commerce.Core.Commands.CommerceCommand base class, which in turn contains Models property:

namespace Sitecore.Commerce.Core.Commands
{
public class CommerceCommand : ICommerceCommand
{
/*.....*/
public List<Model> Models { get; set; }
/*.....*/
}
}

Every time a command is used, this property gets populated by CommandActivity with the full instances of the entity models that were affected by that command. This includes versions, localizations, and such entity types:

namespace Sitecore.Commerce.Core
{
public class CommandActivity : Activity
{
/*...*/
private static void Activity_ActivityComplete(object sender, EventArgs e)
{
/*...*/
commandActivity.Command.Models.AddRange((IEnumerable<Model>) commandActivity.Context.GetModels());
/*...*/
}
/*...*/
}
}
Eventually a poor command ends up with hundreds of thousands hefty models in its Models property. This is a common architecture approach - to collect data updates and write them in a single batch before the disposal of the command object thus reducing number of calls to the database. However, it does not exactly work with huge number of updated entities. I'd like to think that Sitecore is planning on implementing some kind of collection monitor that would drain the Models list once it reaches a certain size. For now (as of Commerce 9 Update 3) that is your responsibility. While it might look like a bad design decision from the performance point of view, instantiating a new command before each use made import considerably faster. Don't forget, you can always do it using the CommerceCommander:

var createCategoryCommand = this.commerceCommander.Command<CreateCategoryCommand>();
Of course, you can improve performance even further by creating a new command after each 100th, 1000th, or 10000th use. Just watch your RAM...

Comments

Popular posts from this blog

SharePoint 2013 and "search scopes"

While SharePoint 2013 introduced some undeniably handy features in the content discovery department, some of the old functionality has changed so much that you'll have to forget many of the design tricks that worked in 2007 and 2010.  One of such pain points is deprecating search scopes in SharePoint 2013. The requirement Let's start with a trivial situation. You have a requirement that states: "A user should be able to perform different types of search from any page/sub site on the portal by selecting a type of search and specifying keyword(s)". This would be considered an out-of-box functionality in 2007/2010: you just enable search scopes dropdown (considering the search box control is already in the master page), and define scopes. Piece of cake! Well, not so much in 2013.   The problem The thing is - there's no search scopes in SharePoint 2013. OK, to be exact - there's no user-definable search scopes, as we still have a couple of built-in ...

Navigation Settings in SharePoint 2013 Site Definitions

Who likes creating SharePoint site definitions from scratch? Well, it's not my favorite part of the SharePoint development either. An initially good idea of setting up your whole site through an XML file, received implementation and support far from being perfect. That quickly lead developers to look for alternative ways of defining and provisioning sites, primarily through the SharePoint API. A simple ONET.XML with beefy code-packed features became de-facto standard. What was supposed to be a template became compiled code. I was hoping SharePoint 2013 would at least attempt to do something about it… Tasked with creating a few publishing portal site definitions, I was unpleasantly surprised to find out that any attempt to use my new site definition to provision a site collection root results in both Global and Current Navigation switched to "Managed Navigation" mode, while I needed it to stay in the good old "Structural Navigation" one. This mode is new to Sh...

Catalog versioning in Sitecore Commerce Engine

Since the release of Sitecore Experience Commerce 9.0 Update-2 entity versioning is enabled by default for all catalog-related commerce entities: Catalogs , Categories and SellableItems . While certainly being a desirable feature, versioning can have a negative effect in certain use cases. Here are a couple of examples: One of the clients decided to use the BizFx business tools as a Product Information Management system. While one can argue on effectiveness of such decision, the real pain came up after the 9.0.2 upgrade when a quick 2-step process of updating the product description suddenly became an unintuitive and clumsy struggle through a dozen of steps. The client absolutely hated that experience and did not really care about versioning. Another client was using an external PIM that we were integrating with by synchronizing the deltas with SXC Catalog system. In 9.0.2 that resulted in creating a new version for every entity update. Not only these versions were useless (sin...