1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::SNMP - adaptor to integrate Net::SNMP into Anyevent. |
3 | AnyEvent::SNMP - adaptor to integrate Net::SNMP into AnyEvent. |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::SNMP; |
7 | use AnyEvent::SNMP; |
8 | use Net::SNMP; |
8 | use Net::SNMP; |
… | |
… | |
23 | my @result = $cv->wait; |
23 | my @result = $cv->wait; |
24 | |
24 | |
25 | =head1 DESCRIPTION |
25 | =head1 DESCRIPTION |
26 | |
26 | |
27 | This module implements an alternative "event dispatcher" for Net::SNMP, |
27 | This module implements an alternative "event dispatcher" for Net::SNMP, |
28 | using AnyEvent as a backend. |
28 | using AnyEvent as a backend. This integrates Net::SNMP into AnyEvent. That |
29 | |
29 | means you can make non-blocking Net::SNMP calls and as long as other |
30 | This integrates Net::SNMP into AnyEvent: You can make non-blocking |
30 | parts of your program also use AnyEvent (or some event loop supported by |
31 | Net::SNMP calls and as long as other parts of your program also use |
31 | AnyEvent), they will run in parallel. |
32 | AnyEvent (or some event loop supported by AnyEvent), they will run in |
|
|
33 | parallel. |
|
|
34 | |
32 | |
35 | Also, the Net::SNMP scheduler is very inefficient with respect to both CPU |
33 | Also, the Net::SNMP scheduler is very inefficient with respect to both CPU |
36 | and memory usage. Most AnyEvent backends (including the pure-perl backend) |
34 | and memory usage. Most AnyEvent backends (including the pure-perl backend) |
37 | fare much better than the Net::SNMP dispatcher. |
35 | fare much better than the Net::SNMP dispatcher. |
38 | |
36 | |
|
|
37 | Another major added fetaure of this module over Net::SNMP is automatic |
|
|
38 | rate-adjustments: Net::SNMP is so slow that firing a few thousand |
|
|
39 | requests can cause many timeouts simply because Net::SNMP cannot process |
|
|
40 | the replies in time. This module automatically adapts the send rate to |
|
|
41 | avoid false timeouts caused by slow reply processing. |
|
|
42 | |
39 | A potential disadvantage is that replacing the dispatcher is not at all |
43 | A potential disadvantage of this module is that replacing the dispatcher |
40 | a documented thing to do, so future changes in Net::SNP might break this |
44 | is not at all a documented thing to do, so future changes in Net::SNP |
41 | module (or the many similar ones). |
45 | might break this module (or the many similar ones). |
42 | |
46 | |
43 | This module does not export anything and does not require you to do |
47 | This module does not export anything and does not require you to do |
44 | anything special apart from loading it I<before doing any non-blocking |
48 | anything special apart from loading it I<before doing any non-blocking |
45 | requests with Net::SNMP>. It is recommended but not required to load this |
49 | requests with Net::SNMP>. It is recommended but not required to load this |
46 | module before C<Net::SNMP>. |
50 | module before C<Net::SNMP>. |
… | |
… | |
49 | |
53 | |
50 | =over 4 |
54 | =over 4 |
51 | |
55 | |
52 | =item $AnyEvent::SNMP::MAX_OUTSTANDING (default: C<50>, dynamic) |
56 | =item $AnyEvent::SNMP::MAX_OUTSTANDING (default: C<50>, dynamic) |
53 | |
57 | |
|
|
58 | =item AnyEvent::SNMP::set_max_outstanding $new_value |
|
|
59 | |
54 | Use this package variable to restrict the number of outstanding SNMP |
60 | Use this package variable to restrict the number of outstanding SNMP |
55 | requests at any point in time. |
61 | requests at any point in time. |
56 | |
62 | |
57 | Net::SNMP is very fast at creating and sending SNMP requests, but much |
63 | Net::SNMP is very fast at creating and sending SNMP requests, but much |
58 | slower at parsing (big, bulk) responses. This makes it easy to request a |
64 | slower at parsing (big, bulk) responses. This makes it easy to request a |
… | |
… | |
61 | In the best case, this can lead to unnecessary delays (and even time-outs, |
67 | In the best case, this can lead to unnecessary delays (and even time-outs, |
62 | as the data has been received but not yet processed) and in the worst |
68 | as the data has been received but not yet processed) and in the worst |
63 | case, this can lead to packet loss, when the receive queue overflows and |
69 | case, this can lead to packet loss, when the receive queue overflows and |
64 | the kernel can no longer accept new packets. |
70 | the kernel can no longer accept new packets. |
65 | |
71 | |
66 | To avoid this, you can (and should) limit the number of outstanding requests |
72 | To avoid this, you can (and should) limit the number of outstanding |
67 | to a number low enough so that parsing time doesn't introduce noticable delays. |
73 | requests to a number low enough so that parsing time doesn't introduce |
|
|
74 | noticable delays. |
68 | |
75 | |
69 | Unfortunately, this number depends not only on processing speed and load |
76 | Unfortunately, this number depends not only on processing speed and load |
70 | of the machine running Net::SNMP, but also on the network latency and the |
77 | of the machine running Net::SNMP, but also on the network latency and the |
71 | speed of your SNMP agents. |
78 | speed of your SNMP agents. |
72 | |
79 | |
73 | AnyEvent::SNMP tries to dynamically adjust this number dynamically upwards |
80 | AnyEvent::SNMP tries to dynamically adjust this number dynamically upwards |
74 | and downwards. |
81 | and downwards. |
75 | |
82 | |
|
|
83 | Increasing C<$MAX_OUTSTANDING> will not automatically use the |
|
|
84 | extra request slots. To increase C<$MAX_OUTSTANDING> and make |
|
|
85 | C<AnyEvent::SNMP> make use of the extra paralellity, call |
|
|
86 | C<AnyEvent::SNMP::set_max_outstanding> with the new value, e.g.: |
|
|
87 | |
|
|
88 | AnyEvent::SNMP::set_max_outstanding 500; |
|
|
89 | |
|
|
90 | Although due to the dynamic adjustment, this might have little lasting |
|
|
91 | effect. |
|
|
92 | |
76 | Note that you can use L<Net::SNMP::XS> to speed up parsing of responses |
93 | Note that you can use L<Net::SNMP::XS> to speed up parsing of responses |
77 | considerably. |
94 | considerably. |
78 | |
95 | |
79 | =item $AnyEvent::SNMP::MIN_RECVQUEUE (default: C<4>) |
96 | =item $AnyEvent::SNMP::MIN_RECVQUEUE (default: C<8>) |
80 | |
97 | |
81 | =item $AnyEvent::SNMP::MAX_RECVQUEUE (default: C<64>) |
98 | =item $AnyEvent::SNMP::MAX_RECVQUEUE (default: C<64>) |
82 | |
99 | |
83 | These values specify the minimum and maximum receive queue length (in |
100 | These values specify the minimum and maximum receive queue length (in |
84 | units of one response packet). |
101 | units of one response packet). |
… | |
… | |
132 | } |
149 | } |
133 | |
150 | |
134 | use Net::SNMP (); |
151 | use Net::SNMP (); |
135 | use AnyEvent (); |
152 | use AnyEvent (); |
136 | |
153 | |
137 | our $VERSION = '0.2'; |
154 | our $VERSION = '1.0'; |
138 | |
155 | |
139 | $Net::SNMP::DISPATCHER = instance Net::SNMP::Dispatcher; |
156 | $Net::SNMP::DISPATCHER = instance Net::SNMP::Dispatcher; |
140 | |
157 | |
141 | our $MESSAGE_PROCESSING = $Net::SNMP::Dispatcher::MESSAGE_PROCESSING; |
158 | our $MESSAGE_PROCESSING = $Net::SNMP::Dispatcher::MESSAGE_PROCESSING; |
142 | |
159 | |
143 | # avoid the method call |
|
|
144 | my $timer = sub { shift->timer (@_) }; |
|
|
145 | AnyEvent::post_detect { $timer = AnyEvent->can ("timer") }; |
|
|
146 | |
|
|
147 | our $BUSY; |
160 | our $BUSY; |
|
|
161 | our $DONE; # finished all jobs |
148 | our %TRANSPORT; # address => [count, watcher] |
162 | our @TRANSPORT; # fileno => [count, watcher] |
149 | our @QUEUE; |
163 | our @QUEUE; |
150 | our $MAX_OUTSTANDING = 50; |
164 | our $MAX_OUTSTANDING = 50; |
151 | our $MIN_RECVQUEUE = 4; |
165 | our $MIN_RECVQUEUE = 8; |
152 | our $MAX_RECVQUEUE = 64; |
166 | our $MAX_RECVQUEUE = 64; |
153 | |
167 | |
154 | sub kick_job; |
168 | sub kick_job; |
155 | |
169 | |
156 | sub _send_pdu { |
170 | sub _send_pdu { |
… | |
… | |
175 | $MESSAGE_PROCESSING->msg_handle_delete ($pdu->msg_id) |
189 | $MESSAGE_PROCESSING->msg_handle_delete ($pdu->msg_id) |
176 | if $pdu->expect_response; |
190 | if $pdu->expect_response; |
177 | |
191 | |
178 | # A crude attempt to recover from temporary failures. |
192 | # A crude attempt to recover from temporary failures. |
179 | if ($retries-- > 0 && ($!{EAGAIN} || $!{EWOULDBLOCK} || $!{ENOSPC})) { |
193 | if ($retries-- > 0 && ($!{EAGAIN} || $!{EWOULDBLOCK} || $!{ENOSPC})) { |
180 | my $retry_w; $retry_w = AnyEvent->$timer (after => $pdu->timeout, cb => sub { |
194 | my $retry_w; $retry_w = AE::timer $pdu->timeout, 0, sub { |
181 | undef $retry_w; |
195 | undef $retry_w; |
182 | _send_pdu ($pdu, $retries); |
196 | _send_pdu ($pdu, $retries); |
183 | }); |
197 | }; |
184 | } else { |
198 | } else { |
185 | --$BUSY; |
199 | --$BUSY; |
186 | kick_job; |
200 | kick_job; |
187 | } |
201 | } |
188 | |
202 | |
… | |
… | |
192 | } |
206 | } |
193 | |
207 | |
194 | # Schedule the timeout handler if the message expects a response. |
208 | # Schedule the timeout handler if the message expects a response. |
195 | if ($pdu->expect_response) { |
209 | if ($pdu->expect_response) { |
196 | my $transport = $msg->transport; |
210 | my $transport = $msg->transport; |
|
|
211 | my $fileno = $transport->fileno; |
197 | |
212 | |
198 | # register the transport |
213 | # register the transport |
199 | unless ($TRANSPORT{$transport+0}[0]++) { |
214 | unless ($TRANSPORT[$fileno][0]++) { |
200 | $TRANSPORT{$transport+0}[1] = AnyEvent->io (fh => $transport->socket, poll => 'r', cb => sub { |
215 | $TRANSPORT[$fileno][1] = AE::io $transport->socket, 0, sub { |
201 | for my $count (1..$MAX_RECVQUEUE) { # handle up to this many requests in one go |
216 | for my $count (1..$MAX_RECVQUEUE) { # handle up to this many requests in one go |
202 | # Create a new Message object to receive the response |
217 | # Create a new Message object to receive the response |
203 | my ($msg, $error) = Net::SNMP::Message->new (-transport => $transport); |
218 | my ($msg, $error) = Net::SNMP::Message->new (-transport => $transport); |
204 | |
219 | |
205 | if (!defined $msg) { |
220 | if (!defined $msg) { |
… | |
… | |
207 | } |
222 | } |
208 | |
223 | |
209 | # Read the message from the Transport Layer |
224 | # Read the message from the Transport Layer |
210 | if (!defined $msg->recv) { |
225 | if (!defined $msg->recv) { |
211 | if ($transport->connectionless) { |
226 | if ($transport->connectionless) { |
|
|
227 | # if we handled very few replies and we have queued work, try |
|
|
228 | # to increase the parallelity as we probably can handle more. |
212 | if ($count < $MIN_RECVQUEUE && @QUEUE) { |
229 | if ($count < $MIN_RECVQUEUE && @QUEUE) { |
213 | ++$MAX_OUTSTANDING; |
230 | ++$MAX_OUTSTANDING; |
214 | kick_job; |
231 | kick_job; |
215 | } |
232 | } |
216 | } else { |
233 | } else { |
217 | # for some reason, connected-oriented transports seem to need this |
234 | # for some reason, connected-oriented transports seem to need this |
218 | delete $TRANSPORT{$transport+0} |
235 | delete $TRANSPORT[$fileno] |
219 | unless --$TRANSPORT{$transport+0}[0]; |
236 | unless --$TRANSPORT[$fileno][0]; |
220 | } |
237 | } |
221 | |
238 | |
222 | $msg->error; |
239 | $msg->error; |
223 | return; |
240 | return; |
224 | } |
241 | } |
… | |
… | |
247 | undef $$rtimeout_w; |
264 | undef $$rtimeout_w; |
248 | |
265 | |
249 | --$BUSY; |
266 | --$BUSY; |
250 | kick_job; |
267 | kick_job; |
251 | |
268 | |
252 | unless (--$TRANSPORT{$transport+0}[0]) { |
269 | unless (--$TRANSPORT[$fileno][0]) { |
253 | delete $TRANSPORT{$transport+0}; |
270 | delete $TRANSPORT[$fileno]; |
254 | return; |
271 | return; |
255 | } |
272 | } |
256 | } |
273 | } |
257 | } |
274 | } |
258 | |
275 | |
|
|
276 | # when we end up here, we successfully handled $MAX_RECVQUEUE |
|
|
277 | # replies in one iteration, so assume we are overloaded |
|
|
278 | # and reduce the amount of parallelity. |
259 | $MAX_OUTSTANDING = (int $MAX_OUTSTANDING * 0.9) || 1; |
279 | $MAX_OUTSTANDING = (int $MAX_OUTSTANDING * 0.95) || 1; |
260 | }); |
280 | }; |
261 | } |
281 | } |
262 | |
282 | |
263 | $msg->timeout_id (\(my $rtimeout_w = |
283 | $msg->timeout_id (\(my $rtimeout_w = |
264 | AnyEvent->$timer (after => $pdu->timeout, cb => sub { |
284 | AE::timer $pdu->timeout, 0, sub { |
265 | my $rtimeout_w = $msg->timeout_id; |
285 | my $rtimeout_w = $msg->timeout_id; |
266 | if ($$rtimeout_w) { |
286 | if ($$rtimeout_w) { |
267 | undef $$rtimeout_w; |
287 | undef $$rtimeout_w; |
268 | delete $TRANSPORT{$transport+0} |
288 | delete $TRANSPORT[$fileno] |
269 | unless --$TRANSPORT{$transport+0}[0]; |
289 | unless --$TRANSPORT[$fileno][0]; |
270 | } |
290 | } |
271 | |
291 | |
272 | if ($retries--) { |
292 | if ($retries--) { |
273 | _send_pdu ($pdu, $retries); |
293 | _send_pdu ($pdu, $retries); |
274 | } else { |
294 | } else { |
… | |
… | |
277 | |
297 | |
278 | --$BUSY; |
298 | --$BUSY; |
279 | kick_job; |
299 | kick_job; |
280 | } |
300 | } |
281 | }) |
301 | }) |
282 | )); |
302 | ); |
283 | } else { |
303 | } else { |
284 | --$BUSY; |
304 | --$BUSY; |
285 | kick_job; |
305 | kick_job; |
286 | } |
306 | } |
287 | } |
307 | } |
… | |
… | |
290 | while ($BUSY < $MAX_OUTSTANDING) { |
310 | while ($BUSY < $MAX_OUTSTANDING) { |
291 | my $pdu = shift @QUEUE |
311 | my $pdu = shift @QUEUE |
292 | or last; |
312 | or last; |
293 | |
313 | |
294 | ++$BUSY; |
314 | ++$BUSY; |
295 | |
|
|
296 | _send_pdu $pdu, $pdu->retries; |
315 | _send_pdu $pdu, $pdu->retries; |
297 | } |
316 | } |
|
|
317 | |
|
|
318 | $DONE and $DONE->() unless $BUSY; |
298 | } |
319 | } |
|
|
320 | |
299 | sub send_pdu($$$) { |
321 | sub send_pdu($$$) { |
300 | my (undef, $pdu, $delay) = @_; |
322 | my (undef, $pdu, $delay) = @_; |
301 | |
323 | |
302 | # $delay is not very sensibly implemented by AnyEvent::SNMP, |
324 | # $delay is not very sensibly implemented by AnyEvent::SNMP, |
303 | # but apparently it is not a very sensible feature. |
325 | # but apparently it is not a very sensible feature. |
304 | if ($delay > 0) { |
326 | if ($delay > 0) { |
305 | ++$BUSY; |
327 | ++$BUSY; |
306 | my $delay_w; $delay_w = AnyEvent->$timer (after => $delay, cb => sub { |
328 | my $delay_w; $delay_w = AE::timer $delay, 0, sub { |
307 | undef $delay_w; |
329 | undef $delay_w; |
|
|
330 | push @QUEUE, $pdu; |
308 | --$BUSY; |
331 | --$BUSY; |
309 | push @QUEUE, $pdu; |
|
|
310 | kick_job; |
332 | kick_job; |
311 | }); |
333 | }; |
312 | return 1; |
334 | return 1; |
313 | } |
335 | } |
314 | |
336 | |
315 | push @QUEUE, $pdu; |
337 | push @QUEUE, $pdu; |
316 | kick_job; |
338 | kick_job; |
317 | |
339 | |
318 | 1 |
340 | 1 |
319 | } |
341 | } |
320 | |
342 | |
321 | sub activate($) { |
343 | sub activate($) { |
322 | AnyEvent->one_event while $BUSY; |
344 | while ($BUSY) { |
|
|
345 | $DONE = AE::cv; |
|
|
346 | $DONE->recv; |
|
|
347 | undef $DONE; |
|
|
348 | } |
323 | } |
349 | } |
324 | |
350 | |
325 | sub one_event($) { |
351 | sub one_event($) { |
|
|
352 | # should not ever be used |
326 | AnyEvent->one_event; |
353 | AnyEvent->one_event; #d# todo |
|
|
354 | } |
|
|
355 | |
|
|
356 | sub set_max_outstanding($) { |
|
|
357 | $MAX_OUTSTANDING = $_[0]; |
|
|
358 | kick_job; |
327 | } |
359 | } |
328 | |
360 | |
329 | =head1 SEE ALSO |
361 | =head1 SEE ALSO |
330 | |
362 | |
331 | L<AnyEvent>, L<Net::SNMP>, L<Net::SNMP::XS>, L<Net::SNMP::EV>. |
363 | L<AnyEvent>, L<Net::SNMP>, L<Net::SNMP::XS>, L<Net::SNMP::EV>. |