diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.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 cf4cc385e7c6
children
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java	Sun Apr 21 10:59:18 2013 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Recommendations.java	Sun Apr 21 12:48:09 2013 +0200
@@ -36,6 +36,7 @@
 import de.intevation.artifactdatabase.data.StateData;
 
 import de.intevation.flys.artifacts.datacage.templating.Builder;
+import de.intevation.flys.artifacts.datacage.templating.BuilderPool;
 
 
 /**
@@ -62,68 +63,68 @@
 
     private static Recommendations INSTANCE;
 
-    public static class BuilderProvider
+    public static class BuilderPoolProvider
     {
-        protected Builder builder;
+        protected BuilderPool builderPool;
 
-        public BuilderProvider() {
+        public BuilderPoolProvider() {
         }
 
-        public BuilderProvider(Builder builder) {
-            this.builder = builder;
+        public BuilderPoolProvider(BuilderPool builderPool) {
+            this.builderPool = builderPool;
         }
 
-        public Builder getBuilder() {
-            return builder;
+        public BuilderPool getBuilderPool() {
+            return builderPool;
         }
     } // class BuilderProvider
 
-    public static class FileBuilderProvider
-    extends             BuilderProvider
+    public static class FileBuilderPoolProvider
+    extends             BuilderPoolProvider
     {
         protected File file;
         protected long lastModified;
 
-        public FileBuilderProvider() {
+        public FileBuilderPoolProvider() {
         }
 
-        public FileBuilderProvider(File file) {
+        public FileBuilderPoolProvider(File file) {
             this.file    = file;
             lastModified = Long.MIN_VALUE;
         }
 
         @Override
-        public synchronized Builder getBuilder() {
+        public synchronized BuilderPool getBuilderPool() {
             long modified = file.lastModified();
             if (modified > lastModified) {
                 lastModified = modified;
                 try {
                     Document template = loadTemplate(file);
-                    builder = new Builder(template);
+                    builderPool = new BuilderPool(template);
                 }
                 catch (IOException ioe) {
                     log.error(ioe);
                 }
             }
-            return builder;
+            return builderPool;
         }
 
-        public BuilderProvider toStaticProvider() {
-            return new BuilderProvider(builder);
+        public BuilderPoolProvider toStaticProvider() {
+            return new BuilderPoolProvider(builderPool);
         }
     } // class BuilderProvider
 
-    protected BuilderProvider builderProvider;
+    protected BuilderPoolProvider builderPoolProvider;
 
     public Recommendations() {
     }
 
-    public Recommendations(BuilderProvider builderProvider) {
-        this.builderProvider = builderProvider;
+    public Recommendations(BuilderPoolProvider builderPoolProvider) {
+        this.builderPoolProvider = builderPoolProvider;
     }
 
-    public Builder getBuilder() {
-        return builderProvider.getBuilder();
+    public BuilderPool getBuilderPool() {
+        return builderPoolProvider.getBuilderPool();
     }
 
     protected static void artifactToParameters(
@@ -143,7 +144,6 @@
         }
     }
 
-
     /**
      * Put Key/Values from \param src to \param dst, but uppercase
      * both Keys and Values.
@@ -270,8 +270,7 @@
                     CONNECTION_USER, userConnection, false));
             }
 
-
-            getBuilder().build(connections, result, parameters);
+            getBuilderPool().build(connections, result, parameters);
         }
         finally {
             if (userConnection != null) {
@@ -290,27 +289,17 @@
 
 
     protected static Document loadTemplate(File file) throws IOException {
-        InputStream in = null;
+        InputStream in = new FileInputStream(file);
 
         try {
-            in = new FileInputStream(file);
-
             Document template = XMLUtils.parseDocument(in);
-
             if (template == null) {
                 throw new IOException("cannot load template");
             }
             return template;
         }
         finally {
-            if (in != null) {
-                try {
-                    in.close();
-                }
-                catch (IOException ioe) {
-                    log.error(ioe);
-                }
-            }
+            in.close();
         }
     }
 
@@ -322,14 +311,14 @@
             return null;
         }
 
-        FileBuilderProvider fbp = new FileBuilderProvider(file);
+        FileBuilderPoolProvider fbp = new FileBuilderPoolProvider(file);
 
-        if (fbp.getBuilder() == null) {
+        if (fbp.getBuilderPool() == null) {
             log.error("failed loading builder");
             return null;
         }
 
-        BuilderProvider bp = DEVELOPMENT_MODE
+        BuilderPoolProvider bp = DEVELOPMENT_MODE
             ? fbp
             : fbp.toStaticProvider();
 

http://dive4elements.wald.intevation.org