fixes formatting on collection.go
[arvados.git] / services / keep-balance / collection.go
1 package main
2
3 import (
4         "fmt"
5         "time"
6
7         "git.curoverse.com/arvados.git/sdk/go/arvados"
8 )
9
10 func countCollections(c *arvados.Client, params arvados.ResourceListParams) (int, error) {
11         var page arvados.CollectionList
12         var zero int
13         params.Limit = &zero
14         params.Count = true
15         err := c.RequestAndDecode(&page, "GET", "arvados/v1/collections", nil, params)
16         return page.ItemsAvailable, err
17 }
18
19 // EachCollection calls f once for every readable
20 // collection. EachCollection stops if it encounters an error, such as
21 // f returning a non-nil error.
22 //
23 // The progress function is called periodically with done (number of
24 // times f has been called) and total (number of times f is expected
25 // to be called).
26 //
27 // If pageSize > 0 it is used as the maximum page size in each API
28 // call; otherwise the maximum allowed page size is requested.
29 func EachCollection(c *arvados.Client, pageSize int, f func(arvados.Collection) error, progress func(done, total int)) error {
30         if progress == nil {
31                 progress = func(_, _ int) {}
32         }
33
34         expectCount, err := countCollections(c, arvados.ResourceListParams{})
35         if err != nil {
36                 return err
37         }
38
39         limit := pageSize
40         if limit <= 0 {
41                 // Use the maximum page size the server allows
42                 limit = 1<<31 - 1
43         }
44         params := arvados.ResourceListParams{
45                 Limit:  &limit,
46                 Order:  "modified_at, uuid",
47                 Count:  false,
48                 Select: []string{"uuid", "manifest_text", "modified_at", "portable_data_hash", "replication_desired"},
49         }
50         var last arvados.Collection
51         var filterTime time.Time
52         callCount := 0
53         for {
54                 progress(callCount, expectCount)
55                 var page arvados.CollectionList
56                 err := c.RequestAndDecode(&page, "GET", "arvados/v1/collections", nil, params)
57                 if err != nil {
58                         return err
59                 }
60                 for _, coll := range page.Items {
61                         if last.ModifiedAt != nil && *last.ModifiedAt == *coll.ModifiedAt && last.UUID >= coll.UUID {
62                                 continue
63                         }
64                         callCount++
65                         err = f(coll)
66                         if err != nil {
67                                 return err
68                         }
69                         last = coll
70                 }
71                 if last.ModifiedAt == nil || *last.ModifiedAt == filterTime {
72                         if page.ItemsAvailable > len(page.Items) {
73                                 // TODO: use "mtime=X && UUID>Y"
74                                 // filters to get all collections with
75                                 // this timestamp, then use "mtime>X"
76                                 // to get the next timestamp.
77                                 return fmt.Errorf("BUG: Received an entire page with the same modified_at timestamp (%v), cannot make progress", filterTime)
78                         }
79                         break
80                 }
81                 filterTime = *last.ModifiedAt
82                 params.Filters = []arvados.Filter{{
83                         Attr:     "modified_at",
84                         Operator: ">=",
85                         Operand:  filterTime,
86                 }, {
87                         Attr:     "uuid",
88                         Operator: "!=",
89                         Operand:  last.UUID,
90                 }}
91         }
92         progress(callCount, expectCount)
93
94         if checkCount, err := countCollections(c, arvados.ResourceListParams{Filters: []arvados.Filter{{
95                 Attr:     "modified_at",
96                 Operator: "<=",
97                 Operand:  filterTime}}}); err != nil {
98                 return err
99         } else if callCount < checkCount {
100                 return fmt.Errorf("Retrieved %d collections with modtime <= T=%q, but server now reports there are %d collections with modtime <= T", callCount, filterTime, checkCount)
101         }
102
103         return nil
104 }