Permalink
Browse files

Implement InsStatusStopping, return tickets from Scale

  • Loading branch information...
1 parent a1f332e commit 55969038ca401f7836acbf3f303f68ddaae9d68a @cloudhead cloudhead committed Nov 27, 2012
Showing with 40 additions and 20 deletions.
  1. +1 −1 cmd/visor/scale.go
  2. +13 −3 instance.go
  3. +2 −0 proctype.go
  4. +1 −1 snapshot_test.go
  5. +10 −8 visor.go
  6. +13 −7 visor_test.go
View
@@ -37,7 +37,7 @@ func runScale(cmd *Command, args []string) {
os.Exit(1)
}
- err = visor.Scale(args[0], args[1], args[2], f, cmdScale.Snapshot)
+ _, _, err = visor.Scale(args[0], args[1], args[2], f, cmdScale.Snapshot)
if err != nil {
log.Fatal(err)
}
View
@@ -21,9 +21,10 @@ const statusPath = "status"
const stopPath = "stop"
const (
- InsStatusPending InsStatus = "pending"
- InsStatusClaimed = "claimed"
- InsStatusRunning = "running"
+ InsStatusPending InsStatus = "pending"
+ InsStatusClaimed = "claimed"
+ InsStatusRunning = "running"
+ InsStatusStopping = "stopping"
InsStatusFailed = "failed"
InsStatusExited = "exited"
@@ -84,6 +85,15 @@ func GetInstance(s Snapshot, id int64) (ins *Instance, err error) {
return
}
+ if status == InsStatusRunning {
+ _, _, err := s.get(p + "/stop")
+ if err == nil {
+ status = InsStatusStopping
+ } else if !IsErrNoEnt(err) {
+ return nil, err
+ }
+ }
+
f, err = s.getFile(p+"/object", new(listCodec))
if err != nil {
return
View
@@ -169,6 +169,8 @@ func (p *ProcType) getInstances(ids []string) (ins []*Instance, err error) {
return
}
+// TODO consider moving to (*App).GetProcType(name)
+
// GetProcType fetches a ProcType from the coordinator
func GetProcType(s Snapshot, app *App, name string) (p *ProcType, err error) {
path := app.dir.prefix(procsPath, string(name))
View
@@ -158,7 +158,7 @@ func TestSnapshotGetScale(t *testing.T) {
t.Error("expected initial scale of 0")
}
- err = Scale(app.Name, rev.Ref, pty.Name, 9, s)
+ _, _, err = Scale(app.Name, rev.Ref, pty.Name, 9, s)
if err != nil {
t.Fatal(err)
}
View
@@ -97,37 +97,39 @@ func ClaimNextPort(s Snapshot) (port int, err error) {
return
}
-func Scale(app string, revision string, processName string, factor int, s Snapshot) (err error) {
+func Scale(app string, revision string, processName string, factor int, s Snapshot) (tickets []*Instance, current int, err error) {
if factor < 0 {
- return errors.New("scaling factor needs to be a positive integer")
+ return nil, -1, errors.New("scaling factor needs to be a positive integer")
}
exists, _, err := s.conn.Exists(path.Join(appsPath, app, revsPath, revision))
if !exists || err != nil {
- return fmt.Errorf("%s@%s not found", app, revision)
+ return nil, -1, fmt.Errorf("%s@%s not found", app, revision)
}
exists, _, err = s.conn.Exists(path.Join(appsPath, app, procsPath, processName))
if !exists || err != nil {
- return fmt.Errorf("proc '%s' doesn't exist", processName)
+ return nil, -1, fmt.Errorf("proc '%s' doesn't exist", processName)
}
list, err := getInstanceIds(s, app, revision, processName)
if err != nil {
- return
+ return nil, -1, err
}
- current := len(list)
+ current = len(list)
if factor > current {
// Scale up
- tickets := factor - current
+ ntickets := factor - current
- for i := 0; i < tickets; i++ {
+ for i := 0; i < ntickets; i++ {
var ticket *Instance
ticket, err = RegisterInstance(app, revision, processName, s)
if err != nil {
return
}
+ tickets = append(tickets, ticket)
+
s = s.FastForward(ticket.Rev)
}
} else if factor < current {
View
@@ -52,15 +52,15 @@ func TestScaleErrors(t *testing.T) {
// Scale up
- err := Scale("fnord", rev.Ref, pty.Name, scale, s)
+ _, _, err := Scale("fnord", rev.Ref, pty.Name, scale, s)
if err == nil {
t.Error("expected error (bad arguments)")
}
- err = Scale(app.Name, "fnord", pty.Name, scale, s)
+ _, _, err = Scale(app.Name, "fnord", pty.Name, scale, s)
if err == nil {
t.Error("expected error (bad arguments)")
}
- err = Scale(app.Name, rev.Ref, "fnord", scale, s)
+ _, _, err = Scale(app.Name, rev.Ref, "fnord", scale, s)
if err == nil {
t.Error("expected error (bad arguments)")
}
@@ -78,7 +78,10 @@ func TestScale(t *testing.T) {
// Scale up
- err := Scale(app.Name, rev.Ref, pty.Name, scale, s)
+ _, current, err := Scale(app.Name, rev.Ref, pty.Name, scale, s)
+ if current != 0 {
+ t.Fatal("expected current scale to = 0")
+ }
if err != nil {
t.Fatal(err)
}
@@ -94,12 +97,15 @@ func TestScale(t *testing.T) {
// Scale down
- err = Scale(app.Name, rev.Ref, pty.Name, -1, s)
+ _, _, err = Scale(app.Name, rev.Ref, pty.Name, -1, s)
if err == nil {
t.Error("expected error on a non-positive scaling factor")
}
- err = Scale(app.Name, rev.Ref, pty.Name, 1, s)
+ _, current, err = Scale(app.Name, rev.Ref, pty.Name, 1, s)
+ if current != 5 {
+ t.Fatal("expected current scale to = 5")
+ }
if err != nil {
t.Fatal(err)
}
@@ -110,7 +116,7 @@ func TestScale(t *testing.T) {
t.Fatalf("expected %d instances, got %d", scale, scale1)
}
- err = Scale(app.Name, rev.Ref, pty.Name, 0, s)
+ _, _, err = Scale(app.Name, rev.Ref, pty.Name, 0, s)
if err != nil {
t.Fatal(err)
}

0 comments on commit 5596903

Please sign in to comment.