~themue/juju-core/go-state-relation-endpoint-verification

« back to all changes in this revision

Viewing changes to state/machine.go

state: store units inside their respective services

Making this change simplifies a lot of code - no need
to keep a separate sequence numbering system for units,
for example.

(original proposal at https://codereview.appspot.com/6247066/)

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
// Id returns the machine id.
25
25
func (m *Machine) Id() int {
26
 
        return machineId(m.key)
 
26
        return keySeq(m.key)
27
27
}
28
28
 
29
29
// AgentAlive returns whether the respective remote agent is alive.
80
80
        return strconv.Itoa(m.Id())
81
81
}
82
82
 
83
 
// zkKey returns the ZooKeeper key of the machine.
84
 
func (m *Machine) zkKey() string {
85
 
        return m.key
86
 
}
87
 
 
88
83
// zkPath returns the ZooKeeper base path for the machine.
89
84
func (m *Machine) zkPath() string {
90
 
        return path.Join(zkMachinesPath, m.zkKey())
 
85
        return path.Join(zkMachinesPath, m.key)
91
86
}
92
87
 
93
88
// zkAgentPath returns the ZooKeeper path for the machine agent.
95
90
        return path.Join(m.zkPath(), "agent")
96
91
}
97
92
 
98
 
// machineId returns the machine id corresponding to machineKey.
99
 
func machineId(machineKey string) (id int) {
100
 
        if machineKey == "" {
101
 
                panic("machineId: empty machine key")
 
93
// keySeq returns the sequence number part of
 
94
// the the given machine or unit key.
 
95
func keySeq(key string) (id int) {
 
96
        if key == "" {
 
97
                panic("keySeq: empty key")
102
98
        }
103
 
        i := strings.Index(machineKey, "-")
 
99
        i := strings.LastIndex(key, "-")
104
100
        var id64 int64
105
101
        var err error
106
102
        if i >= 0 {
107
 
                id64, err = strconv.ParseInt(machineKey[i+1:], 10, 32)
 
103
                id64, err = strconv.ParseInt(key[i+1:], 10, 32)
108
104
        }
109
105
        if i < 0 || err != nil {
110
 
                panic("machineId: invalid machine key: " + machineKey)
 
106
                panic("keySeq: invalid key: " + key)
111
107
        }
112
108
        return int(id64)
113
109
}