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"
15 "git.curoverse.com/arvados.git/sdk/go/httpserver"
18 //go:generate go run generate.go
20 // CollectionList is used as a template to auto-generate List()
21 // methods for other types; see generate.go.
23 func (conn *Conn) CollectionList(ctx context.Context, options arvados.ListOptions) (arvados.CollectionList, error) {
25 var merged arvados.CollectionList
26 err := conn.splitListRequest(ctx, options, func(ctx context.Context, _ string, backend arvados.API, options arvados.ListOptions) ([]string, error) {
27 cl, err := backend.CollectionList(ctx, options)
33 if len(merged.Items) == 0 {
36 merged.Items = append(merged.Items, cl.Items...)
38 uuids := make([]string, 0, len(cl.Items))
39 for _, item := range cl.Items {
40 uuids = append(uuids, item.UUID)
44 sort.Slice(merged.Items, func(i, j int) bool { return merged.Items[i].UUID < merged.Items[j].UUID })
48 // Call fn on one or more local/remote backends if opts indicates a
49 // federation-wide list query, i.e.:
51 // * There is at least one filter of the form
52 // ["uuid","in",[a,b,c,...]] or ["uuid","=",a]
54 // * One or more of the supplied UUIDs (a,b,c,...) has a non-local
57 // * There are no other filters
59 // (If opts doesn't indicate a federation-wide list query, fn is just
60 // called once with the local backend.)
62 // fn is called more than once only if the query meets the following
71 // * Each filter must be either "uuid = ..." or "uuid in [...]".
73 // * The maximum possible response size (total number of objects that
74 // could potentially be matched by all of the specified filters)
75 // exceeds the local cluster's response page size limit.
77 // If the query involves multiple backends but doesn't meet these
78 // restrictions, an error is returned without calling fn.
80 // Thus, the caller can assume that either:
82 // * splitListRequest() returns an error, or
84 // * fn is called exactly once, or
86 // * fn is called more than once, with options that satisfy the above
89 // Each call to fn indicates a single (local or remote) backend and a
90 // corresponding options argument suitable for sending to that
92 func (conn *Conn) splitListRequest(ctx context.Context, opts arvados.ListOptions, fn func(context.Context, string, arvados.API, arvados.ListOptions) ([]string, error)) error {
94 var matchAllFilters map[string]bool
95 for _, f := range opts.Filters {
96 matchThisFilter := map[string]bool{}
101 if f.Operator == "=" {
102 if uuid, ok := f.Operand.(string); ok {
103 matchThisFilter[uuid] = true
105 return httpErrorf(http.StatusBadRequest, "invalid operand type %T for filter %q", f.Operand, f)
107 } else if f.Operator == "in" {
108 if operand, ok := f.Operand.([]interface{}); ok {
109 // skip any elements that aren't
110 // strings (thus can't match a UUID,
111 // thus can't affect the response).
112 for _, v := range operand {
113 if uuid, ok := v.(string); ok {
114 matchThisFilter[uuid] = true
117 } else if strings, ok := f.Operand.([]string); ok {
118 for _, uuid := range strings {
119 matchThisFilter[uuid] = true
122 return httpErrorf(http.StatusBadRequest, "invalid operand type %T in filter %q", f.Operand, f)
129 if matchAllFilters == nil {
130 matchAllFilters = matchThisFilter
132 // Reduce matchAllFilters to the intersection
133 // of matchAllFilters ∩ matchThisFilter.
134 for uuid := range matchAllFilters {
135 if !matchThisFilter[uuid] {
136 delete(matchAllFilters, uuid)
142 // Collate UUIDs in matchAllFilters by remote cluster ID --
143 // e.g., todoByRemote["aaaaa"]["aaaaa-4zz18-000000000000000"]
144 // will be true -- and count the total number of UUIDs we're
145 // filtering on, so we can compare it to our max page size
148 todoByRemote := map[string]map[string]bool{}
149 for uuid := range matchAllFilters {
151 // Cannot match anything, just drop it
153 if todoByRemote[uuid[:5]] == nil {
154 todoByRemote[uuid[:5]] = map[string]bool{}
156 todoByRemote[uuid[:5]][uuid] = true
161 if len(todoByRemote) > 1 {
163 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query: each filter must be either 'uuid = ...' or 'uuid in [...]'")
165 if opts.Count != "none" {
166 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query unless count==\"none\"")
168 if opts.Limit >= 0 || opts.Offset != 0 || len(opts.Order) > 0 {
169 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query with limit, offset, or order parameter")
171 if max := conn.cluster.API.MaxItemsPerResponse; nUUIDs > max {
172 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query because number of UUIDs (%d) exceeds page size limit %d", nUUIDs, max)
174 selectingUUID := false
175 for _, attr := range opts.Select {
180 if opts.Select != nil && !selectingUUID {
181 return httpErrorf(http.StatusBadRequest, "cannot execute federated list query with a select parameter that does not include uuid")
185 ctx, cancel := context.WithCancel(ctx)
187 errs := make(chan error, len(todoByRemote))
188 for clusterID, todo := range todoByRemote {
189 go func(clusterID string, todo map[string]bool) {
190 // This goroutine sends exactly one value to
192 batch := make([]string, 0, len(todo))
193 for uuid := range todo {
194 batch = append(batch, uuid)
197 var backend arvados.API
198 if clusterID == conn.cluster.ClusterID {
200 } else if backend = conn.remotes[clusterID]; backend == nil {
201 errs <- httpErrorf(http.StatusNotFound, "cannot execute federated list query: no proxy available for cluster %q", clusterID)
206 if len(batch) > len(todo) {
207 // Reduce batch to just the todo's
209 for uuid := range todo {
210 batch = append(batch, uuid)
213 remoteOpts.Filters = []arvados.Filter{{"uuid", "in", batch}}
215 done, err := fn(ctx, clusterID, backend, remoteOpts)
221 for _, uuid := range done {
222 if _, ok := todo[uuid]; ok {
228 errs <- httpErrorf(http.StatusBadGateway, "cannot make progress in federated list query: cluster %q returned none of the requested UUIDs", clusterID)
236 // Wait for all goroutines to return, then return the first
237 // non-nil error, if any.
239 for range todoByRemote {
240 if err := <-errs; err != nil && firstErr == nil {
242 // Signal to any remaining fn() calls that
243 // further effort is futile.
250 func httpErrorf(code int, format string, args ...interface{}) error {
251 return httpserver.ErrorWithStatus(fmt.Errorf(format, args...), code)