本文整理汇总了Golang中github.com/tsuru/tsuru/safe.NewBuffer函数的典型用法代码示例。如果您正苦于以下问题:Golang NewBuffer函数的具体用法?Golang NewBuffer怎么用?Golang NewBuffer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewBuffer函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: removeNode
func (a *autoScaleConfig) removeNode(chosenNode *cluster.Node) error {
_, hasIaas := chosenNode.Metadata["iaas"]
if !hasIaas {
return fmt.Errorf("no IaaS information in node (%s) metadata: %#v", chosenNode.Address, chosenNode.Metadata)
}
err := a.provisioner.getCluster().Unregister(chosenNode.Address)
if err != nil {
return fmt.Errorf("unable to unregister node (%s) for removal: %s", chosenNode.Address, err)
}
buf := safe.NewBuffer(nil)
err = a.provisioner.moveContainers(urlToHost(chosenNode.Address), "", buf)
if err != nil {
a.provisioner.getCluster().Register(chosenNode.Address, chosenNode.Metadata)
return fmt.Errorf("unable to move containers from node (%s): %s - log: %s", chosenNode.Address, err, buf.String())
}
m, err := iaas.FindMachineByIdOrAddress(chosenNode.Metadata["iaas-id"], urlToHost(chosenNode.Address))
if err != nil {
a.logError("unable to find machine for removal in iaas: %s", err)
return nil
}
err = m.Destroy()
if err != nil {
a.logError("unable to destroy machine in IaaS: %s", err)
}
return nil
}
示例2: TestAppShellSpecifyUnit
func (s *S) TestAppShellSpecifyUnit(c *check.C) {
a := app.App{
Name: "someapp",
Platform: "zend",
Teams: []string{s.team.Name},
}
err := s.conn.Apps().Insert(a)
c.Assert(err, check.IsNil)
defer s.conn.Apps().Remove(bson.M{"name": a.Name})
defer s.logConn.Logs(a.Name).DropCollection()
err = s.provisioner.Provision(&a)
c.Assert(err, check.IsNil)
defer s.provisioner.Destroy(&a)
s.provisioner.AddUnits(&a, 5, "web", nil)
unit := s.provisioner.Units(&a)[3]
url := fmt.Sprintf("/shell?:app=%s&width=140&height=38&term=xterm&unit=%s", a.Name, unit.Name)
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, check.IsNil)
buf := safe.NewBuffer([]byte("echo teste"))
recorder := provisiontest.Hijacker{Conn: &provisiontest.FakeConn{Buf: buf}}
err = remoteShellHandler(&recorder, request, s.token)
c.Assert(err, check.IsNil)
shells := s.provisioner.Shells(unit.Name)
c.Assert(shells, check.HasLen, 1)
c.Assert(shells[0].App.GetName(), check.Equals, a.Name)
c.Assert(shells[0].Width, check.Equals, 140)
c.Assert(shells[0].Height, check.Equals, 38)
c.Assert(shells[0].Term, check.Equals, "xterm")
c.Assert(shells[0].Unit, check.Equals, unit.Name)
for _, u := range s.provisioner.Units(&a) {
if u.Name != unit.Name {
c.Check(s.provisioner.Shells(u.Name), check.HasLen, 0)
}
}
}
示例3: TestEnsureContainersStartedOnlyChild
func (s *S) TestEnsureContainersStartedOnlyChild(c *check.C) {
err := nodecontainer.AddNewContainer("p1", &nodecontainer.NodeContainerConfig{
Name: "c1",
Config: docker.Config{
Image: "myregistry/tsuru/bs",
},
})
c.Assert(err, check.IsNil)
p, err := dockertest.StartMultipleServersCluster()
c.Assert(err, check.IsNil)
defer p.Destroy()
clust := p.Cluster()
nodes, err := clust.Nodes()
c.Assert(err, check.IsNil)
c.Assert(nodes, check.HasLen, 2)
nodes[0].Metadata["pool"] = "p1"
_, err = p.Cluster().UpdateNode(nodes[0])
c.Assert(err, check.IsNil)
nodes[1].Metadata["pool"] = "p2"
_, err = p.Cluster().UpdateNode(nodes[1])
c.Assert(err, check.IsNil)
buf := safe.NewBuffer(nil)
err = ensureContainersStarted(p, buf, true, nil)
c.Assert(err, check.IsNil)
client, err := docker.NewClient(nodes[0].Address)
c.Assert(err, check.IsNil)
containers, err := client.ListContainers(docker.ListContainersOptions{All: true})
c.Assert(err, check.IsNil)
c.Assert(containers, check.HasLen, 1)
client2, err := docker.NewClient(nodes[1].Address)
c.Assert(err, check.IsNil)
containers2, err := client2.ListContainers(docker.ListContainersOptions{All: true})
c.Assert(err, check.IsNil)
c.Assert(containers2, check.HasLen, 0)
}
示例4: TestSSHToContainerHandler
func (s *HandlersSuite) TestSSHToContainerHandler(c *gocheck.C) {
sshServer := newMockSSHServer(c, 2e9)
defer sshServer.Shutdown()
coll := collection()
defer coll.Close()
container := container{
ID: "9930c24f1c4x",
AppName: "makea",
Type: "python",
Status: provision.StatusStarted.String(),
IP: "127.0.0.4",
HostPort: "9025",
HostAddr: "localhost",
SSHHostPort: sshServer.port,
PrivateKey: string(fakeServerPrivateKey),
User: sshUsername(),
}
err := coll.Insert(container)
c.Assert(err, gocheck.IsNil)
defer coll.RemoveAll(bson.M{"appname": "makea"})
tmpDir, err := ioutil.TempDir("", "containerssh")
defer os.RemoveAll(tmpDir)
filepath := path.Join(tmpDir, "file.txt")
file, err := os.Create(filepath)
c.Assert(err, gocheck.IsNil)
file.Write([]byte("hello"))
file.Close()
buf := safe.NewBuffer([]byte("cat " + filepath + "\nexit\n"))
recorder := hijacker{conn: &fakeConn{buf}}
request, err := http.NewRequest("GET", "/?:container_id="+container.ID, nil)
c.Assert(err, gocheck.IsNil)
err = sshToContainerHandler(&recorder, request, nil)
c.Assert(err, gocheck.IsNil)
}
示例5: SetUpTest
func (s *S) SetUpTest(c *check.C) {
config.Set("docker:api-timeout", 2)
iaas.ResetAll()
repositorytest.Reset()
queue.ResetQueue()
repository.Manager().CreateUser(s.user.Email)
s.p = &dockerProvisioner{storage: &cluster.MapStorage{}}
err := s.p.Initialize()
c.Assert(err, check.IsNil)
queue.ResetQueue()
s.server, err = dtesting.NewServer("127.0.0.1:0", nil, nil)
c.Assert(err, check.IsNil)
s.p.cluster, err = cluster.New(nil, s.p.storage,
cluster.Node{Address: s.server.URL(), Metadata: map[string]string{"pool": "test-default"}},
)
c.Assert(err, check.IsNil)
mainDockerProvisioner = s.p
err = dbtest.ClearAllCollectionsExcept(s.storage.Apps().Database, []string{"users", "tokens", "teams"})
c.Assert(err, check.IsNil)
err = clearClusterStorage(s.clusterSess)
c.Assert(err, check.IsNil)
routertest.FakeRouter.Reset()
opts := provision.AddPoolOptions{Name: "test-default", Default: true}
err = provision.AddPool(opts)
c.Assert(err, check.IsNil)
s.storage.Tokens().Remove(bson.M{"appname": bson.M{"$ne": ""}})
s.logBuf = safe.NewBuffer(nil)
log.SetLogger(log.NewWriterLogger(s.logBuf, true))
s.token = createTokenForUser(s.user, "*", string(permission.CtxGlobal), "", c)
}
示例6: TestFollowLogsAndCommitForward
func (s *S) TestFollowLogsAndCommitForward(c *check.C) {
err := s.newFakeImage(s.p, "tsuru/python", nil)
c.Assert(err, check.IsNil)
app := provisiontest.NewFakeApp("mightyapp", "python", 1)
nextImgName, err := appNewImageName(app.GetName())
c.Assert(err, check.IsNil)
cont := container.Container{AppName: "mightyapp", ID: "myid123", BuildingImage: nextImgName}
err = cont.Create(&container.CreateArgs{
App: app,
ImageID: "tsuru/python",
Commands: []string{"foo"},
Provisioner: s.p,
})
c.Assert(err, check.IsNil)
buf := safe.NewBuffer(nil)
args := runContainerActionsArgs{writer: buf, provisioner: s.p}
context := action.FWContext{Params: []interface{}{args}, Previous: cont}
imageId, err := followLogsAndCommit.Forward(context)
c.Assert(err, check.IsNil)
c.Assert(imageId, check.Equals, "tsuru/app-mightyapp:v1")
c.Assert(buf.String(), check.Not(check.Equals), "")
var dbCont container.Container
coll := s.p.Collection()
defer coll.Close()
err = coll.Find(bson.M{"id": cont.ID}).One(&dbCont)
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Equals, "not found")
_, err = s.p.Cluster().InspectContainer(cont.ID)
c.Assert(err, check.NotNil)
c.Assert(err.Error(), check.Matches, "No such container.*")
err = s.p.Cluster().RemoveImage("tsuru/app-mightyapp:v1")
c.Assert(err, check.IsNil)
}
示例7: TestBindAndHealthcheckBackward
func (s *S) TestBindAndHealthcheckBackward(c *check.C) {
appName := "my-fake-app"
err := s.newFakeImage(s.p, "tsuru/app-"+appName, nil)
c.Assert(err, check.IsNil)
fakeApp := provisiontest.NewFakeApp(appName, "python", 0)
s.p.Provision(fakeApp)
defer s.p.Destroy(fakeApp)
buf := safe.NewBuffer(nil)
args := changeUnitsPipelineArgs{
app: fakeApp,
provisioner: s.p,
writer: buf,
toAdd: map[string]*containersToAdd{"web": {Quantity: 2}},
imageId: "tsuru/app-" + appName,
}
containers, err := addContainersWithHost(&args)
c.Assert(err, check.IsNil)
c.Assert(containers, check.HasLen, 2)
context := action.BWContext{Params: []interface{}{args}, FWResult: containers}
for _, c := range containers {
u := c.AsUnit(fakeApp)
fakeApp.BindUnit(&u)
}
bindAndHealthcheck.Backward(context)
c.Assert(err, check.IsNil)
u1 := containers[0].AsUnit(fakeApp)
c.Assert(fakeApp.HasBind(&u1), check.Equals, false)
u2 := containers[1].AsUnit(fakeApp)
c.Assert(fakeApp.HasBind(&u2), check.Equals, false)
}
示例8: TestMoveContainerErrorStarted
func (s *S) TestMoveContainerErrorStarted(c *check.C) {
p, err := s.startMultipleServersCluster()
c.Assert(err, check.IsNil)
err = s.newFakeImage(p, "tsuru/app-myapp", nil)
c.Assert(err, check.IsNil)
appInstance := provisiontest.NewFakeApp("myapp", "python", 0)
p.Provision(appInstance)
imageId, err := image.AppCurrentImageName(appInstance.GetName())
c.Assert(err, check.IsNil)
addedConts, err := addContainersWithHost(&changeUnitsPipelineArgs{
toHost: "localhost",
toAdd: map[string]*containersToAdd{"web": {Quantity: 2}},
app: appInstance,
imageId: imageId,
provisioner: p,
})
c.Assert(err, check.IsNil)
err = addedConts[0].SetStatus(p, provision.StatusError, true)
c.Assert(err, check.IsNil)
appStruct := &app.App{
Name: appInstance.GetName(),
}
err = s.storage.Apps().Insert(appStruct)
c.Assert(err, check.IsNil)
buf := safe.NewBuffer(nil)
_, err = p.moveContainer(addedConts[0].ID[:6], "127.0.0.1", buf)
c.Assert(err, check.IsNil)
containers, err := p.listContainersByHost("localhost")
c.Assert(err, check.IsNil)
c.Assert(containers, check.HasLen, 1)
containers, err = p.listContainersByHost("127.0.0.1")
c.Assert(err, check.IsNil)
c.Assert(containers, check.HasLen, 1)
c.Assert(containers[0].Status, check.Equals, provision.StatusStarting.String())
}
示例9: TestFollowLogsAndCommitForwardWaitFailure
func (s *S) TestFollowLogsAndCommitForwardWaitFailure(c *check.C) {
s.server.PrepareFailure("failed to wait for the container", "/containers/.*/wait")
defer s.server.ResetFailure("failed to wait for the container")
err := s.newFakeImage(s.p, "tsuru/python", nil)
c.Assert(err, check.IsNil)
app := provisiontest.NewFakeApp("myapp", "python", 1)
cont := container.Container{AppName: "mightyapp"}
err = cont.Create(&container.CreateArgs{
App: app,
ImageID: "tsuru/python",
Commands: []string{"foo"},
Provisioner: s.p,
})
c.Assert(err, check.IsNil)
err = cont.Start(&container.StartArgs{
Provisioner: s.p,
App: app,
})
c.Assert(err, check.IsNil)
err = cont.Stop(s.p)
c.Assert(err, check.IsNil)
buf := safe.NewBuffer(nil)
args := runContainerActionsArgs{writer: buf, provisioner: s.p}
context := action.FWContext{Params: []interface{}{args}, Previous: cont}
imageId, err := followLogsAndCommit.Forward(context)
c.Assert(err, check.ErrorMatches, `.*failed to wait for the container\n$`)
c.Assert(imageId, check.IsNil)
}
示例10: TestRebalanceContainers
func (s *S) TestRebalanceContainers(c *check.C) {
p, err := s.startMultipleServersCluster()
c.Assert(err, check.IsNil)
err = s.newFakeImage(p, "tsuru/app-myapp", nil)
c.Assert(err, check.IsNil)
appInstance := provisiontest.NewFakeApp("myapp", "python", 0)
defer p.Destroy(appInstance)
p.Provision(appInstance)
imageId, err := appCurrentImageName(appInstance.GetName())
c.Assert(err, check.IsNil)
_, err = addContainersWithHost(&changeUnitsPipelineArgs{
toHost: "localhost",
toAdd: map[string]*containersToAdd{"web": {Quantity: 5}},
app: appInstance,
imageId: imageId,
provisioner: p,
})
c.Assert(err, check.IsNil)
appStruct := &app.App{
Name: appInstance.GetName(),
}
err = s.storage.Apps().Insert(appStruct)
c.Assert(err, check.IsNil)
buf := safe.NewBuffer(nil)
err = p.rebalanceContainers(buf, false)
c.Assert(err, check.IsNil)
c1, err := p.listContainersByHost("localhost")
c.Assert(err, check.IsNil)
c2, err := p.listContainersByHost("127.0.0.1")
c.Assert(err, check.IsNil)
c.Assert((len(c1) == 3 && len(c2) == 2) || (len(c1) == 2 && len(c2) == 3), check.Equals, true)
}
示例11: TestEnsureContainersStartedForceStopOnlyOnFailure
func (s *S) TestEnsureContainersStartedForceStopOnlyOnFailure(c *check.C) {
config.Set("docker:bs:image", "myregistry/tsuru/bs")
_, err := nodecontainer.InitializeBS()
c.Assert(err, check.IsNil)
var reqs []*http.Request
server, err := testing.NewServer("127.0.0.1:0", nil, func(r *http.Request) {
reqs = append(reqs, r)
})
c.Assert(err, check.IsNil)
defer server.Stop()
errCount := 0
server.CustomHandler("/containers/big-sibling", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqs = append(reqs, r)
if r.Method != "DELETE" {
server.DefaultHandler().ServeHTTP(w, r)
return
}
errCount++
if errCount > 2 {
server.DefaultHandler().ServeHTTP(w, r)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}))
p, err := dockertest.NewFakeDockerProvisioner(server.URL())
c.Assert(err, check.IsNil)
defer p.Destroy()
client, err := docker.NewClient(server.URL())
c.Assert(err, check.IsNil)
buf := safe.NewBuffer(nil)
err = ensureContainersStarted(p, buf, true, nil)
c.Assert(err, check.IsNil)
reqs = nil
err = ensureContainersStarted(p, buf, true, nil)
c.Assert(err, check.IsNil)
var paths []string
for _, r := range reqs {
paths = append(paths, r.Method+" "+r.URL.Path)
}
c.Assert(paths, check.DeepEquals, []string{
"POST /images/create",
"POST /containers/create",
"POST /containers/big-sibling/stop",
"DELETE /containers/big-sibling",
"DELETE /containers/big-sibling",
"DELETE /containers/big-sibling",
"POST /containers/create",
"GET /version",
"POST /containers/big-sibling/start",
})
c.Assert(reqs[3].URL.Query().Get("force"), check.Equals, "")
c.Assert(reqs[4].URL.Query().Get("force"), check.Equals, "1")
c.Assert(reqs[5].URL.Query().Get("force"), check.Equals, "1")
containers, err := client.ListContainers(docker.ListContainersOptions{All: true})
c.Assert(err, check.IsNil)
c.Assert(containers, check.HasLen, 1)
container, err := client.InspectContainer(containers[0].ID)
c.Assert(err, check.IsNil)
c.Assert(container.Name, check.Equals, nodecontainer.BsDefaultName)
}
示例12: TestRebalanceContainersManyApps
func (s *S) TestRebalanceContainersManyApps(c *check.C) {
p, err := s.startMultipleServersCluster()
c.Assert(err, check.IsNil)
err = s.newFakeImage(p, "tsuru/app-myapp", nil)
c.Assert(err, check.IsNil)
err = s.newFakeImage(p, "tsuru/app-otherapp", nil)
c.Assert(err, check.IsNil)
appInstance := provisiontest.NewFakeApp("myapp", "python", 0)
defer p.Destroy(appInstance)
p.Provision(appInstance)
appInstance2 := provisiontest.NewFakeApp("otherapp", "python", 0)
defer p.Destroy(appInstance2)
p.Provision(appInstance2)
imageId, err := image.AppCurrentImageName(appInstance.GetName())
c.Assert(err, check.IsNil)
_, err = addContainersWithHost(&changeUnitsPipelineArgs{
toHost: "localhost",
toAdd: map[string]*containersToAdd{"web": {Quantity: 1}},
app: appInstance,
imageId: imageId,
provisioner: p,
})
c.Assert(err, check.IsNil)
imageId2, err := image.AppCurrentImageName(appInstance2.GetName())
c.Assert(err, check.IsNil)
_, err = addContainersWithHost(&changeUnitsPipelineArgs{
toHost: "localhost",
toAdd: map[string]*containersToAdd{"web": {Quantity: 1}},
app: appInstance2,
imageId: imageId2,
provisioner: p,
})
c.Assert(err, check.IsNil)
appStruct := &app.App{
Name: appInstance.GetName(),
Pool: "test-default",
}
err = s.storage.Apps().Insert(appStruct)
c.Assert(err, check.IsNil)
appStruct2 := &app.App{
Name: appInstance2.GetName(),
Pool: "test-default",
}
err = s.storage.Apps().Insert(appStruct2)
c.Assert(err, check.IsNil)
buf := safe.NewBuffer(nil)
c1, err := p.listContainersByHost("localhost")
c.Assert(err, check.IsNil)
c.Assert(c1, check.HasLen, 2)
err = p.rebalanceContainers(buf, false)
c.Assert(err, check.IsNil)
c1, err = p.listContainersByHost("localhost")
c.Assert(err, check.IsNil)
c.Assert(c1, check.HasLen, 1)
c2, err := p.listContainersByHost("127.0.0.1")
c.Assert(err, check.IsNil)
c.Assert(c2, check.HasLen, 1)
}
示例13: TestBindAndHealthcheckDontHealtcheckForErroredApps
func (s *S) TestBindAndHealthcheckDontHealtcheckForErroredApps(c *check.C) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
conn, err := db.Conn()
c.Assert(err, check.IsNil)
defer conn.Close()
dbApp := &app.App{Name: "myapp"}
err = conn.Apps().Insert(dbApp)
c.Assert(err, check.IsNil)
defer conn.Apps().Remove(bson.M{"name": dbApp.Name})
imageName := "tsuru/app-" + dbApp.Name
customData := map[string]interface{}{
"healthcheck": map[string]interface{}{
"path": "/x/y",
"status": http.StatusOK,
},
"procfile": "web: python myapp.py",
}
err = s.newFakeImage(s.p, imageName, customData)
c.Assert(err, check.IsNil)
fakeApp := provisiontest.NewFakeApp(dbApp.Name, "python", 0)
s.p.Provision(fakeApp)
defer s.p.Destroy(fakeApp)
buf := safe.NewBuffer(nil)
contOpts := newContainerOpts{
Status: "error",
}
oldContainer, err := s.newContainer(&contOpts, nil)
c.Assert(err, check.IsNil)
args := changeUnitsPipelineArgs{
app: fakeApp,
provisioner: s.p,
writer: buf,
toAdd: map[string]*containersToAdd{"web": {Quantity: 2}},
imageId: "tsuru/app-" + dbApp.Name,
toRemove: []container{*oldContainer},
}
containers, err := addContainersWithHost(&args)
c.Assert(err, check.IsNil)
c.Assert(containers, check.HasLen, 2)
url, _ := url.Parse(server.URL)
host, port, _ := net.SplitHostPort(url.Host)
containers[0].HostAddr = host
containers[0].HostPort = port
containers[1].HostAddr = host
containers[1].HostPort = port
context := action.FWContext{Params: []interface{}{args}, Previous: containers}
result, err := bindAndHealthcheck.Forward(context)
c.Assert(err, check.IsNil)
resultContainers := result.([]container)
c.Assert(resultContainers, check.DeepEquals, containers)
u1 := containers[0].asUnit(fakeApp)
u2 := containers[1].asUnit(fakeApp)
c.Assert(fakeApp.HasBind(&u1), check.Equals, true)
c.Assert(fakeApp.HasBind(&u2), check.Equals, true)
}
示例14: TestRebalanceContainersByHost
func (s *S) TestRebalanceContainersByHost(c *check.C) {
otherServer, err := dtesting.NewServer("localhost:0", nil, nil)
c.Assert(err, check.IsNil)
defer otherServer.Stop()
otherUrl := strings.Replace(otherServer.URL(), "127.0.0.1", "localhost", 1)
p := &dockerProvisioner{}
err = p.Initialize()
c.Assert(err, check.IsNil)
p.storage = &cluster.MapStorage{}
p.scheduler = &segregatedScheduler{provisioner: p}
p.cluster, err = cluster.New(p.scheduler, p.storage,
cluster.Node{Address: s.server.URL(), Metadata: map[string]string{"pool": "pool1"}},
cluster.Node{Address: otherUrl, Metadata: map[string]string{"pool": "pool1"}},
)
c.Assert(err, check.IsNil)
opts := provision.AddPoolOptions{Name: "pool1"}
err = provision.AddPool(opts)
c.Assert(err, check.IsNil)
err = provision.AddTeamsToPool("pool1", []string{"team1"})
c.Assert(err, check.IsNil)
err = s.newFakeImage(p, "tsuru/app-myapp", nil)
c.Assert(err, check.IsNil)
appInstance := provisiontest.NewFakeApp("myapp", "python", 0)
defer p.Destroy(appInstance)
p.Provision(appInstance)
imageId, err := appCurrentImageName(appInstance.GetName())
c.Assert(err, check.IsNil)
_, err = addContainersWithHost(&changeUnitsPipelineArgs{
toHost: "localhost",
toAdd: map[string]*containersToAdd{"web": {Quantity: 5}},
app: appInstance,
imageId: imageId,
provisioner: p,
})
c.Assert(err, check.IsNil)
appStruct := &app.App{
Name: appInstance.GetName(),
TeamOwner: "team1",
Pool: "pool1",
}
err = s.storage.Apps().Insert(appStruct)
c.Assert(err, check.IsNil)
c1, err := p.listContainersByHost("localhost")
c.Assert(err, check.IsNil)
c.Assert(c1, check.HasLen, 5)
c2, err := p.listContainersByHost("127.0.0.1")
c.Assert(err, check.IsNil)
c.Assert(c2, check.HasLen, 0)
err = p.Cluster().Unregister(otherUrl)
c.Assert(err, check.IsNil)
buf := safe.NewBuffer(nil)
err = p.rebalanceContainersByHost(net.URLToHost(otherUrl), buf)
c.Assert(err, check.IsNil)
c.Assert(p.scheduler.ignoredContainers, check.IsNil)
c2, err = p.listContainersByHost("127.0.0.1")
c.Assert(err, check.IsNil)
c.Assert(c2, check.HasLen, 5)
}
示例15: rebalanceIfNeeded
func (a *autoScaleConfig) rebalanceIfNeeded(event *autoScaleEvent, groupMetadata string, nodes []*cluster.Node) error {
if a.preventRebalance {
return nil
}
var rebalanceFilter map[string]string
if a.groupByMetadata != "" {
rebalanceFilter = map[string]string{a.groupByMetadata: groupMetadata}
}
if event.Action == "" {
// No action yet, check if we need rebalance
_, gap, err := a.provisioner.containerGapInNodes(nodes)
buf := safe.NewBuffer(nil)
dryProvisioner, err := a.provisioner.rebalanceContainersByFilter(buf, nil, rebalanceFilter, true)
if err != nil {
return fmt.Errorf("unable to run dry rebalance to check if rebalance is needed: %s - log: %s", err, buf.String())
}
if dryProvisioner == nil {
return nil
}
_, gapAfter, err := dryProvisioner.containerGapInNodes(nodes)
if err != nil {
return fmt.Errorf("couldn't find containers from rebalanced nodes: %s", err)
}
if math.Abs((float64)(gap-gapAfter)) > 2.0 {
err = event.update(scaleActionRebalance, fmt.Sprintf("gap is %d, after rebalance gap will be %d", gap, gapAfter))
if err != nil {
return fmt.Errorf("unable to update event: %s", err)
}
}
}
if event.Action != "" && event.Action != scaleActionRemove {
a.logDebug("running rebalance, due to %q for %q: %s", event.Action, event.MetadataValue, event.Reason)
buf := safe.NewBuffer(nil)
var writer io.Writer = buf
if a.writer != nil {
writer = io.MultiWriter(buf, a.writer)
}
_, err := a.provisioner.rebalanceContainersByFilter(writer, nil, rebalanceFilter, false)
if err != nil {
return fmt.Errorf("unable to rebalance containers: %s - log: %s", err.Error(), buf.String())
}
return nil
}
return nil
}