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

Delta Between Two Patch Sets: state/machine.go

Issue 8256043: state: add machine status; refractor unit status (Closed)
Left Patch Set: Created 12 years ago
Right Patch Set: state: add machine status; refractor unit status Created 12 years 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « state/constraints.go ('k') | state/machine_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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
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 } 310 }
301 // 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
302 // been removed. 312 // been removed.
303 return onAbort(m.st.runner.Run(ops, "", nil), nil) 313 return onAbort(m.st.runner.Run(ops, "", nil), nil)
304 } 314 }
305 315
306 // Refresh refreshes the contents of the machine from the underlying 316 // Refresh refreshes the contents of the machine from the underlying
307 // 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
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 if err := m.st.runner.Run(ops, "", nil); err != txn.ErrAborted { 449 if err := m.st.runner.Run(ops, "", nil); err != txn.ErrAborted {
440 return err 450 return err
441 } 451 }
442 if m, err = m.st.Machine(m.doc.Id); err != nil { 452 if m, err = m.st.Machine(m.doc.Id); err != nil {
443 return err 453 return err
444 } 454 }
445 } 455 }
446 return ErrExcessiveContention 456 return ErrExcessiveContention
447 } 457 }
448 458
449 // Status returns the status of the machine's agent. 459 // Status returns the status of the machine.
fwereade 2013/04/02 15:33:42 I think drop the "agent" bit.
dimitern 2013/04/03 09:33:00 Done.
450 func (m *Machine) Status() (status params.MachineStatus, info string, err error) { 460 func (m *Machine) Status() (status params.MachineStatus, info string, err error) {
451 doc := &machineStatusDoc{} 461 doc := &machineStatusDoc{}
452 » if err := getStatus(m.st, m, doc); IsNotFound(err) { 462 » if err := getStatus(m.st, m.globalKey(), doc); err != nil {
453 » » return params.MachinePending, "", nil
454 » } else if err != nil {
455 return "", "", err 463 return "", "", err
456 } 464 }
457 return doc.Status, doc.StatusInfo, nil 465 return doc.Status, doc.StatusInfo, nil
458 } 466 }
459 467
460 // SetStatus sets the status of the machine. 468 // SetStatus sets the status of the machine.
461 func (m *Machine) SetStatus(status params.MachineStatus, info string) error { 469 func (m *Machine) SetStatus(status params.MachineStatus, info string) error {
462 if status == params.MachinePending { 470 if status == params.MachinePending {
463 » » panic("machine status must not be set to pending") 471 » » panic("cannot set machine status to pending")
rog 2013/04/02 15:29:07 i prefer panic messages to say what went wrong rat
dimitern 2013/04/03 09:33:00 Fair enough, changed.
464 } else if status == params.MachineError && info == "" { 472 } else if status == params.MachineError && info == "" {
465 » » panic("must set info for machine error status") 473 » » panic("machine error status with no info")
rog 2013/04/02 15:29:07 panic("machine error status with no info") ?
dimitern 2013/04/03 09:33:00 Done.
466 } 474 }
467 doc := &machineStatusDoc{status, info} 475 doc := &machineStatusDoc{status, info}
468 ops := []txn.Op{{ 476 ops := []txn.Op{{
469 C: m.st.machines.Name, 477 C: m.st.machines.Name,
470 Id: m.doc.Id, 478 Id: m.doc.Id,
471 Assert: notDeadDoc, 479 Assert: notDeadDoc,
472 }, 480 },
473 » » setStatusOp(m.st, m, doc), 481 » » updateStatusOp(m.st, m.globalKey(), doc),
474 } 482 }
475 if err := m.st.runner.Run(ops, "", nil); err != nil { 483 if err := m.st.runner.Run(ops, "", nil); err != nil {
476 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))
477 } 485 }
478 return nil 486 return nil
479 } 487 }
LEFTRIGHT

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