1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/arvados"
16 "git.arvados.org/arvados.git/sdk/go/httpserver"
19 //go:generate go run generate.go
21 // CollectionList is used as a template to auto-generate List()
22 // methods for other types; see generate.go.
24 func (conn *Conn) generated_CollectionList(ctx context.Context, options arvados.ListOptions) (arvados.CollectionList, error) {
26 var merged arvados.CollectionList
27 var needSort atomic.Value
29 err := conn.splitListRequest(ctx, options, func(ctx context.Context, _ string, backend arvados.API, options arvados.ListOptions) ([]string, error) {
30 options.ForwardedFor = conn.cluster.ClusterID + "-" + options.ForwardedFor
31 cl, err := backend.CollectionList(ctx, options)
37 if len(merged.Items) == 0 {
39 } else if len(cl.Items) > 0 {
40 merged.Items = append(merged.Items, cl.Items...)
43 uuids := make([]string, 0, len(cl.Items))
44 for _, item := range cl.Items {
45 uuids = append(uuids, item.UUID)
49 if needSort.Load().(bool) {
50 // Apply the default/implied order, "modified_at desc"
51 sort.Slice(merged.Items, func(i, j int) bool {
52 mi, mj := merged.Items[i].ModifiedAt, merged.Items[j].ModifiedAt
56 if merged.Items == nil {
57 // Return empty results as [], not null
58 // (https://github.com/golang/go/issues/27589 might be
59 // a better solution in the future)
60 merged.Items = []arvados.Collection{}
65 // Call fn on one or more local/remote backends if opts indicates a
66 // federation-wide list query, i.e.:
68 // - There is at least one filter of the form
69 // ["uuid","in",[a,b,c,...]] or ["uuid","=",a]
71 // - One or more of the supplied UUIDs (a,b,c,...) has a non-local
74 // - There are no other filters
76 // (If opts doesn't indicate a federation-wide list query, fn is just
77 // called once with the local backend.)
79 // fn is called more than once only if the query meets the following
88 // - Each filter is either "uuid = ..." or "uuid in [...]".
90 // - The maximum possible response size (total number of objects
91 // that could potentially be matched by all of the specified
92 // filters) exceeds the local cluster's response page size limit.
94 // If the query involves multiple backends but doesn't meet these
95 // restrictions, an error is returned without calling fn.
97 // Thus, the caller can assume that either:
99 // - splitListRequest() returns an error, or
101 // - fn is called exactly once, or
103 // - fn is called more than once, with options that satisfy the above
106 // Each call to fn indicates a single (local or remote) backend and a
107 // corresponding options argument suitable for sending to that
109 func (conn *Conn) splitListRequest(ctx context.Context, opts arvados.ListOptions, fn func(context.Context, string, arvados.API, arvados.ListOptions) ([]string, error)) error {
111 if opts.BypassFederation || opts.ForwardedFor != "" {
112 // Client requested no federation. Pass through.
113 _, err := fn(ctx, conn.cluster.ClusterID, conn.local, opts)
116 if opts.ClusterID != "" {
117 // Client explicitly selected cluster
118 _, err := fn(ctx, conn.cluster.ClusterID, conn.chooseBackend(opts.ClusterID), opts)
123 var matchAllFilters map[string]bool
124 for _, f := range opts.Filters {
125 matchThisFilter := map[string]bool{}
126 if f.Attr != "uuid" {
130 if f.Operator == "=" {
131 if uuid, ok := f.Operand.(string); ok {
132 matchThisFilter[uuid] = true
134 return httpErrorf(http.StatusBadRequest, "invalid operand type %T for filter %q", f.Operand, f)
136 } else if f.Operator == "in" {
137 if operand, ok := f.Operand.([]interface{}); ok {
138 // skip any elements that aren't
139 // strings (thus can't match a UUID,
140 // thus can't affect the response).
141 for _, v := range operand {
142 if uuid, ok := v.(string); ok {
143 matchThisFilter[uuid] = true
146 } else if strings, ok := f.Operand.([]string); ok {
147 for _, uuid := range strings {
148 matchThisFilter[uuid] = true
151 return httpErrorf(http.StatusBadRequest, "invalid operand type %T in filter %q", f.Operand, f)
158 if matchAllFilters == nil {
159 matchAllFilters = matchThisFilter
161 // Reduce matchAllFilters to the intersection
162 // of matchAllFilters ∩ matchThisFilter.
163 for uuid := range matchAllFilters {
164 if !matchThisFilter[uuid] {
165 delete(matchAllFilters, uuid)
171 if matchAllFilters == nil {
172 // Not filtering by UUID at all; just query the local
174 _, err := fn(ctx, conn.cluster.ClusterID, conn.local, opts)
178 // Collate UUIDs in matchAllFilters by remote cluster ID --
179 // e.g., todoByRemote["aaaaa"]["aaaaa-4zz18-000000000000000"]
180 // will be true -- and count the total number of UUIDs we're
181 // filtering on, so we can compare it to our max page size
184 todoByRemote := map[string]map[string]bool{}
185 for uuid := range matchAllFilters {
187 // Cannot match anything, just drop it
189 if todoByRemote[uuid[:5]] == nil {
190 todoByRemote[uuid[:5]] = map[string]bool{}
192 todoByRemote[uuid[:5]][uuid] = true
197 if len(todoByRemote) == 0 {
200 if len(todoByRemote) == 1 && todoByRemote[conn.cluster.ClusterID] != nil {
201 // All UUIDs are local, so proxy a single request. The
202 // generic case has some limitations (see below) which
203 // we don't want to impose on local requests.
204 _, err := fn(ctx, conn.cluster.ClusterID, conn.local, opts)
208 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query: each filter must be either 'uuid = ...' or 'uuid in [...]'")
210 if opts.Count != "none" {
211 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query unless count==\"none\"")
213 if (opts.Limit >= 0 && opts.Limit < int64(nUUIDs)) || opts.Offset != 0 || len(opts.Order) > 0 {
214 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query with limit (%d) < nUUIDs (%d), offset (%d) > 0, or order (%v) parameter", opts.Limit, nUUIDs, opts.Offset, opts.Order)
216 if max := conn.cluster.API.MaxItemsPerResponse; nUUIDs > max {
217 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query because number of UUIDs (%d) exceeds page size limit %d", nUUIDs, max)
220 ctx, cancel := context.WithCancel(ctx)
222 errs := make(chan error, len(todoByRemote))
223 for clusterID, todo := range todoByRemote {
224 go func(clusterID string, todo map[string]bool) {
225 // This goroutine sends exactly one value to
227 batch := make([]string, 0, len(todo))
228 for uuid := range todo {
229 batch = append(batch, uuid)
232 var backend arvados.API
233 if clusterID == conn.cluster.ClusterID {
235 } else if backend = conn.remotes[clusterID]; backend == nil {
236 errs <- httpErrorf(http.StatusNotFound, "cannot execute federated list query: no proxy available for cluster %q", clusterID)
240 if remoteOpts.Select != nil {
241 // We always need to select UUIDs to
242 // use the response, even if our
244 remoteOpts.Select = append([]string{"uuid"}, remoteOpts.Select...)
247 if len(batch) > len(todo) {
248 // Reduce batch to just the todo's
250 for uuid := range todo {
251 batch = append(batch, uuid)
254 remoteOpts.Filters = []arvados.Filter{{"uuid", "in", batch}}
256 done, err := fn(ctx, clusterID, backend, remoteOpts)
258 errs <- httpErrorf(http.StatusBadGateway, "%s", err.Error())
262 for _, uuid := range done {
263 if _, ok := todo[uuid]; ok {
269 // Zero items == no more
270 // results exist, no need to
273 } else if !progress {
274 errs <- httpErrorf(http.StatusBadGateway, "cannot make progress in federated list query: cluster %q returned %d items but none had the requested UUIDs", clusterID, len(done))
282 // Wait for all goroutines to return, then return the first
283 // non-nil error, if any.
285 for range todoByRemote {
286 if err := <-errs; err != nil && firstErr == nil {
288 // Signal to any remaining fn() calls that
289 // further effort is futile.
296 func httpErrorf(code int, format string, args ...interface{}) error {
297 return httpserver.ErrorWithStatus(fmt.Errorf(format, args...), code)