15851: Merge branch 'master' into 15851-empty-items-array
[arvados.git] / lib / controller / federation / generated.go
index 2e6eda7ff6d5fc3e2ad510bc7fda0414dba68c7c..fb91a84960547d6dc6099e2e7e3ca69fb162afd8 100755 (executable)
@@ -8,6 +8,7 @@ import (
        "context"
        "sort"
        "sync"
+       "sync/atomic"
 
        "git.curoverse.com/arvados.git/sdk/go/arvados"
 )
@@ -19,6 +20,8 @@ import (
 func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerList, error) {
        var mtx sync.Mutex
        var merged arvados.ContainerList
+       var needSort atomic.Value
+       needSort.Store(false)
        err := conn.splitListRequest(ctx, options, func(ctx context.Context, _ string, backend arvados.API, options arvados.ListOptions) ([]string, error) {
                cl, err := backend.ContainerList(ctx, options)
                if err != nil {
@@ -28,8 +31,9 @@ func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions
                defer mtx.Unlock()
                if len(merged.Items) == 0 {
                        merged = cl
-               } else {
+               } else if len(cl.Items) > 0 {
                        merged.Items = append(merged.Items, cl.Items...)
+                       needSort.Store(true)
                }
                uuids := make([]string, 0, len(cl.Items))
                for _, item := range cl.Items {
@@ -37,7 +41,13 @@ func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions
                }
                return uuids, nil
        })
-       sort.Slice(merged.Items, func(i, j int) bool { return merged.Items[i].UUID < merged.Items[j].UUID })
+       if needSort.Load().(bool) {
+               // Apply the default/implied order, "modified_at desc"
+               sort.Slice(merged.Items, func(i, j int) bool {
+                       mi, mj := merged.Items[i].ModifiedAt, merged.Items[j].ModifiedAt
+                       return mj.Before(mi)
+               })
+       }
        if merged.Items == nil {
                // Return empty results as [], not null
                // (https://github.com/golang/go/issues/27589 might be
@@ -50,6 +60,8 @@ func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions
 func (conn *Conn) SpecimenList(ctx context.Context, options arvados.ListOptions) (arvados.SpecimenList, error) {
        var mtx sync.Mutex
        var merged arvados.SpecimenList
+       var needSort atomic.Value
+       needSort.Store(false)
        err := conn.splitListRequest(ctx, options, func(ctx context.Context, _ string, backend arvados.API, options arvados.ListOptions) ([]string, error) {
                cl, err := backend.SpecimenList(ctx, options)
                if err != nil {
@@ -59,8 +71,9 @@ func (conn *Conn) SpecimenList(ctx context.Context, options arvados.ListOptions)
                defer mtx.Unlock()
                if len(merged.Items) == 0 {
                        merged = cl
-               } else {
+               } else if len(cl.Items) > 0 {
                        merged.Items = append(merged.Items, cl.Items...)
+                       needSort.Store(true)
                }
                uuids := make([]string, 0, len(cl.Items))
                for _, item := range cl.Items {
@@ -68,7 +81,13 @@ func (conn *Conn) SpecimenList(ctx context.Context, options arvados.ListOptions)
                }
                return uuids, nil
        })
-       sort.Slice(merged.Items, func(i, j int) bool { return merged.Items[i].UUID < merged.Items[j].UUID })
+       if needSort.Load().(bool) {
+               // Apply the default/implied order, "modified_at desc"
+               sort.Slice(merged.Items, func(i, j int) bool {
+                       mi, mj := merged.Items[i].ModifiedAt, merged.Items[j].ModifiedAt
+                       return mj.Before(mi)
+               })
+       }
        if merged.Items == nil {
                // Return empty results as [], not null
                // (https://github.com/golang/go/issues/27589 might be