Top Banner
JRuby on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, Sun Microsystems, Inc. TS-6490
56

JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

Jun 10, 2018

Download

Documents

hadien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

JRuby on Rails Deployment:What They Didn't Tell You

Nick Sieger, Staff Engineer, Sun Microsystems, Inc.

TS-6490

Page 2: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 2

Who is Nick?

Lead Developer on a Rails project at SunJRuby core team memberJRuby Rails add-ons maintainer JavaTM programming language – 7 yearsRuby – 3 years

First JavaOneSM conference!

Page 3: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 3

coming soon!

Page 4: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 4

Agenda

Rails deployment backgroundMechanics of JRuby on RailsPreparationsPackagingPost-deployment

Page 5: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 5

Page 6: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 6

Page 7: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 7

Page 8: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 8

Brief history of Rails deployment

Page 9: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 9

The Concurrency Question

“Rails is not thread-safe? WTF?”

“Servlets have been multi-threaded since, like,1997!”

Each Rails request must execute in isolation.

Page 10: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 10

Context for Concurrency

Writing thread-safe code is hard

+ Ruby's userland threads don't yield much benefit

Not worth the effort

Rails scales up by adding processes

Page 11: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 11

CGI/FGCI

http://flickr.com/photos/counteragent/2190576349/

Page 12: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 12

Mongrel

http://flickr.com/photos/bugbunnybambam/2172466178/

Page 13: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 13

Developed by “that guy”...

Page 14: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 14

Mongrel Model

Page 15: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 15

The Scalability Myth

Rails scales just fine...

Page 16: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 16

http://god.rubyforge.org/

Page 17: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 17

= * 180

http://highscalability.com/scaling-twitter-making-twitter-10000-percent-faster

Page 18: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 18

JRuby – Rails Deployment Evolved

Page 19: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 19

Instead of multiple processes...

Page 20: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 20

The Java Virtual Machine (JVM™):All Ur Rails Are Belong To Us

Page 21: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 21

Interlude...

Page 22: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 22

“JRuby: Colon Blow for your OS Processes”

Page 23: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 23

Application Startup

Page 24: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 24

Dispatching to Rails

Page 25: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 25

Setting Up Rails for JRuby

Page 26: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 26

Developing Your Rails Application

TS-4806: JRuby on Rails: Web Development EvolvedTS-5249: The NetBeans™ Ruby IDE: You Thought Rails Development Was Fun BeforeBoth yesterday

Page 27: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 27

Database ConnectivityInstall the ActiveRecord Java DataBase Connectivity (JDBCTM) adapter gem (mysql)

$ jruby -S gem install activerecord-jdbcmysql-adapterSuccessfully installed activerecord-jdbc-adapter-0.8Successfully installed jdbc-mysql-5.0.4Successfully installed activerecord-jdbcmysql-adapter-0.83 gems installedInstalling ri documentation for activerecord-jdbc-adapter-0.8...Installing ri documentation for jdbc-mysql-5.0.4...Installing ri documentation for activerecord-jdbcmysql-adapter-0.8...Installing RDoc documentation for activerecord-jdbc-adapter-0.8...Installing RDoc documentation for jdbc-mysql-5.0.4...Installing RDoc documentation for activerecord-jdbcmysql-adapter-0.8...

Page 28: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 28

Page 29: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 29

Configure the Database

# config/database.yml<% jdbc = defined?(JRUBY_VERSION) ? 'jdbc' : '' %>development: adapter: <%= jdbc %>mysql encoding: utf8 database: testapp_development username: root password: socket: /tmp/mysql.sock

# same for test/production...

Page 30: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 30

Examine dependencies

Gems and libraries with native code == FAIL on JRubyLook for replacements/equivalents:• Mongrel, Hpricot OK

• RMagick, ImageScience => ImageVoodoo

• OpenSSL => JRuby-OpenSSL gem

• Ruby/LDAP => JRuby/LDap

• json => json_pure

Page 31: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 31

Try It!

$ jruby -S gem install mongrelSuccessfully installed mongrel-1.1.3-java1 gem installedInstalling ri documentation for mongrel-1.1.3-java...Installing RDoc documentation for mongrel-1.1.3-java...$ jruby script/server=> Booting Mongrel (use 'script/server webrick' to force WEBrick)=> Rails application starting on http://0.0.0.0:3000=> Call with -d to detach=> Ctrl-C to shutdown server** Starting Mongrel listening at 0.0.0.0:3000** Starting Rails with development environment...** Rails loaded.** Loading any Rails specific GemPlugins** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).** Rails signals registered. HUP => reload (without restart). It might not work well.** Mongrel 1.1.3 available at 0.0.0.0:3000** Use CTRL-C to stop.

Page 32: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 32

Packaging with Warbler

Page 33: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 33

Application Layout (Rails)

Page 34: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 34

Application Layout (WAR)

Page 35: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 35

Using Warbler

Installed as a gem (jruby -S gem install warbler)

JRuby and servlet adapter bits includedAutomates war configuration

Page 36: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 36

Installing Warbler as a Plugin

$ jruby -S warble pluginize/Users/nicksieger/Projects/jruby/trunk/jruby/bin/jruby -S gem unpack warblerUnpacked gem: '/Users/nicksieger/Projects/rails/testapp/vendor/plugins/warbler-0.9.5'$ jruby -S rake -T | grep warrake war # Create testapp.warrake war:app # Copy all application files into the ...rake war:clean # Clean up the .war file and the stagi...rake war:gems # Unpack all gems into WEB-INF/gemsrake war:jar # Run the jar command to create the .warrake war:java_classes # Copy java classes into the .warrake war:java_libs # Copy all java libraries into the .warrake war:public # Copy all public HTML files to the ro...rake war:webxml # Generate a web.xml file for the webapp

Page 37: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 37

Configuring Warbler

Generate config file• jruby -S warble config -or-

• jruby script/generate warble• => config/warble.rb

# Warbler web application assembly configuration fileWarbler::Config.new do |config| # ...

# Gems to be packaged in the webapp. ... config.gems += ["activerecord-jdbcmysql-adapter", "jruby-openssl"] config.gems["rails"] = "2.0.2"

# ...end

Page 38: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 38

Demo: Packaging and Deploying a Rails Application to GlassFish™ project

Page 39: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 39

Tuning Your Deployment

Page 40: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 40

Runtime Pool Size

http://flickr.com/photos/86974734@N00/2206011624/

Page 41: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 41

Runtime Pool Size

config/warble.rb:

Warbler::Config.new do |config| # ...

# Control the pool of Rails runtimes config.webxml.jruby.min.runtimes = 2 config.webxml.jruby.max.runtimes = 4end

Page 42: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 42

Loggingif defined?(JRUBY_VERSION) && defined?($servlet_context) # Logger expects an object that responds to #write and #close device = Object.new def device.write(message) $servlet_context.log(message) end def device.close; end

# Make these accessible to wire in the log device class << RAILS_DEFAULT_LOGGER public :instance_variable_get, :instance_variable_set end

old_device = RAILS_DEFAULT_LOGGER.instance_variable_get "@log" old_device.close rescue nil RAILS_DEFAULT_LOGGER.instance_variable_set "@log", deviceend

Page 43: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 43

Session handling

Rails sessions != Servlet sessions

config.action_controller.session_store = :java_servlet_store# ...class CGI::Session::JavaServletStore def initialize(session, options) end def restore; end def update; end def close; endend

Page 44: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 44

Database: JNDI

production: adapter: <%= jdbc %>mysql jndi: jdbc/testapp_production encoding: utf8 database: testapp_production username: root password: socket: /tmp/mysql.sock

Page 45: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 45

Database connections

Use JNDI DataSource with a connection poolBut Rails doesn't close connections by default (!)

# config/initializers/close_connections.rbif defined?($servlet_context) require 'action_controller/dispatcher' ActionController::Dispatcher.after_dispatch do ActiveRecord::Base.clear_active_connections! endend

Page 46: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 46

Caching and File stat'ing

Ensure view caching is enabled (default in Rails 2.0.2)• config.action_view.cache_template_loading = true

Avoid asset ID timestamp checks with RAILS_ASSET_ID• ENV['RAILS_ASSET_ID'] = “r#{source_revision}”

Ensure full-page cache directory points to root of WAR• config.action_controller.page_cache_directory• Rails.public_path coming in 2.1

Page 47: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 47

Performance

Page 48: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 48

Page 49: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 49

Page 50: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 50

Precompiling Rails

Compile Ruby code to bytecode before executionSystem property: -Djruby.compile.mode=FORCELeverage existing tools for profiling, troubleshooting

class ErrorsController < ApplicationController def index java.lang.Thread.dumpStack endend

Page 51: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 51

java.lang.Exception: Stack traceat java.lang.Thread.dumpStack(Thread.java:1176)at ruby...testapp.app.controllers.errors_controller.index__1 (.../app/controllers/errors_controller.rb:3)at ruby...action_controller.base.perform_action__66 (.../gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:1158) ...at ruby...action_controller.base.process__25 (.../gems/1.8/gems/actionpack-2.0.2/lib/action_controller/base.rb:388)at ruby...action_controller.dispatcher.handle_request__18 (.../gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:171)at ruby...action_controller.dispatcher.__rescue_5 (.../gems/1.8/gems/actionpack-2.0.2/lib/action_controller/dispatcher.rb:115)

Page 52: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 52

Introducing JRuby-Rack

http://wiki.jruby.org/wiki/JRuby_Rack0.9 -- First public release today!Also released – Warbler 0.9.9 – bundles jruby-rack 0.9

Page 53: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 53

Introducing JRuby-Rack

Adapter from servlets to Rack• Rack == Ruby web server abstraction layer

• http://rack.rubyforge.org/

Eliminate manual setupRuns Rails, Merb, any Rack-compatible frameworkSeamless integration with non-Rails servlet components

Page 54: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 54

Demo: JRuby-Rack and Servlet Integration

Page 55: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

2008 JavaOneSM Conference | java.com.sun/javaone | 55

Your Questions

Page 56: JRuby on Rails Deployment: What They Didn't Tell You on Rails Deployment: What They Didn't Tell You Nick Sieger, Staff Engineer, ... Precompiling Rails Compile Ruby code to bytecode

JRuby on Rails Deployment: What They Didn't Tell YouNick Siegerhttp://blog.nicksieger.com/

TS-6490