1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
18 // KeepService is an arvados#keepService record
19 type KeepService struct {
20 UUID string `json:"uuid"`
21 ServiceHost string `json:"service_host"`
22 ServicePort int `json:"service_port"`
23 ServiceSSLFlag bool `json:"service_ssl_flag"`
24 ServiceType string `json:"service_type"`
25 ReadOnly bool `json:"read_only"`
26 CreatedAt time.Time `json:"created_at"`
27 ModifiedAt time.Time `json:"modified_at"`
30 type KeepMount struct {
31 UUID string `json:"uuid"`
32 DeviceID string `json:"device_id"`
33 ReadOnly bool `json:"read_only"`
34 Replication int `json:"replication"`
35 StorageClasses map[string]bool `json:"storage_classes"`
38 // KeepServiceList is an arvados#keepServiceList record
39 type KeepServiceList struct {
40 Items []KeepService `json:"items"`
41 ItemsAvailable int `json:"items_available"`
42 Offset int `json:"offset"`
43 Limit int `json:"limit"`
46 // KeepServiceIndexEntry is what a keep service's index response tells
47 // us about a stored block.
48 type KeepServiceIndexEntry struct {
50 // Time of last write, in nanoseconds since Unix epoch
54 // EachKeepService calls f once for every readable
55 // KeepService. EachKeepService stops if it encounters an
56 // error, such as f returning a non-nil error.
57 func (c *Client) EachKeepService(f func(KeepService) error) error {
58 params := ResourceListParams{}
60 var page KeepServiceList
61 err := c.RequestAndDecode(&page, "GET", "arvados/v1/keep_services", nil, params)
65 for _, item := range page.Items {
71 params.Offset = params.Offset + len(page.Items)
72 if params.Offset >= page.ItemsAvailable {
78 func (s *KeepService) url(path string) string {
81 f = "https://%s:%d/%s"
85 return fmt.Sprintf(f, s.ServiceHost, s.ServicePort, path)
88 // String implements fmt.Stringer
89 func (s *KeepService) String() string {
93 func (s *KeepService) Mounts(c *Client) ([]KeepMount, error) {
94 url := s.url("mounts")
95 req, err := http.NewRequest("GET", url, nil)
99 var mounts []KeepMount
100 err = c.DoAndDecode(&mounts, req)
102 return nil, fmt.Errorf("GET %v: %v", url, err)
107 // Touch updates the timestamp on the given block.
108 func (s *KeepService) Touch(ctx context.Context, c *Client, blk string) error {
109 req, err := http.NewRequest("TOUCH", s.url(blk), nil)
113 resp, err := c.Do(req.WithContext(ctx))
117 defer resp.Body.Close()
118 if resp.StatusCode != http.StatusOK {
119 body, _ := ioutil.ReadAll(resp.Body)
120 return fmt.Errorf("%s %s: %s", resp.Proto, resp.Status, body)
125 // Untrash moves/copies the given block out of trash.
126 func (s *KeepService) Untrash(ctx context.Context, c *Client, blk string) error {
127 req, err := http.NewRequest("PUT", s.url("untrash/"+blk), nil)
131 resp, err := c.Do(req.WithContext(ctx))
135 defer resp.Body.Close()
136 if resp.StatusCode != http.StatusOK {
137 body, _ := ioutil.ReadAll(resp.Body)
138 return fmt.Errorf("%s %s: %s", resp.Proto, resp.Status, body)
143 // IndexMount returns an unsorted list of blocks at the given mount point.
144 func (s *KeepService) IndexMount(ctx context.Context, c *Client, mountUUID string, prefix string) ([]KeepServiceIndexEntry, error) {
145 return s.index(ctx, c, s.url("mounts/"+mountUUID+"/blocks?prefix="+prefix))
148 // Index returns an unsorted list of blocks that can be retrieved from
150 func (s *KeepService) Index(ctx context.Context, c *Client, prefix string) ([]KeepServiceIndexEntry, error) {
151 return s.index(ctx, c, s.url("index/"+prefix))
154 func (s *KeepService) index(ctx context.Context, c *Client, url string) ([]KeepServiceIndexEntry, error) {
155 req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
157 return nil, fmt.Errorf("NewRequestWithContext(%v): %v", url, err)
159 resp, err := c.Do(req)
161 return nil, fmt.Errorf("Do(%v): %v", url, err)
162 } else if resp.StatusCode != 200 {
163 return nil, fmt.Errorf("%v: %d %v", url, resp.StatusCode, resp.Status)
165 defer resp.Body.Close()
167 var entries []KeepServiceIndexEntry
168 scanner := bufio.NewScanner(resp.Body)
171 if scanner.Err() != nil {
172 // If we encounter a read error (timeout,
173 // connection failure), stop now and return it
174 // below, so it doesn't get masked by the
175 // ensuing "badly formatted response" error.
179 return nil, fmt.Errorf("Index response contained non-terminal blank line")
181 line := scanner.Text()
186 fields := strings.Split(line, " ")
187 if len(fields) != 2 {
188 return nil, fmt.Errorf("Malformed index line %q: %d fields", line, len(fields))
190 mtime, err := strconv.ParseInt(fields[1], 10, 64)
192 return nil, fmt.Errorf("Malformed index line %q: mtime: %v", line, err)
195 // An old version of keepstore is giving us
196 // timestamps in seconds instead of
197 // nanoseconds. (This threshold correctly
198 // handles all times between 1970-01-02 and
202 entries = append(entries, KeepServiceIndexEntry{
203 SizedDigest: SizedDigest(fields[0]),
207 if err := scanner.Err(); err != nil {
208 return nil, fmt.Errorf("Error scanning index response: %v", err)
211 return nil, fmt.Errorf("Index response had no EOF marker")