RVM How-to

December 6, 2011 Leave a comment

# install rvm to local

$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

# update shell rc to load rvm
$ vi ~/.bashrc
> # load local RVM
> export PATH=:/home/seung/.rvm/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin
> [[ -s "/home/seung/.rvm/scripts/rvm" ]] && source "/home/seung/.rvm/scripts/rvm"

# show OS-dependent requirements
$ rvm requirements

# install libs for ruby 1.9.2
$ sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel

# install ruby versions
$ rvm install 1.9.2
$ rvm install 1.8.6

# switch between ruby versions
$ rvm use 1.9.2
$ rvm use 1.8.6
$ rvm system

# switching ruby and gemset by entering project directory
$ vi my_project/.rvmrc
> rvm 1.8.6@project_name

$ rvm gemset create 'project_name'

Quoted from screencast

Categories: Ruby on Rails

MySQL Loading Large Data Into InnoDB Table

October 24, 2011 Leave a comment

When InnoDB loads the file, it creates one big transaction with a lot of undo log entries. One good trick to avoid this is split the data file into reasonable size.

Before running the loads, executed the following commands from that script file for the load data connection session:
— SET foreign_key_checks = 0
— SET unique_checks = 0
— SET sql_log_bin = 0

For example:
"set foreign_key_checks=0; set sql_log_bin=0; set unique_checks=0; load data local infile 'infile.txt' into table load_test fields terminated by '\t' lines terminated by '\n' (col1, col2);"

Avoid creating duplicate index.

Categories: Ops

MySQL Tuning for InnoDB

October 24, 2011 Leave a comment

Rule of thumb for tuning mysql:

⚈  Start with released copy of my.cnf / my.ini.
⚈  Set key_buffer_size and innodb_buffer_pool_size according to engine usage and RAM.
⚈  Slow queries can usually be ‘fixed’ via indexes, schema changes, or SELECT changes, not by tuning.
⚈  Don’t get carried away with the Query cache until you understand what it can and cannot do.
⚈  Don’t change anything else unless you run into trouble (eg, max connections).
⚈  Be sure the changes are under the [mysqld] section, not some other section.

From Rick James, MySQL Performance Forum

I have followed above recommendation to tweak “my.cnf” for MySQL running on Amazon EC2 micro instance. Here’s list of changes I made from my-innodb-heavy-4G.cnf.

query_cache_size = 0          <= disable query cache
#log-bin=mysql-bin             <= disable binary log
slow_query_log = 0             <= disable slow query
#long_query_time = 2
key_buffer_size = 10M        <= minimum key_buffer_size
innodb_buffer_pool_size = 1G    <= 60% of available memory
#innodb_write_io_threads = 8   <= my version doesn't take these
#innodb_read_io_threads = 8
innodb_flush_log_at_trx_commit = 0
innodb_log_group_home_dir = /var/run/mysqld  <= local disk
innodb_file_per_table          <= use separate file for table
log-error = /var/log/mysqld.log

Here’s couple of useful command:


More to read at secaserver.

Categories: Ops

and vs && in Ruby

August 5, 2011 Leave a comment

There are some differences between ‘and’ and ‘&&’. This can cause a bug that cannot be easily detected. General recommendation is this:

Do not use ‘and’ at all.

Detail here.

Categories: Ruby on Rails

Ruby Concurrent Programming

August 4, 2011 Leave a comment


Multi-thread programming in Ruby works only if thread does heavy I/O tasks. Because Ruby doesn’t support native thread and/or keeps Global Interpreter Lock.
Multi-process programming works in Ruby with better IPC.

A tuple space provides a repository of tuples that can be accessed concurrently.

Built-in library to support tuple space for Ruby.
Tuple can be an array or hash
Supports read, read_all, write, take, notify operations.
Operations are synchronous with optional timeout.
Volatile data

Code Sample (server)

require ‘rinda/tuplespace’

ts = Rinda::TupleSpace.new
DRb.start_service(‘druby://localhost:1234’, ts)

Code Sample (client)

require ‘rinda/tuplespace’

ts = DRBObject.new(nil, ‘druby://localhost:1234’)

AMQP (Advanced Message Queuing Protocol)
: is an open Internet (or “wire”) Protocol standard for message-queuing communications.
: http://www.amqp.org/

: AMQP exchange implementation on OTP (Open Telecom Platform)
: download, install, and run.
: http://www.rabbitmq.com/

: Ruby amqp gem is a widely used, feature-rich, well-maintained, fast asynchronous AMQP 0.9.1 Ruby client.

:Bunny is a Ruby library that enables you to interact with an AMQP-compliant message broker/server, such as RabbitMQ, in a synchronous way.

: ØMQ (ZeroMQ, 0MQ, zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry whole messages across various transports like in-process, inter-process, TCP, and multicast. You can connect sockets N-to-N with patterns like fanout, pub-sub, task distribution, and request-reply. It’s fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has a score of language APIs and runs on most operating systems. ØMQ is from iMatix and is LGPL open source.
: http://www.zeromq.org/

Categories: Ruby on Rails

File system tweak for faster testing

July 21, 2011 Leave a comment

How to tweek file system for faster regression testing

1. File system options

* journal_data_writeback

* noatime


2. Change log

$ cat /etc/fstab
/dev/hda3 / ext3 defaults,noatime,data=writeback 1 1
/dev/hdb1 /tmp ext3 defaults,noatime,data=writeback 1 2

$ cat /boot/grub/grub.conf
kernel /vmlinuz-2.6.18-194.8.1.el5 ro rootflags=data=writeback root=/dev/hda3
$ sudo /sbin/update-grub

$ sudo /sbin/tune2fs -o journal_data_writeback /dev/hdb1
$ sudo /sbin/tune2fs -o journal_data_writeback /dev/hdd1
$ sudo /sbin/tune2fs -l /dev/hdb1 | grep mount
$ sudo /sbin/tune2fs -l /dev/hdd1 | grep mount
$ sudo mount -o noatime /dev/hdb1 /media/hdb1

Categories: Ruby on Rails

Set RAILS_ENV for cron job

April 25, 2011 Leave a comment

/usr/bin/env RAILS_ENV=production ruby /…path/app/automatic/tweet_today.rb

from Rails Forum

Categories: Ruby on Rails