Skip to main content

Writing to Multiple File Systems from the Same Deployer

This topic is not new. It comes back regularly, pretty much with every single enterprise client I have implemented. "Why do we need different Deployers for different file systems?". "Why can't just Tridion publish to different file systems?". And so on...

Recently, it came up again, so I setup a small PoC to see how feasible it is to write a Storage Extension (in SDL Tridion 2011SP1) that would perform the typical CRUD operations a Deployer would perform, only on multiple file systems.

The idea behind the storage extension is to have several file systems defined in the cd_storage_conf.xml that would be grouped under one logical name. Then have an item type mapping (e.g. Page) that would point to the group of file system. The goal is to have that item type created, updated, removed, etc on each of the file systems defined in the group.

The cd_storage_conf.xml Storages node would look something like this:


    <Storage Type="filesystem" Class="com.tridion.storage.filesystem.FSDAOFactory"
        Id="MultiFS" defaultFilesystem="false">
        <Root Path="not-used" />
    </Storage>

    <Storage Type="filesystem" Class="com.tridion.storage.filesystem.FSDAOFactory"
        Id="FileRoot1" defaultFilesystem="false">
        <Root Path="C:\Temp\Root1" />
    </Storage>

    <Storage Type="filesystem" Class="com.tridion.storage.filesystem.FSDAOFactory"
        Id="FileRoot2" defaultFilesystem="false">
        <Root Path="D:\Temp\Root2" />
    </Storage>

    <StorageGroup Id="MultiFS">
        <Storage Id="FileRoot1"/>
        <Storage Id="FileRoot2"/>
    </StorageGroup>


Item mapping for the Page type would point to the MultiFS id:


    <ItemTypes defaultStorageId="brokerdb" cached="true">
        <Item typeMapping="Page" cached="false" storageId="MultiFS" />
    </ItemTypes>


In order to make the setup-above work, I had to create my own DAO (Data Access Object) storage extension. There is a reference to the DAO bundle definition in the cd_storage_conf.xml:


    <StorageBindings>
        <Bundle src="multifs_dao_bundle.xml" />
    </StorageBindings>


The file multifs_dao_bundle.xml contains the definition of my custom DAO:


<StorageDAOBundles>
    <StorageDAOBundle type="filesystem">
        <StorageDAO typeMapping="Page"
            class="com.tridion.extension.multifs.MultiFSDAO" />
    </StorageDAOBundle>
</StorageDAOBundles>


The whole logic lies in the class MultiFSDAO, which acts like a wrapper around an array of com.tridion.storage.filesystem.FSPageDAO objects. A helper configuration class reads the StorageGroup node from cd_storage_conf.xml and then reads the Root/@path (i.e. storage location) value for each referenced Storage node.


public class MultiFSDAO extends FSBaseDAO implements PageDAO {

    private FSPageDAO[] pageDAOs;

    public MultiFSDAO(String storageId, String storageName, File storageLocation) {
        super(storageId, storageName, storageLocation);
        createDAOs(storageId, storageName, null);
    }

    public MultiFSDAO(String storageId, String storageName, File storageLocation, FSEntityManager entityManager) {
        super(storageId, storageName, storageLocation, entityManager);
        createDAOs(storageId, storageName, entityManager);
    }

    private void createDAOs(String storageId, String storageName, FSEntityManager entityManager) {
        MultiFSConfiguration configuration = MultiFSConfiguration.getInstance();
        Map<String, String> storageGroups = configuration.getStorageGroups();
        String groups = storageGroups.get(storageId);
        if (groups == null) {
            groups = storageId;
        }

        String storageIds[] = groups.split(",");
        pageDAOs = new FSPageDAO[storageIds.length];
        Map<String, String> storageLocations = configuration.getStorageLocations();

        for (int i = 0; i < storageIds.length; i++) {
            String id = storageIds[i];
            String location = storageLocations.get(id);

            if (entityManager == null) {
                pageDAOs[i] = new FSPageDAO(id, storageName, new File(location));
            } else {
                pageDAOs[i] = new FSPageDAO(id, storageName, new File(location), entityManager);
            }
        }
    }


Once we have the array of FSPageDAO objects, it's a simple matter of just implementing the CRUD operations on the collection of FSPageDAOs.


public void create(CharacterData page, String relativePath) throws StorageException {
    for (PageDAO pageDAO : pageDAOs) {
        pageDAO.create(page, relativePath);
    }
}

public Collection<CharacterData> findAll(int publicationId) throws StorageException {
    Collection<CharacterData> result = null;
    for (PageDAO pageDAO : pageDAOs) {
        result = pageDAO.findAll(publicationId);
    }

    return result;
}

public CharacterData findByPrimaryKey(int publicationId, int pageId) throws StorageException {
    CharacterData result = null;
    for (PageDAO pageDAO : pageDAOs) {
        result = pageDAO.findByPrimaryKey(publicationId, pageId);
    }

    return result;
}

public void remove(int publicationId, int pageId, String relativePath) throws StorageException {
    for (PageDAO pageDAO : pageDAOs) {
        pageDAO.remove(publicationId, pageId, relativePath);
    }
}

public void update(CharacterData page, String originalRelativePath, String newRelativePath) throws StorageException {
    for (PageDAO pageDAO : pageDAOs) {
        pageDAO.update(page, originalRelativePath, newRelativePath);
    }
}


The big disclaimer: the code-above is by no means production ready -- I just used it for a small PoC. I have not tested it thoroughly either. It does deploy pages to multiple file systems, but I did not try any corner cases. I don't even think it works in all scenarios: think about here at transactionality, or what happens (or should happen) if a destination failed. The deploy will not be rolled back. What happens upon unpublish of a previously failed published? And the questions could go on... Use at your own discretion!

Comments

Popular posts from this blog

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as

Event System to Create Mapped Structure Groups for Binary Publish

As a continuation of last week's Publish Binaries to Mapped Structure Group , this week's TBB is in fact the Event System part of that solution. Make sure you do check out the previous post first, which explains why and what this Event System does. To reiterate, the Event System intercepts a Multimedia Component save, take its Folder path and create a 1-to-1 mapping of Structure Groups. The original code was written, again, by my colleague Eric Huiza : [ TcmExtension ( "MyEvents" )] public class EventsManager  : TcmExtension {     private Configuration configuration;     private readonly Regex SAFE_DIRNAME_REGEX = new Regex ( @"[\W_]+" );     public EventsManager() {         ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap ();         fileMap.ExeConfigFilename = Path .GetDirectoryName( Assembly .GetExecutingAssembly().Location) + "\\EventSystem.config" ;         configuration = ConfigurationManager