Author: ssmiweve
Date: 2007-08-20 19:45:22 +0200 (Mon, 20 Aug 2007)
New Revision: 5635

Added:
   
trunk/search-command-control-spi/src/main/java/no/sesat/search/mode/executor/ThrottledSearchCommandExecutor.java
Log:
cleanup

Added: 
trunk/search-command-control-spi/src/main/java/no/sesat/search/mode/executor/ThrottledSearchCommandExecutor.java
===================================================================
--- 
trunk/search-command-control-spi/src/main/java/no/sesat/search/mode/executor/ThrottledSearchCommandExecutor.java
                            (rev 0)
+++ 
trunk/search-command-control-spi/src/main/java/no/sesat/search/mode/executor/ThrottledSearchCommandExecutor.java
    2007-08-20 17:45:22 UTC (rev 5635)
@@ -0,0 +1,113 @@
+/*
+ * Copyright (2005-2007) Schibsted Søk AS
+ * This file is part of SESAT.
+ * You can use, redistribute, and/or modify it, under the terms of the SESAT 
License.
+ * You should have received a copy of the SESAT License along with this 
program.  
+ * If not, see https://dev.sesat.no/confluence/display/SESAT/SESAT+License
+ *
+ */
+package no.sesat.search.mode.executor;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import no.sesat.search.mode.command.SearchCommand;
+import no.sesat.search.mode.config.SearchConfiguration;
+import no.sesat.search.result.ResultItem;
+import no.sesat.search.result.ResultList;
+
+/**
+ * An extension to the ParallelSearchCommandExecutor that supports individual 
thread pools for each skin's different
+ *  commands.
+ * Since each command's SearchConfiguration is a singleton (against the given 
the skin, mode, and id).
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Mck</a>
+ * @version <tt>$Id$</tt>
+ */
+final class ThrottledSearchCommandExecutor extends 
AbstractSearchCommandExecutor {
+
+    private static final Map<SearchConfiguration,ExecutorService> EXECUTORS 
+            = new HashMap<SearchConfiguration,ExecutorService>();
+    
+    private static final ReadWriteLock EXECUTORS_LOCK = new 
ReentrantReadWriteLock();
+    
+    public ThrottledSearchCommandExecutor(){}
+
+    @Override
+    public Map<Future<ResultList<? extends ResultItem>>, SearchCommand> 
waitForAll(
+            final Map<Future<ResultList<? extends ResultItem>>,SearchCommand> 
results, 
+            final int timeoutInMillis) throws InterruptedException, 
TimeoutException, ExecutionException {
+        
+        try{
+            return super.waitForAll(results, timeoutInMillis);
+            
+        }finally{
+            
+            for(SearchCommand command : results.values()){
+                
+                final ThreadPoolExecutor executor = 
getExecutorService(command);
+                
+                if(command.isCancelled()){
+
+                    // we freeze thread pool at current size (excluding the 
just failed callable)
+                    executor.setMaximumPoolSize(Math.max(1, 
executor.getActiveCount()));
+
+                }else if(Integer.MAX_VALUE > executor.getMaximumPoolSize()){
+
+                    // command was successful unfreeze thread pool
+                    executor.setMaximumPoolSize(Integer.MAX_VALUE);
+                }
+            }
+        }
+    }
+
+    protected ThreadPoolExecutor getExecutorService(final SearchCommand 
command) {
+        
+        ThreadPoolExecutor service;
+        try{
+            
+            EXECUTORS_LOCK.readLock().lock();
+            service = 
(ThreadPoolExecutor)EXECUTORS.get(command.getSearchConfiguration());
+        
+        }finally{
+            EXECUTORS_LOCK.readLock().unlock();
+        }
+        
+        if(null == service){
+            try{
+            
+                EXECUTORS_LOCK.writeLock().lock();
+                
+                service = new ThreadPoolExecutor(
+                        1, 
+                        Integer.MAX_VALUE, 
+                        60L, 
+                        TimeUnit.SECONDS, 
+                        new SynchronousQueue<Runnable>());
+                
+                EXECUTORS.put(command.getSearchConfiguration(), service);
+                
+            }finally{
+                EXECUTORS_LOCK.writeLock().unlock();
+            }
+        }
+        
+        return service;
+    }
+    
+    protected Collection<ExecutorService> getExecutorServices() {
+        return EXECUTORS.values();
+    }
+        
+    
+}


Property changes on: 
trunk/search-command-control-spi/src/main/java/no/sesat/search/mode/executor/ThrottledSearchCommandExecutor.java
___________________________________________________________________
Name: svn:keywords
   + Id

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to