Merge lp:~therve/landscape-client/limit-network-data into lp:~landscape/landscape-client/trunk

Proposed by Thomas Herve
Status: Merged
Approved by: Free Ekanayaka
Approved revision: 325
Merged at revision: 323
Proposed branch: lp:~therve/landscape-client/limit-network-data
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 100 lines (+49/-10)
2 files modified
landscape/monitor/networkactivity.py (+13/-2)
landscape/monitor/tests/test_networkactivity.py (+36/-8)
To merge this branch: bzr merge lp:~therve/landscape-client/limit-network-data
Reviewer Review Type Date Requested Status
Free Ekanayaka (community) Approve
Kevin McDermott (community) Approve
Review via email: mp+57642@code.launchpad.net

Description of the change

The way to count the number of items is a bit tricky. It will also won't push the older first, but I think it's fine.

To post a comment you must log in.
Revision history for this message
Kevin McDermott (bigkevmcd) wrote :

+ max_free_space_items_to_exchange = 200

Am not sure this is the right name for a limit on network data :-)

Other than that, +1

review: Approve
Revision history for this message
Kevin McDermott (bigkevmcd) wrote :

It might be nice to make this generic... :-)

325. By Thomas Herve

Rename class variable

Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Cool, +1!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/monitor/networkactivity.py'
2--- landscape/monitor/networkactivity.py 2010-09-20 20:53:24 +0000
3+++ landscape/monitor/networkactivity.py 2011-04-14 10:32:33 +0000
4@@ -21,6 +21,8 @@
5 run_interval = 30
6 _rollover_maxint = 0
7
8+ max_network_items_to_exchange = 200
9+
10 def __init__(self, network_activity_file="/proc/net/dev",
11 create_time=time.time):
12 self._source_file = network_activity_file
13@@ -39,10 +41,19 @@
14 self.call_on_accepted("network-activity", self.exchange, True)
15
16 def create_message(self):
17- network_activity = self._network_activity
18+ network_activity = {}
19+ items = 0
20+ for interface, data in list(self._network_activity.items()):
21+ if data:
22+ network_activity[interface] = []
23+ while data and items < self.max_network_items_to_exchange:
24+ item = data.pop(0)
25+ network_activity[interface].append(item)
26+ items += 1
27+ if items >= self.max_network_items_to_exchange:
28+ break
29 if not network_activity:
30 return
31- self._network_activity = {}
32 return {"type": "network-activity", "activities": network_activity}
33
34 def send_message(self, urgent):
35
36=== modified file 'landscape/monitor/tests/test_networkactivity.py'
37--- landscape/monitor/tests/test_networkactivity.py 2010-09-17 13:41:22 +0000
38+++ landscape/monitor/tests/test_networkactivity.py 2011-04-14 10:32:33 +0000
39@@ -10,8 +10,8 @@
40 stats_template = """\
41 Inter-| Receive | Transmit
42 face |bytes packets compressed multicast|bytes packets errs drop fifo
43- lo:%(lo_in)d %(lo_in_p)d 0 0 %(lo_out)d %(lo_out_p)d 0 0 0
44- eth0: %(eth0_in)d 12539 0 62 %(eth0_out)d 12579 0 0 0
45+ lo:%(lo_in)d %(lo_in_p)d 0 0 %(lo_out)d %(lo_out_p)d 0 0 0
46+ eth0: %(eth0_in)d 12539 0 62 %(eth0_out)d 12579 0 0 0
47 %(extra)s
48 """
49
50@@ -31,12 +31,12 @@
51 def write_activity(self, lo_in=0, lo_out=0, eth0_in=0, eth0_out=0,
52 extra="", lo_in_p=0, lo_out_p=0, **kw):
53 kw.update(dict(
54- lo_in = lo_in,
55- lo_out = lo_out,
56- lo_in_p = lo_in_p,
57- lo_out_p = lo_out_p,
58- eth0_in = eth0_in,
59- eth0_out = eth0_out,
60+ lo_in=lo_in,
61+ lo_out=lo_out,
62+ lo_in_p=lo_in_p,
63+ lo_out_p=lo_out_p,
64+ eth0_in=eth0_in,
65+ eth0_out=eth0_out,
66 extra=extra))
67 self.activity_file.seek(0, 0)
68 self.activity_file.truncate()
69@@ -188,3 +188,31 @@
70 def test_config(self):
71 """The network activity plugin is enabled by default."""
72 self.assertIn("NetworkActivity", self.config.plugin_factories)
73+
74+ def test_limit_amount_of_items(self):
75+ """
76+ The network plugin doesn't send too many items at once in a single
77+ network message, to not crush the server.
78+ """
79+ def extra(data):
80+ result = ""
81+ for i in range(50):
82+ result += (
83+"""eth%d: %d 12539 0 62 %d 12579 0 0 0\n """
84+ % (i, data, data))
85+ return result
86+ for i in range(1, 10):
87+ data = i * 1000
88+ self.write_activity(lo_out=data, eth0_out=data, extra=extra(data))
89+ self.plugin.run()
90+ self.reactor.advance(self.monitor.step_size)
91+ # We have created 408 items. It should be sent in 3 messages.
92+ message = self.plugin.create_message()
93+ items = sum(len(i) for i in message["activities"].values())
94+ self.assertEqual(200, items)
95+ message = self.plugin.create_message()
96+ items = sum(len(i) for i in message["activities"].values())
97+ self.assertEqual(200, items)
98+ message = self.plugin.create_message()
99+ items = sum(len(i) for i in message["activities"].values())
100+ self.assertEqual(8, items)

Subscribers

People subscribed via source and target branches

to all changes: