1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 // KeepService represents a keepstore server that is being rebalanced.
18 type KeepService struct {
23 // String implements fmt.Stringer.
24 func (srv *KeepService) String() string {
25 return fmt.Sprintf("%s (%s:%d, %s)", srv.UUID, srv.ServiceHost, srv.ServicePort, srv.ServiceType)
28 var ksSchemes = map[bool]string{false: "http", true: "https"}
30 // URLBase returns scheme://host:port for this server.
31 func (srv *KeepService) URLBase() string {
32 return fmt.Sprintf("%s://%s:%d", ksSchemes[srv.ServiceSSLFlag], srv.ServiceHost, srv.ServicePort)
35 // CommitPulls sends the current list of pull requests to the storage
36 // server (even if the list is empty).
37 func (srv *KeepService) CommitPulls(c *arvados.Client) error {
38 return srv.put(c, "pull", srv.ChangeSet.Pulls)
41 // CommitTrash sends the current list of trash requests to the storage
42 // server (even if the list is empty).
43 func (srv *KeepService) CommitTrash(c *arvados.Client) error {
44 return srv.put(c, "trash", srv.ChangeSet.Trashes)
47 // Perform a PUT request at path, with data (as JSON) in the request
49 func (srv *KeepService) put(c *arvados.Client, path string, data interface{}) error {
50 // We'll start a goroutine to do the JSON encoding, so we can
51 // stream it to the http client through a Pipe, rather than
52 // keeping the entire encoded version in memory.
53 jsonR, jsonW := io.Pipe()
55 // errC communicates any encoding errors back to our main
57 errC := make(chan error, 1)
60 enc := json.NewEncoder(jsonW)
61 errC <- enc.Encode(data)
65 url := srv.URLBase() + "/" + path
66 req, err := http.NewRequest("PUT", url, ioutil.NopCloser(jsonR))
68 return fmt.Errorf("building request for %s: %v", url, err)
70 err = c.DoAndDecode(nil, req)
72 // If there was an error encoding the request body, report
73 // that instead of the response: obviously we won't get a
74 // useful response if our request wasn't properly encoded.
75 if encErr := <-errC; encErr != nil {
76 return fmt.Errorf("encoding data for %s: %v", url, encErr)