Following on the previous post about monitoring beanstalkd with cacti, I have just uploaded the corresponding templates and script to github.
I hope you’ll find them useful !
Following on the previous post about monitoring beanstalkd with cacti, I have just uploaded the corresponding templates and script to github.
I hope you’ll find them useful !
Recently, we reworked our web frontend infrastructure to separate our corporate website from the Silentale web application. The main goal was to make sure we don’t have to involve our operations resources (a.k.a … me
) too much when we update our corporate web site …
This involved setting up a few new virtual hosts and names:
Host: request header, the SSL connection must have already been established, therefore a proper certificate must have been presented to the client, and nginx cannot decide which one to send (as it can’t read the Host: header) !
Ok, i’m dumb, and I shouldn’t forget you can’t host serveral SSL servers with different names from the same IP address unless you buy one of these expensive wildcard certificates (buying certificates is as close to extortion as to ‘renew’ a domain name or trying to use one of these “limited unlimited 3G+” offers, but that will be the subject of another post …)
So my problem could have been solved if i could assign multiple elastic IPs to the same EC2 instance, and make each of my SSL servers listen on each one of them … unfortunately, you just can’t do that
. An EC2 instance gets an address on a RFC1918 subnet, and that’s the only one you can see from it. In order to be able to connect to it, amazon creates some sort of reverse NAT between the outside world and your instance (i’d like to know how they handle that in their border routers. It must be really interesting and challenging to operate
)
If someone from Amazon reads me, this feature isn’t critical, but it prevents any kind of mutualization on your cloud. I don’t think it is part of your core business, but it may represent a significant share of it in the future (low cost hosters, etc …)
[update: the script and templates discussed below are available on github: http://github.com/earzur/cacti-beanstalkd-templates]
At Silentale, we use working queues to orchestrate the different services in our platform.
Passing messages between different processes is a pretty common pattern for distributing a process across many computers and our platform makes an extensive use of Beanstalkd, which is a fast, lightweight and robust server that does just that: let us implement working queues.
Monitoring the number of jobs available (ready, in the beanstalkd idiom), the number of workers consuming jobs, and the number of jobs that failed (buried) allows operators to get a pretty good idea of how the platform is performing and add or remove capacity when needed. This is particularly useful in our AWS-based environment, when you can add and remove capacity in no time.
For instance, when sending a new batch of invitations to beta users to test our product, we get significant spikes in our working queues, and may need to temporarily add new servers, and then shut them down when we have finished processing the queues.
The protocol used by beanstalkd is pretty simple and provide ways to easily collect usage statistics about the server itself and every individual queue that you might have defined.
Another tool we use a lot at Silentale is Cacti. By default, Cacti allows to graph data collected through snmp-enabled servers pretty easily, so you can graph metrics about your servers such as network bandwidth, load average, and memory usage. But you are not limited to snmp only, you can also graph data collected through scripts. This is a bit more involved, as the Mysql Cacti templates for cacti demonstrates.
After spending a while looking for an equivalent project that would support beanstalkd and work in our environment, I went on writing my own cacti templates for monitoring our beanstalk queues …
The beanstalk-client gem’s ‘tube_stats’ method returns a hash containing a lot of different values, out of which the ones that we want to graph.
#!/usr/bin/env ruby require 'rubygems' require 'beanstalk-client' require 'trollop' script_name = __FILE__.split('/').last opts = Trollop::options do version "#{script_name} © 2009 Silentale S.A." banner <<-EOS display statistics about a queue on a beanstalkd server EOS opt :server, 'beanstalk server address', :type => :string opt :port, 'beanstalk server port (default: 11300)', :type => :integer opt :queue, 'name of the beanstalk queue', :type => :string end Trollop::die :server, 'is mandatory' unless opts[:server] Trollop::die :queue, 'is mandatory' unless opts[:queue] opts[:port] = 11300 unless opts[:port] B = Beanstalk::Connection.new "#{opts[:server]}:#{opts[:port]}" ts=B.stats_tube opts[:queue] ts.delete 'name' result = '' ts.keys.sort.each do |k| result << "#{k}:#{ts[k]} " end puts result
We just connect to the server, issue the tube_stats method, and dump the resulting hash, after having sorted the keys by names.
name: value pairs, separated by spaces. Cacti can directly parse the output using what is called a data input method. Data input methods link external scripts with data fields that can be used in data templates, then those data templates can be used in graph templates.
One unfortunate problem with cacti is that everything has to be done using the GUI, involving a lot of clicks and typing. I didn’t find any way to circumvent this problem, using scripts or any automated tool.
If you managed to read this far and know of any such tool (or a pointer to cacti’s data model) so we can script such tasks, I will be more than happy to receive comments
As you can see in this screenshot, you need to specify the full path to the data-collection script, specify input parameters between brackets (< and >) (here we define the <server>,<port> and <queue>), and then specify the resulting data field one by one.
Now we need to define a data template, that cacti will use to update the RRD archives and generate the graphs.
Those fields have to be carefully named, I have used a convention which I found in the MySQL cacti templates. A few letters in capitals, the same for every fields in the template, followed by the name of the parameter. It is extremely useful to do that, because cacti orders the field names alphabetically in the field selection dropdown list, and there can be many of them ! so remember to carefuly name your input fields in this screen !
In this screen you also tell cacti how to collect the input parameters for your script. This way, cacti will allow you to specify default values when you will create graphs, by associating hosts to the graph templates. Checking the box at the left of the input field will make cacti ask for the associated input value in the forms where you create graphs for hosts. Not checking the box will let cacti decide for you. Here, i’ve left the host field blank with the box unchecked to make sure cacti will automatically fill it with host name to which you are attaching a graph using the template.
In our platform, we want new users, those that just registered, to see messages in their timeline as soon as possible, so we are using a nifty beanstalkd feature which allow us to mark some jobs “urgent” and make sure that those jobs will be handled before the others. Another very nice feature is the ability to delay the jobs for later handling. For instance, if our backend fails at parsing some kind of messages because of encoding issues or whatever. Instead of giving up, we decided to just delay those jobs for a while, get an alert, try to fix the problem in our backend and let the messages flow back in again. That’s the kind of things that beanstalkd and working queues can help you do. I just can’t figure the complexity of doing such things in a monolithic, old-school process !
Let’s define a graph that will display
Then you’ll be presented with a screen where you will have to painfully enter the names of the fields you wish to graph, and you will quickly realise why I insisted on the naming convention for the data fields above …
Not wanting to disclose too much about our operations, i have edited this screenshot to remove figures and hostnames, but you get the idea. The most interesting one is on the upper right corner, with the crawler queue, where we have a constant pool (green stripe on the lower part) of workers pulling crawling jobs (IMAP,POP,twitter, etc …) and posting every new message they’ve found in another queue whose job is to index them … One of my goals as an operator is to keep all these queues as flat as possible. The whitespace indicates that I need to add some crawling capacity, because some jobs are not performed on-time. They are ultimately, but not within the window of time that we allowed ourselves to operate … we are also currently working on some changes that should help reduce the spikes in that particular graph.
Having such monitoring in place is invaluable and has been absolutely necessary for us to keep our service online !
Lazy loading has been a hot topic highly discussed over the internet recently. The amount of bandwidth for the average user has grown tenfold during the last few years, but the web itself has evolved a lot, and so have the contents of the temp folder.
This has created a need to dynamically load a piece of script (to make the host machine download it), instead of just forcing the user download everything from the very first load.
First, here is some Ruby code:
class Message @content = "Welcome Alexandre and Catalin!" @@date = Time.at(0) def self.content @content end def self.date @@date end end class Email > Message end puts Message.content # => "Welcome Alexandre and Catalin!" puts Email.content # => nil puts Message.date # => "Thu Jan 01 01:00:00 +0100 1970" puts Email.date # => "Thu Jan 01 01:00:00 +0100 1970"
At first, when you see “@myvariable” in a class method (prefixed by “self”), you tell yourself that, as you are at the class level, using “@myvariable” and “@@my_variable” has the same behaviour.
But you saw in the previous code that it’s not true. In fact, it has to be not true. Why? Because updating “Email.date” will also update “Message.date”.
The excellent post Class and Instance Variables In Ruby by John Nunemaker details all of this perfectly. He suggests a module ClassLevelInheritableAttributes to avoid inheritance side effects when using class variables.
With this module you can inherit from class variables, but setting your own value in your subclass won’t update the value set in the superclass. Cool.
The Profile concept is pretty common in web applications. To define a Profile class, a Ruby developer usually creates a profile.rb file in its lib directory.
# profile.rb class Profile end
To use this class in a executable script, we can add the lib path in the Ruby load path and then require all files without having to always specify the absolute or relative path of the file we want to load.
#!/usr/bin/env ruby # my_app # Add the 'lib' directory in the Ruby load path $: << File.expand_path File.join(File.dirname(__FILE__), '..', 'lib') # Load the Profile class require 'profile' # Check that the Profile class has been loaded puts 'The Profile class is not loaded' unless defined? Profile
We end up with the following directory structure:
my_app/ bin/ my_app lib/ profile.rb
Execute the my_app script and you’ll see this:
The Profile class is not loaded % cumulative self self total time seconds seconds calls ms/call ms/call name 0.00 0.00 0.00 2 0.00 0.00 IO#write 0.00 0.00 0.00 1 0.00 0.00 Kernel.puts 0.00 0.01 0.00 1 0.00 10.00 #toplevel
Not only our shiny new Profile class is not loaded, but also some profiling stuff are written to STDERR.
The problem is that a profile.rb file is already defined in the Ruby distribution. If you use the Ruby distribution provided by Leopard, check this file: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/profile.rb.
require 'profiler' END { Profiler__::print_profile(STDERR) } Profiler__::start_profile
It starts the Ruby profiler and shows the result at the end of the program -pretty cool BTW-.
To solve this problem, you have to insert you library directories at the beginning of the Ruby load path. That’s what Ruby gems do. Or you can make sure that you always require files by their complete path.
Make a choice for your whole project, because the require statement will load files twice if you do not always give it the same paths. Example:
# Load the Profile class require 'profile' # The 'profile.rb' file is read a second time. require '/home/marcel/code/my_project/lib/profile'
Loading the same class multiple times can be dangerous if you use aliasmethodchain. It will create an infinite loop.
When you begin to learn DRb, you quickly land on the famous Chad Fowler page, entitled “Intro to DRb”.
The “Concurrency” chapter is particularly interesting when you want to make a local resource available in the wild, allowing one request at a time on your resource.
So, with DRb, a dash of method_missing and a pinch of mutex, you have a perfect recipe to remotely access and protect your resource… but: DRb is not what we use to call a high availability entry point.
Let’s code the proof. Here is the code of the DRb server, it simulates a long and expensive task:
require 'drb' class Server def initialize() @i = 0 @mutex = Mutex.new end def method_missing(name, *args) @mutex.synchronize do @i += 1 p @i sleep 1 # You CPU works very hard here.. end @i end end server = DRb.start_service("druby://:34100", Server.new) p "listening" server.thread.join
You can now start the server via the ruby server.rb command and begin to code the client. It creates 100 processes, each one calling the DRb server and writing the response on the standard output:
require 'drb' client = DRbObject.new(nil, "druby://:34100") pids = [] 100.times do pids << fork { p "#{Time.now}: #{client.call}" } end p "#{Time.now}: created the 100 processes" pids.each { |pid| Process.waitpid(pid) } p "#{Time.now}: done"
Launch the client and see what happen:
"Sat Oct 05 11:54:17 +0200 2008: 2" "Sat Oct 05 11:54:17 +0200 2008: 3" "Sat Oct 05 11:54:20 +0200 2008: created the 100 processes" "Sat Oct 05 11:54:17 +0200 2008: 4" "Sat Oct 05 11:54:17 +0200 2008: 5" "Sat Oct 05 11:54:17 +0200 2008: 6" "Sat Oct 05 11:54:17 +0200 2008: 7" "Sat Oct 05 11:54:17 +0200 2008: 8" "Sat Oct 05 11:54:17 +0200 2008: 9" "Sat Oct 05 11:54:18 +0200 2008: 10" ....... "Sat Oct 05 11:54:18 +0200 2008: 59" "Sat Oct 05 11:54:18 +0200 2008: 60" "Sat Oct 05 11:54:19 +0200 2008: 61" "Sat Oct 05 11:54:19 +0200 2008: 61" DRb::DRbConnError: druby://:34100 - # method open in drb.rb at line 736 method each in drb.rb at line 729 method open in drb.rb at line 729 method initialize in drb.rb at line 1189 method new in drb.rb at line 1169 method open in drb.rb at line 1169 method method_missing in drb.rb at line 1085 method with_friend in drb.rb at line 1103 method method_missing in drb.rb at line 1084 at top level in client.rb at line 9 method fork in client.rb at line 8 at top level in client.rb at line 8 method times in client.rb at line 7 at top level in client.rb at line 7
Almost the 100 processes are created before the DRb server returned its first calculation. After several tries on a PowerBook and an EC2 instance, the DRb server rejects any new client after about 65 simultaneous requests.
But, it’s important to mention that the DRb server did not crash at all. You simply have to wait that it handles the remaining requests.
DRb is pretty good, allowing the Ruby developers to code remote services in no time.
If you need high availability services, your next step could be REST servers, distributed/dispatched thanks to HAProxy or nginx. And of course, you should also take a look at Erlang.
You probably read about how easy it was to implement the Proxy design pattern in Ruby.
Thanks to the Ruby method_missing method, you can pass messages to underlying objects. See the previous article Local resource available in the wild, thanks to DRb for a fully described example.
But there’s one caveat, you have to be very careful when implementing your method_missing method.
Take this code for example:
def method_missing(name, *args, &block) # Get the first arg, which contain information about which underlying # object to call. id = arg[0] # Call the corresponding underlying object with the first argument removed my_underlying_objects[id].__send__(name, *args[1..-1], &block) end
If you execute this code, you’ll be stuck in an infinite loop. Why ? There’s a typo, one typo which will cause a segmentation fault. I wrote arg[0] instead of args[0].
To detect this problem before it happens, we can take advantage of the Kernel#caller method. It generates the current execution stask. Here is how we can use it to detect that the current object is calling himself:
def method_missing(name, *args, &block) # Check that we're not calling 'method_missing' recursively if caller.first.include?(__FILE__) raise "#{self.class} is calling itself -method #{name}-. Verify that you do not call a non existing method !!" end # Get the first arg, which contain information about which underlying # object to call. id = args[0] # Call the corresponding underlying object with the first argument removed my_underlying_objects[id].__send__(name, *args[1..-1], &block) end
That’s all, we just check that the caller method is not in the current file. If your method_missing code become more and more complex, especially if it includes some meta-programming tricks, you’ll feel A LOT safer!
One last thing: Kernel#caller is not what we could call a non-expensive method, you should only use it in development.
In any Unix system, to go to your Home directory, you’d type this:
cd ~The ~ character is hidden on Mac keyboards but like all other special characters, it is cleverly placed. You just have to push those keys:
n.
Why cleverly placed ? Think about the spanish ñ character. Try all key combinations with the
with and without the
key. You’ll be surprised. After several minutes, you’ll understand why each special character is placed on one specific character key.
But let’s get back to the subject of this post: to go to your Home directory, you don’t have to type the ~ character, just type:
cdThat’s it.
Now that you know everything about special characters, it’s time to learn the Mac OS X keyboard shortcuts. Feel free to visit the Mac Central website, your place for good, concise, Mac related hints.
It’s always useful to check the code of those downloaded Ruby gems. You should try for at least two reasons: learning and submitting bugs. It’s always a good idea to give technical details about bugs you encounter. The community is small and responsive, get involved
You’re lucky, Leopard user, Apple made a great work embedding Ruby in Mac OS X 10.5. You have the latest version of Ruby, 1.8.6, and RubyGems installed.
Apple guys set a specific directory for all Ruby related things.
/Library/Ruby/Gems/1.8/
This directory contains exploded gems and their documentation. For example, you can examine the content of the ActiveRecord gem and its documentation with those two commands (I hope you use TextMate…).
mate /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/ open /Library/Ruby/Gems/1.8/doc/activerecord-2.1.0/rdoc/index.html
No need to google “activerecord”, everything’s on you hard drive.
Note: Rubygems has evolved since the release of Leopard. But hopefully, it’s simple to update it. Just download the last version and one simple command will do the work.