1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
11 "git.curoverse.com/arvados.git/sdk/go/arvados"
14 func countCollections(c *arvados.Client, params arvados.ResourceListParams) (int, error) {
15 var page arvados.CollectionList
18 params.Count = "exact"
19 err := c.RequestAndDecode(&page, "GET", "arvados/v1/collections", nil, params)
20 return page.ItemsAvailable, err
23 // EachCollection calls f once for every readable
24 // collection. EachCollection stops if it encounters an error, such as
25 // f returning a non-nil error.
27 // The progress function is called periodically with done (number of
28 // times f has been called) and total (number of times f is expected
31 // If pageSize > 0 it is used as the maximum page size in each API
32 // call; otherwise the maximum allowed page size is requested.
33 func EachCollection(c *arvados.Client, pageSize int, f func(arvados.Collection) error, progress func(done, total int)) error {
35 progress = func(_, _ int) {}
38 expectCount, err := countCollections(c, arvados.ResourceListParams{
40 IncludeOldVersions: true,
46 // Note the obvious way to get all collections (sorting by
47 // UUID) would be much easier, but would lose data: If a
48 // client were to move files from collection with uuid="zzz"
49 // to a collection with uuid="aaa" around the time when we
50 // were fetching the "mmm" page, we would never see those
51 // files' block IDs at all -- even if the client is careful to
52 // save "aaa" before saving "zzz".
54 // Instead, we get pages in modified_at order. Collections
55 // that are modified during the run will be re-fetched in a
60 // Use the maximum page size the server allows
63 params := arvados.ResourceListParams{
65 Order: "modified_at, uuid",
67 Select: []string{"uuid", "unsigned_manifest_text", "modified_at", "portable_data_hash", "replication_desired"},
69 IncludeOldVersions: true,
71 var last arvados.Collection
72 var filterTime time.Time
74 gettingExactTimestamp := false
76 progress(callCount, expectCount)
77 var page arvados.CollectionList
78 err := c.RequestAndDecode(&page, "GET", "arvados/v1/collections", nil, params)
82 for _, coll := range page.Items {
83 if last.ModifiedAt != nil && *last.ModifiedAt == *coll.ModifiedAt && last.UUID >= coll.UUID {
93 if len(page.Items) == 0 && !gettingExactTimestamp {
95 } else if last.ModifiedAt == nil {
96 return fmt.Errorf("BUG: Last collection on the page (%s) has no modified_at timestamp; cannot make progress", last.UUID)
97 } else if len(page.Items) > 0 && *last.ModifiedAt == filterTime {
98 // If we requested time>=X and never got a
99 // time>X then we might not have received all
100 // items with time==X yet. Switch to
101 // gettingExactTimestamp mode (if we're not
102 // there already), advancing our UUID
103 // threshold with each request, until we get
105 gettingExactTimestamp = true
106 params.Filters = []arvados.Filter{{
115 } else if gettingExactTimestamp {
116 // This must be an empty page (in this mode,
117 // an unequal timestamp is impossible) so we
118 // can start getting pages of newer
120 gettingExactTimestamp = false
121 params.Filters = []arvados.Filter{{
127 // In the normal case, we know we have seen
128 // all collections with modtime<filterTime,
129 // but we might not have seen all that have
130 // modtime=filterTime. Hence we use >= instead
131 // of > and skip the obvious overlapping item,
132 // i.e., the last item on the previous
133 // page. In some edge cases this can return
134 // collections we have already seen, but
135 // avoiding that would add overhead in the
136 // overwhelmingly common cases, so we don't
138 filterTime = *last.ModifiedAt
139 params.Filters = []arvados.Filter{{
150 progress(callCount, expectCount)
152 if checkCount, err := countCollections(c, arvados.ResourceListParams{
153 Filters: []arvados.Filter{{
156 Operand: filterTime}},
158 IncludeOldVersions: true,
161 } else if callCount < checkCount {
162 return fmt.Errorf("Retrieved %d collections with modtime <= T=%q, but server now reports there are %d collections with modtime <= T", callCount, filterTime, checkCount)