SlideShare a Scribd company logo
Hacking Tomcat Secrets Revealed
Talk Sponsored    By
Actual    Sponsor
Who am I – bla bla [email_address] Tomcat Committer / ASF member Co-designed the Comet implementation Implemented NIO connector in 6 Responsible for session replication and clustering Been involved with ASF since 2001
Who are We Top Level Project – tomcat.apache.org 24 committers on file Active/core group is much smaller Find us on  [email_address] Work on what interests you and work without guidance Our people skills are improving ;)
Welcome to Tomcat We are Hiring!!!
What we will cover History of Tomcat The Basics Configuration and Container Architecture The Advanced Swappable Components Tomcat Connectors Servlet Container JSP Compiler Developing/Debugging Tomcat
What We Will Not Cover Too Basic stuff – This is a technical presentation Configuration details How the actual JSP .jsp to .java compiler works Forked Tomcat code bases, how they differ and why they happened Older versions of Tomcat, we will work with Tomcat 6, no looking back  
History of Tomcat Started out as a reference implemenation by Sun Microsystem Donated to ASF – Tomcat 3 (roughly 1999) Tomcat 4 – New specs & First rewrite – Codename Catalina Tomcat 5.0 New specs Tomcat 5.5 – 2nd Rewrite – Performance Tomcat 6.0 – New specs, New Cool Features
Basics server.xml Main configuration file Builds server on “the fly” Parsed using commons-digester Tomcat has hard coded rule sets for the parsing Every element/component is swappable
Basics <ElementName className=“the implementation” attribute=“call setAttribute”/> Example: <Server className=“o.a.c.core.StandardServer”  port=&quot;8005“ shutdown=&quot;SHUTDOWN&quot;>
Basics Entire server.xml parsed based on rules Look for these rules: Catalina.java org/apache/catalina/startup/ Even web.xml is parsed using the digester
Basics Catalina.java-createStartDigester Digester digester = new Digester(); digester.setValidating(false); digester.setClassLoader( StandardServer.class.getClassLoader()); digester.addObjectCreate(&quot;Server&quot;,  &quot;org.apache.catalina.core.StandardServer“, &quot;className&quot;); digester.addSetProperties(&quot;Server&quot;);
Basics The exception <Connector> <Connector  className=“ignored” protocol=“nested object” ConnectorCreateRule.java - begin digester.push( new Connector( attributes.getValue(&quot;protocol&quot;))); protocol -> nested className
Basics Tomcat – The Server Services Engine (Catalina) Context JSPs Servlets Valves AJP Connector 8009 SSL Connector 8443 8080 HTTP Connector Hosts Realm Valves Valves
Basics Service/Engine/Host/Context All are “Containers” All implement LifecycleListeners LifecycleEvents How objects get initialized, started and stopped Object relationships are established during creation(digester parsing)
Basics Last Basics – I promise conf/web.xml Default web.xml Merged with webapps WEB-INF/web.xml DefaultServlet –static content JSP Servlet – JSP files conf/context.xml Merged with apps <Context> definition
Advanced Connectors – the entry point Servlet Engine and Container Design Jasper – The JSP engine Valves – interceptor pattern Developing and Debugging How to join – if you are interested
Performance Tip Tomcat produces very little GC Most objects are pooled Even though makers of VM say, never pool objects Prevents CPU/GC jigsaw pattern Resetting fields is faster than GC old object, create new object and initialize No GC lock up surprises
Connectors HTTP Connector – protocol= o.a.coyote.http11.Http11Protocol o.a.coyote.http11.Http11AprProtocol o.a.coyote.http11.Http11NioProtocol HTTP/1.1 aliased to Http11 and Http11Apr AJP Connector org.apache.jk.server.JkCoyoteHandler org.apache.coyote.ajp.AjpAprProtocol AJP/1.3 aliased to the two above
Connectors <Handler> There are some pretty ugly interdependencies here. While re-factoring would resolve that, time has been  spent improving performance. Connector Protocol EndPoint Processor Adapter Engine
Connectors Request Process 1. New HTTP Request All java.io/java.nio/apr socket logic is in the EndPoint Processor sets up input/output buffers HTTP Parsing logic is in here Parses request, if request is available Once the request is parsed The CoyoteAdapter (bridge between connector and engine) Passes the request to the servlet engine Connector Protocol EndPoint Processor Adapter Engine
Performance Tip MessageBytes.java All HTTP Parsing doesn’t deal with strings Every chunk of data gets parsed into a MessageBytes object This object represents a ‘string’ in the HTTP header Avoid string comparison routines Doesn’t contain copy of byte[], but a pointer to the original data with an offset and length
Performance Tip Use Http11Protocol  Keep Alive is turned off Kernel accept filter is in place Use Http11AprProtocol Take advantage of SEND_FILE Native SSL handling Comet Support Use Http11NioProtocol Take advantage of SEND_FILE Large number of sockets using Keep Alive APR is not available or JNI is not preferred Comet Support
Advanced CoyoteAdapter.java Creates Request/Response objects Maps Request/Response to  A Host object (StandardHost) A Context object (StandardContext) A Servlet (StandardWrapper) Parses Session Cookie URL  Cookie Grabs Engine’s valve and passes the request into the servlet engine
Performance Tip DefaultServlet.java Handles all static content Gets deployed into every webapp through conf/web.xml If SEND_FILE support is enabled it will use it It’s a REGULAR SERVLET!! Files and their attributes are cached You can replace with your own implementation
Advanced Servlet Invokation Chain o.a.c.servlets.DefaultServlet.doGet javax.servlet.http.HttpServlet.service o.a.c.core.ApplicationFilterChain.doFilter o.a.c.core.StandardWrapperValve.invoke o.a.c.core.StandardContextValve.invoke o.a.c.core.StandardHostValve.invoke o.a.c.valves.ErrorReportValve.invoke o.a.c.core.StandardEngineValve.invoke o.a.c.connector.CoyoteAdapter.service o.a.coyote.http11.Http11NioProcessor.process o.a.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process o.a.tomcat.util.net.NioEndpoint$SocketProcessor.run java.util.concurrent.ThreadPoolExecutor$Worker.runTask java.util.concurrent.ThreadPoolExecutor$Worker.run java.lang.Thread.run 1. Everything starts with the thread 2. NIO Connector defaults  to ThreadPoolExecutor 3. SocketProcessor –  Simple Runnable to invoke  Handler.process 4. HttpProcessor –  parses HTTP request 5. CoyoteAdapter Creates Request Response pair 6. StandardEngineValve First valve in the engine  container 7. ErrorReportValve Catches Throwable  Reports 400+ errors 8. StandardHostValve Sets context class loader 9. StandardContextValve Invokes (spec) ServletRequestListeners 10. StandardWrapperValve Invokes (spec) FilterChain 11. ApplicationFilterChain Represents Servlet (spec) FilterChain, invokes servlet 12. The Servlet Execution of the servlet
Advanced Reading Data From the InputStream o.a.c.http11.InternalNioInputBuffer$SocketInputBuffer.doRead o.a.c.http11.filters.ChunkedInputFilter.readBytes o.a.c.http11.http11.filters.ChunkedInputFilter.parseChunkHeader  o.a.c.http11.http11.filters.ChunkedInputFilter.doRead o.a.c.http11.http11.InternalNioInputBuffer.doRead o.a.c.http11.Request.doRead o.a.catalina.connector.InputBuffer.realReadBytes o.a.t.u.buf.ByteChunk.substract o.a.catalina.connector.InputBuffer.read o.a.catalina.connector.CoyoteInputStream.read comet.CometEchoServlet.echo
Performance Tip JspServlet.java Handles all JSP files Gets deployed into every webapp through conf/web.xml Mapping done through URL patterns (per spec) It’s a REGULAR SERVLET!! Connects into the Jasper engine
Advanced How are JSP files handled Through the JspServlet (same invocation path) JspServletWrapper created Contains JspCompilationContext Holds a JSP class loader Invokes compile on the compilation context Processes annotations Loads .class file through class loader Finally invokes service(req,resp) on the generated servlet o.a.jasper.compiler.Compiler Generates .java file from .jsp o.a.jasper.compiler.JDTCompiler Generates .class file from .java
Advanced Deployment of Applications  <Context> - StandardContext <Listener> - LifecycleListener <Loader>  - WebappLoader <Manager>  - StandardManager <Realm>  - No default <Resources> - FileDirContext <Valve>  - No default </Context> ContextRuleSet.java – parses contexts in server.xml
Advanced Deployment of applications The deployer is HostConfig.java Each StandardHost.java object holds a reference to a HostConfig Deploy order context.xml files WAR files Directories /ROOT is hardcoded for path=“” Runtime deployment triggered by LifecycleEvent
Advanced Developing/Debugging SVN Repo for TC 6 is simplified trunk/java – all you need svn co/ant download/ant – builds the system Run inside a debugger by emulating the catalina.sh/catalina.bat if you wish Everything is java, breakpoints anywhere
Performance Tip Does it scale Yes, its been tested with 16k concurrent and active connections on a –Xmx512m system Performance increases well as new CPUs are added RAM is your “max # concurrent connection” limitation Simple tests run at  http://blog.covalent.net/roller/covalent/entry/20070308
Performance Tip Tuning Mostly in the application itself, Tomcat default is pretty good When it comes down to nuts and bolts, the tuning is in the connectors NIO connector, by far the most tuning options (see docs) Socket and App buffers usually the most important aspect for “write-speed” APR connector, speedy little devil, not as many options, but relies on APR below being well tuned.
Performance Tip Tuning the servlet engine Sure, it can be done, but not through configuration Most common bottlenecks turn out to be synchronized statements or locks Compared to the webapp or the connector, spending time tuning the engine is not worth the time
Conclusion Not so difficult on first impression Slightly confusing on second impression Once you get a hang of it, go crazy Modular design, and nasty deps Not the typical text book java design Find something interesting? want to contribute? – take initiative, don’t be shy
Want to join? Ideas for needed projects Better deployer Documentation Administration tool Better JMX support Remote and Cluster deployments Live status – dash board SIP support The list goes on, take your pick!
Q & A Lots and Lots covered Only a drop in the sea, but enough to get you started not enough time [email_address]  – anytime [email_address]  – be brave http://people.apache.org/~fhanik  for the presentation

More Related Content

PPT
Hacking Tomcat
PDF
Javascript TDD with Jasmine, Karma, and Gulp
PDF
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
PPTX
ASP.NET vNext ANUG 20140817
PPTX
Solving anything in VCL
PDF
Advanced VCL: how to use restart
KEY
Curator intro
PDF
Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik
Hacking Tomcat
Javascript TDD with Jasmine, Karma, and Gulp
Tech Talks_25.04.15_Session 3_Tibor Sulyan_Distributed coordination with zook...
ASP.NET vNext ANUG 20140817
Solving anything in VCL
Advanced VCL: how to use restart
Curator intro
Apache Karaf - Building OSGi applications on Apache Karaf - T Frank & A Grzesik

What's hot (20)

PDF
VCL template abstraction model and automated deployments to Fastly
PDF
DOSUG Taking Apache Camel For A Ride
PDF
Going crazy with Varnish and Symfony
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
PPT
GTLAB Overview
PPTX
Introduction to Apache Camel
PDF
Making the most out of kubernetes audit logs
PDF
Caching reboot: javax.cache & Ehcache 3
PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
PDF
Migrating to a bazel based CI system: 6 learnings
PPTX
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
PDF
Apache Camel: Jetty Component With Example
PDF
MongoDB World 2019: Becoming an Ops Manager Backup Superhero!
PDF
Advanced Oracle Troubleshooting
PDF
Scalable Django Architecture
PPTX
A Heart Rate Validating Admission Webhook
PDF
Maven 3.0 at Øredev
ODP
Mastering Namespaces in PHP
KEY
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PDF
HTTP caching with Varnish
VCL template abstraction model and automated deployments to Fastly
DOSUG Taking Apache Camel For A Ride
Going crazy with Varnish and Symfony
We Are All Testers Now: The Testing Pyramid and Front-End Development
GTLAB Overview
Introduction to Apache Camel
Making the most out of kubernetes audit logs
Caching reboot: javax.cache & Ehcache 3
Beyond Breakpoints: A Tour of Dynamic Analysis
Migrating to a bazel based CI system: 6 learnings
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
Apache Camel: Jetty Component With Example
MongoDB World 2019: Becoming an Ops Manager Backup Superhero!
Advanced Oracle Troubleshooting
Scalable Django Architecture
A Heart Rate Validating Admission Webhook
Maven 3.0 at Øredev
Mastering Namespaces in PHP
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
HTTP caching with Varnish

Viewers also liked (10)

PDF
Tomcat
PPS
Horoscopo Das Flores
PPTX
PPTX
Ejemplos aplicaciones web
PDF
Tomcat y Jboss
DOC
Jetty Vs Tomcat
PDF
Jetty 9 – The Next Generation Servlet Container
PDF
Introducción a Tomcat
PPT
Tomcat configuration
PPTX
Apache tomcat
Tomcat
Horoscopo Das Flores
Ejemplos aplicaciones web
Tomcat y Jboss
Jetty Vs Tomcat
Jetty 9 – The Next Generation Servlet Container
Introducción a Tomcat
Tomcat configuration
Apache tomcat

Similar to Hackingtomcat (20)

PDF
Introduction to Apache Tomcat 7 Presentation
PPTX
Jsp and jstl
PPT
Tomcat 6: Evolving our server
PPT
Java Servlets
PPT
Web Applications and Deployment
DOC
Java Servlets & JSP
PPT
B2 2006 tomcat_clusters
PDF
Play Framework: async I/O with Java and Scala
PDF
Servlets Java Slides & Presentation
PDF
Servlets
PDF
19servlets
PPTX
Web container and Apache Tomcat
ODP
Sun Web Server Brief
PPT
Web Application Deployment
PDF
Bt0083 server side programing 2
PPT
Web Server/App Server Connectivity
PPTX
Play framework : A Walkthrough
PPT
Auxiliary : Tomcat
PDF
PPT
1 java servlets and jsp
Introduction to Apache Tomcat 7 Presentation
Jsp and jstl
Tomcat 6: Evolving our server
Java Servlets
Web Applications and Deployment
Java Servlets & JSP
B2 2006 tomcat_clusters
Play Framework: async I/O with Java and Scala
Servlets Java Slides & Presentation
Servlets
19servlets
Web container and Apache Tomcat
Sun Web Server Brief
Web Application Deployment
Bt0083 server side programing 2
Web Server/App Server Connectivity
Play framework : A Walkthrough
Auxiliary : Tomcat
1 java servlets and jsp

More from Aung Khant (20)

PPS
Introducing Msd
PDF
Securing Php App
PDF
Securing Web Server Ibm
PDF
Security Design Patterns
PDF
Security Code Review
PDF
Security Engineering Executive
PDF
Security Engineeringwith Patterns
PDF
Security Web Servers
PDF
Security Testing Web App
PDF
Session Fixation
PDF
Sql Injection Paper
PPT
Sql Injection Adv Owasp
PDF
Php Security Iissues
PDF
Sql Injection White Paper
PDF
S Shah Web20
PDF
S Vector4 Web App Sec Management
PDF
Php Security Value1
PDF
Privilege Escalation
PDF
Php Security Workshop
PDF
Preventing Xs Sin Perl Apache
Introducing Msd
Securing Php App
Securing Web Server Ibm
Security Design Patterns
Security Code Review
Security Engineering Executive
Security Engineeringwith Patterns
Security Web Servers
Security Testing Web App
Session Fixation
Sql Injection Paper
Sql Injection Adv Owasp
Php Security Iissues
Sql Injection White Paper
S Shah Web20
S Vector4 Web App Sec Management
Php Security Value1
Privilege Escalation
Php Security Workshop
Preventing Xs Sin Perl Apache

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PDF
Electronic commerce courselecture one. Pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
KodekX | Application Modernization Development
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Big Data Technologies - Introduction.pptx
cuic standard and advanced reporting.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Encapsulation theory and applications.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
Electronic commerce courselecture one. Pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KodekX | Application Modernization Development
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Empathic Computing: Creating Shared Understanding
Encapsulation_ Review paper, used for researhc scholars
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

Hackingtomcat

  • 3. Actual Sponsor
  • 4. Who am I – bla bla [email_address] Tomcat Committer / ASF member Co-designed the Comet implementation Implemented NIO connector in 6 Responsible for session replication and clustering Been involved with ASF since 2001
  • 5. Who are We Top Level Project – tomcat.apache.org 24 committers on file Active/core group is much smaller Find us on [email_address] Work on what interests you and work without guidance Our people skills are improving ;)
  • 6. Welcome to Tomcat We are Hiring!!!
  • 7. What we will cover History of Tomcat The Basics Configuration and Container Architecture The Advanced Swappable Components Tomcat Connectors Servlet Container JSP Compiler Developing/Debugging Tomcat
  • 8. What We Will Not Cover Too Basic stuff – This is a technical presentation Configuration details How the actual JSP .jsp to .java compiler works Forked Tomcat code bases, how they differ and why they happened Older versions of Tomcat, we will work with Tomcat 6, no looking back 
  • 9. History of Tomcat Started out as a reference implemenation by Sun Microsystem Donated to ASF – Tomcat 3 (roughly 1999) Tomcat 4 – New specs & First rewrite – Codename Catalina Tomcat 5.0 New specs Tomcat 5.5 – 2nd Rewrite – Performance Tomcat 6.0 – New specs, New Cool Features
  • 10. Basics server.xml Main configuration file Builds server on “the fly” Parsed using commons-digester Tomcat has hard coded rule sets for the parsing Every element/component is swappable
  • 11. Basics <ElementName className=“the implementation” attribute=“call setAttribute”/> Example: <Server className=“o.a.c.core.StandardServer” port=&quot;8005“ shutdown=&quot;SHUTDOWN&quot;>
  • 12. Basics Entire server.xml parsed based on rules Look for these rules: Catalina.java org/apache/catalina/startup/ Even web.xml is parsed using the digester
  • 13. Basics Catalina.java-createStartDigester Digester digester = new Digester(); digester.setValidating(false); digester.setClassLoader( StandardServer.class.getClassLoader()); digester.addObjectCreate(&quot;Server&quot;, &quot;org.apache.catalina.core.StandardServer“, &quot;className&quot;); digester.addSetProperties(&quot;Server&quot;);
  • 14. Basics The exception <Connector> <Connector className=“ignored” protocol=“nested object” ConnectorCreateRule.java - begin digester.push( new Connector( attributes.getValue(&quot;protocol&quot;))); protocol -> nested className
  • 15. Basics Tomcat – The Server Services Engine (Catalina) Context JSPs Servlets Valves AJP Connector 8009 SSL Connector 8443 8080 HTTP Connector Hosts Realm Valves Valves
  • 16. Basics Service/Engine/Host/Context All are “Containers” All implement LifecycleListeners LifecycleEvents How objects get initialized, started and stopped Object relationships are established during creation(digester parsing)
  • 17. Basics Last Basics – I promise conf/web.xml Default web.xml Merged with webapps WEB-INF/web.xml DefaultServlet –static content JSP Servlet – JSP files conf/context.xml Merged with apps <Context> definition
  • 18. Advanced Connectors – the entry point Servlet Engine and Container Design Jasper – The JSP engine Valves – interceptor pattern Developing and Debugging How to join – if you are interested
  • 19. Performance Tip Tomcat produces very little GC Most objects are pooled Even though makers of VM say, never pool objects Prevents CPU/GC jigsaw pattern Resetting fields is faster than GC old object, create new object and initialize No GC lock up surprises
  • 20. Connectors HTTP Connector – protocol= o.a.coyote.http11.Http11Protocol o.a.coyote.http11.Http11AprProtocol o.a.coyote.http11.Http11NioProtocol HTTP/1.1 aliased to Http11 and Http11Apr AJP Connector org.apache.jk.server.JkCoyoteHandler org.apache.coyote.ajp.AjpAprProtocol AJP/1.3 aliased to the two above
  • 21. Connectors <Handler> There are some pretty ugly interdependencies here. While re-factoring would resolve that, time has been spent improving performance. Connector Protocol EndPoint Processor Adapter Engine
  • 22. Connectors Request Process 1. New HTTP Request All java.io/java.nio/apr socket logic is in the EndPoint Processor sets up input/output buffers HTTP Parsing logic is in here Parses request, if request is available Once the request is parsed The CoyoteAdapter (bridge between connector and engine) Passes the request to the servlet engine Connector Protocol EndPoint Processor Adapter Engine
  • 23. Performance Tip MessageBytes.java All HTTP Parsing doesn’t deal with strings Every chunk of data gets parsed into a MessageBytes object This object represents a ‘string’ in the HTTP header Avoid string comparison routines Doesn’t contain copy of byte[], but a pointer to the original data with an offset and length
  • 24. Performance Tip Use Http11Protocol Keep Alive is turned off Kernel accept filter is in place Use Http11AprProtocol Take advantage of SEND_FILE Native SSL handling Comet Support Use Http11NioProtocol Take advantage of SEND_FILE Large number of sockets using Keep Alive APR is not available or JNI is not preferred Comet Support
  • 25. Advanced CoyoteAdapter.java Creates Request/Response objects Maps Request/Response to A Host object (StandardHost) A Context object (StandardContext) A Servlet (StandardWrapper) Parses Session Cookie URL Cookie Grabs Engine’s valve and passes the request into the servlet engine
  • 26. Performance Tip DefaultServlet.java Handles all static content Gets deployed into every webapp through conf/web.xml If SEND_FILE support is enabled it will use it It’s a REGULAR SERVLET!! Files and their attributes are cached You can replace with your own implementation
  • 27. Advanced Servlet Invokation Chain o.a.c.servlets.DefaultServlet.doGet javax.servlet.http.HttpServlet.service o.a.c.core.ApplicationFilterChain.doFilter o.a.c.core.StandardWrapperValve.invoke o.a.c.core.StandardContextValve.invoke o.a.c.core.StandardHostValve.invoke o.a.c.valves.ErrorReportValve.invoke o.a.c.core.StandardEngineValve.invoke o.a.c.connector.CoyoteAdapter.service o.a.coyote.http11.Http11NioProcessor.process o.a.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process o.a.tomcat.util.net.NioEndpoint$SocketProcessor.run java.util.concurrent.ThreadPoolExecutor$Worker.runTask java.util.concurrent.ThreadPoolExecutor$Worker.run java.lang.Thread.run 1. Everything starts with the thread 2. NIO Connector defaults to ThreadPoolExecutor 3. SocketProcessor – Simple Runnable to invoke Handler.process 4. HttpProcessor – parses HTTP request 5. CoyoteAdapter Creates Request Response pair 6. StandardEngineValve First valve in the engine container 7. ErrorReportValve Catches Throwable Reports 400+ errors 8. StandardHostValve Sets context class loader 9. StandardContextValve Invokes (spec) ServletRequestListeners 10. StandardWrapperValve Invokes (spec) FilterChain 11. ApplicationFilterChain Represents Servlet (spec) FilterChain, invokes servlet 12. The Servlet Execution of the servlet
  • 28. Advanced Reading Data From the InputStream o.a.c.http11.InternalNioInputBuffer$SocketInputBuffer.doRead o.a.c.http11.filters.ChunkedInputFilter.readBytes o.a.c.http11.http11.filters.ChunkedInputFilter.parseChunkHeader o.a.c.http11.http11.filters.ChunkedInputFilter.doRead o.a.c.http11.http11.InternalNioInputBuffer.doRead o.a.c.http11.Request.doRead o.a.catalina.connector.InputBuffer.realReadBytes o.a.t.u.buf.ByteChunk.substract o.a.catalina.connector.InputBuffer.read o.a.catalina.connector.CoyoteInputStream.read comet.CometEchoServlet.echo
  • 29. Performance Tip JspServlet.java Handles all JSP files Gets deployed into every webapp through conf/web.xml Mapping done through URL patterns (per spec) It’s a REGULAR SERVLET!! Connects into the Jasper engine
  • 30. Advanced How are JSP files handled Through the JspServlet (same invocation path) JspServletWrapper created Contains JspCompilationContext Holds a JSP class loader Invokes compile on the compilation context Processes annotations Loads .class file through class loader Finally invokes service(req,resp) on the generated servlet o.a.jasper.compiler.Compiler Generates .java file from .jsp o.a.jasper.compiler.JDTCompiler Generates .class file from .java
  • 31. Advanced Deployment of Applications <Context> - StandardContext <Listener> - LifecycleListener <Loader> - WebappLoader <Manager> - StandardManager <Realm> - No default <Resources> - FileDirContext <Valve> - No default </Context> ContextRuleSet.java – parses contexts in server.xml
  • 32. Advanced Deployment of applications The deployer is HostConfig.java Each StandardHost.java object holds a reference to a HostConfig Deploy order context.xml files WAR files Directories /ROOT is hardcoded for path=“” Runtime deployment triggered by LifecycleEvent
  • 33. Advanced Developing/Debugging SVN Repo for TC 6 is simplified trunk/java – all you need svn co/ant download/ant – builds the system Run inside a debugger by emulating the catalina.sh/catalina.bat if you wish Everything is java, breakpoints anywhere
  • 34. Performance Tip Does it scale Yes, its been tested with 16k concurrent and active connections on a –Xmx512m system Performance increases well as new CPUs are added RAM is your “max # concurrent connection” limitation Simple tests run at http://blog.covalent.net/roller/covalent/entry/20070308
  • 35. Performance Tip Tuning Mostly in the application itself, Tomcat default is pretty good When it comes down to nuts and bolts, the tuning is in the connectors NIO connector, by far the most tuning options (see docs) Socket and App buffers usually the most important aspect for “write-speed” APR connector, speedy little devil, not as many options, but relies on APR below being well tuned.
  • 36. Performance Tip Tuning the servlet engine Sure, it can be done, but not through configuration Most common bottlenecks turn out to be synchronized statements or locks Compared to the webapp or the connector, spending time tuning the engine is not worth the time
  • 37. Conclusion Not so difficult on first impression Slightly confusing on second impression Once you get a hang of it, go crazy Modular design, and nasty deps Not the typical text book java design Find something interesting? want to contribute? – take initiative, don’t be shy
  • 38. Want to join? Ideas for needed projects Better deployer Documentation Administration tool Better JMX support Remote and Cluster deployments Live status – dash board SIP support The list goes on, take your pick!
  • 39. Q & A Lots and Lots covered Only a drop in the sea, but enough to get you started not enough time [email_address] – anytime [email_address] – be brave http://people.apache.org/~fhanik for the presentation