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

Scaling Policies

This post is part of a bigger topic Autoscaling Publishers in AWS . In a previous post we talked about the Auto Scaling Groups , but we didn't go into details on the Scaling Policies. This is the purpose of this blog post. As defined earlier, the Scaling Policies define the rules according to which the group size is increased or decreased. These rules are based on instance metrics (e.g. CPU), CloudWatch custom metrics, or even CloudWatch alarms and their states and values. We defined a Scaling Policy with Steps, called 'increase_group_size', which is triggered first by the CloudWatch Alarm 'Publish_Alarm' defined earlier. Also depending on the size of the monitored CloudWatch custom metric 'Waiting for Publish', the Scaling Policy with Steps can add a difference number of instances to the group. The scaling policy sets the number of instances in group to 1 if there are between 1000 and 2000 items Waiting for Publish in the queue. It also sets the

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

Toolkit - Dynamic Content Queries

This post if part of a series about the  File System Toolkit  - a custom content delivery API for SDL Tridion. This post presents the Dynamic Content Query capability. The requirements for the Toolkit API are that it should be able to provide CustomMeta queries, pagination, and sorting -- all on the file system, without the use third party tools (database, search engines, indexers, etc). Therefore I had to implement a simple database engine and indexer -- which is described in more detail in post Writing My Own Database Engine . The querying logic does not make use of cache. This means the query logic is executed every time. When models are requested, the models are however retrieved using the ModelFactory and those are cached. Query Class This is the main class for dynamic content queries. It is the entry point into the execution logic of a query. The class takes as parameter a Criterion (presented below) which triggers the execution of query in all sub-criteria of a Criterio