mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? <!-- Describe what has changed in this PR --> - Extracted the logic about backlog management to a new struct called `backlogManagerImpl` implementing the `backlogManager` interface. - `physicalTaskQueueManagerImpl` now has a `backlogManagerImpl` which in turn hosts `taskReader`, `taskWriter`, and other related structs. - interface between `backlogManager` and `physicalTaskQueueManager` is clearly defined. ## Why? <!-- Tell your future self why have you made these changes --> Better abstraction with clear boundaries that allows for easier testing. Now we can create a backlogManageImpl instance for testing without having to create a physical queue manager, and partition manager and a matching engine! ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> refactored existing tests. ## Potential risks <!-- Assuming the worst case, what can be broken when deploying this change to production? --> No logic change. ## Documentation <!-- Have you made sure this change doesn't falsify anything currently stated in `docs/`? If significant new behavior is added, have you described that in `docs/`? --> No ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) --> No
157 lines
5.6 KiB
Go
157 lines
5.6 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package matching
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
persistencespb "go.temporal.io/server/api/persistence/v1"
|
|
"go.temporal.io/server/common/primitives/timestamp"
|
|
)
|
|
|
|
func TestDeliverBufferTasks(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
defer controller.Finish()
|
|
|
|
tests := []func(tlm *physicalTaskQueueManagerImpl){
|
|
func(tlm *physicalTaskQueueManagerImpl) { close(tlm.backlogMgr.taskReader.taskBuffer) },
|
|
func(tlm *physicalTaskQueueManagerImpl) { tlm.backlogMgr.taskReader.gorogrp.Cancel() },
|
|
func(tlm *physicalTaskQueueManagerImpl) {
|
|
rps := 0.1
|
|
tlm.matcher.UpdateRatelimit(&rps)
|
|
tlm.backlogMgr.taskReader.taskBuffer <- &persistencespb.AllocatedTaskInfo{
|
|
Data: &persistencespb.TaskInfo{},
|
|
}
|
|
err := tlm.matcher.rateLimiter.Wait(context.Background()) // consume the token
|
|
assert.NoError(t, err)
|
|
tlm.backlogMgr.taskReader.gorogrp.Cancel()
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
// TODO: do not create pq manager, directly create backlog manager
|
|
tlm := mustCreateTestPhysicalTaskQueueManager(t, controller)
|
|
tlm.backlogMgr.taskReader.gorogrp.Go(tlm.backlogMgr.taskReader.dispatchBufferedTasks)
|
|
test(tlm)
|
|
// dispatchBufferedTasks should stop after invocation of the test function
|
|
tlm.backlogMgr.taskReader.gorogrp.Wait()
|
|
}
|
|
}
|
|
|
|
func TestDeliverBufferTasks_NoPollers(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
defer controller.Finish()
|
|
|
|
// TODO: do not create pq manager, directly create backlog manager
|
|
tlm := mustCreateTestPhysicalTaskQueueManager(t, controller)
|
|
tlm.backlogMgr.taskReader.taskBuffer <- &persistencespb.AllocatedTaskInfo{
|
|
Data: &persistencespb.TaskInfo{},
|
|
}
|
|
tlm.backlogMgr.taskReader.gorogrp.Go(tlm.backlogMgr.taskReader.dispatchBufferedTasks)
|
|
time.Sleep(100 * time.Millisecond) // let go routine run first and block on tasksForPoll
|
|
tlm.backlogMgr.taskReader.gorogrp.Cancel()
|
|
tlm.backlogMgr.taskReader.gorogrp.Wait()
|
|
}
|
|
|
|
func TestReadLevelForAllExpiredTasksInBatch(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
defer controller.Finish()
|
|
|
|
// TODO: do not create pq manager, directly create backlog manager
|
|
tlm := mustCreateTestPhysicalTaskQueueManager(t, controller)
|
|
tlm.backlogMgr.db.rangeID = int64(1)
|
|
tlm.backlogMgr.db.ackLevel = int64(0)
|
|
tlm.backlogMgr.taskAckManager.setAckLevel(tlm.backlogMgr.db.ackLevel)
|
|
tlm.backlogMgr.taskAckManager.setReadLevel(tlm.backlogMgr.db.ackLevel)
|
|
require.Equal(t, int64(0), tlm.backlogMgr.taskAckManager.getAckLevel())
|
|
require.Equal(t, int64(0), tlm.backlogMgr.taskAckManager.getReadLevel())
|
|
|
|
// Add all expired tasks
|
|
tasks := []*persistencespb.AllocatedTaskInfo{
|
|
{
|
|
Data: &persistencespb.TaskInfo{
|
|
ExpiryTime: timestamp.TimeNowPtrUtcAddSeconds(-60),
|
|
CreateTime: timestamp.TimeNowPtrUtcAddSeconds(-60 * 60),
|
|
},
|
|
TaskId: 11,
|
|
},
|
|
{
|
|
Data: &persistencespb.TaskInfo{
|
|
ExpiryTime: timestamp.TimeNowPtrUtcAddSeconds(-60),
|
|
CreateTime: timestamp.TimeNowPtrUtcAddSeconds(-60 * 60),
|
|
},
|
|
TaskId: 12,
|
|
},
|
|
}
|
|
|
|
require.NoError(t, tlm.backlogMgr.taskReader.addTasksToBuffer(context.TODO(), tasks))
|
|
require.Equal(t, int64(0), tlm.backlogMgr.taskAckManager.getAckLevel())
|
|
require.Equal(t, int64(12), tlm.backlogMgr.taskAckManager.getReadLevel())
|
|
|
|
// Now add a mix of valid and expired tasks
|
|
require.NoError(t, tlm.backlogMgr.taskReader.addTasksToBuffer(context.TODO(), []*persistencespb.AllocatedTaskInfo{
|
|
{
|
|
Data: &persistencespb.TaskInfo{
|
|
ExpiryTime: timestamp.TimeNowPtrUtcAddSeconds(-60),
|
|
CreateTime: timestamp.TimeNowPtrUtcAddSeconds(-60 * 60),
|
|
},
|
|
TaskId: 13,
|
|
},
|
|
{
|
|
Data: &persistencespb.TaskInfo{
|
|
ExpiryTime: timestamp.TimeNowPtrUtcAddSeconds(-60),
|
|
CreateTime: timestamp.TimeNowPtrUtcAddSeconds(-60 * 60),
|
|
},
|
|
TaskId: 14,
|
|
},
|
|
}))
|
|
require.Equal(t, int64(0), tlm.backlogMgr.taskAckManager.getAckLevel())
|
|
require.Equal(t, int64(14), tlm.backlogMgr.taskAckManager.getReadLevel())
|
|
}
|
|
|
|
func TestTaskWriterShutdown(t *testing.T) {
|
|
controller := gomock.NewController(t)
|
|
defer controller.Finish()
|
|
|
|
tlm := mustCreateTestPhysicalTaskQueueManager(t, controller)
|
|
tlm.Start()
|
|
err := tlm.WaitUntilInitialized(context.Background())
|
|
require.NoError(t, err)
|
|
|
|
err = tlm.backlogMgr.SpoolTask(&persistencespb.TaskInfo{})
|
|
require.NoError(t, err)
|
|
|
|
// stop the task writer explicitly
|
|
tlm.backlogMgr.taskWriter.Stop()
|
|
|
|
// now attempt to add a task
|
|
err = tlm.backlogMgr.SpoolTask(&persistencespb.TaskInfo{})
|
|
require.Error(t, err)
|
|
}
|