CF Summit Notes: ColdFusion Internals

October 19, 2014

CF Summit Notes: ColdFusion Internals

(I didn't catch the name of who gave this presentation, which bums me out. Not only because I like to give credit where credit is due, but also because this presentation was one of the Summit highlights for me. I learned a great deal about the internals of CF and how/why certain things can affect the performance of my applications. Very well done!)

life of a CF request
CF is java
java byte code gets created/compiled
goes into apache tomcat

1. Request validation
CF will validate your request

2. Path Resolution
CF uses request URL to resolve the absolute path of the CFM handling the request

3. application resolution
locations application,
creates app scope
invokes the lifecycle method

4. parse
parse the CF using grammar
generate AST using the grammar

5. Bytecode generation
use ASB generation i previous phase and convert it into java byte code

4 and 5 are done by "template class loader"

Request Validation Phase --
We (dev's) are part of request validation
settings in CF for this
i.e. "Request size limits"
Admin / Server Settings, at the bottom os "request size limits"
here you can specify the max parameters a request can have, the max size (so nobody uploads 1TB files, etc)
Request Limits --
(Admin / Server Settings / Request Tuning page)
"Max number of simultaneous web service requests"
by default this only allows 5 concurrent requests
if your app expects a LOT of these, you should increase this.

Path Resolution --
CF will resolve absolute path of the CF handling the request
at this time we have the request URL (http://locathohost/foo.cfm)
from that, we get the virtual path (/app/foo.cfm)
from that we get the Absolute path (c:\wwwroot\app\foo.cfm)
if this isn't available, it will check the Server Mappings and try to find the CFM there
if it's still not available, it will try the web server via a Connector and that will resolve the path for us
You can cache the web server path using an admin setting to speed this up
ADmin -- SErver Settings -- CAcheing, "Cache Web Server Paths"
-- 1st request, uses the Connector. from the next one on, it uses the cached path, gives you some performance benefits, because we're not making more calls to the Connector to get the paths

Application Resolution --
Finds the Application.cfc
Creates App Scope
Involves LifeCycle methods (onApplicationStart, OnError, etc)
How do we find the app.cfc:
check in current directory
if it's found, we resolved it, we can go on to create app scope
if it's not there, we check the parent directory
if it's not there, we go one level up and so on until it's found.
Admin / Server Settings / Settings / "Application.cfc lookup order -- can set this to optimize the folder order in which we look for App CFC

The most important setting in App.CFC is the NAME of the app
we use the name as the key for storing all of the settings
if you don't specify a name, CF creates an unnamed app
might face weird issues because of that

CF can disable the creation of unnamed applications
Admin /Svr Settings / "Disable creation of unnamed applications"
CF will throw an error if you forget and try to do this in your code (after you turn on that setting)

Parsing --
Template Reader reads the CFM
Java CC grammar
returns an "Abstract Syntax Tree" (AST)
Templat Reader --
Reads the CFM and creates the stream

Automatic Encoding identification
start
is BOM present? (byte order mark)
if available, uses that appropriate encoding
but not all encodings have a BOM (only 6 or 7 of them)
if not present, CF will try to guess
for that cf uses "icu4j" -- open source library for guessing the encoding of a file
if it's a 100% match, CF uses that
if less than 100% match, uses the system encoding.

Dfile.encoding = "" in CF config to specify the encoding to use.
can also use cfprocessingdirective tag to specify the encoding of a particular file

Byte Code Generation --
we generated the AST before
from here we generate Byte Code Engineering Library (BCEL)
you can see the classes generated in this folder

Template Classloader
special class loader.
has a cache associated with it
when a file is loaded, it's put into this cache. 2nd time the file is requested, it's pulled from this cache
if not there, it' parses the file (if it is there, it checks the last modified date to see if it needs to be re-parsed or not)
generates the byte code
loads the byte code

Most CFCs in Production don't change after every request
File operations are expenses
so enable Trusted Cache, to turn off these file IO's and improve performance

5 Main phases for CF filter chain
1. Request Validation
2. Path Resolution
3. Application Resolution
4. Parsing
5. Bytecode generation

Classloading --
can set the class paths in the JVM settings
-Dcoldfusion.classpath = "/lib/updates,/lib/home"

in JVM.config
Class loader checks the paths you give it in order
so checks /lib/updates first
if it's not there, checks /lib/home second

RESTful web services --
"a software system designed to support interoperable machine-to-machine interaction over a network"
"machine to machine"
there should be 2 machines -- a client and server
client can be anything (java, javascirpt, whatever)
server is CF

a web service is RESTful when 4 contains are satisfied
1. addressability
2. multiple representations (xml, json, etc)
3. uniform and constrained interfaces
-- get, post, put, delete
4. stateless communication
for scaling of the server. shouldn't save any client-side specific data. client will send all the client related data in the request.

How does a RESTful web service work?
2 logical sections

registration
usage

but why register it?
if it's just a CFM, why register it?
registration is the process of making your service ready for use
so
/students/StudentService.cfc?id=112
would be
/students/112

inputs: application root path and the service name

register application
CF scans for REST CFCs (resetsettings.cfclocation)
then CF generates the skeleton class (restsettings.skipCFCWithError)
then annotates the skeleton (the annotations are what make it into a web service)
then registers it with Jersey