Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 package state | 1 package state |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "labix.org/v2/mgo" | 5 "labix.org/v2/mgo" |
6 "labix.org/v2/mgo/txn" | 6 "labix.org/v2/mgo/txn" |
7 "launchpad.net/juju-core/constraints" | 7 "launchpad.net/juju-core/constraints" |
8 "launchpad.net/juju-core/state/api/params" | 8 "launchpad.net/juju-core/state/api/params" |
9 "launchpad.net/juju-core/state/presence" | 9 "launchpad.net/juju-core/state/presence" |
10 "launchpad.net/juju-core/trivial" | 10 "launchpad.net/juju-core/trivial" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 type machineDoc struct { | 52 type machineDoc struct { |
53 Id string `bson:"_id"` | 53 Id string `bson:"_id"` |
54 Series string | 54 Series string |
55 InstanceId InstanceId | 55 InstanceId InstanceId |
56 Principals []string | 56 Principals []string |
57 Life Life | 57 Life Life |
58 Tools *Tools `bson:",omitempty"` | 58 Tools *Tools `bson:",omitempty"` |
59 TxnRevno int64 `bson:"txn-revno"` | 59 TxnRevno int64 `bson:"txn-revno"` |
60 Jobs []MachineJob | 60 Jobs []MachineJob |
61 PasswordHash string | 61 PasswordHash string |
62 } | |
63 | |
64 // machineStatusDoc represents the internal state of a machine status in MongoDB . | |
65 // The implicit _id field is explicitly set to the global key of the | |
66 // associated machine in the document's creation transaction, but omitted to | |
67 // allow direct use of the document in both create and update transactions. | |
68 type machineStatusDoc struct { | |
69 Status params.MachineStatus | |
70 StatusInfo string | |
62 } | 71 } |
63 | 72 |
64 func newMachine(st *State, doc *machineDoc) *Machine { | 73 func newMachine(st *State, doc *machineDoc) *Machine { |
65 machine := &Machine{ | 74 machine := &Machine{ |
66 st: st, | 75 st: st, |
67 doc: *doc, | 76 doc: *doc, |
68 } | 77 } |
69 machine.annotator = annotator{ | 78 machine.annotator = annotator{ |
70 globalKey: machine.globalKey(), | 79 globalKey: machine.globalKey(), |
71 tag: machine.Tag(), | 80 tag: machine.Tag(), |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
288 if m.doc.Life != Dead { | 297 if m.doc.Life != Dead { |
289 return fmt.Errorf("machine is not dead") | 298 return fmt.Errorf("machine is not dead") |
290 } | 299 } |
291 ops := []txn.Op{ | 300 ops := []txn.Op{ |
292 { | 301 { |
293 C: m.st.machines.Name, | 302 C: m.st.machines.Name, |
294 Id: m.doc.Id, | 303 Id: m.doc.Id, |
295 Assert: txn.DocExists, | 304 Assert: txn.DocExists, |
296 Remove: true, | 305 Remove: true, |
297 }, | 306 }, |
307 removeStatusOp(m.st, m.globalKey()), | |
298 removeConstraintsOp(m.st, m.globalKey()), | 308 removeConstraintsOp(m.st, m.globalKey()), |
299 annotationRemoveOp(m.st, m.globalKey()), | 309 annotationRemoveOp(m.st, m.globalKey()), |
300 removeStatusOp(m.st, m), | |
fwereade
2013/04/03 11:42:34
I don't really like the inconsistency here. IMO we
dimitern
2013/04/03 14:29:27
As discussed, I'm removing globalKeyer until there
| |
301 } | 310 } |
302 // The only abort conditions in play indicate that the machine has alrea dy | 311 // The only abort conditions in play indicate that the machine has alrea dy |
303 // been removed. | 312 // been removed. |
304 return onAbort(m.st.runner.Run(ops, "", nil), nil) | 313 return onAbort(m.st.runner.Run(ops, "", nil), nil) |
305 } | 314 } |
306 | 315 |
307 // Refresh refreshes the contents of the machine from the underlying | 316 // Refresh refreshes the contents of the machine from the underlying |
308 // state. It returns an error that satisfies IsNotFound if the machine has | 317 // state. It returns an error that satisfies IsNotFound if the machine has |
309 // been removed. | 318 // been removed. |
310 func (m *Machine) Refresh() error { | 319 func (m *Machine) Refresh() error { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 if m, err = m.st.Machine(m.doc.Id); err != nil { | 452 if m, err = m.st.Machine(m.doc.Id); err != nil { |
444 return err | 453 return err |
445 } | 454 } |
446 } | 455 } |
447 return ErrExcessiveContention | 456 return ErrExcessiveContention |
448 } | 457 } |
449 | 458 |
450 // Status returns the status of the machine. | 459 // Status returns the status of the machine. |
451 func (m *Machine) Status() (status params.MachineStatus, info string, err error) { | 460 func (m *Machine) Status() (status params.MachineStatus, info string, err error) { |
452 doc := &machineStatusDoc{} | 461 doc := &machineStatusDoc{} |
453 » if err := getStatus(m.st, m, doc); IsNotFound(err) { | 462 » if err := getStatus(m.st, m.globalKey(), doc); err != nil { |
454 » » if err := m.Refresh(); IsNotFound(err) { | |
455 » » » return "", "", fmt.Errorf("cannot get status of machine %q: not found", m) | |
456 » » } | |
457 » » return params.MachinePending, "", nil | |
458 » } else if err != nil { | |
459 return "", "", err | 463 return "", "", err |
460 } | 464 } |
461 return doc.Status, doc.StatusInfo, nil | 465 return doc.Status, doc.StatusInfo, nil |
462 } | 466 } |
463 | 467 |
464 // SetStatus sets the status of the machine. | 468 // SetStatus sets the status of the machine. |
465 func (m *Machine) SetStatus(status params.MachineStatus, info string) error { | 469 func (m *Machine) SetStatus(status params.MachineStatus, info string) error { |
466 if status == params.MachinePending { | 470 if status == params.MachinePending { |
467 panic("cannot set machine status to pending") | 471 panic("cannot set machine status to pending") |
468 } else if status == params.MachineError && info == "" { | 472 } else if status == params.MachineError && info == "" { |
469 panic("machine error status with no info") | 473 panic("machine error status with no info") |
470 } | 474 } |
471 doc := &machineStatusDoc{status, info} | 475 doc := &machineStatusDoc{status, info} |
472 ops := []txn.Op{{ | 476 ops := []txn.Op{{ |
473 C: m.st.machines.Name, | 477 C: m.st.machines.Name, |
474 Id: m.doc.Id, | 478 Id: m.doc.Id, |
475 Assert: notDeadDoc, | 479 Assert: notDeadDoc, |
476 }, | 480 }, |
477 » » setStatusOp(m.st, m, doc), | 481 » » updateStatusOp(m.st, m.globalKey(), doc), |
478 } | 482 } |
479 if err := m.st.runner.Run(ops, "", nil); err != nil { | 483 if err := m.st.runner.Run(ops, "", nil); err != nil { |
480 return fmt.Errorf("cannot set status of machine %q: %v", m, onAb ort(err, errNotAlive)) | 484 return fmt.Errorf("cannot set status of machine %q: %v", m, onAb ort(err, errNotAlive)) |
481 } | 485 } |
482 return nil | 486 return nil |
483 } | 487 } |
LEFT | RIGHT |