Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(546)

Side by Side Diff: state/watcher.go

Issue 6720049: state: machine units watcher reports unassignment
Patch Set: state: machine units watcher reports unassignment Created 12 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« state/machine_test.go ('K') | « state/machine_test.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package state 1 package state
2 2
3 import ( 3 import (
4 "labix.org/v2/mgo" 4 "labix.org/v2/mgo"
5 "launchpad.net/juju-core/environs/config" 5 "launchpad.net/juju-core/environs/config"
6 "launchpad.net/juju-core/state/watcher" 6 "launchpad.net/juju-core/state/watcher"
7 "launchpad.net/tomb" 7 "launchpad.net/tomb"
8 "strings" 8 "strings"
9 ) 9 )
10 10
(...skipping 1285 matching lines...) Expand 10 before | Expand all | Expand 10 after
1296 } 1296 }
1297 return pending, nil 1297 return pending, nil
1298 } 1298 }
1299 1299
1300 func (w *MachineUnitsWatcher) merge(pending []string, unit string) (new []string , err error) { 1300 func (w *MachineUnitsWatcher) merge(pending []string, unit string) (new []string , err error) {
1301 doc := unitDoc{} 1301 doc := unitDoc{}
1302 err = w.st.units.FindId(unit).One(&doc) 1302 err = w.st.units.FindId(unit).One(&doc)
1303 if err != nil && err != mgo.ErrNotFound { 1303 if err != nil && err != mgo.ErrNotFound {
1304 return nil, err 1304 return nil, err
1305 } 1305 }
1306 found := err != mgo.ErrNotFound
1306 life, known := w.known[unit] 1307 life, known := w.known[unit]
1307 » if err == mgo.ErrNotFound { 1308 » isprincipal := doc.Principal == ""
niemeyer 2012/10/19 00:10:25 If it wasn't found, that's not right.
1308 » » if known { 1309 » w.known[unit] = doc.Life
niemeyer 2012/10/19 00:10:25 If it wasn't found, that's certainly not right eit
1309 » » » w.st.watcher.Unwatch(w.st.units.Name, unit, w.in) 1310 » if !known {
1310 » » » if life != Dead { 1311 » » if _, ok := w.known[doc.Principal]; !ok && !isprincipal {
niemeyer 2012/10/19 00:10:25 Why are we looking for doc.Principal when it can b
1311 » » » » return append(pending, unit), nil 1312 » » » return pending, nil
1312 » » » }
1313 } 1313 }
1314 return pending, nil
1315 }
1316 if !known {
1317 w.st.watcher.Watch(w.st.units.Name, unit, doc.TxnRevno, w.in) 1314 w.st.watcher.Watch(w.st.units.Name, unit, doc.TxnRevno, w.in)
niemeyer 2012/10/19 00:10:25 If the document is !known && !found, we'll get her
1318 pending = append(pending, unit) 1315 pending = append(pending, unit)
niemeyer 2012/10/19 00:10:25 So if the document was previously unknown, we don'
1319 » } else if life != doc.Life { 1316 » } else {
1320 » » found := false 1317 » » var unassigned bool
1321 » » for _, v := range pending { 1318 » » if isprincipal {
1322 » » » if v == unit { 1319 » » » unassigned = (doc.MachineId != nil) && *doc.MachineId != w.machine.doc.Id || doc.MachineId == nil
niemeyer 2012/10/19 00:10:25 doc.MachineId == nil || *doc.MachineId != w.machin
1323 » » » » found = true 1320 » » } else if _, ok := w.known[doc.Principal]; !ok {
1321 » » » unassigned = true
1322 » » }
1323 » » if !found || unassigned {
1324 » » » delete(w.known, unit)
1325 » » » w.st.watcher.Unwatch(w.st.units.Name, unit, w.in)
1326 » » » if doc.Life != Dead && !hasString(pending, unit) {
niemeyer 2012/10/19 00:10:25 By now I'm finding pretty hard to recognize all th
1327 » » » » pending = append(pending, unit)
1324 } 1328 }
1329 for _, subunit := range doc.Subordinates {
1330 pending, err = w.merge(pending, subunit)
1331 if err != nil {
1332 return nil, err
1333 }
1334 }
1335 return pending, nil
1325 } 1336 }
1326 » » if !found { 1337 » » if doc.Life != life && !hasString(pending, unit) {
1327 pending = append(pending, unit) 1338 pending = append(pending, unit)
1328 } 1339 }
1329 } 1340 }
1330 w.known[unit] = doc.Life
1331 for _, subunit := range doc.Subordinates { 1341 for _, subunit := range doc.Subordinates {
1332 if _, ok := w.known[subunit]; !ok { 1342 if _, ok := w.known[subunit]; !ok {
1333 pending, err = w.merge(pending, subunit) 1343 pending, err = w.merge(pending, subunit)
1334 if err != nil { 1344 if err != nil {
1335 return nil, err 1345 return nil, err
1336 } 1346 }
1337 } 1347 }
1338 } 1348 }
1339 return pending, nil 1349 return pending, nil
1340 } 1350 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1375 if len(changes) > 0 { 1385 if len(changes) > 0 {
1376 out = w.out 1386 out = w.out
1377 } 1387 }
1378 case out <- changes: 1388 case out <- changes:
1379 out = nil 1389 out = nil
1380 changes = nil 1390 changes = nil
1381 } 1391 }
1382 } 1392 }
1383 panic("unreachable") 1393 panic("unreachable")
1384 } 1394 }
OLDNEW
« state/machine_test.go ('K') | « state/machine_test.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b