Files
tasks/service/history/replication/sequential_batch_queue.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

118 lines
3.3 KiB
Go

package replication
import (
"sync"
"go.temporal.io/server/common/collection"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/metrics"
ctasks "go.temporal.io/server/common/tasks"
)
type (
SequentialBatchableTaskQueue struct {
id interface{}
sync.Mutex
taskQueue collection.Queue[*batchedTask]
lastTask *batchedTask
batchedIndividualTaskHandler func(task TrackableExecutableTask)
logger log.Logger
metricsHandler metrics.Handler
}
)
func NewSequentialBatchableTaskQueue(
task TrackableExecutableTask,
batchedIndividualTaskHandler func(task TrackableExecutableTask),
logger log.Logger,
metricsHandler metrics.Handler,
) ctasks.SequentialTaskQueue[TrackableExecutableTask] {
return &SequentialBatchableTaskQueue{
id: task.QueueID(),
taskQueue: collection.NewPriorityQueue[*batchedTask](
sequentialBatchableTaskQueueCompareLess,
),
batchedIndividualTaskHandler: batchedIndividualTaskHandler,
logger: logger,
metricsHandler: metricsHandler,
}
}
func (q *SequentialBatchableTaskQueue) ID() interface{} {
return q.id
}
func (q *SequentialBatchableTaskQueue) Peek() TrackableExecutableTask {
q.Lock()
defer q.Unlock()
return q.taskQueue.Peek()
}
// Add will try to batch input task with the last task in the queue. Since most likely incoming task
// are ordered by task ID, we only try to batch incoming task with last task in the queue.
func (q *SequentialBatchableTaskQueue) Add(task TrackableExecutableTask) {
q.Lock()
defer q.Unlock()
if q.lastTask != nil && q.lastTask.AddTask(task) {
return
}
incomingTask := q.createBatchedTask(task)
q.taskQueue.Add(incomingTask)
q.updateLastTask(incomingTask)
}
func (q *SequentialBatchableTaskQueue) Remove() (task TrackableExecutableTask) {
q.Lock()
defer q.Unlock()
taskToRemove := q.taskQueue.Remove()
if taskToRemove == q.lastTask {
q.lastTask = nil
}
return taskToRemove
}
func (q *SequentialBatchableTaskQueue) IsEmpty() bool {
q.Lock()
defer q.Unlock()
return q.taskQueue.IsEmpty()
}
func (q *SequentialBatchableTaskQueue) Len() int {
q.Lock()
defer q.Unlock()
return q.taskQueue.Len()
}
func (q *SequentialBatchableTaskQueue) updateLastTask(task *batchedTask) {
if q.lastTask == nil || sequentialBatchableTaskQueueCompareLess(q.lastTask, task) {
q.lastTask = task
}
}
func (q *SequentialBatchableTaskQueue) createBatchedTask(task TrackableExecutableTask) *batchedTask {
return &batchedTask{
batchedTask: task,
individualTasks: []TrackableExecutableTask{task},
state: batchStateOpen,
// This is to add individual task back to this queue, so it can be processed again. This is based on an assumption: only one thread is
// interacting with the queue. And this is a shortcut because a proper way is to resubmit the individual tasks back to scheduler.
// But that requires a refactor on scheduler and task lifecycle and could be risky to included in this feature implementation.
//
individualTaskHandler: func(task TrackableExecutableTask) {
q.Add(task)
},
logger: q.logger,
metricsHandler: q.metricsHandler,
}
}
func sequentialBatchableTaskQueueCompareLess(this *batchedTask, that *batchedTask) bool {
return SequentialTaskQueueCompareLess(this, that)
}