[ARVADOS] updated: de03c92e6887f1ba9fa05d39e2ce1e5bd9354966
Git user
git at public.curoverse.com
Fri May 20 14:24:26 EDT 2016
Summary of changes:
services/keep-balance/balance.go | 50 ++++++++++++++++++++++
.../{main_test.go => balance_run_test.go} | 32 +++++++-------
services/keep-balance/integration_test.go | 2 +-
services/keep-balance/main.go | 48 +--------------------
4 files changed, 70 insertions(+), 62 deletions(-)
rename services/keep-balance/{main_test.go => balance_run_test.go} (92%)
via de03c92e6887f1ba9fa05d39e2ce1e5bd9354966 (commit)
from d836ae6711f710c05980d606ee5bd017587ecbc1 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
commit de03c92e6887f1ba9fa05d39e2ce1e5bd9354966
Author: Tom Clegg <tom at curoverse.com>
Date: Fri May 20 14:23:22 2016 -0400
9162: Move Run to (*Balancer)Run
diff --git a/services/keep-balance/balance.go b/services/keep-balance/balance.go
index 95b260f..ffe24f7 100644
--- a/services/keep-balance/balance.go
+++ b/services/keep-balance/balance.go
@@ -4,6 +4,7 @@ import (
"fmt"
"log"
"runtime"
+ "os"
"sync"
"time"
@@ -34,6 +35,55 @@ type Balancer struct {
mutex sync.Mutex
}
+// Run performs a balance operation using the given config and
+// runOptions. It should only be called once on a given Balancer
+// object. Typical usage:
+//
+// err = (&Balancer{}).Run(config, runOptions)
+func (bal *Balancer) Run(config Config, runOptions RunOptions) (err error) {
+ bal.Dumper = runOptions.Dumper
+ bal.Logger = runOptions.Logger
+ if bal.Logger == nil {
+ bal.Logger = log.New(os.Stderr, "", log.LstdFlags)
+ }
+
+ defer timeMe(bal.Logger, "Run")()
+
+ if len(config.KeepServiceList.Items) > 0 && config.KeepServiceTypes != nil {
+ err = fmt.Errorf("cannot specify both KeepServiceList and KeepServiceTypes in config")
+ } else if len(config.KeepServiceList.Items) > 0 {
+ err = bal.SetKeepServices(config.KeepServiceList)
+ } else {
+ err = bal.DiscoverKeepServices(&config.Client, config.KeepServiceTypes)
+ }
+ if err != nil {
+ return
+ }
+
+ if err = bal.CheckSanityEarly(&config.Client); err != nil {
+ return
+ }
+ if err = bal.GetCurrentState(&config.Client); err != nil {
+ return
+ }
+ bal.ComputeChangeSets()
+ bal.PrintStatistics()
+ if err = bal.CheckSanityLate(); err != nil {
+ return
+ }
+ if runOptions.CommitPulls {
+ err = bal.CommitPulls(&config.Client)
+ if err != nil {
+ // Skip trash if we can't pull. (Too cautious?)
+ return
+ }
+ }
+ if runOptions.CommitTrash {
+ err = bal.CommitTrash(&config.Client)
+ }
+ return
+}
+
func (bal *Balancer) SetKeepServices(srvList arvados.KeepServiceList) error {
bal.KeepServices = make(map[string]*KeepService)
for _, srv := range srvList.Items {
diff --git a/services/keep-balance/main_test.go b/services/keep-balance/balance_run_test.go
similarity index 92%
rename from services/keep-balance/main_test.go
rename to services/keep-balance/balance_run_test.go
index 99a3b4f..6e9b872 100644
--- a/services/keep-balance/main_test.go
+++ b/services/keep-balance/balance_run_test.go
@@ -16,7 +16,7 @@ import (
check "gopkg.in/check.v1"
)
-var _ = check.Suite(&mainSuite{})
+var _ = check.Suite(&runSuite{})
type reqTracker struct {
reqs []http.Request
@@ -163,13 +163,13 @@ func (s *stubServer) serveKeepstorePull() *reqTracker {
return s.serveStatic("/pull", `{}`)
}
-type mainSuite struct {
+type runSuite struct {
stub stubServer
config Config
}
// make a log.Logger that writes to the current test's c.Log().
-func (s *mainSuite) logger(c *check.C) *log.Logger {
+func (s *runSuite) logger(c *check.C) *log.Logger {
r, w := io.Pipe()
go func() {
buf := make([]byte, 10000)
@@ -189,7 +189,7 @@ func (s *mainSuite) logger(c *check.C) *log.Logger {
return log.New(w, "", log.LstdFlags)
}
-func (s *mainSuite) SetUpTest(c *check.C) {
+func (s *runSuite) SetUpTest(c *check.C) {
s.config = Config{
Client: arvados.Client{
AuthToken: "xyzzy",
@@ -200,11 +200,11 @@ func (s *mainSuite) SetUpTest(c *check.C) {
s.stub.logf = c.Logf
}
-func (s *mainSuite) TearDownTest(c *check.C) {
+func (s *runSuite) TearDownTest(c *check.C) {
s.stub.Close()
}
-func (s *mainSuite) TestRefuseZeroCollections(c *check.C) {
+func (s *runSuite) TestRefuseZeroCollections(c *check.C) {
opts := RunOptions{
CommitPulls: true,
CommitTrash: true,
@@ -215,12 +215,12 @@ func (s *mainSuite) TestRefuseZeroCollections(c *check.C) {
s.stub.serveFourDiskKeepServices()
s.stub.serveKeepstoreIndexFoo4Bar1()
trashReqs := s.stub.serveKeepstoreTrash()
- _, err := Run(s.config, opts)
+ err := (&Balancer{}).Run(s.config, opts)
c.Check(err, check.ErrorMatches, "received zero collections")
c.Check(trashReqs.Count(), check.Equals, 0)
}
-func (s *mainSuite) TestServiceTypes(c *check.C) {
+func (s *runSuite) TestServiceTypes(c *check.C) {
opts := RunOptions{
CommitPulls: true,
CommitTrash: true,
@@ -232,13 +232,13 @@ func (s *mainSuite) TestServiceTypes(c *check.C) {
s.stub.serveFourDiskKeepServices()
indexReqs := s.stub.serveKeepstoreIndexFoo4Bar1()
trashReqs := s.stub.serveKeepstoreTrash()
- _, err := Run(s.config, opts)
+ err := (&Balancer{}).Run(s.config, opts)
c.Check(err, check.ErrorMatches, "received zero collections")
c.Check(indexReqs.Count(), check.Equals, 0)
c.Check(trashReqs.Count(), check.Equals, 0)
}
-func (s *mainSuite) TestRefuseNonAdmin(c *check.C) {
+func (s *runSuite) TestRefuseNonAdmin(c *check.C) {
opts := RunOptions{
CommitPulls: true,
CommitTrash: true,
@@ -249,13 +249,13 @@ func (s *mainSuite) TestRefuseNonAdmin(c *check.C) {
s.stub.serveFourDiskKeepServices()
trashReqs := s.stub.serveKeepstoreTrash()
pullReqs := s.stub.serveKeepstorePull()
- _, err := Run(s.config, opts)
+ err := (&Balancer{}).Run(s.config, opts)
c.Check(err, check.ErrorMatches, "Current user .* is not .* admin user")
c.Check(trashReqs.Count(), check.Equals, 0)
c.Check(pullReqs.Count(), check.Equals, 0)
}
-func (s *mainSuite) TestDryRun(c *check.C) {
+func (s *runSuite) TestDryRun(c *check.C) {
opts := RunOptions{
CommitPulls: false,
CommitTrash: false,
@@ -267,7 +267,8 @@ func (s *mainSuite) TestDryRun(c *check.C) {
s.stub.serveKeepstoreIndexFoo4Bar1()
trashReqs := s.stub.serveKeepstoreTrash()
pullReqs := s.stub.serveKeepstorePull()
- bal, err := Run(s.config, opts)
+ var bal Balancer
+ err := bal.Run(s.config, opts)
c.Check(err, check.IsNil)
c.Check(trashReqs.Count(), check.Equals, 0)
c.Check(pullReqs.Count(), check.Equals, 0)
@@ -275,7 +276,7 @@ func (s *mainSuite) TestDryRun(c *check.C) {
c.Check(stats.pulls, check.Not(check.Equals), 0)
}
-func (s *mainSuite) TestCommit(c *check.C) {
+func (s *runSuite) TestCommit(c *check.C) {
opts := RunOptions{
CommitPulls: true,
CommitTrash: true,
@@ -288,7 +289,8 @@ func (s *mainSuite) TestCommit(c *check.C) {
s.stub.serveKeepstoreIndexFoo4Bar1()
trashReqs := s.stub.serveKeepstoreTrash()
pullReqs := s.stub.serveKeepstorePull()
- bal, err := Run(s.config, opts)
+ var bal Balancer
+ err := bal.Run(s.config, opts)
c.Check(err, check.IsNil)
c.Check(trashReqs.Count(), check.Equals, 4)
c.Check(pullReqs.Count(), check.Equals, 4)
diff --git a/services/keep-balance/integration_test.go b/services/keep-balance/integration_test.go
index 349c5e4..9a5092c 100644
--- a/services/keep-balance/integration_test.go
+++ b/services/keep-balance/integration_test.go
@@ -78,7 +78,7 @@ func (s *integrationSuite) TestBalanceAPIFixtures(c *check.C) {
CommitTrash: true,
Logger: log.New(logBuf, "", log.LstdFlags),
}
- _, err := Run(s.config, opts)
+ err := (&Balancer{}).Run(s.config, opts)
c.Check(err, check.IsNil)
if iter == 0 {
c.Check(logBuf.String(), check.Matches, `(?ms).*ChangeSet{Pulls:1.*`)
diff --git a/services/keep-balance/main.go b/services/keep-balance/main.go
index 41a2567..d5f243c 100644
--- a/services/keep-balance/main.go
+++ b/services/keep-balance/main.go
@@ -86,7 +86,7 @@ func main() {
}
var err error
if *once {
- _, err = Run(config, runOptions)
+ err = (&Balancer{}).Run(config, runOptions)
} else {
err = RunForever(config, runOptions)
}
@@ -114,7 +114,7 @@ func RunForever(config Config, runOptions RunOptions) error {
log.Printf("starting in service mode, running every %v and on SIGUSR1", config.RunPeriod)
for {
- _, err := Run(config, runOptions)
+ err := (&Balancer{}).Run(config, runOptions)
if err != nil {
log.Print("run failed: ", err)
} else {
@@ -134,50 +134,6 @@ func RunForever(config Config, runOptions RunOptions) error {
}
}
-func Run(config Config, runOptions RunOptions) (bal *Balancer, err error) {
- if runOptions.Logger == nil {
- runOptions.Logger = log.New(os.Stderr, "", log.LstdFlags)
- }
- bal = &Balancer{
- Logger: runOptions.Logger,
- Dumper: runOptions.Dumper,
- }
-
- if len(config.KeepServiceList.Items) > 0 && config.KeepServiceTypes != nil {
- err = fmt.Errorf("cannot specify both KeepServiceList and KeepServiceTypes in config")
- } else if len(config.KeepServiceList.Items) > 0 {
- err = bal.SetKeepServices(config.KeepServiceList)
- } else {
- err = bal.DiscoverKeepServices(&config.Client, config.KeepServiceTypes)
- }
- if err != nil {
- return
- }
-
- if err = bal.CheckSanityEarly(&config.Client); err != nil {
- return
- }
- if err = bal.GetCurrentState(&config.Client); err != nil {
- return
- }
- bal.ComputeChangeSets()
- bal.PrintStatistics()
- if err = bal.CheckSanityLate(); err != nil {
- return
- }
- if runOptions.CommitPulls {
- err = bal.CommitPulls(&config.Client)
- if err != nil {
- // Skip trash if we can't pull. (Too cautious?)
- return
- }
- }
- if runOptions.CommitTrash {
- err = bal.CommitTrash(&config.Client)
- }
- return
-}
-
type duration time.Duration
func (d *duration) UnmarshalJSON(data []byte) error {
-----------------------------------------------------------------------
hooks/post-receive
--
More information about the arvados-commits
mailing list