view flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/BuilderPool.java @ 5779:ebec12def170

Datacage: Add a pool of builders to make it multi threadable. XML DOM is not thread safe. Therefore the old implementation only allowed one thread to use the builder at a time. As the complexity of the configuration has increased over time this has become a bottleneck of the whole application because it took quiet some time to build a result. Furthermore the builder code path is visited very frequent. So many concurrent requests were piled up resulting in long waits for the users. To mitigate this problem a round robin pool of builders is used now. Each of the pooled builders has an independent copy of the XML template and can be run in parallel. The number of builders is determined by the system property 'flys.datacage.pool.size'. It defaults to 4.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 21 Apr 2013 12:48:09 +0200
parents
children d38004f0c52f
line wrap: on
line source
package de.intevation.flys.artifacts.datacage.templating;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
import java.util.Map;

import java.sql.SQLException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

/** A little round robin pool of builders to mitigate 
  * the fact the XML DOM documents are not thread safe.
  */
public class BuilderPool
{
    private static Logger log = Logger.getLogger(BuilderPool.class);

    private static final int DEFAULT_POOL_SIZE = 4;

    private static final int POOL_SIZE = Math.max(
        Integer.getInteger("flys.datacage.pool.size", DEFAULT_POOL_SIZE), 1);

    private Deque<Builder> pool;

    public BuilderPool(Document document) {
        this(document, POOL_SIZE);
    }

    public BuilderPool(Document document, int poolSize) {

        if (log.isDebugEnabled()) {
            log.debug("Create build pool with " + poolSize + " elements.");
        }

        pool = new ArrayDeque<Builder>(poolSize);
        for (int i = 0; i < poolSize; ++i) {
            Document doc = i > 0 // Clone all but the first.
                ? cloneDocument(document)
                : document;
            pool.add(new Builder(doc));
        }
    }

    private final static Document cloneDocument(Document document) {
        try {
            TransformerFactory tfactory = TransformerFactory.newInstance();
            Transformer xformer = tfactory.newTransformer();
            DOMSource src = new DOMSource(document);
            DOMResult dst = new DOMResult();
            xformer.transform(src, dst);
            return (Document)dst.getNode();
        }
        catch (TransformerConfigurationException tce) {
            log.error(tce);
        }
        catch (TransformerException te) {
            log.error(te);
        }

        log.error(
            "Returning original DOM document. " +
            "This will result in threading errors!");

        return document;
    }

    public void build(
        List<Builder.NamedConnection> connections,
        Node                          output,
        Map<String, Object>           parameters
    )
    throws SQLException
    {
        Builder builder;
        synchronized (pool) {
            try {
                while ((builder = pool.poll()) == null) {
                    pool.wait();
                }
            }
            catch (InterruptedException ie) {
                log.debug("Waiting for builder interrupted. Build canceled.");
                return;
            }
        }
        try {
            builder.build(connections, output, parameters);
        }
        finally {
            synchronized (pool) {
                pool.add(builder);
                pool.notify();
            }
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org