Skip to content

Commit 5528782

Browse files
tsuzukayamaTiago Suzukayama
andauthored
fix: sort operations before pagination (#660)
Co-authored-by: Tiago Suzukayama <tiago.suzukayama@wildlifestudios.com>
1 parent 7590c7f commit 5528782

File tree

10 files changed

+44
-42
lines changed

10 files changed

+44
-42
lines changed

.github/workflows/lint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Checkout
1313
uses: actions/checkout@v2
1414
- name: Restore cache
15-
uses: actions/cache@v2
15+
uses: actions/cache@v4
1616
with:
1717
path: ~/go/pkg/mod
1818
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

.github/workflows/test.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Checkout
1313
uses: actions/checkout@v2
1414
- name: Restore cache
15-
uses: actions/cache@v2
15+
uses: actions/cache@v4
1616
with:
1717
path: ~/go/pkg/mod
1818
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
@@ -32,7 +32,7 @@ jobs:
3232
- name: Checkout
3333
uses: actions/checkout@v2
3434
- name: Restore cache
35-
uses: actions/cache@v2
35+
uses: actions/cache@v4
3636
with:
3737
path: ~/go/pkg/mod
3838
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
@@ -59,7 +59,7 @@ jobs:
5959
- name: Checkout
6060
uses: actions/checkout@v2
6161
- name: Restore cache
62-
uses: actions/cache@v2
62+
uses: actions/cache@v4
6363
with:
6464
path: ~/go/pkg/mod
6565
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

internal/adapters/storage/redis/operation/operation_storage_redis.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (r *redisOperationStorage) ListSchedulerActiveOperations(ctx context.Contex
224224
return operations, nil
225225
}
226226

227-
func (r *redisOperationStorage) ListSchedulerFinishedOperations(ctx context.Context, schedulerName string, page, pageSize int64) (operations []*operation.Operation, total int64, err error) {
227+
func (r *redisOperationStorage) ListSchedulerFinishedOperations(ctx context.Context, schedulerName string, page, pageSize int64, order string) (operations []*operation.Operation, total int64, err error) {
228228
if err := r.CleanExpiredOperations(ctx, schedulerName); err != nil {
229229
return nil, -1, fmt.Errorf("failed to clean scheduler expired operations: %w", err)
230230
}
@@ -234,7 +234,7 @@ func (r *redisOperationStorage) ListSchedulerFinishedOperations(ctx context.Cont
234234
return nil, -1, fmt.Errorf("failed to count finished operations: %w", err)
235235
}
236236

237-
operationsIDs, err := r.getFinishedOperationsFromHistory(ctx, schedulerName, page, pageSize)
237+
operationsIDs, err := r.getFinishedOperationsFromHistory(ctx, schedulerName, page, pageSize, order)
238238
operations = make([]*operation.Operation, len(operationsIDs))
239239
executions := make([]redis.Cmder, len(operationsIDs))
240240

@@ -275,7 +275,7 @@ func (r *redisOperationStorage) ListSchedulerFinishedOperations(ctx context.Cont
275275
}
276276

277277
func (r *redisOperationStorage) CleanOperationsHistory(ctx context.Context, schedulerName string) error {
278-
operationsIDs, err := r.getFinishedOperationsFromHistory(ctx, schedulerName, 0, -1)
278+
operationsIDs, err := r.getFinishedOperationsFromHistory(ctx, schedulerName, 0, -1, "")
279279
if err != nil {
280280
return err
281281
}
@@ -381,13 +381,15 @@ func (r *redisOperationStorage) getAllFinishedOperationIDs(ctx context.Context,
381381
return append(operationsIDs, noActionOpIDs...), nil
382382
}
383383

384-
func (r *redisOperationStorage) getFinishedOperationsFromHistory(ctx context.Context, schedulerName string, page, pageSize int64) (operationsIDs []string, err error) {
384+
func (r *redisOperationStorage) getFinishedOperationsFromHistory(ctx context.Context, schedulerName string, page, pageSize int64, order string) (operationsIDs []string, err error) {
385385
metrics.RunWithMetrics(operationStorageMetricLabel, func() error {
386-
operationsIDs, err = r.client.ZRevRangeByScore(ctx, r.buildSchedulerHistoryOperationsKey(schedulerName), &redis.ZRangeBy{
387-
Min: "-inf",
388-
Max: "+inf",
386+
operationsIDs, err = r.client.Sort(ctx, r.buildSchedulerHistoryOperationsKey(schedulerName), &redis.Sort{
387+
By: createdAtRedisKey,
389388
Offset: page * pageSize,
390389
Count: pageSize,
390+
Get: nil,
391+
Order: order,
392+
Alpha: false,
391393
}).Result()
392394
return err
393395
})

internal/adapters/storage/redis/operation/operation_storage_redis_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
384384
require.NoError(t, err)
385385
}
386386

387-
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, page, pageSize)
387+
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, page, pageSize, "desc")
388388
assert.NoError(t, err)
389389
assert.NotEmptyf(t, operationsReturned, "expected at least one operation")
390390
assert.Equal(t, expectedOperations, operationsReturned)
@@ -424,7 +424,7 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
424424
require.NoError(t, err)
425425
}
426426

427-
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, page, pageSize)
427+
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, page, pageSize, "desc")
428428
assert.NoError(t, err)
429429
assert.NotEmptyf(t, operationsReturned, "expected at least one operation")
430430
assert.Equal(t, expectedOperations, operationsReturned)
@@ -463,7 +463,7 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
463463
require.NoError(t, err)
464464
}
465465

466-
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, page, pageSize)
466+
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, page, pageSize, "")
467467
assert.NoError(t, err)
468468
assert.Empty(t, operationsReturned, "expected result to be empty")
469469
assert.Equal(t, int64(4), total)
@@ -476,7 +476,7 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
476476
definitionProvider, _ := createOperationDefinitionProvider(t)
477477
storage := NewRedisOperationStorage(client, clock, operationsTTLMap, definitionProvider)
478478

479-
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10)
479+
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10, "")
480480
assert.NoError(t, err)
481481
assert.Empty(t, operationsReturned, "expected result to be empty")
482482
assert.Equal(t, int64(0), total)
@@ -497,7 +497,7 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
497497
require.NoError(t, err)
498498
}
499499

500-
operationsReturned, _, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10)
500+
operationsReturned, _, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10, "")
501501
assert.NoError(t, err)
502502
assert.Empty(t, operationsReturned)
503503
ids, _ := client.ZRange(context.Background(), storage.buildSchedulerHistoryOperationsKey(schedulerName), 0, -1).Result()
@@ -557,7 +557,7 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
557557
require.NoError(t, err)
558558
}
559559

560-
_, _, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10)
560+
_, _, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10, "")
561561
assert.ErrorContains(t, err, "failed to build operation from the hash: failed to parse operation status: strconv.Atoi: parsing \"\": invalid syntax")
562562
})
563563

@@ -568,7 +568,7 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
568568
definitionProvider, _ := createOperationDefinitionProvider(t)
569569
storage := NewRedisOperationStorage(client, clock, operationsTTLMap, definitionProvider)
570570
client.Close()
571-
_, _, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10)
571+
_, _, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10, "")
572572
assert.ErrorContains(t, err, "failed to clean scheduler expired operations: failed to list operations for \"test-scheduler\" when trying to clean expired operations")
573573
})
574574
})
@@ -1113,7 +1113,7 @@ func TestCleanOperationsHistory(t *testing.T) {
11131113
definitionProvider, _ := createOperationDefinitionProvider(t)
11141114
storage := NewRedisOperationStorage(client, clock, operationsTTLMap, definitionProvider)
11151115

1116-
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10)
1116+
operationsReturned, total, err := storage.ListSchedulerFinishedOperations(context.Background(), schedulerName, 0, 10, "")
11171117
assert.NoError(t, err)
11181118
assert.Empty(t, operationsReturned)
11191119
assert.Equal(t, total, int64(0))

internal/api/handlers/operations_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (h *OperationsHandler) queryActiveOperations(ctx context.Context, scheduler
176176
}
177177

178178
func (h *OperationsHandler) queryFinishedOperations(ctx context.Context, schedulerName, sortingOrder string, page, pageSize int64) ([]*operation.Operation, uint32, error) {
179-
finishedOperationEntities, total, err := h.operationManager.ListSchedulerFinishedOperations(ctx, schedulerName, page-1, pageSize)
179+
finishedOperationEntities, total, err := h.operationManager.ListSchedulerFinishedOperations(ctx, schedulerName, page-1, pageSize, sortingOrder)
180180
if err != nil {
181181
h.logger.Error("error listing finished operations", zap.Error(err))
182182
return nil, uint32(0), status.Error(codes.Unknown, err.Error())

internal/api/handlers/operations_handler_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func TestListOperations(t *testing.T) {
196196
mockCtrl := gomock.NewController(t)
197197
operationManager := mock.NewMockOperationManager(mockCtrl)
198198

199-
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15)).Return(finishedOperations, int64(3), nil)
199+
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15), "desc").Return(finishedOperations, int64(3), nil)
200200

201201
mux := runtime.NewServeMux()
202202
err := api.RegisterOperationsServiceHandlerServer(context.Background(), mux, ProvideOperationsHandler(operationManager))
@@ -218,7 +218,7 @@ func TestListOperations(t *testing.T) {
218218
mockCtrl := gomock.NewController(t)
219219
operationManager := mock.NewMockOperationManager(mockCtrl)
220220

221-
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15)).Return(finishedOperations, int64(3), nil)
221+
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15), "asc").Return(finishedOperations, int64(3), nil)
222222

223223
mux := runtime.NewServeMux()
224224
err := api.RegisterOperationsServiceHandlerServer(context.Background(), mux, ProvideOperationsHandler(operationManager))
@@ -241,7 +241,7 @@ func TestListOperations(t *testing.T) {
241241
mockCtrl := gomock.NewController(t)
242242
operationManager := mock.NewMockOperationManager(mockCtrl)
243243

244-
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15)).Return(finishedOperations, int64(3), nil)
244+
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15), "desc").Return(finishedOperations, int64(3), nil)
245245

246246
mux := runtime.NewServeMux()
247247
err := api.RegisterOperationsServiceHandlerServer(context.Background(), mux, ProvideOperationsHandler(operationManager))
@@ -358,7 +358,7 @@ func TestListOperations(t *testing.T) {
358358
mockCtrl := gomock.NewController(t)
359359
operationManager := mock.NewMockOperationManager(mockCtrl)
360360

361-
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15)).Return(finishedOperations, int64(3), nil)
361+
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15), "desc").Return(finishedOperations, int64(3), nil)
362362

363363
mux := runtime.NewServeMux()
364364
err := api.RegisterOperationsServiceHandlerServer(context.Background(), mux, ProvideOperationsHandler(operationManager))
@@ -380,7 +380,7 @@ func TestListOperations(t *testing.T) {
380380
mockCtrl := gomock.NewController(t)
381381
operationManager := mock.NewMockOperationManager(mockCtrl)
382382

383-
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(10)).Return(finishedOperations, int64(13), nil)
383+
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(10), "desc").Return(finishedOperations, int64(13), nil)
384384

385385
mux := runtime.NewServeMux()
386386
err := api.RegisterOperationsServiceHandlerServer(context.Background(), mux, ProvideOperationsHandler(operationManager))
@@ -485,7 +485,7 @@ func TestListOperations(t *testing.T) {
485485
mockCtrl := gomock.NewController(t)
486486
operationManager := mock.NewMockOperationManager(mockCtrl)
487487

488-
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15)).Return(nil, int64(0), errors.NewErrUnexpected("error listing finished operations"))
488+
operationManager.EXPECT().ListSchedulerFinishedOperations(gomock.Any(), schedulerName, int64(0), int64(15), "desc").Return(nil, int64(0), errors.NewErrUnexpected("error listing finished operations"))
489489

490490
mux := runtime.NewServeMux()
491491
err := api.RegisterOperationsServiceHandlerServer(context.Background(), mux, ProvideOperationsHandler(operationManager))

internal/core/ports/mock/operation_ports_mock.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/core/ports/operation_ports.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type OperationManager interface {
5252
// ListSchedulerActiveOperations returns a list of operations with active status for the given scheduler.
5353
ListSchedulerActiveOperations(ctx context.Context, schedulerName string) ([]*operation.Operation, error)
5454
// ListSchedulerFinishedOperations returns a list of operations with finished status for the given scheduler.
55-
ListSchedulerFinishedOperations(ctx context.Context, schedulerName string, page, pageSize int64) ([]*operation.Operation, int64, error)
55+
ListSchedulerFinishedOperations(ctx context.Context, schedulerName string, page, pageSize int64, order string) ([]*operation.Operation, int64, error)
5656
// EnqueueOperationCancellationRequest enqueues a cancellation request for the given operation.
5757
EnqueueOperationCancellationRequest(ctx context.Context, schedulerName, operationID string) error
5858
// WatchOperationCancellationRequests starts an async process to watch for cancellation requests for the given operation and cancels it.
@@ -92,7 +92,7 @@ type OperationStorage interface {
9292
// ListSchedulerActiveOperations list scheduler active operations.
9393
ListSchedulerActiveOperations(ctx context.Context, schedulerName string) ([]*operation.Operation, error)
9494
// ListSchedulerFinishedOperations list scheduler finished operations.
95-
ListSchedulerFinishedOperations(ctx context.Context, schedulerName string, page, pageSize int64) ([]*operation.Operation, int64, error)
95+
ListSchedulerFinishedOperations(ctx context.Context, schedulerName string, page, pageSize int64, order string) ([]*operation.Operation, int64, error)
9696
// UpdateOperationStatus only updates the operation status for the given
9797
// operation ID.
9898
UpdateOperationStatus(ctx context.Context, schedulerName, operationID string, status operation.Status) error

internal/core/services/operations/operation_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ func (om *OperationManager) ListSchedulerActiveOperations(ctx context.Context, s
187187
return ops, nil
188188
}
189189

190-
func (om *OperationManager) ListSchedulerFinishedOperations(ctx context.Context, schedulerName string, page, pageSize int64) (result []*operation.Operation, total int64, err error) {
191-
result, total, err = om.Storage.ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize)
190+
func (om *OperationManager) ListSchedulerFinishedOperations(ctx context.Context, schedulerName string, page, pageSize int64, order string) (result []*operation.Operation, total int64, err error) {
191+
result, total, err = om.Storage.ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize, order)
192192
if err != nil {
193193
return nil, -1, fmt.Errorf("failed to list finished operations for scheduler %s : %w", schedulerName, err)
194194
}

internal/core/services/operations/operation_manager_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
641641
page := int64(0)
642642
pageSize := int64(10)
643643

644-
operationStorage.EXPECT().ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize).Return(operationsResult, int64(3), nil)
645-
operations, total, err := opManager.ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize)
644+
operationStorage.EXPECT().ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize, "").Return(operationsResult, int64(3), nil)
645+
operations, total, err := opManager.ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize, "")
646646
require.NoError(t, err)
647647
assert.Equal(t, int64(3), total)
648648
require.ElementsMatch(t, operationsResult, operations)
@@ -664,8 +664,8 @@ func TestListSchedulerFinishedOperations(t *testing.T) {
664664
page := int64(0)
665665
pageSize := int64(10)
666666

667-
operationStorage.EXPECT().ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize).Return(nil, int64(0), errors.New("some error"))
668-
_, _, err := opManager.ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize)
667+
operationStorage.EXPECT().ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize, "").Return(nil, int64(0), errors.New("some error"))
668+
_, _, err := opManager.ListSchedulerFinishedOperations(ctx, schedulerName, page, pageSize, "")
669669
require.ErrorContains(t, err, "failed to list finished operations for scheduler test-scheduler : some error")
670670
})
671671
}

0 commit comments

Comments
 (0)