/
README
342 lines (235 loc) · 11.4 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
Nanite: self assembling fabric of ruby daemons
=============================================================
Nanite is a new way of thinking about building cloud ready web applications. Having
a scalable message queueing back-end with all the discovery and dynamic load based
dispatch that Nanite has is a very scalable way to construct web application back-ends.
A Nanite system has two types of components. There are nanite agents, these are the
daemons where you implement your code functionality as actors. And then there are
mappers.
Mappers are the control nodes of the system. There can be any number of mappers, these
typically run inside of your merb or rails app running on the thin webserver
(eventmachine is needed) but you can also run command line mappers from the shell.
Each Nanite agent sends a ping to the mapper exchange every @ping_time seconds. All of
the mappers are subscribed to this exchange and they all get a copy of the ping with your
status update. If the mappers do not get a ping from a certain agent within a timeout
@ping_time the mappers will remove that agent from any dispatch. When the agent comes
back online or gets less busy it will re-advertise itself to the mapper exchange therefore
adding itself back to the dispatch. This makes for a very nice self-healing cluster of
worker processes and a cluster of front-end mapper processes.
In your Nanites you can have any number of actor classes. These actors are like controllers
in Rails or Merb and this is where you implement your own custom functionality. An actor looks
like:
class Foo < Nanite::Actor
expose :bar
def bar(payload)
"got payload: #{payload}"
end
end
Nanite::Dispatcher.register(Foo.new)
The methods that you 'expose' on the actors are advertised to the mappers like:
Foo#bar => /foo/bar
Mock#list => /mock/list
Every agent advertises its status every time it pings the mapper cluster.
The default status that is advertised is the load average as a float. This
is used for the default request dispatching based on least loaded server.
You can change what is advertised as status to anything you want that is
comparable(<=>) by doing something like this in your agent's init.rb file:
Nanite.status_proc = lambda { MyApp.some_statistic_indicating_load }
This proc will be recalled every @ping_time and sent to the mappers.
This is the 'fitness function' for selecting the least loaded Nanite. You can
dream up any scheme of populating this function so the mappers can select the
right Nanites based on their status.
==============================================================================
Quick note about security:
Nanite security is based upon RabbitMQ vhosts so anything attached to one vhost
can talk to anything else on the same vhost. So you generally want one vhost
per app space.
==============================================================================
Installation
=============
Nanite has a lot of moving parts. Follow the directions below and we'll get you up
and running in no-time.
Install Erlang
--
See the Erlang website for the latest info : http://www.erlang.org/download.html
In your chosen source dir which we'll refer to as <SRC>:
$ cd <SRC>
$ wget http://www.erlang.org/download/otp_src_R12B-5.tar.gz
$ tar -zxvf otp_src_R12B-5.tar.gz
$ cd otp_src_R12B-5
$ ./configure --enable-hipe --enable-darwin-universal
$ make
$ sudo make install
Install Nanite Ruby Gem
--
Installing the gem gives us access to the various nanite commands in the default binary path.
$ cd <SRC
$ git clone git://github.com/ezmobius/nanite.git
$ cd nanite
$ rake gem
$ sudo gem install pkg/nanite-<VERSION>.gem
Install EventMachine Ruby Gem
--
$ sudo gem install eventmachine
Install AMQP Ruby Gem
--
$ cd <SRC>
$ git clone git://github.com/tmm1/amqp.git
$ cd amqp && rake gem && sudo gem install amqp-<VERSION>.gem
Install RabbitMQ from source tarball (OS X)
--
The following websites may be useful:
RabbitMQ website for the latest info : http://www.rabbitmq.com/download.html
RabbitMQ source download : http://www.rabbitmq.com/server.html
RabbitMQ build instructions : http://www.rabbitmq.com/build-server.html
RabbitMQ install instructions : http://www.rabbitmq.com/install.html
These instructions assume v. 1.5.0 (beta)
$ cd ~/<SRC>
$ mkdir rabbit (this will be <RABBIT> hereunder)
$ cd <RABBIT>
$ wget http://www.rabbitmq.com/releases/rabbitmq-server/v1.5.0/rabbitmq-server-1.5.0.tar.gz
$ tar -zxvf rabbitmq-server-1.5.0.tar.gz
$ cd /usr/local/lib/erlang/lib
$ sudo ln -s <SRC>/<RABBIT>/rabbitmq-server-1.5.0 ./rabbitmq-server-1.5.0
* Add the <SRC>/<RABBIT>/rabbitmq-server/scripts to your $PATH
Install RabbitMQ (Linux)
--
TODO: (I'm told it's easier here)
Test your Erlang & RabbitMQ install
--
Start an Erlang shell
$ erl
Enter the following commands in the Erlang shell
(When installed correctly each should print a great deal of erlang config info):
eunit:module_info().
rabbit:module_info().
Exit the Erlang shell with (or Ctrl-c):
q().
Start RabbitMQ
--
$ cd <SRC>/<RABBIT>/rabbitmq-server-1.5.0
$ make run
(Ctrl-c to exit)
In the future you can run RabbitMQ:
(in the foreground)
sudo rabbitmq-server
(in the background)
sudo rabbitmq-server -detached
(check status)
rabbitmqctl status
(stop server)
rabbitmqctl stop
You can learn more about RabbitMQ admin here:
http://www.rabbitmq.com/admin-guide.html
Test Ruby/EventMachine/AMQP end-to-end with a running RabbitMQ instance
--
You can test that all the moving parts are working end-to-end by running one of the AMQP example programs. There are a number of example tests provided with AMQP but the one we will try
simply queues and prints two messages immediately, and another 5 seconds later.
$ cd <SRC>/amqp
$ ruby examples/mq/simple-get.rb
Test Nanite
--
First we'll do a little setup and run a script which adds an agent account (nanite), a mapper account (mapper)
and a '/nanite' vhost. RabbitMQ broker must be running before you run this script.
$ cd nanite
$ ./bin/rabbitconf
Now lets run a few agents. Each of these is a long running process and needs to run in its own shell.
$ cd examples/simpleagent
$ nanite
Now open a new shell and run the same command again.
You should see something like the follow as the output of each:
"advertise_services"
["/simple/echo"]
Now we need to run a mapper. Mappers can be run from within your Merb or Rails app, from
an interactive irb shell, or from the command line. For this example we'll run it from the
command line so open a third shell window and run the following:
$ cd examples
$ ./cli.rb
Which should soon return something like the following. Note that the
'55a7f300c454203eacc218b6fbd2edc6' in this example is the identifier for the agent
that ran the task for you. This is auto-generated.
{"55a7f300c454203eacc218b6fbd2edc6"=>"hello nanite"}
Note : If you want the agents to run with a name you specify instead of the auto-generated id
you can run them with something like:
$ nanite -t bob
Now if you want to make this interesting, you can issue a ctrl-c in one of the agents windows to kill
it. And then run cli.rb again. You should see that you still get a result back since the mapper is finding the
remaining agent to do its work.
If you want to try requesting work to be done by an agent from an interactive command shell try the following.
This assumes that you have the agents running as indicated in the example above.
$ cd nanite
$ ./bin/nanite-mapper -i -u mapper -p testing -v /nanite
>> Nanite.request('/simple/echo') {|res| p res }
By default this will dispatch to the agent with the lowest reported load average.
There are a few other selectors as well:
# run this request on *all* agents that expose the /foo/bar Foo#bar actor
>> Nanite.request('/foo/bar', 'hi', :selector => :all) {|res| p res }
# run this request on one random agent that expose the /whatever/hello Whatever#hello actor
>> Nanite.request('/whatever/hello', 42, :selector => :random) {|res| p res }
You can create your own selectors based on arbitrary stuff you put in status from your agents
see mapper.rb for examples of how least_loaded, all and random are implemented.
You can run as many mappers as you want, they will all be hot masters.
The calls are asynchronous. This means the block you pass to Nanite.request is not run
until the response from the agent(s) have returned. So keep that in mind. Should you
need to poll from an ajax web app for results you should have your block stuff the results in
the database for any web front end to pick up with the next poll:
#merb controller
def kickoffjob(jobtype, payload)
token = Nanite.request(jobtype, payload, :least_loaded) do |res|
# remember this block is called async later when the agent responds
job = PendingJobs.first :ident => res.keys.first
job.result = res.values.first
job.done!
end
# so this happens before the block
PendingJobs.create :ident => token
{:status => 'pending', :token => token}.to_json
end
def poll(token)
job = PendingJobs.first :ident => token
if job.done?
{:status => 'done',
:response => job.result,
:token => token}.to_json
else
{:status => 'pending', :token => token}.to_json
end
end
Have fun!
TROUBLESHOOTING
===============
** IMPORTANT **
If you are using Apple's built in Ruby that comes with Leopard (10.5.x) or Tiger (10.4.x) then your
READLINE lib is hosed and the interactive shell will not work. As soon as you drop into a shell
Apple's fakey READLINE will halt the event machine loop so you won't see any nanites
registering. I don't have a good workaround except to tell you not to use Apple's Ruby,
build your own or use ports.
** What to do if rabbitconf dies with: {badrpc,nodedown} and nothing you do seems to matter **
If rabbitconf dies saying "{badrpc,nodedown}" it means that for some reason,
the rabbitmqctl program rabbitconf is using to setup the agent accounts for nanite is
not able to connect to your RabbitMQ server. Assuming RabbitMQ is running and is
known to work (try the examples that come with the amqp library), then there's a chance
something is going wrong with using short node names in your Erlang install.
The easiest way to verify it is by starting two separate Erlang shells like this (note that "odin" is my hostname):
$ erl -sname fred
(fred@odin)1>
$erl -sname bob
(bob@odin)1>
And then trying to 'ping' one from the other. In the 'fred' node you can do that like
this:
(fred@odin)1> net_adm:ping(bob@odin).
pang
Note : If your hostname has a hyphen in it (e.g. macbook-pro) you should include the user@hostname in quotes like:
net_adm:ping('bob@macbook-pro').
If you see 'pang' (which is apparently Swedish for something like "crap! it's broken.") then short name distribution isn't working for you, and you need to fall back to using a full name. If you see 'pong', then that's not actually your problem.
First, verify that your system's hostname is set to a fully qualified domain name. On OS X it can end in '.local':
$ hostname
odin.local
Then test that *this* will work by starting a node with the full name of 'fred@<your hostname>':
$ erl -name fred@odin.local
(fred@odin.local)1>
then bob@<yourhostname>, and then finally try to ping fred:
$ erl -name bob@odin.local
(bob@odin.local)1> net_adm:ping(fred@odin.local).
pong
In my case, it looks like that worked. Now... on to getting rabbitconf to run! To do that, you need to edit the 'rabbitmq-server' and 'rabbitmqctl' scripts in your RabbitMQ distribution and edit the -sname arguments to use -name and a full name.