17967: Use StorageClasses.*.Default instead of ["default"].
[arvados.git] / services / keep-balance / collection.go
index daedeb8bfcb82b94fbf3007fd1f1a13fe19df432..1e1e51abe7ebcd55e5d0ed7ca55eae02a8b56504 100644 (file)
@@ -163,7 +163,7 @@ func (bal *Balancer) updateCollections(ctx context.Context, c *arvados.Client, c
                        txPending := 0
                        flush := func(final bool) error {
                                err := tx.Commit()
-                               if err != nil {
+                               if err != nil && ctx.Err() == nil {
                                        tx.Rollback()
                                        return err
                                }
@@ -199,10 +199,13 @@ func (bal *Balancer) updateCollections(ctx context.Context, c *arvados.Client, c
                                        // temporarily.
                                        repl = desired
                                }
-                               classes, err := json.Marshal(coll.StorageClassesDesired)
-                               if err != nil {
-                                       bal.logf("BUG? json.Marshal(%v) failed: %s", classes, err)
-                                       continue
+                               classes := emptyJSONArray
+                               if repl > 0 {
+                                       classes, err = json.Marshal(coll.StorageClassesDesired)
+                                       if err != nil {
+                                               bal.logf("BUG? json.Marshal(%v) failed: %s", classes, err)
+                                               continue
+                                       }
                                }
                                needUpdate := coll.ReplicationConfirmed == nil || *coll.ReplicationConfirmed != repl || len(coll.StorageClassesConfirmed) != len(coll.StorageClassesDesired)
                                for i := range coll.StorageClassesDesired {
@@ -221,7 +224,7 @@ func (bal *Balancer) updateCollections(ctx context.Context, c *arvados.Client, c
                                        where uuid=$4`,
                                        repl, thresholdStr, classes, coll.UUID)
                                if err != nil {
-                                       if err != context.Canceled {
+                                       if ctx.Err() == nil {
                                                bal.logf("%s: update failed: %s", coll.UUID, err)
                                        }
                                        continue
@@ -239,8 +242,8 @@ func (bal *Balancer) updateCollections(ctx context.Context, c *arvados.Client, c
        }
        wg.Wait()
        bal.logf("updated %d collections", updated)
-       if err := <-errs; err != nil {
-               return fmt.Errorf("error updating collections: %s", err)
+       if len(errs) > 0 {
+               return fmt.Errorf("error updating collections: %s", <-errs)
        }
        return nil
 }
@@ -259,3 +262,5 @@ func goSendErr(errs chan<- error, f func() error) {
                }
        }()
 }
+
+var emptyJSONArray = []byte("[]")