Merge lp:~mbruzek/charm-helpers/peers into lp:charm-helpers

Proposed by Matt Bruzek
Status: Merged
Merged at revision: 505
Proposed branch: lp:~mbruzek/charm-helpers/peers
Merge into: lp:charm-helpers
Diff against target: 231 lines (+28/-28)
3 files modified
charmhelpers/coordinator.py (+18/-18)
charmhelpers/core/hookenv.py (+5/-5)
tests/core/test_hookenv.py (+5/-5)
To merge this branch: bzr merge lp:~mbruzek/charm-helpers/peers
Reviewer Review Type Date Requested Status
Marco Ceppi Approve
Cory Johns Pending
Review via email: mp+280211@code.launchpad.net

Description of the change

While writing a layer charm that uses reactive and the peers relation we encountered a problem. The peers relation handlers were not called.

With Cory's help we tracked this down to hookenv.py where the code is looking for "peer" and not "peers".

This merge corrects the hookenv.py and the coordination.py file's use of "peers".

The test_hookenv.py originally failed after I made the change so I fixed the test to have the correct "peers" relation in the examples.

To post a comment you must log in.
Revision history for this message
Marco Ceppi (marcoceppi) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/coordinator.py'
2--- charmhelpers/coordinator.py 2015-06-19 07:26:06 +0000
3+++ charmhelpers/coordinator.py 2015-12-10 22:46:25 +0000
4@@ -29,10 +29,10 @@
5 Services Framework Usage
6 ========================
7
8-Ensure a peer relation is defined in metadata.yaml. Instantiate a
9+Ensure a peers relation is defined in metadata.yaml. Instantiate a
10 BaseCoordinator subclass before invoking ServiceManager.manage().
11 Ensure that ServiceManager.manage() is wired up to the leader-elected,
12-leader-settings-changed, peer relation-changed and peer
13+leader-settings-changed, peers relation-changed and peers
14 relation-departed hooks in addition to any other hooks you need, or your
15 service will deadlock.
16
17@@ -90,7 +90,7 @@
18 Traditional Usage
19 =================
20
21-Ensure a peer relationis defined in metadata.yaml.
22+Ensure a peers relation is defined in metadata.yaml.
23
24 If you are using charmhelpers.core.hookenv.Hooks, ensure that a
25 BaseCoordinator subclass is instantiated before calling Hooks.execute.
26@@ -151,7 +151,7 @@
27 hookenv.service_restart('myservice')
28
29 @hooks.hook('install', 'config-changed', 'upgrade-charm',
30- # Peer and leader hooks must be wired up.
31+ # Peers and leader hooks must be wired up.
32 'cluster-relation-changed', 'cluster-relation-departed',
33 'leader-elected', 'leader-settings-changed')
34 def default_hook():
35@@ -174,7 +174,7 @@
36 Locks are released at the end of the hook they are acquired in. This may
37 be the current hook if the unit is leader and the lock is free. It is
38 more likely a future hook (probably leader-settings-changed, possibly
39-the peer relation-changed or departed hook, potentially any hook).
40+the peers relation-changed or departed hook, potentially any hook).
41
42 Whenever a charm needs to perform a coordinated action it will acquire()
43 the lock and perform the action immediately if acquisition is
44@@ -189,16 +189,16 @@
45 If the unit is the leader, then it may be able to grant its own lock
46 and perform the action immediately in the source hook. If the unit is
47 the leader and cannot immediately grant the lock, then its only
48-guaranteed chance of acquiring the lock is in the peer relation-joined,
49-relation-changed or peer relation-departed hooks when another unit has
50-released it (the only channel to communicate to the leader is the peer
51+guaranteed chance of acquiring the lock is in the peers relation-joined,
52+relation-changed or peers relation-departed hooks when another unit has
53+released it (the only channel to communicate to the leader is the peers
54 relation). If the unit is not the leader, then it is unlikely the lock
55 is granted in the source hook (a previous hook must have also made the
56 request for this to happen). A non-leader is notified about the lock via
57 leader settings. These changes may be visible in any hook, even before
58 the leader-settings-changed hook has been invoked. Or the requesting
59 unit may be promoted to leader after making a request, in which case the
60-lock may be granted in leader-elected or in a future peer
61+lock may be granted in leader-elected or in a future peers
62 relation-changed or relation-departed hook.
63
64 This could be simpler if leader-settings-changed was invoked on the
65@@ -255,10 +255,10 @@
66 def __init__(self, relation_key='coordinator', peer_relation_name=None):
67 '''Instatiate a Coordinator.
68
69- Data is stored on the peer relation and in leadership storage
70+ Data is stored on the peers relation and in leadership storage
71 under the provided relation_key.
72
73- The peer relation is identified by peer_relation_name, and defaults
74+ The peers relation is identified by peer_relation_name, and defaults
75 to the first one found in metadata.yaml.
76 '''
77 # Most initialization is deferred, since invoking hook tools from
78@@ -310,13 +310,13 @@
79
80 Do not mindlessly call this method, as it triggers a cascade of
81 hooks. For example, if you call acquire() every time in your
82- peer relation-changed hook you will end up with an infinite loop
83+ peers relation-changed hook you will end up with an infinite loop
84 of hooks. It should almost always be guarded by some condition.
85 '''
86 unit = hookenv.local_unit()
87 ts = self.requests[unit].get(lock)
88 if not ts:
89- # If there is no outstanding request on the peer relation,
90+ # If there is no outstanding request on the peers relation,
91 # create one.
92 self.requests.setdefault(lock, {})
93 self.requests[unit][lock] = _timestamp()
94@@ -329,7 +329,7 @@
95
96 # If the unit making the request also happens to be the
97 # leader, it must handle the request now. Even though the
98- # request has been stored on the peer relation, the peer
99+ # request has been stored on the peers relation, the peers
100 # relation-changed hook will not be triggered.
101 if hookenv.is_leader():
102 return self.grant(lock, unit)
103@@ -476,13 +476,13 @@
104
105 local_unit = hookenv.local_unit()
106
107- # All requests must be stored on the peer relation. This is
108+ # All requests must be stored on the peers relation. This is
109 # the only channel units have to communicate with the leader.
110 # Even the leader needs to store its requests here, as a
111 # different unit may be leader by the time the request can be
112 # granted.
113 if self.relid is None:
114- # The peer relation is not available. Maybe we are early in
115+ # The peers relation is not available. Maybe we are early in
116 # the units's lifecycle. Maybe this unit is standalone.
117 # Fallback to using local state.
118 self.msg('No peer relation. Loading local state')
119@@ -490,7 +490,7 @@
120 else:
121 self.requests = self._load_peer_state()
122 if local_unit not in self.requests:
123- # The peer relation has just been joined. Update any state
124+ # The peers relation has just been joined. Update any state
125 # loaded from our peers with our local state.
126 self.msg('New peer relation. Merging local state')
127 self.requests[local_unit] = self._load_local_state()
128@@ -513,7 +513,7 @@
129 local_unit = hookenv.local_unit()
130
131 if self.relid is None:
132- # No peer relation yet. Fallback to local state.
133+ # No peers relation yet. Fallback to local state.
134 self.msg('No peer relation. Saving local state')
135 self._save_local_state(self.requests[local_unit])
136 else:
137
138=== modified file 'charmhelpers/core/hookenv.py'
139--- charmhelpers/core/hookenv.py 2015-12-08 17:47:29 +0000
140+++ charmhelpers/core/hookenv.py 2015-12-10 22:46:25 +0000
141@@ -492,7 +492,7 @@
142
143 @cached
144 def peer_relation_id():
145- '''Get a peer relation id if a peer relation has been joined, else None.'''
146+ '''Get the peers relation id if a peers relation has been joined, else None.'''
147 md = metadata()
148 section = md.get('peers')
149 if section:
150@@ -517,12 +517,12 @@
151 def relation_to_role_and_interface(relation_name):
152 """
153 Given the name of a relation, return the role and the name of the interface
154- that relation uses (where role is one of ``provides``, ``requires``, or ``peer``).
155+ that relation uses (where role is one of ``provides``, ``requires``, or ``peers``).
156
157 :returns: A tuple containing ``(role, interface)``, or ``(None, None)``.
158 """
159 _metadata = metadata()
160- for role in ('provides', 'requires', 'peer'):
161+ for role in ('provides', 'requires', 'peers'):
162 interface = _metadata.get(role, {}).get(relation_name, {}).get('interface')
163 if interface:
164 return role, interface
165@@ -534,7 +534,7 @@
166 """
167 Given a role and interface name, return a list of relation names for the
168 current charm that use that interface under that role (where role is one
169- of ``provides``, ``requires``, or ``peer``).
170+ of ``provides``, ``requires``, or ``peers``).
171
172 :returns: A list of relation names.
173 """
174@@ -555,7 +555,7 @@
175 :returns: A list of relation names.
176 """
177 results = []
178- for role in ('provides', 'requires', 'peer'):
179+ for role in ('provides', 'requires', 'peers'):
180 results.extend(role_and_interface_to_relations(role, interface_name))
181 return results
182
183
184=== modified file 'tests/core/test_hookenv.py'
185--- tests/core/test_hookenv.py 2015-12-08 17:47:29 +0000
186+++ tests/core/test_hookenv.py 2015-12-10 22:46:25 +0000
187@@ -1448,7 +1448,7 @@
188 'interface': 'req-int',
189 },
190 },
191- 'peer': {
192+ 'peers': {
193 'pee-rel': {
194 'interface': 'pee-int',
195 },
196@@ -1457,7 +1457,7 @@
197 rtri = hookenv.relation_to_role_and_interface
198 self.assertEqual(rtri('pro-rel'), ('provides', 'pro-int'))
199 self.assertEqual(rtri('req-rel'), ('requires', 'req-int'))
200- self.assertEqual(rtri('pee-rel'), ('peer', 'pee-int'))
201+ self.assertEqual(rtri('pee-rel'), ('peers', 'pee-int'))
202
203 @patch.object(hookenv, 'metadata')
204 def test_role_and_interface_to_relations(self, metadata):
205@@ -1475,7 +1475,7 @@
206 'interface': 'int',
207 },
208 },
209- 'peer': {
210+ 'peers': {
211 'pee-rel': {
212 'interface': 'int',
213 },
214@@ -1485,7 +1485,7 @@
215 assertItemsEqual = getattr(self, 'assertItemsEqual', getattr(self, 'assertCountEqual', None))
216 assertItemsEqual(ritr('provides', 'pro-int'), ['pro-rel', 'pro-rel2'])
217 assertItemsEqual(ritr('requires', 'int'), ['req-rel'])
218- assertItemsEqual(ritr('peer', 'int'), ['pee-rel'])
219+ assertItemsEqual(ritr('peers', 'int'), ['pee-rel'])
220
221 @patch.object(hookenv, 'metadata')
222 def test_interface_to_relations(self, metadata):
223@@ -1503,7 +1503,7 @@
224 'interface': 'req-int',
225 },
226 },
227- 'peer': {
228+ 'peers': {
229 'pee-rel': {
230 'interface': 'pee-int',
231 },

Subscribers

People subscribed via source and target branches