1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/sdk/go/arvados"
17 // KeepService represents a keepstore server that is being rebalanced.
18 type KeepService struct {
24 // String implements fmt.Stringer.
25 func (srv *KeepService) String() string {
26 return fmt.Sprintf("%s (%s:%d, %s)", srv.UUID, srv.ServiceHost, srv.ServicePort, srv.ServiceType)
29 var ksSchemes = map[bool]string{false: "http", true: "https"}
31 // URLBase returns scheme://host:port for this server.
32 func (srv *KeepService) URLBase() string {
33 return fmt.Sprintf("%s://%s:%d", ksSchemes[srv.ServiceSSLFlag], srv.ServiceHost, srv.ServicePort)
36 // CommitPulls sends the current list of pull requests to the storage
37 // server (even if the list is empty).
38 func (srv *KeepService) CommitPulls(c *arvados.Client) error {
39 return srv.put(c, "pull", srv.ChangeSet.Pulls)
42 // CommitTrash sends the current list of trash requests to the storage
43 // server (even if the list is empty).
44 func (srv *KeepService) CommitTrash(c *arvados.Client) error {
45 return srv.put(c, "trash", srv.ChangeSet.Trashes)
48 // Perform a PUT request at path, with data (as JSON) in the request
50 func (srv *KeepService) put(c *arvados.Client, path string, data interface{}) error {
51 // We'll start a goroutine to do the JSON encoding, so we can
52 // stream it to the http client through a Pipe, rather than
53 // keeping the entire encoded version in memory.
54 jsonR, jsonW := io.Pipe()
56 // errC communicates any encoding errors back to our main
58 errC := make(chan error, 1)
61 enc := json.NewEncoder(jsonW)
62 errC <- enc.Encode(data)
66 url := srv.URLBase() + "/" + path
67 req, err := http.NewRequest("PUT", url, ioutil.NopCloser(jsonR))
69 return fmt.Errorf("building request for %s: %v", url, err)
71 err = c.DoAndDecode(nil, req)
73 // If there was an error encoding the request body, report
74 // that instead of the response: obviously we won't get a
75 // useful response if our request wasn't properly encoded.
76 if encErr := <-errC; encErr != nil {
77 return fmt.Errorf("encoding data for %s: %v", url, encErr)
83 func (srv *KeepService) discoverMounts(c *arvados.Client) error {
84 mounts, err := srv.Mounts(c)
86 return fmt.Errorf("%s: error retrieving mounts: %v", srv, err)
89 for _, m := range mounts {
90 srv.mounts = append(srv.mounts, &KeepMount{
98 type KeepMount struct {
100 KeepService *KeepService
103 // String implements fmt.Stringer.
104 func (mnt *KeepMount) String() string {
105 return fmt.Sprintf("%s (%s) on %s", mnt.UUID, mnt.DeviceID, mnt.KeepService)