Merge lp:~radix/landscape-client/fix-landscape-client-startup into lp:~landscape/landscape-client/trunk

Proposed by Christopher Armstrong
Status: Merged
Approved by: Christopher Armstrong
Approved revision: 677
Merged at revision: 678
Proposed branch: lp:~radix/landscape-client/fix-landscape-client-startup
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 97 lines (+16/-35)
3 files modified
landscape/amp.py (+0/-26)
landscape/broker/amp.py (+13/-6)
landscape/broker/server.py (+3/-3)
To merge this branch: bzr merge lp:~radix/landscape-client/fix-landscape-client-startup
Reviewer Review Type Date Requested Status
Alberto Donato (community) Approve
Free Ekanayaka (community) Approve
Review via email: mp+164045@code.launchpad.net

Commit message

Get rid of implicit population of the components registry; it's now explicitly created in a function, and used directly when it's needed.

Description of the change

Unfortunately no tests with this one, but it's an almost-pure code refactor.

Basically, the module that happened to populate the global registry of components wasn't being imported any more. I decided to get rid of the global registry entirely as a solution :) now there's just a function that returns the registry (as a dict) when called. now there's no more dependency on import ordering.

To post a comment you must log in.
Revision history for this message
Free Ekanayaka (free.ekanayaka) wrote :

Looks good! +1

[1]

+
+def get_component_registry():
+ return {
+ "broker": RemoteBrokerConnector,
+ "client": RemoteClientConnector,
+ "monitor": RemoteMonitorConnector,
+ "manager": RemoteManagerConnector
+ }

You could use the "name" class attribute of the "component" class attribute, to avoid duplication, e.g.:

return {
    RemoteBrokerConnector.component.name: RemoteBrokerConnector,
    // etc.
}

Later on I believe we could get rid of the ComponentConnector sub-classes too relatively easy (as you did for the ComponentPublisher-based ones), but it'd probably require dropping the RemoteBroker class as pre-requisite. Its two methods could be moved to the Monitor/Manager classes where they are used (just thinking loud).

[2]

+ from landscape.broker.amp import get_component_registry
+ self.connectors_registry = get_component_registry()

I believe one reason for which we had the ComponentRegistry was to avoid circular imports like this. So it'd be great to have a way of avoiding dependency on import ordering while still not introducing circular imports (maybe dropping the ComponentConnector sub-classes will do it, not sure).

review: Approve
Revision history for this message
Alberto Donato (ack) wrote :

+1, looks good!

#1:
+def get_component_registry():
+ return {
+ "broker": RemoteBrokerConnector,
+ "client": RemoteClientConnector,
+ "monitor": RemoteMonitorConnector,
+ "manager": RemoteManagerConnector
+ }

You could write this as

+COMPONENTS = [
+ RemoteBrokerConnector, RemoteClientConnector, RemoteMonitorConnector, RemoteManagerConnector]
+
+def get_component_registry():
+ return dict(
+ (connector_class.component.name, connector_class)
+ for connector_class in COMPONENTS)

#2:
landscape/broker/server.py:305:1: W391 blank line at end of file

review: Approve
677. By Christopher Armstrong

Address comments:
 - don't duplicate name information in get_component_registry
 - lint

Revision history for this message
Christopher Armstrong (radix) wrote :

free [1] fixed

free [2]

I actually almost removed ComponentConnector subclasses, after my branch to remove the ComponentPublisher subclasses. It was a bit trickier than I wanted to get into at the time, mostly because it involved updating watchdog and configuration code, as well as this registry code. Maybe I'll take another look at it before I leave.

ack [1] fixed
ack [2] fixed

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/amp.py'
2--- landscape/amp.py 2013-05-14 22:57:15 +0000
3+++ landscape/amp.py 2013-05-16 16:41:25 +0000
4@@ -151,31 +151,5 @@
5 self._connector = None
6
7
8-class ComponentsRegistry(object):
9- """
10- A global registry for looking up Landscape component connectors by name.
11- """
12-
13- _by_name = {}
14-
15- @classmethod
16- def get(cls, name):
17- """Get the connector class for the given Landscape component.
18-
19- @param name: Name of the Landscape component we want to connect to, for
20- instance C{monitor} or C{manager}.
21- """
22- return cls._by_name[name]
23-
24- @classmethod
25- def register(cls, connector_class):
26- """Register a connector for a Landscape component.
27-
28- @param connector_class: A sub-class of L{ComponentConnector}
29- that can be used to connect to a certain component.
30- """
31- cls._by_name[connector_class.component.name] = connector_class
32-
33-
34 def _get_socket_path(component, config):
35 return os.path.join(config.sockets_path, component.name + ".sock")
36
37=== modified file 'landscape/broker/amp.py'
38--- landscape/broker/amp.py 2013-05-07 22:47:06 +0000
39+++ landscape/broker/amp.py 2013-05-16 16:41:25 +0000
40@@ -1,8 +1,7 @@
41 from twisted.internet.defer import maybeDeferred, execute, succeed
42
43 from landscape.lib.amp import RemoteObject, MethodCallArgument
44-from landscape.amp import (ComponentConnector, ComponentsRegistry,
45- get_remote_methods)
46+from landscape.amp import ComponentConnector, get_remote_methods
47 from landscape.broker.server import BrokerServer
48 from landscape.broker.client import BrokerClient
49 from landscape.monitor.monitor import Monitor
50@@ -90,7 +89,15 @@
51
52 component = Manager
53
54-ComponentsRegistry.register(RemoteBrokerConnector)
55-ComponentsRegistry.register(RemoteClientConnector)
56-ComponentsRegistry.register(RemoteMonitorConnector)
57-ComponentsRegistry.register(RemoteManagerConnector)
58+
59+def get_component_registry():
60+ """Get a mapping of component name to connectors, for all components."""
61+ all_connectors = [
62+ RemoteBrokerConnector,
63+ RemoteClientConnector,
64+ RemoteMonitorConnector,
65+ RemoteManagerConnector
66+ ]
67+ return dict(
68+ (connector.component.name, connector)
69+ for connector in all_connectors)
70
71=== modified file 'landscape/broker/server.py'
72--- landscape/broker/server.py 2013-05-14 22:57:15 +0000
73+++ landscape/broker/server.py 2013-05-16 16:41:25 +0000
74@@ -3,9 +3,8 @@
75 from twisted.internet.defer import Deferred
76
77 from landscape.lib.twisted_util import gather_results
78-from landscape.amp import ComponentsRegistry
79+from landscape.amp import remote
80 from landscape.manager.manager import FAILED
81-from landscape.amp import remote
82
83
84 def event(method):
85@@ -38,10 +37,11 @@
86 @param message_store: The broker's L{MessageStore}.
87 """
88 name = "broker"
89- connectors_registry = ComponentsRegistry
90
91 def __init__(self, config, reactor, exchange, registration,
92 message_store, pinger):
93+ from landscape.broker.amp import get_component_registry
94+ self.connectors_registry = get_component_registry()
95 self._config = config
96 self._reactor = reactor
97 self._exchanger = exchange

Subscribers

People subscribed via source and target branches

to all changes: