Merge lp:~dave-cheney/juju-core/089-state-unit-assigned-machine into lp:~juju/juju-core/trunk

Proposed by Dave Cheney
Status: Rejected
Rejected by: Gustavo Niemeyer
Proposed branch: lp:~dave-cheney/juju-core/089-state-unit-assigned-machine
Merge into: lp:~juju/juju-core/trunk
Diff against target: 247 lines (+38/-42)
8 files modified
cmd/juju/status.go (+2/-2)
cmd/jujud/provisioning_test.go (+1/-3)
juju/deploy_test.go (+5/-5)
state/assign_test.go (+18/-18)
state/service_test.go (+2/-2)
state/unit.go (+6/-6)
worker/firewaller/firewaller.go (+3/-3)
worker/firewaller/firewaller_test.go (+1/-3)
To merge this branch: bzr merge lp:~dave-cheney/juju-core/089-state-unit-assigned-machine
Reviewer Review Type Date Requested Status
The Go Language Gophers Pending
Review via email: mp+122617@code.launchpad.net

Description of the change

state: unit: AssignedMachine returns a machine

This proposal alters AssignedMachine (and it's signature) to return a
*state.Machine rather than an id. All the non test consumers of this
method are actually after a *Machine, not the id anyway, so this saves them
the call to state.Machine(). The test code was altered for the new signature
but is operation is unaffected.

https://codereview.appspot.com/6499071/

To post a comment you must log in.
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

The CL change doesn't seem to reflect the CL description.

https://codereview.appspot.com/6499071/diff/1/cmd/juju/status.go
File cmd/juju/status.go (right):

https://codereview.appspot.com/6499071/diff/1/cmd/juju/status.go#newcode212
cmd/juju/status.go:212: if m, err := unit.AssignedMachine(); err == nil
{
This seems to contradict the CL description. We used to have a machine
id, and now we will fetch a machine value from the database once more
for every single unit we have.

https://codereview.appspot.com/6499071/diff/1/worker/firewaller/firewaller.go
File worker/firewaller/firewaller.go (right):

https://codereview.appspot.com/6499071/diff/1/worker/firewaller/firewaller.go#newcode113
worker/firewaller/firewaller.go:113: if fw.machineds[machine.Id()] ==
nil {
Once again, real code that cares only about the id is now fetching the
machine just to pick its id.

https://codereview.appspot.com/6499071/

Revision history for this message
Roger Peppe (rogpeppe) wrote :

I'm +1 on this CL. It's easy to get a machine id from a Machine, but
getting a Machine from an id requires an additional round trip.

https://codereview.appspot.com/6499071/

Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

On 2012/09/06 12:04:41, rog wrote:
> I'm +1 on this CL. It's easy to get a machine id from a Machine, but
getting a
> Machine from an id requires an additional round trip.

Thus we should *always* be paying for the additional roundtrip to the
database, even when the two only non-test locations involved don't
actually want anything but the machine id? I don't understand.

This seems like a totally straightforward decision, btw. It concerns me
a bit that we're even discussing it. I'll soon be stepping down as core
reviewer and will not be overseeing all such changes anymore.

https://codereview.appspot.com/6499071/

Unmerged revisions

457. By Dave Cheney

AssignedMachine

456. By Roger Peppe

juju: always connect to State.

All uses of juju.Conn other than Bootstrap and Destroy require a state
connection, so make State an exported field of Conn and connect to it
when creating the Conn.

The few places that call Bootstrap can easily call Environ.Bootstrap
instead.

Also add environs.NewFromName to make it easier to open an
environment, rename juju.NewConn to juju.NewConnFromName, and add
juju.NewConnFromEnviron to join the dots.

R=TheMue, niemeyer
CC=
https://codereview.appspot.com/6488077

455. By Roger Peppe

worker: new package to factor out some common code from workers

Also simplify workers a little.

R=niemeyer
CC=
https://codereview.appspot.com/6501072

454. By Roger Peppe

cmd/jujud: fix race in test

We should not call OpenPort until the new instance id has
hit the state.

We also fix a bug in firewaller where it called InstanceId even
when it needed to do nothing, and take the opportunity
to make the firewaller tests a little more concise now that we
have JujuConnSuite.

R=TheMue, dfc
CC=
https://codereview.appspot.com/6499056

453. By Roger Peppe

environs: add BootstrapConfig

Also add flags to environs.FindTools and environs.BestTools,
LoggingSuite to JujuSuite, and make the Config in cloudinit
a *config.Config.

R=niemeyer, dfc
CC=
https://codereview.appspot.com/6500052

452. By Frank Mueller

state: changed environ config to be config.Config

state.EnvironConfig() and the according watcher now
return environs/config.Config. Additionally the new
function state.SetEnvironConfig() replaces the current
configuration. Depending code - mostly tests - has
been changed.

R=niemeyer
CC=
https://codereview.appspot.com/6493055

451. By William Reade

add charm.GitDir

Hopefully uncontroversial; implementing this should help me focus on the
actual charm.Manager implementation more easily.

R=niemeyer
CC=
https://codereview.appspot.com/6493059

450. By Roger Peppe

environs/config: add support for agent version.

Also adjust documentation for schema.Omit slightly
and add a test.

R=niemeyer
CC=
https://codereview.appspot.com/6499055

449. By Roger Peppe

environs: change BestTools so it does not choose later versions

As discussed on-line, when upgrading we want to
choose the latest version with number <= the proposed
version.

R=niemeyer
CC=
https://codereview.appspot.com/6495054

448. By Dave Cheney

environs/dummy: fix build

I thought I ran tests before submitting, but clearly I did not.

R=
CC=
https://codereview.appspot.com/6501063

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cmd/juju/status.go'
2--- cmd/juju/status.go 2012-09-03 17:35:44 +0000
3+++ cmd/juju/status.go 2012-09-04 06:33:21 +0000
4@@ -209,9 +209,9 @@
5 r["public-address"] = addr
6 }
7
8- if id, err := unit.AssignedMachineId(); err == nil {
9+ if m, err := unit.AssignedMachine(); err == nil {
10 // TODO(dfc) we could make this nicer, ie machine/0
11- r["machine"] = id
12+ r["machine"] = m.Id()
13 }
14
15 processVersion(r, unit)
16
17=== modified file 'cmd/jujud/provisioning_test.go'
18--- cmd/jujud/provisioning_test.go 2012-08-30 14:50:43 +0000
19+++ cmd/jujud/provisioning_test.go 2012-09-04 06:33:21 +0000
20@@ -59,9 +59,7 @@
21 c.Check(opRecvTimeout(c, op, dummy.OpStartInstance{}), NotNil)
22
23 // Wait for the instance id to show up in the state.
24- id, err := units[0].AssignedMachineId()
25- c.Assert(err, IsNil)
26- m, err := s.State.Machine(id)
27+ m, err := units[0].AssignedMachine()
28 c.Assert(err, IsNil)
29 w := m.Watch()
30 for _ = range w.Changes() {
31
32=== modified file 'juju/deploy_test.go'
33--- juju/deploy_test.go 2012-09-03 17:35:44 +0000
34+++ juju/deploy_test.go 2012-09-04 06:33:21 +0000
35@@ -172,9 +172,9 @@
36 c.Assert(err, IsNil)
37 c.Assert(units, HasLen, 2)
38
39- id0, err := units[0].AssignedMachineId()
40- c.Assert(err, IsNil)
41- id1, err := units[1].AssignedMachineId()
42- c.Assert(err, IsNil)
43- c.Assert(id0, Not(Equals), id1)
44+ m0, err := units[0].AssignedMachine()
45+ c.Assert(err, IsNil)
46+ m1, err := units[1].AssignedMachine()
47+ c.Assert(err, IsNil)
48+ c.Assert(m0.Id(), Not(Equals), m1.Id())
49 }
50
51=== modified file 'state/assign_test.go'
52--- state/assign_test.go 2012-08-15 11:51:56 +0000
53+++ state/assign_test.go 2012-09-04 06:33:21 +0000
54@@ -39,7 +39,7 @@
55 c.Assert(err, IsNil)
56
57 // Check that the unit has no machine assigned.
58- _, err = s.unit.AssignedMachineId()
59+ _, err = s.unit.AssignedMachine()
60 c.Assert(err, ErrorMatches, `cannot get machine id of unit "wordpress/0": unit not assigned to machine`)
61 }
62
63@@ -63,9 +63,9 @@
64 err = s.unit.AssignToMachine(machineTwo)
65 c.Assert(err, ErrorMatches, `cannot assign unit "wordpress/0" to machine 2: unit already assigned to machine 1`)
66
67- machineId, err := s.unit.AssignedMachineId()
68+ machine, err := s.unit.AssignedMachine()
69 c.Assert(err, IsNil)
70- c.Assert(machineId, Equals, 1)
71+ c.Assert(machine.Id(), Equals, 1)
72 }
73
74 func (s *AssignSuite) TestUnassignUnitFromMachineWithChangingState(c *C) {
75@@ -76,7 +76,7 @@
76
77 err = s.unit.UnassignFromMachine()
78 c.Assert(err, ErrorMatches, `cannot unassign unit "wordpress/0" from machine: environment state has changed`)
79- _, err = s.unit.AssignedMachineId()
80+ _, err = s.unit.AssignedMachine()
81 c.Assert(err, ErrorMatches, `cannot get machine id of unit "wordpress/0": environment state has changed`)
82
83 err = s.State.RemoveService(s.service)
84@@ -84,7 +84,7 @@
85
86 err = s.unit.UnassignFromMachine()
87 c.Assert(err, ErrorMatches, `cannot unassign unit "wordpress/0" from machine: environment state has changed`)
88- _, err = s.unit.AssignedMachineId()
89+ _, err = s.unit.AssignedMachine()
90 c.Assert(err, ErrorMatches, `cannot get machine id of unit "wordpress/0": environment state has changed`)
91 }
92
93@@ -164,19 +164,19 @@
94 err = s.unit.AssignToMachine(m1)
95 c.Assert(err, IsNil)
96
97- id, err := log1Unit.AssignedMachineId()
98+ m, err := log1Unit.AssignedMachine()
99 c.Assert(err, IsNil)
100- c.Check(id, Equals, m1.Id())
101- id, err = log2Unit.AssignedMachineId()
102- c.Check(id, Equals, m1.Id())
103+ c.Check(m.Id(), Equals, m1.Id())
104+ m, err = log2Unit.AssignedMachine()
105+ c.Check(m.Id(), Equals, m1.Id())
106
107 // Check that unassigning the principal unassigns the
108 // subordinates too.
109 err = s.unit.UnassignFromMachine()
110 c.Assert(err, IsNil)
111- _, err = log1Unit.AssignedMachineId()
112+ _, err = log1Unit.AssignedMachine()
113 c.Assert(err, ErrorMatches, `cannot get machine id of unit "logging1/0": unit not assigned to machine`)
114- _, err = log2Unit.AssignedMachineId()
115+ _, err = log2Unit.AssignedMachine()
116 c.Assert(err, ErrorMatches, `cannot get machine id of unit "logging2/0": unit not assigned to machine`)
117 }
118
119@@ -184,16 +184,16 @@
120 // Check nonsensical policy
121 fail := func() { s.State.AssignUnit(s.unit, state.AssignmentPolicy("random")) }
122 c.Assert(fail, PanicMatches, `unknown unit assignment policy: "random"`)
123- _, err := s.unit.AssignedMachineId()
124+ _, err := s.unit.AssignedMachine()
125 c.Assert(err, NotNil)
126 assertMachineCount(c, s.State, 1)
127
128 // Check local placement
129 err = s.State.AssignUnit(s.unit, state.AssignLocal)
130 c.Assert(err, IsNil)
131- mid, err := s.unit.AssignedMachineId()
132+ m, err := s.unit.AssignedMachine()
133 c.Assert(err, IsNil)
134- c.Assert(mid, Equals, 0)
135+ c.Assert(m.Id(), Equals, 0)
136 assertMachineCount(c, s.State, 1)
137
138 // Check unassigned placement with no unused machines
139@@ -201,9 +201,9 @@
140 c.Assert(err, IsNil)
141 err = s.State.AssignUnit(unit1, state.AssignUnused)
142 c.Assert(err, IsNil)
143- mid, err = unit1.AssignedMachineId()
144+ m, err = unit1.AssignedMachine()
145 c.Assert(err, IsNil)
146- c.Assert(mid, Equals, 1)
147+ c.Assert(m.Id(), Equals, 1)
148 assertMachineCount(c, s.State, 2)
149
150 // Check unassigned placement on an unused machine
151@@ -212,9 +212,9 @@
152 c.Assert(err, IsNil)
153 err = s.State.AssignUnit(unit2, state.AssignUnused)
154 c.Assert(err, IsNil)
155- mid, err = unit2.AssignedMachineId()
156+ m, err = unit2.AssignedMachine()
157 c.Assert(err, IsNil)
158- c.Assert(mid, Equals, 2)
159+ c.Assert(m.Id(), Equals, 2)
160 assertMachineCount(c, s.State, 3)
161
162 // Check cannot assign subordinates to machines
163
164=== modified file 'state/service_test.go'
165--- state/service_test.go 2012-08-21 15:39:59 +0000
166+++ state/service_test.go 2012-09-04 06:33:21 +0000
167@@ -105,9 +105,9 @@
168 c.Assert(principal, Equals, false)
169
170 // Check the subordinate unit has been assigned its principal's machine.
171- id, err := subZero.AssignedMachineId()
172+ m, err = subZero.AssignedMachine()
173 c.Assert(err, IsNil)
174- c.Assert(id, Equals, m.Id())
175+ c.Assert(m.Id(), Equals, m.Id())
176
177 // Check that subordinate units must be added to other units.
178 _, err = logging.AddUnit()
179
180=== modified file 'state/unit.go'
181--- state/unit.go 2012-08-21 15:39:59 +0000
182+++ state/unit.go 2012-09-04 06:33:21 +0000
183@@ -233,21 +233,21 @@
184 return u.principalKey == ""
185 }
186
187-// AssignedMachineId returns the id of the assigned machine.
188-func (u *Unit) AssignedMachineId() (id int, err error) {
189+// AssignedMachine returns the machine assigned to this unit.
190+func (u *Unit) AssignedMachine() (machine *Machine, err error) {
191 defer trivial.ErrorContextf(&err, "cannot get machine id of unit %q", u)
192 topology, err := readTopology(u.st.zk)
193 if err != nil {
194- return 0, err
195+ return nil, err
196 }
197 if !topology.HasUnit(u.key) {
198- return 0, stateChanged
199+ return nil, stateChanged
200 }
201 machineKey, err := topology.UnitMachineKey(u.key)
202 if err != nil {
203- return 0, err
204+ return nil, err
205 }
206- return keySeq(machineKey), nil
207+ return u.st.Machine(keySeq(machineKey))
208 }
209
210 // AssignToMachine assigns this unit to a given machine.
211
212=== modified file 'worker/firewaller/firewaller.go'
213--- worker/firewaller/firewaller.go 2012-08-31 16:14:09 +0000
214+++ worker/firewaller/firewaller.go 2012-09-04 06:33:21 +0000
215@@ -106,14 +106,14 @@
216 for _, unit := range change.Added {
217 unitd := newUnitData(unit, fw)
218 fw.unitds[unit.Name()] = unitd
219- machineId, err := unit.AssignedMachineId()
220+ machine, err := unit.AssignedMachine()
221 if err != nil {
222 fw.tomb.Kill(err)
223 }
224- if fw.machineds[machineId] == nil {
225+ if fw.machineds[machine.Id()] == nil {
226 panic("machine of added unit is not watched")
227 }
228- unitd.machined = fw.machineds[machineId]
229+ unitd.machined = fw.machineds[machine.Id()]
230 unitd.machined.unitds[unit.Name()] = unitd
231 if fw.serviceds[unit.ServiceName()] == nil {
232 service, err := fw.st.Service(unit.ServiceName())
233
234=== modified file 'worker/firewaller/firewaller_test.go'
235--- worker/firewaller/firewaller_test.go 2012-08-31 05:41:43 +0000
236+++ worker/firewaller/firewaller_test.go 2012-09-04 06:33:21 +0000
237@@ -64,9 +64,7 @@
238 units, err := s.Conn.AddUnits(svc, 1)
239 c.Assert(err, IsNil)
240 u := units[0]
241- id, err := u.AssignedMachineId()
242- c.Assert(err, IsNil)
243- m, err := s.State.Machine(id)
244+ m, err := u.AssignedMachine()
245 c.Assert(err, IsNil)
246 return u, m
247 }

Subscribers

People subscribed via source and target branches