[grizzly~git:c2b0c1c1] [master] + implement improvement #1662

  • From: oleksiys@...
  • To: commits@...
  • Subject: [grizzly~git:c2b0c1c1] [master] + implement improvement #1662
  • Date: Mon, 17 Mar 2014 22:25:26 +0000

Project:    grizzly
Repository: git
Revision:   c2b0c1c14285a5c2df90f06dc642189cb077d3a7
Author:     oleksiys
Date:       2014-03-13 01:10:11 UTC
Link:       

Log Message:
------------
[master] + implement improvement #1664
https://java.net/jira/browse/GRIZZLY-1664

"Add Eclipse project files to .gitignore"

[2.3.x] + fix issue #1663
https://java.net/jira/browse/GRIZZLY-1663
"NPE when registering a servlet in grizzly-httpservice"

[master] + implement improvement #1662
https://java.net/jira/browse/GRIZZLY-1662
"Check that kernel ThreadPoolConfig size is big enough to run configured 
number of SelectorRunners"



Revisions:
----------
530338b3f0f68fa7926083b7f4a46619c69876cc
1e01b986257f203d7b5cb09d301d0f32460caf7a
c2b0c1c14285a5c2df90f06dc642189cb077d3a7


Modified Paths:
---------------
.gitignore
extras/grizzly-httpservice/src/main/java/org/glassfish/grizzly/osgi/httpservice/OSGiCleanMapper.java
extras/grizzly-httpservice/src/main/java/org/glassfish/grizzly/osgi/httpservice/OSGiMainHandler.java
modules/grizzly/src/main/java/org/glassfish/grizzly/nio/NIOTransport.java
modules/grizzly/src/main/java/org/glassfish/grizzly/nio/NIOTransportBuilder.java
modules/grizzly/src/test/java/org/glassfish/grizzly/ThreadPoolsTest.java


Diffs:
------
--- a/.gitignore
+++ b/.gitignore
@@ -8,6 +8,10 @@ atlassian-ide-plugin.xml
 target
 .idea
 hotspot.log
+nbactions.xml
+.classpath
+.project
+.settings/
 modules/bundles/comet/src
 modules/bundles/core/src
 modules/bundles/http/src

--- 
a/extras/grizzly-httpservice/src/main/java/org/glassfish/grizzly/osgi/httpservice/OSGiCleanMapper.java
+++ 
b/extras/grizzly-httpservice/src/main/java/org/glassfish/grizzly/osgi/httpservice/OSGiCleanMapper.java
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -40,7 +40,6 @@
 
 package org.glassfish.grizzly.osgi.httpservice;
 
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -70,11 +69,11 @@ class OSGiCleanMapper {
     private static final Set<Servlet> registeredServlets = new 
HashSet<Servlet>(16);
 
     private Set<String> localAliases = new HashSet<String>(4);
-    private HashMap<HttpContext, ArrayList<OSGiServletHandler>> 
contextServletHandlerMap =
-            new HashMap<HttpContext, ArrayList<OSGiServletHandler>>(3);
+    private final HashMap<HttpContext, List<OSGiServletHandler>> 
contextServletHandlerMap =
+            new HashMap<HttpContext, List<OSGiServletHandler>>(3);
     private final Logger logger;
 
-    protected Map<HttpContext, OSGiServletContext> 
httpContextToServletContextMap =
+    protected final Map<HttpContext, OSGiServletContext> 
httpContextToServletContextMap =
                 new HashMap<HttpContext, OSGiServletContext>();
 
 
@@ -272,11 +271,22 @@ class OSGiCleanMapper {
         return contextServletHandlerMap.get(httpContext);
     }
 
-    public void addContext(HttpContext httpContext, 
ArrayList<OSGiServletHandler> servletHandlers) {
-        contextServletHandlerMap.put(httpContext, servletHandlers);
-        httpContextToServletContextMap.put(httpContext, new 
OSGiServletContext(httpContext, logger));
+    public void addContext(final HttpContext httpContext,
+            final List<OSGiServletHandler> servletHandlers) {
+        addContext(httpContext, null, servletHandlers);
     }
 
+    public void addContext(final HttpContext httpContext,
+            OSGiServletContext servletCtx,
+            final List<OSGiServletHandler> servletHandlers) {
+        if (servletCtx == null) {
+            servletCtx = new OSGiServletContext(httpContext, logger);
+        }
+        
+        contextServletHandlerMap.put(httpContext, servletHandlers);
+        httpContextToServletContextMap.put(httpContext, servletCtx);
+    }
+    
     public OSGiServletContext getServletContext(final HttpContext 
httpContext) {
         return httpContextToServletContextMap.get(httpContext);
     }--- 
a/extras/grizzly-httpservice/src/main/java/org/glassfish/grizzly/osgi/httpservice/OSGiMainHandler.java
+++ 
b/extras/grizzly-httpservice/src/main/java/org/glassfish/grizzly/osgi/httpservice/OSGiMainHandler.java
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2009-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -57,7 +57,6 @@ import org.glassfish.grizzly.http.server.HttpHandler;
 import org.glassfish.grizzly.http.server.Request;
 import org.glassfish.grizzly.http.server.Response;
 import org.glassfish.grizzly.http.server.util.MappingData;
-import org.glassfish.grizzly.http.util.HttpStatus;
 
 /**
  * OSGi Main HttpHandler.
@@ -438,10 +437,11 @@ public class OSGiMainHandler extends HttpHandler 
implements OSGiHandler {
             Servlet servlet, HttpContext httpContext, Dictionary initparams) 
{
         OSGiServletHandler osgiServletHandler;
 
-        if (mapper.containsContext(httpContext) && 
mapper.getContext(httpContext) != null) {
+        List<OSGiServletHandler> servletHandlers =
+                mapper.getContext(httpContext);
+        if (servletHandlers != null) {
             logger.debug("Reusing ServletHandler");
             // new servlet handler for same configuration, different servlet 
and alias
-            List<OSGiServletHandler> servletHandlers = 
mapper.getContext(httpContext);
             osgiServletHandler = 
servletHandlers.get(0).newServletHandler(servlet);
             servletHandlers.add(osgiServletHandler);
         } else {
@@ -457,18 +457,25 @@ public class OSGiMainHandler extends HttpHandler 
implements OSGiHandler {
             } else {
                 params = new HashMap<String, String>(0);
             }
-            OSGiServletContext servletContext = 
mapper.getServletContext(httpContext);
+
+            servletHandlers = new ArrayList<OSGiServletHandler>(1);
+            mapper.addContext(httpContext,
+                    mapper.getServletContext(httpContext),
+                    servletHandlers);
+            
+            final OSGiServletContext servletContext =
+                    mapper.getServletContext(httpContext);
+
+            assert servletContext != null;
+
             osgiServletHandler =
                     new OSGiServletHandler(servlet,
                                            httpContext,
                                            servletContext,
                                            params,
                                            logger);
-            ArrayList<OSGiServletHandler> servletHandlers = new 
ArrayList<OSGiServletHandler>(1);
             servletHandlers.add(osgiServletHandler);
-            mapper.addContext(httpContext, servletHandlers);
             
osgiServletHandler.setFilterChainFactory(servletContext.getFilterChainFactory());
-
         }
 
         return osgiServletHandler;

--- 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/NIOTransport.java
+++ 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/NIOTransport.java
@@ -79,6 +79,7 @@ import 
org.glassfish.grizzly.strategies.SameThreadIOStrategy;
 import org.glassfish.grizzly.strategies.WorkerThreadIOStrategy;
 import org.glassfish.grizzly.threadpool.AbstractThreadPool;
 import org.glassfish.grizzly.threadpool.GrizzlyExecutorService;
+import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
 import org.glassfish.grizzly.utils.Futures;
 
 /**
@@ -263,8 +264,11 @@ public abstract class NIOTransport extends 
AbstractTransport
     public void setSelectorRunnersCount(final int selectorRunnersCount) {
         if (selectorRunnersCount > 0) {
             this.selectorRunnersCount = selectorRunnersCount;
-            kernelPoolConfig.setCorePoolSize(selectorRunnersCount);
-            kernelPoolConfig.setMaxPoolSize(selectorRunnersCount);
+            if (kernelPoolConfig != null &&
+                    kernelPoolConfig.getMaxPoolSize() < 
selectorRunnersCount) {
+                kernelPoolConfig.setCorePoolSize(selectorRunnersCount)
+                                .setMaxPoolSize(selectorRunnersCount);
+            }
             notifyProbesConfigChanged(this);
         }
     }
@@ -407,7 +411,7 @@ public abstract class NIOTransport extends 
AbstractTransport
                 throw new IllegalStateException("No processor available.");
             }
 
-            final int selectorRunnersCount = getSelectorRunnersCount();
+            final int selectorRunnersCnt = getSelectorRunnersCount();
 
             if (nioChannelDistributor == null) {
                 nioChannelDistributor =
@@ -415,6 +419,19 @@ public abstract class NIOTransport extends 
AbstractTransport
             }
 
             if (kernelPool == null) {
+                if (kernelPoolConfig == null) {
+                    kernelPoolConfig = ThreadPoolConfig.newConfig()
+                            .setCorePoolSize(selectorRunnersCnt)
+                            .setMaxPoolSize(selectorRunnersCnt)
+                            .setPoolName("grizzly-nio-kernel");
+                } else if (kernelPoolConfig.getMaxPoolSize() < 
selectorRunnersCnt) {
+                    LOGGER.log(Level.INFO, "Adjusting kernel thread pool to 
max "
+                            + "size {0} to handle configured number of 
SelectorRunners",
+                            selectorRunnersCnt);
+                    kernelPoolConfig.setCorePoolSize(selectorRunnersCnt)
+                            .setMaxPoolSize(selectorRunnersCnt);
+                }
+
                 kernelPoolConfig.setMemoryManager(memoryManager);
                 setKernelPool0(
                         GrizzlyExecutorService.createInstance(
@@ -433,13 +450,13 @@ public abstract class NIOTransport extends 
AbstractTransport
                 }
             }
 
-                   /* By default TemporarySelector pool size should be equal
-                   to the number of processing threads */
+            /* By default TemporarySelector pool size should be equal
+            to the number of processing threads */
             int selectorPoolSize =
                     TemporarySelectorPool.DEFAULT_SELECTORS_COUNT;
             if (workerThreadPool instanceof AbstractThreadPool) {
                 if (strategy instanceof SameThreadIOStrategy) {
-                    selectorPoolSize = selectorRunnersCount;
+                    selectorPoolSize = selectorRunnersCnt;
                 } else {
                     selectorPoolSize = Math.min(
                             ((AbstractThreadPool) 
workerThreadPool).getConfig()--- 
a/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/NIOTransportBuilder.java
+++ 
b/modules/grizzly/src/main/java/org/glassfish/grizzly/nio/NIOTransportBuilder.java
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2011-2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -313,14 +313,10 @@ public abstract class NIOTransportBuilder<T extends 
NIOTransportBuilder> {
         NIOTransport transport = create();
         transport.setIOStrategy(ioStrategy);
         if (workerConfig != null) {
-            transport.setWorkerThreadPoolConfig(workerConfig);
+            transport.setWorkerThreadPoolConfig(workerConfig.copy());
         }
-        ThreadPoolConfig kernelLocal = kernelConfig;
-        if (kernelLocal != null) {
-            transport.setKernelThreadPoolConfig(kernelLocal);
-        } else {
-            kernelLocal = ThreadPoolConfig.newConfig();
-            transport.setKernelThreadPoolConfig(kernelLocal);
+        if (kernelConfig != null) {
+            transport.setKernelThreadPoolConfig(kernelConfig.copy());
         }
         transport.setSelectorProvider(selectorProvider);
         transport.setSelectorHandler(selectorHandler);
@@ -345,20 +341,6 @@ public abstract class NIOTransportBuilder<T extends 
NIOTransportBuilder> {
 
     // ------------------------------------------------------- Protected 
Methods
 
-
-    /**
-     * <p>
-     * Configure the {@link org.glassfish.grizzly.nio.SelectorRunner} pool's
-     * default core and max pool size.
-     * </p>
-     * @param config
-     */
-    protected ThreadPoolConfig configSelectorPool(final ThreadPoolConfig 
config) {
-        final int runnerCount = getRunnerCount();
-        config.setPoolName("Grizzly-kernel");
-        return 
config.setCorePoolSize(runnerCount).setMaxPoolSize(runnerCount);
-    }
-
     /**
      * See: <a 
href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ205";>http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ205</a>
      */--- 
a/modules/grizzly/src/test/java/org/glassfish/grizzly/ThreadPoolsTest.java
+++ b/modules/grizzly/src/test/java/org/glassfish/grizzly/ThreadPoolsTest.java
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
  *
  * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common Development
@@ -51,6 +51,10 @@ import java.util.Map;
 import java.util.concurrent.ArrayBlockingQueue;
 
 import static junit.framework.Assert.assertEquals;
+import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
+import org.glassfish.grizzly.nio.transport.TCPNIOTransportBuilder;
+import org.glassfish.grizzly.strategies.SameThreadIOStrategy;
+import org.glassfish.grizzly.strategies.WorkerThreadIOStrategy;
 
 public class ThreadPoolsTest {
 
@@ -74,4 +78,47 @@ public class ThreadPoolsTest {
         assertEquals("Pool did not properly initialize threads based on core 
pool size configuration.", 5, ((Map) workers.get(fixedThreadPool)).size());
     }
 
+    @Test
+    public void testCustomThreadPoolSameThreadStrategy() throws Exception {
+
+        final int poolSize = 
Math.max(Runtime.getRuntime().availableProcessors()/2, 1);
+        final ThreadPoolConfig poolCfg = ThreadPoolConfig.newConfig();
+        poolCfg.setCorePoolSize(poolSize).setMaxPoolSize(poolSize);
+
+        final TCPNIOTransport tcpTransport = 
TCPNIOTransportBuilder.newInstance()
+                .reuseAddress(true)
+                .ioStrategy(SameThreadIOStrategy.getInstance())
+                .selectorThreadPoolConfig(poolCfg)
+                .workerThreadPoolConfig(null)
+                .build();
+        try {
+            tcpTransport.start();
+        } finally {
+            tcpTransport.shutdownNow();
+        }
+    }
+
+    @Test
+    public void testCustomThreadPoolWorkerThreadStrategy() throws Exception {
+
+        final int selectorPoolSize =  
Math.max(Runtime.getRuntime().availableProcessors()/2, 1);
+        final ThreadPoolConfig selectorPoolCfg = 
ThreadPoolConfig.newConfig();
+        
selectorPoolCfg.setCorePoolSize(selectorPoolSize).setMaxPoolSize(selectorPoolSize);
+
+        final int workerPoolSize = 
Runtime.getRuntime().availableProcessors() * 2 ;
+        final ThreadPoolConfig workerPoolCfg = ThreadPoolConfig.newConfig();
+        
workerPoolCfg.setCorePoolSize(workerPoolSize).setMaxPoolSize(workerPoolSize);
+
+       final TCPNIOTransport tcpTransport = 
TCPNIOTransportBuilder.newInstance()
+                .reuseAddress(true)
+                .ioStrategy(WorkerThreadIOStrategy.getInstance())
+                .selectorThreadPoolConfig(selectorPoolCfg)
+                .workerThreadPoolConfig(workerPoolCfg)
+                .build();
+        try {
+            tcpTransport.start();
+        } finally {
+            tcpTransport.shutdownNow();
+        }
+    }    
 }





[grizzly~git:c2b0c1c1] [master] + implement improvement #1662

oleksiys 03/17/2014
Terms of Use; Privacy Policy; Copyright ©2013-2017 (revision 20160708.bf2ac18)
 
 
Close
loading
Please Confirm
Close