Merge branch '21383-misc-fixes'. Refs #21383
[arvados.git] / services / keep-balance / keep_service.go
index f65355d0d5f885f10ea463d6ebc949f0d35ed64b..f50fbfd946775e86fa667de2a89eddb1e728acc4 100644 (file)
@@ -1,18 +1,24 @@
-package main
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+package keepbalance
 
 import (
+       "context"
        "encoding/json"
        "fmt"
        "io"
        "io/ioutil"
        "net/http"
 
-       "git.curoverse.com/arvados.git/sdk/go/arvados"
+       "git.arvados.org/arvados.git/sdk/go/arvados"
 )
 
 // KeepService represents a keepstore server that is being rebalanced.
 type KeepService struct {
        arvados.KeepService
+       mounts []*KeepMount
        *ChangeSet
 }
 
@@ -30,19 +36,19 @@ func (srv *KeepService) URLBase() string {
 
 // CommitPulls sends the current list of pull requests to the storage
 // server (even if the list is empty).
-func (srv *KeepService) CommitPulls(c *arvados.Client) error {
-       return srv.put(c, "pull", srv.ChangeSet.Pulls)
+func (srv *KeepService) CommitPulls(ctx context.Context, c *arvados.Client) error {
+       return srv.put(ctx, c, "pull", srv.ChangeSet.Pulls)
 }
 
 // CommitTrash sends the current list of trash requests to the storage
 // server (even if the list is empty).
-func (srv *KeepService) CommitTrash(c *arvados.Client) error {
-       return srv.put(c, "trash", srv.ChangeSet.Trashes)
+func (srv *KeepService) CommitTrash(ctx context.Context, c *arvados.Client) error {
+       return srv.put(ctx, c, "trash", srv.ChangeSet.Trashes)
 }
 
 // Perform a PUT request at path, with data (as JSON) in the request
 // body.
-func (srv *KeepService) put(c *arvados.Client, path string, data interface{}) error {
+func (srv *KeepService) put(ctx context.Context, c *arvados.Client, path string, data interface{}) error {
        // We'll start a goroutine to do the JSON encoding, so we can
        // stream it to the http client through a Pipe, rather than
        // keeping the entire encoded version in memory.
@@ -59,7 +65,7 @@ func (srv *KeepService) put(c *arvados.Client, path string, data interface{}) er
        }()
 
        url := srv.URLBase() + "/" + path
-       req, err := http.NewRequest("PUT", url, ioutil.NopCloser(jsonR))
+       req, err := http.NewRequestWithContext(ctx, "PUT", url, ioutil.NopCloser(jsonR))
        if err != nil {
                return fmt.Errorf("building request for %s: %v", url, err)
        }
@@ -74,3 +80,28 @@ func (srv *KeepService) put(c *arvados.Client, path string, data interface{}) er
 
        return err
 }
+
+func (srv *KeepService) discoverMounts(c *arvados.Client) error {
+       mounts, err := srv.Mounts(c)
+       if err != nil {
+               return fmt.Errorf("%s: error retrieving mounts: %v", srv, err)
+       }
+       srv.mounts = nil
+       for _, m := range mounts {
+               srv.mounts = append(srv.mounts, &KeepMount{
+                       KeepMount:   m,
+                       KeepService: srv,
+               })
+       }
+       return nil
+}
+
+type KeepMount struct {
+       arvados.KeepMount
+       KeepService *KeepService
+}
+
+// String implements fmt.Stringer.
+func (mnt *KeepMount) String() string {
+       return fmt.Sprintf("%s (%s) on %s", mnt.UUID, mnt.DeviceID, mnt.KeepService)
+}