15851: Return items [], not null, in empty set responses.
[arvados.git] / lib / controller / federation / list.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package federation
6
7 import (
8         "context"
9         "fmt"
10         "net/http"
11         "sort"
12         "sync"
13
14         "git.curoverse.com/arvados.git/sdk/go/arvados"
15         "git.curoverse.com/arvados.git/sdk/go/httpserver"
16 )
17
18 //go:generate go run generate.go
19
20 // CollectionList is used as a template to auto-generate List()
21 // methods for other types; see generate.go.
22
23 func (conn *Conn) CollectionList(ctx context.Context, options arvados.ListOptions) (arvados.CollectionList, error) {
24         var mtx sync.Mutex
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)
28                 if err != nil {
29                         return nil, err
30                 }
31                 mtx.Lock()
32                 defer mtx.Unlock()
33                 if len(merged.Items) == 0 {
34                         merged = cl
35                 } else {
36                         merged.Items = append(merged.Items, cl.Items...)
37                 }
38                 uuids := make([]string, 0, len(cl.Items))
39                 for _, item := range cl.Items {
40                         uuids = append(uuids, item.UUID)
41                 }
42                 return uuids, nil
43         })
44         sort.Slice(merged.Items, func(i, j int) bool { return merged.Items[i].UUID < merged.Items[j].UUID })
45         if merged.Items == nil {
46                 // Return empty results as [], not null
47                 // (https://github.com/golang/go/issues/27589 might be
48                 // a better solution in the future)
49                 merged.Items = []arvados.Collection{}
50         }
51         return merged, err
52 }
53
54 // Call fn on one or more local/remote backends if opts indicates a
55 // federation-wide list query, i.e.:
56 //
57 // * There is at least one filter of the form
58 //   ["uuid","in",[a,b,c,...]] or ["uuid","=",a]
59 //
60 // * One or more of the supplied UUIDs (a,b,c,...) has a non-local
61 //   prefix.
62 //
63 // * There are no other filters
64 //
65 // (If opts doesn't indicate a federation-wide list query, fn is just
66 // called once with the local backend.)
67 //
68 // fn is called more than once only if the query meets the following
69 // restrictions:
70 //
71 // * Count=="none"
72 //
73 // * Limit<0
74 //
75 // * len(Order)==0
76 //
77 // * Each filter must be either "uuid = ..." or "uuid in [...]".
78 //
79 // * The maximum possible response size (total number of objects that
80 //   could potentially be matched by all of the specified filters)
81 //   exceeds the local cluster's response page size limit.
82 //
83 // If the query involves multiple backends but doesn't meet these
84 // restrictions, an error is returned without calling fn.
85 //
86 // Thus, the caller can assume that either:
87 //
88 // * splitListRequest() returns an error, or
89 //
90 // * fn is called exactly once, or
91 //
92 // * fn is called more than once, with options that satisfy the above
93 //   restrictions.
94 //
95 // Each call to fn indicates a single (local or remote) backend and a
96 // corresponding options argument suitable for sending to that
97 // backend.
98 func (conn *Conn) splitListRequest(ctx context.Context, opts arvados.ListOptions, fn func(context.Context, string, arvados.API, arvados.ListOptions) ([]string, error)) error {
99         cannotSplit := false
100         var matchAllFilters map[string]bool
101         for _, f := range opts.Filters {
102                 matchThisFilter := map[string]bool{}
103                 if f.Attr != "uuid" {
104                         cannotSplit = true
105                         continue
106                 }
107                 if f.Operator == "=" {
108                         if uuid, ok := f.Operand.(string); ok {
109                                 matchThisFilter[uuid] = true
110                         } else {
111                                 return httpErrorf(http.StatusBadRequest, "invalid operand type %T for filter %q", f.Operand, f)
112                         }
113                 } else if f.Operator == "in" {
114                         if operand, ok := f.Operand.([]interface{}); ok {
115                                 // skip any elements that aren't
116                                 // strings (thus can't match a UUID,
117                                 // thus can't affect the response).
118                                 for _, v := range operand {
119                                         if uuid, ok := v.(string); ok {
120                                                 matchThisFilter[uuid] = true
121                                         }
122                                 }
123                         } else if strings, ok := f.Operand.([]string); ok {
124                                 for _, uuid := range strings {
125                                         matchThisFilter[uuid] = true
126                                 }
127                         } else {
128                                 return httpErrorf(http.StatusBadRequest, "invalid operand type %T in filter %q", f.Operand, f)
129                         }
130                 } else {
131                         cannotSplit = true
132                         continue
133                 }
134
135                 if matchAllFilters == nil {
136                         matchAllFilters = matchThisFilter
137                 } else {
138                         // Reduce matchAllFilters to the intersection
139                         // of matchAllFilters ∩ matchThisFilter.
140                         for uuid := range matchAllFilters {
141                                 if !matchThisFilter[uuid] {
142                                         delete(matchAllFilters, uuid)
143                                 }
144                         }
145                 }
146         }
147
148         if matchAllFilters == nil {
149                 // Not filtering by UUID at all; just query the local
150                 // cluster.
151                 _, err := fn(ctx, conn.cluster.ClusterID, conn.local, opts)
152                 return err
153         }
154
155         // Collate UUIDs in matchAllFilters by remote cluster ID --
156         // e.g., todoByRemote["aaaaa"]["aaaaa-4zz18-000000000000000"]
157         // will be true -- and count the total number of UUIDs we're
158         // filtering on, so we can compare it to our max page size
159         // limit.
160         nUUIDs := 0
161         todoByRemote := map[string]map[string]bool{}
162         for uuid := range matchAllFilters {
163                 if len(uuid) != 27 {
164                         // Cannot match anything, just drop it
165                 } else {
166                         if todoByRemote[uuid[:5]] == nil {
167                                 todoByRemote[uuid[:5]] = map[string]bool{}
168                         }
169                         todoByRemote[uuid[:5]][uuid] = true
170                         nUUIDs++
171                 }
172         }
173
174         if len(todoByRemote) > 1 {
175                 if cannotSplit {
176                         return httpErrorf(http.StatusBadRequest, "cannot execute federated list query: each filter must be either 'uuid = ...' or 'uuid in [...]'")
177                 }
178                 if opts.Count != "none" {
179                         return httpErrorf(http.StatusBadRequest, "cannot execute federated list query unless count==\"none\"")
180                 }
181                 if opts.Limit >= 0 || opts.Offset != 0 || len(opts.Order) > 0 {
182                         return httpErrorf(http.StatusBadRequest, "cannot execute federated list query with limit, offset, or order parameter")
183                 }
184                 if max := conn.cluster.API.MaxItemsPerResponse; nUUIDs > max {
185                         return httpErrorf(http.StatusBadRequest, "cannot execute federated list query because number of UUIDs (%d) exceeds page size limit %d", nUUIDs, max)
186                 }
187                 selectingUUID := false
188                 for _, attr := range opts.Select {
189                         if attr == "uuid" {
190                                 selectingUUID = true
191                         }
192                 }
193                 if opts.Select != nil && !selectingUUID {
194                         return httpErrorf(http.StatusBadRequest, "cannot execute federated list query with a select parameter that does not include uuid")
195                 }
196         }
197
198         ctx, cancel := context.WithCancel(ctx)
199         defer cancel()
200         errs := make(chan error, len(todoByRemote))
201         for clusterID, todo := range todoByRemote {
202                 go func(clusterID string, todo map[string]bool) {
203                         // This goroutine sends exactly one value to
204                         // errs.
205                         batch := make([]string, 0, len(todo))
206                         for uuid := range todo {
207                                 batch = append(batch, uuid)
208                         }
209
210                         var backend arvados.API
211                         if clusterID == conn.cluster.ClusterID {
212                                 backend = conn.local
213                         } else if backend = conn.remotes[clusterID]; backend == nil {
214                                 errs <- httpErrorf(http.StatusNotFound, "cannot execute federated list query: no proxy available for cluster %q", clusterID)
215                                 return
216                         }
217                         remoteOpts := opts
218                         for len(todo) > 0 {
219                                 if len(batch) > len(todo) {
220                                         // Reduce batch to just the todo's
221                                         batch = batch[:0]
222                                         for uuid := range todo {
223                                                 batch = append(batch, uuid)
224                                         }
225                                 }
226                                 remoteOpts.Filters = []arvados.Filter{{"uuid", "in", batch}}
227
228                                 done, err := fn(ctx, clusterID, backend, remoteOpts)
229                                 if err != nil {
230                                         errs <- err
231                                         return
232                                 }
233                                 progress := false
234                                 for _, uuid := range done {
235                                         if _, ok := todo[uuid]; ok {
236                                                 progress = true
237                                                 delete(todo, uuid)
238                                         }
239                                 }
240                                 if !progress {
241                                         errs <- httpErrorf(http.StatusBadGateway, "cannot make progress in federated list query: cluster %q returned none of the requested UUIDs", clusterID)
242                                         return
243                                 }
244                         }
245                         errs <- nil
246                 }(clusterID, todo)
247         }
248
249         // Wait for all goroutines to return, then return the first
250         // non-nil error, if any.
251         var firstErr error
252         for range todoByRemote {
253                 if err := <-errs; err != nil && firstErr == nil {
254                         firstErr = err
255                         // Signal to any remaining fn() calls that
256                         // further effort is futile.
257                         cancel()
258                 }
259         }
260         return firstErr
261 }
262
263 func httpErrorf(code int, format string, args ...interface{}) error {
264         return httpserver.ErrorWithStatus(fmt.Errorf(format, args...), code)
265 }