www.YoYoBrain.com - Accelerators for Memory and Learning Questions for

Category: Introduction - (16 questions) Mongrel is described in what way in the "A container for Ruby on Mongrel pdf available from O Reilly Rails" Mongrel is compared with what web servers production performance: Fast CGI or SCGI in the Rails world in terms of production performance and development Development: WEBrick simplicity/speed Creator of Mongrel Zed A Shawwww.zedshaw.com Mongrel is developed on what mixture of Ruby and C programming/scripting languages Documentation for Mongrel mongrel..org/docs/index.html The creators of Mongrel describe it how? a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI. It is framework agnostic Three key technologies that are used for A custom HTTP 1.1 parser (based on RFC Mongrel's internals standard, written using in C and Java as a Rby extension) Simple server that uses the parser and URIClassifier to process requests, find the right handlers, then pass the results to the handler for processing Handlers are responsible for using HttpRequet and HttpResponse objects to "do their thing and then return results" Component of Mongrel responsible for Handlers dealing with HttpRequest and HttpResponse How does Mongrel support threading one thread per request, but it will start closing connections when it gets "overloaded"while Mongrel is processing HTTP requests and sending responses it uses Ruby's threading system What platforms that already work with and Og+Nitro Mongrel are throught to be "thread-safe" Have not been heavily tested Is thread safe? no How does Mongrel handle Rails" " Ruby on Rails is not thread safe so there is a synchronized block around the calls to Dispatcher.dispatch. This means that everything is threaded right before and right after Rails runs. While Rails is running there is only one controller in operation at a time. This is why people typically have to run a small set of Mongrel processes (a "Pack of Mongrels") to get good concurrency." How does mongrel handle things if you have It doesn't, and this can cause performance long running actions? problems....and the author of Mongrelrecommends you look for ways to offlaod work to another process so the rails app can keep working Describe how mongrel works (esoteric flashcard) A request hits mongrel. Mongrel makes a thread and parses the HTTP request headers. If the body is small, then it puts the body into a StringIO. If the body is large then it streams the body to a temp file. When the request is "cooked" it call the RailsHandler. The RailsHandler sees if the file is possibly page cached, if so then it sends the cached page. Now you're finally ready to process the Rails request. LOCK! Still locked, Mongrel calls the Rails Dispatcher to handle the request, passing in the headers, and StringIO or Tempfile for body. When Rails is done, UNLOCK! . Rails has (hopefully) put all of its output into a StringIO. Mongrel then takes this StringIO output, any output headers, and streams them back to the client super fast.

minor processing related command to remove svn files so they cannot be browsed on your find . -name ".svn" -exec rm -rf {} \; Does mongrel have SSL no Category: Configuration - (17 questions) Recommend solution to avoid giving people use svn export rather than checkout to get access to your .svn files your code Recommended way to get Mongrel installed due to the way fedora chops up it packages, on Fedora Mongrel author recommends installing Ruby from Source, then installing Mongrel Common causes of Mongrel stopping proper Lots, moany related to using resources that functioning are not shared properly between multiple processes Common problem with logs that can stop Configuring Logger to rotate log files. Logger Mongrel's proper functioning doesn't do this reliably between processes, so use an external log rotation like logrotate. IMPORTANT security idiosynchrocy of using doesn't use svn export to create Capistrano and svn to deploy the deployment directory so there's a bunch of extra information in the .svn files. Without special config changes to your web server you'll be letting people access these. common cause of Mongrel not functioning not having MySQL timeouts properly set properly related to MySQL Common cause of Mongrel not functioning using Pstore - no faster than db and has properly related to sessions frequent locking and coordination issues Common cause of Mongrel not functioning Sharing a file or similar service without properly related to file sharing proper locking. SQLite and BerkleyDB are both culprits here. Common cause of mongrel not functioning Using up too much CPU or memory on properly related to hardware will cause the oomkiller to kill your Mongrel process. Recommended way to get mongrel servers For Rails the best way is either memcached within a cluster to synch sessions (with or database session storage. Do not use Rails) PStore''' It is broken, will crash or lock your Mongrel processes, and really isn't all that fast. what is num-procs? affects resource eaten by deployments - Determines how many active requests are allowed before clients are denied and old requests are killed off. how does the timeout setting throttle your The timeout option is what you use if you server want to make sure that a Mongrel server can't take on too much work (i.e. you need to throttle it). What it does is sleep for N 100th/second after each accept. This means that it will slow down the number of incoming clients. Very handy if you have a shared hosting system and don't want people to eat your servers. Known problem related to MySQL " If you find that Mongrel stops working after a long idle time and you're using MySQL then you're hitting a bug in the MySQL driver that doesn't properly timeout connections. What happens is the MySQL server side of the connection times out and closes, but the MySQL client doesn't detect this and just sits there. What you have to do is set:

ActiveRecord::Base.verification_timeout = 14400

Or to any value that is lower than the MySQL server's interactive_timeout setting. This will make sure that ActiveRecord checks the connection often enough to reset the connection.

" idiosyncrocy of Mongrel and PIDs There was a change to the 0.3.13.4 release and on that makes it so Mongrel doesn't try to "fix" invalid paths. You should change your configurations so that they give explicit full paths to both the log and pid files. Most common cause of MySQL "lost The most common cause is that you're using connection to database" errors mysql.rb that comes with Rails rather than the MySQL compiled ruby driver from gems. Do this:

sudo gem install mysql

And pick the one for your system (don't type sudo if you're win32). How can you run some code before rails implest way is to put it in your production.rb starts and configure it there, but if you need it done long before Rails starts, then you can throw it into a mongrel.conf and run that file with the -S option (see mongrel_rails --help). The mongrel.conf is a Ruby script that lets you configure Mongrel with special Ruby code, but you can also put other Ruby code in it. What does BAD CLIENT mean " It means that a request came in which Mongrel rejects because it doesn't follow the RFC grammar. Mongrel is pretty relaxed about most requests, but in order to block the majority of security attacks for web servers it is strict about characters used, header formats, status line formats, etc. This is also based on matching the RFC's grammar specification to a Ragel grammar specification, so it's easy to compare. If you need to know why the client is triggering this, then simply hit your Mongrel processes with USR1 signals and they'll log the full request data and parameters that were collected. Then, if you think the request is valid send me this data and I'll look. If it's not valid than fix the client. Mongrel takes the stance that all clients are written by software developers and that they should follow the standard. By doing this it reduces the bugs and potential security holes found in many other web servers. It also means that if you absolutely have to allow a bad client, then you'll need to not use Mongrel. " Category: Syntax and Commands - (21 questions) purpose of stop -f same as stop --force forces shutdown Mongrel: purpose of -p option -p FILE or --pid FILE tells mongrel where to write the PID install command for mongrel via sudo gem install mongrel purpose of start -c -c PATH same as start --chdir PATH

changes to this directory before starting command to stop Mongrel mongrel_rails stop command to run mongrel in the background mongrel_rails start -d command to see options you can set for mongrel_rails start -h Mongrel start command Option Mongrel provides for debugging mongrel_rails start -B option. memory leaks It will log objects that get created to log/mongrel_debug and you can look in there to find out what object is causing the problems. command option that lets you set an arbitrary uri as your prefix mongrel_rails start -e production --prefix=/someprefix -a option of start command same as --address defaults to 0.0.0.0 purpose of -l option same as --log provide name of logfile ...must be absolute path to log file -n option on start, default value n,--num-procs (:num_processors) Maximum number of concurrent processing threads to allow. Once this number of threads is reached, Mongrel will start denying new connections and trying to kill some of the old threads. Default: 1024 purpose of start option -t,--timeout (:timeout) -t,--timeout (:timeout) Time to pause between accepting clients in centiseconds (1/100th). This is a throttle mechanism. Default: 0 start -c option same as --chdir - directory to change before staring Mongrel. cwd is fshort change working directory, set this to be your Rails root directory if you wont' be running Mongrel in that directory purpose of -r option of start command same as --root - provide with a docroot. sets the document root. if you are using a path different than the default public or you are running mongrel under a different base URI you should set this option. it defaults to public. -B option of start command same as --debug. enables debugging mode...everything is logged to log/mongrel_debug directory. Note this slows performance greatly -C option of start command specifies a configuration YAML file that sets startup options - takes an absolute path. same as --config.

the YAML file can be generate with the -G option.

there is no default value -S option of start command same as --script give it a config script in the form of a ruby file. Loads the given ruby file and runs it after rails is configured....thus you could change the configuration...load custom mongrel handlers, extra libraries etc. etc. purpose of the -G option of the start same as --generate command generates a YAML configuraiton file using the options spce'd in the command line + defaults of any options not specified...later use -C to use this file start command option to have your rails --prefix uri application mounted on a given URI prefix note rails strips the URI off all requests so do not end it in a "/" purpose of --user and --group options of start must be run together - mongrel changes to command the user after the listening socket is created. allows you to run Mongrel as something other than root,

Category: Performance Tuning - (28 questions) Magic numbers for for number of mongrel is not one...anecdotally the recommendation processes per CPU is 8-12 per cpu but real recommendation is to follow tuning procedure found on mongrel.rubyforget Common cause of mongrel not being able to need to install the seperate ruby-devel build on unix package three mongrel rails commands start stop restart how can you get mongrel to provide you with install the mongrel_status plugin gem status gives you a status command command to run mongrel in production mode mongrel_rails start -e production Recommended deployment for set up for use a load balancer Rails production enviroment command for running Mongrel as standalone mongrel_rails start server command for running mongrel in background mongrel_rails start -d on Unix as standalone How do you make your Mongrel app start wiret a start script that resides at automatically with the system etc/init.d/mongrel_myapp has to be executable, looks like this

#!/bin/sh # # Mongrel startup script # # description: Mongrel daemon for myapp

APP_DIR=/home/user/myapp PORT=80 RAILS_ENV=production case "$1" in start) # # Start Mongrel # mongrel_rails start -d -p $PORT -e $RAILS_ENV -c $APP_DIR ;; stop) # # Stop Mongrel # mongrel_rails stop -c $APP_DIR ;; restart) # # Restart Mongrel # mongrel_rails restart -c $APP_DIR ;; *) echo "Usage: ./myapp (start|stop|restart)" exit 1;; esac REcommended minimum configuration in a Use a TCP balancer that allows multiple production environment for Mongrel Mongrel instance that appear to be one server to the outside wold Simple way to set up TCP balancing with use Mongrel Cluster and either Pen or Mongrel Balance Pen (http://siag.nu/pen/) or Balance (http://www.inlab.de/balance.html). how does HTTP balancing work with n HTTP balancer like Pound mongrel (http://www.apsis.ch/pound/) provides everything a TCP balancer does, along with support for SSL. You can also serve your Rails application alongside a PHP application, since it is smart enough to route different URIs to different applications. what is Pound HTTP balancer like Pound (http://www.apsis.ch/pound/) provides everything a TCP balancer does, along with support for SSL. Good sources of configuration how to for here is some good documentation on how to mongrel and apache with Capistrano do it by Coda Hale (http://blog.codahale.com/2006/06/19/time-fo r-a-grown-up-server-rails-mongrel-apache-ca pistrano-and-you/), Charles Brian Quinn (http://mongrel.rubyforge.org/docs/apache.ht ml), and Bradley Taylor (http://fluxura.com/articles/2006/05/19/apach e-for-static-and-mongrel-for-rails-with-mod_d eflate-and-capistrano-support). Apache version required to run a clust of Apache 2.2 - it requires load balancing, ust mongrel servers use apache 2.1+ implied step one of configuring mongrel and get it to work with a single mongrel instance apache prior to configuring a cluster First step to running apache first start mongrel

$ mongrel_rails start -p 8000 -e production -d Second step to configuring single mongrel Add virtual host configuration to httpd.conf or instance with apache, done AFTER starting host.conf mongrel Then add the following VirtualHost configuration to your httpd.conf or vhost.conf file. If your apache is set up with the directory /etc/httpd/conf.d, you can create the file /etc/httpd/conf.d/myapp.conf and put it in there instead. It makes for a smaller and less confusing httpd.conf file. The rest of this section assumes you have a directory called conf.d that you can put configuration files into.

ServerName myhostname.com ServerAlias www.myhostname.com ProxyPass / http://www.myhostname.com:8000/ ProxyPassReverse / http://www.myhostname.com:8000/ ProxyPreserveHost on After modifying Apache's VirtualHost Restart apache server configuration, what steps are necessary How does mongrel help when performance it doesn't issues are related to memory How do you fire up a mongrel rails cluster mongrel_rails cluster::start When do you need to run cluster::configure only once command command to find out what version of Apache httpd -v you have on redhat/fedora linux 7 modules loaded that need to be available and loaded in httpd.conf mod_proxy

mod_proxy_balancer

mod_rewrite

mod_headers

mod_cache and either mod_memcache or mod_filecache for caching (optional)

mod_ssl if you need SSL (optional)

mod_status if you will be using the load-balancing manager (optional) command to see what modules are compiled with apache in linux $ ldd /usr/sbin/httpd (Linux) How do you enable Load balancing in an o load mod_proxy_balancer and mod_status. apache/mongrel configuration Check in httpd.conf to make sure they are being loaded, or check to make sure it was compiled into Apache with ldd /usr/sbin/httpd

How can you set up apache/mongrel so that Create the file you can view and use the load balancer /etc/httpd/conf.d/myapp_proxy_manager.conf and add these lines to allow you to see the balancer on localhost at port 8080.

Listen 8080 SetHandler balancer-manager Deny from all Allow from localhost purpose of mod_proxy_balancer 's balancer change the balance factor for a particular manager Mongrel instance, or put it in the off-line mode. Category: Debugging - (16 questions) Files created by starting Mongrel with -B

rails.log

objects.log

files.log

threads.log ALL CREATED IN log/mongrel_debug directory purpose of rails.log log of request params that go from mongrel to rails purpose of objects.log existing top 20 count of object types before and after earch request purpose of files.log logs open files before and after each request purpose of threads.log logs all active threads before and after each request in logfiles, what is REQUEST_URI the handler path plus the PATH_INFO where is Mongrel's handler at by default / info to be sure you check in logs when Path info debugging Server Software Request URI Request method to check to see if you are opening files and files.log not closing them (such that rails would stop responding) check which log file to figure out what requests are coming in, rails.log and where they are going, use which log file Mongrel: sign that your application is not the number of files open at the start of each closing files request would increase, and there would be many files open when Mongrel exits. log file to help you pinpoint which objects are objects.log it gives you the top 20 objects, using the most memory the number of instances monkey technique to see if an object is start Mongrel (debug mode)...then tart hitting leaking memory each of your pages several times each. observe where the object count in objects.log begins to grow dramatically file that tells you if you are leaking threads threads.log common cause of thread leaks using IO.popen and not cleaning up what can you observe that will show a thread in threads.log, the number of threads leak? increases and does not decrease Category: mongrel_cluster - (2 questions) command to configure mongrel_cluster (after cluster::configure, along with any options you its installed) want to include in the configuration. The -N option allows you to specify the number of Mongrel servers you want in the cluster.

mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N4 creates a file mongrel_cluster.yml to your mongrel_rails cluster::configure -e config directory, with the settings you production -p 8000 -a 127.0.0.1 -N4does specified what?