Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #55 from soundcloud/feature/consolidate-instance-info

Consolidate Instance & InstanceInfo types
  • Loading branch information...
commit 4b6d37397316cf1b29cb248071dfd39ed0f3976c 2 parents a7ede49 + 48331da
Alexis Sellier authored
View
4 event.go
@@ -165,7 +165,7 @@ func GetEventInfo(s Snapshot, ev *Event) (info interface{}, err error) {
}
case EvInsReg:
path := ev.Path
- info, err = GetInstanceInfo(s, path["instance"])
+ info, err = GetInstance(s, path["instance"])
if err != nil {
fmt.Printf("error getting instance info: %s\n", err)
@@ -173,7 +173,7 @@ func GetEventInfo(s Snapshot, ev *Event) (info interface{}, err error) {
}
case EvInsStateChange:
path := ev.Path
- info, err = GetInstanceInfo(s, path["instance"])
+ info, err = GetInstance(s, path["instance"])
if err != nil {
fmt.Printf("error getting instance info: %s\n", err)
View
14 event_test.go
@@ -164,9 +164,7 @@ func TestEventProcTypeUnregistered(t *testing.T) {
}
func TestEventInstanceRegistered(t *testing.T) {
s, app, l := eventSetup("regmouse")
- rev := NewRevision(app, "stable", s)
- pty := NewProcType(app, "web", s)
- ins, _ := NewInstance(pty, rev, "127.0.0.1:8080", s)
+ ins, _ := NewInstance("web", "stable", app.Name, "127.0.0.1:8080", s)
go WatchEvent(s, l)
@@ -179,9 +177,7 @@ func TestEventInstanceRegistered(t *testing.T) {
}
func TestEventInstanceUnregistered(t *testing.T) {
s, app, l := eventSetup("unregmouse")
- rev := NewRevision(app, "stable", s)
- pty := NewProcType(app, "web", s)
- ins, _ := NewInstance(pty, rev, "127.0.0.1:8080", s)
+ ins, _ := NewInstance("web", "stable", app.Name, "127.0.0.1:8080", s)
ins, err := ins.Register()
if err != nil {
t.Error(err)
@@ -199,10 +195,8 @@ func TestEventInstanceUnregistered(t *testing.T) {
}
func TestEventInstanceStateChange(t *testing.T) {
- s, app, l := eventSetup("statemouse")
- rev := NewRevision(app, "stable-state", s)
- pty := NewProcType(app, "web-state", s)
- ins, _ := NewInstance(pty, rev, "127.0.0.1:8081", s)
+ s, _, l := eventSetup("statemouse")
+ ins, _ := NewInstance("web-state", "stable-state", "statemouse", "127.0.0.1:8081", s)
ins, err := ins.Register()
if err != nil {
View
86 instance.go
@@ -24,9 +24,9 @@ const (
const INSTANCES_PATH = "instances"
-// InstanceInfo represents instance information as ids,
-// when it's impractical to use the full Instance type.
-type InstanceInfo struct {
+// An Instance represents a running process of a specific type.
+type Instance struct {
+ Path
Name string
AppName string
RevisionName string
@@ -37,37 +37,20 @@ type InstanceInfo struct {
State State
}
-func (i InstanceInfo) AddrString() string {
- return i.Host + ":" + strconv.Itoa(i.Port)
-}
-func (i InstanceInfo) RevString() string {
- return i.AppName + "-" + i.RevisionName
-}
-func (i InstanceInfo) LogString() string {
- return fmt.Sprintf("%s (%s)", i.RevString(), i.AddrString())
-}
-
-// An Instance represents a running process of a specific type.
-type Instance struct {
- Path
- ProcType *ProcType // ProcType the instance belongs to
- Revision *Revision
- Addr *net.TCPAddr // TCP address of the running instance
- State State // Current state of the instance
-}
-
// NewInstance creates and returns a new Instance object.
-func NewInstance(pty *ProcType, rev *Revision, addr string, snapshot Snapshot) (ins *Instance, err error) {
+func NewInstance(pty string, rev string, app string, addr string, snapshot Snapshot) (ins *Instance, err error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return
}
ins = &Instance{
- Addr: tcpAddr,
- ProcType: pty,
- Revision: rev,
- State: InsStateInitial,
+ Host: tcpAddr.IP.String(),
+ Port: tcpAddr.Port,
+ AppName: app,
+ ProcessName: ProcessName(pty),
+ RevisionName: rev,
+ State: InsStateInitial,
}
ins.Path = Path{snapshot, "/instances/" + ins.Id()}
@@ -86,6 +69,10 @@ func (i *Instance) createSnapshot(rev int64) Snapshotable {
return &tmp
}
+func (i *Instance) ProctypePath() string {
+ return path.Join(APPS_PATH, i.AppName, PROCS_PATH, string(i.ProcessName), INSTANCES_PATH, i.Id())
+}
+
// Register registers an instance with the registry.
func (i *Instance) Register() (instance *Instance, err error) {
exists, _, err := i.conn.Exists(i.Path.Dir)
@@ -106,7 +93,7 @@ func (i *Instance) Register() (instance *Instance, err error) {
}
now := time.Now().UTC().String()
- s, err := i.Snapshot.Set(i.ProcType.InstancePath(i.Id()), now)
+ s, err := i.Snapshot.Set(i.ProctypePath(), now)
instance = i.FastForward(s.Rev)
return
@@ -114,7 +101,7 @@ func (i *Instance) Register() (instance *Instance, err error) {
// Unregister unregisters an instance with the registry.
func (i *Instance) Unregister() (err error) {
- err = i.Snapshot.Del(i.ProcType.InstancePath(i.Id()))
+ err = i.Snapshot.Del(i.ProctypePath())
if err != nil {
return
}
@@ -122,17 +109,6 @@ func (i *Instance) Unregister() (err error) {
return
}
-func (i *InstanceInfo) Unregister(s Snapshot) (err error) {
- p := path.Join(APPS_PATH, i.AppName, PROCS_PATH, string(i.ProcessName), INSTANCES_PATH, i.Name)
-
- err = s.Del(p)
- if err != nil {
- return
- }
- err = s.Del(path.Join(INSTANCES_PATH, i.Name))
- return
-}
-
// UpdateState updates the instance's state file in
// the coordinator to the given value.
func (i *Instance) UpdateState(s State) (ins *Instance, err error) {
@@ -147,21 +123,33 @@ func (i *Instance) UpdateState(s State) (ins *Instance, err error) {
}
func (i *Instance) Id() string {
- return strings.Replace(strings.Replace(i.Addr.String(), ".", "-", -1), ":", "-", -1)
+ return fmt.Sprintf("%s-%d", strings.Replace(i.Host, ".", "-", -1), i.Port)
}
func (i *Instance) String() string {
return strings.Join([]string{
- i.ProcType.App.Name,
- i.Revision.Ref,
- string(i.ProcType.Name),
- i.Addr.IP.String(),
- fmt.Sprintf("%d", i.Addr.Port),
+ i.AppName,
+ i.RevisionName,
+ string(i.ProcessName),
+ i.Host,
+ fmt.Sprintf("%d", i.Port),
}, " ")
}
-// GetInstanceInfo returns an InstanceInfo from the given app, rev, proc and instance ids.
-func GetInstanceInfo(s Snapshot, insName string) (ins *InstanceInfo, err error) {
+func (i *Instance) AddrString() string {
+ return i.Host + ":" + strconv.Itoa(i.Port)
+}
+
+func (i *Instance) RevString() string {
+ return i.AppName + "-" + i.RevisionName
+}
+
+func (i *Instance) LogString() string {
+ return fmt.Sprintf("%s (%s)", i.RevString(), i.AddrString())
+}
+
+// GetInstance returns an Instance from the given app, rev, proc and instance ids.
+func GetInstance(s Snapshot, insName string) (ins *Instance, err error) {
p := path.Join(INSTANCES_PATH, insName)
state, _, err := s.conn.Get(p+"/state", nil)
@@ -180,7 +168,7 @@ func GetInstanceInfo(s Snapshot, insName string) (ins *InstanceInfo, err error)
return
}
- ins = &InstanceInfo{
+ ins = &Instance{
Name: insName,
AppName: fields[0],
RevisionName: fields[1],
View
20 instance_test.go
@@ -30,7 +30,7 @@ func instanceSetup(addr string, pType ProcessName) (ins *Instance) {
rev.ArchiveUrl = "archive"
pty := NewProcType(app, pType, s)
- ins, err = NewInstance(pty, rev, addr, s)
+ ins, err = NewInstance(string(pty.Name), rev.Ref, app.Name, addr, s)
if err != nil {
panic(err)
}
@@ -75,7 +75,7 @@ func TestInstanceRegister(t *testing.T) {
t.Error("Instance registration failed")
}
- check, _, err = ins.conn.Exists(ins.ProcType.InstancePath(ins.Id()))
+ check, _, err = ins.conn.Exists(ins.ProctypePath())
if err != nil {
t.Error(err)
}
@@ -89,23 +89,23 @@ func TestInstanceRegister(t *testing.T) {
}
}
-func TestGetInstanceInfo(t *testing.T) {
+func TestGetInstance(t *testing.T) {
ins := instanceSetup("localhost:9494", "web")
_, err := ins.Register()
if err != nil {
t.Errorf("Instance registration failed: %s", err)
}
- i, err := GetInstanceInfo(ins.Snapshot, ins.Id())
+ i, err := GetInstance(ins.Snapshot, ins.Id())
if err != nil {
t.Error(err)
}
if i.Name != ins.Id() ||
- i.Port != ins.Addr.Port ||
- i.RevisionName != ins.Revision.Ref ||
- i.AppName != ins.ProcType.App.Name ||
- i.ProcessName != ins.ProcType.Name ||
- i.Host != ins.Addr.IP.String() {
- t.Error("InstanceInfo fields don't match Instance")
+ i.Port != ins.Port ||
+ i.RevisionName != ins.RevisionName ||
+ i.AppName != ins.AppName ||
+ i.ProcessName != ins.ProcessName ||
+ i.Host != ins.Host {
+ t.Error("Instance fields don't match Instance")
}
}
View
6 proctype.go
@@ -102,16 +102,16 @@ func (p *ProcType) GetInstanceNames() (ins []string, err error) {
return
}
-func (p *ProcType) GetInstanceInfos() (ins []*InstanceInfo, err error) {
+func (p *ProcType) GetInstances() (ins []*Instance, err error) {
insNames, err := p.GetInstanceNames()
if err != nil {
return
}
for _, insName := range insNames {
- var i *InstanceInfo
+ var i *Instance
- i, err = GetInstanceInfo(p.Snapshot, insName)
+ i, err = GetInstance(p.Snapshot, insName)
if err != nil {
return
}
View
4 visor/app-instances-purge.go
@@ -50,7 +50,7 @@ func runAppInstancesPurge(cmd *Command, args []string) {
continue
}
- ins, err := pty.GetInstanceInfos()
+ ins, err := pty.GetInstances()
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching instances for %s: %s\n", pty.Name, err.Error())
os.Exit(2)
@@ -58,7 +58,7 @@ func runAppInstancesPurge(cmd *Command, args []string) {
for _, i := range ins {
if i.State == visor.InsStateDead && i.RevisionName == *revname {
- err := i.Unregister(s)
+ err := i.Unregister()
if err != nil {
fmt.Fprintf(os.Stderr, "Error removing instance %s: %s\n", i.Name, err.Error())
}
View
2  visor/app-instances.go
@@ -48,7 +48,7 @@ func runAppInstances(cmd *Command, args []string) {
continue
}
- ins, err := pty.GetInstanceInfos()
+ ins, err := pty.GetInstances()
if err != nil {
fmt.Fprintf(os.Stderr, "Error fetching instances for %s %s\n", pty.Name, err.Error())
os.Exit(2)
Please sign in to comment.
Something went wrong with that request. Please try again.