Files
tasks/service/matching/task_gc.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

102 lines
3.0 KiB
Go

package matching
import (
"context"
"sync/atomic"
"time"
"go.temporal.io/server/common/persistence"
)
type taskGC struct {
tqCtx context.Context
db *taskQueueDB
config *taskQueueConfig
lock int64
ackLevel int64
lastDeleteTime time.Time
}
var maxTimeBetweenTaskDeletes = time.Second
// newTaskGC returns an instance of a task garbage collector object
// taskGC internally maintains a delete cursor and attempts to delete
// a batch of tasks everytime Run() method is called.
//
// In order for the taskGC to actually delete tasks when Run() is called, one of
// two conditions must be met
// - Size Threshold: More than MaxDeleteBatchSize tasks are waiting to be deleted (rough estimation)
// - Time Threshold: Time since previous delete was attempted exceeds maxTimeBetweenTaskDeletes
//
// Finally, the Run() method is safe to be called from multiple threads. The underlying
// implementation will make sure only one caller executes Run() and others simply bail out
func newTaskGC(
tqCtx context.Context,
db *taskQueueDB,
config *taskQueueConfig,
) *taskGC {
return &taskGC{
tqCtx: tqCtx,
db: db,
config: config,
}
}
// Run deletes a batch of completed tasks, if it's possible to do so
// Only attempts deletion if size or time thresholds are met
func (tgc *taskGC) Run(ackLevel int64) {
tgc.tryDeleteNextBatch(ackLevel, false)
}
// RunNow deletes a batch of completed tasks if it's possible to do so
// This method attempts deletions without waiting for size/time threshold to be met
func (tgc *taskGC) RunNow(ackLevel int64) {
tgc.tryDeleteNextBatch(ackLevel, true)
}
func (tgc *taskGC) tryDeleteNextBatch(ackLevel int64, ignoreTimeCond bool) {
if !tgc.tryLock() {
return
}
defer tgc.unlock()
batchSize := tgc.config.MaxTaskDeleteBatchSize()
if !tgc.checkPrecond(ackLevel, batchSize, ignoreTimeCond) {
return
}
tgc.lastDeleteTime = time.Now().UTC()
ctx, cancel := context.WithTimeout(tgc.tqCtx, ioTimeout)
defer cancel()
n, err := tgc.db.CompleteTasksLessThan(ctx, ackLevel+1, batchSize, subqueueZero)
if err != nil {
return
}
// implementation behavior for CompleteTasksLessThan:
// - unit test, cassandra: always return UnknownNumRowsAffected (in this case means "all")
// - sql: return number of rows affected (should be <= batchSize)
// if we get UnknownNumRowsAffected or a smaller number than our limit, we know we got
// everything <= ackLevel, so we can reset ours. if not, we may have to try again.
if n == persistence.UnknownNumRowsAffected || n < batchSize {
tgc.ackLevel = ackLevel
}
}
func (tgc *taskGC) checkPrecond(ackLevel int64, batchSize int, ignoreTimeCond bool) bool {
backlog := ackLevel - tgc.ackLevel
if backlog >= int64(batchSize) {
return true
}
return backlog > 0 && (ignoreTimeCond || time.Now().UTC().Sub(tgc.lastDeleteTime) > maxTimeBetweenTaskDeletes)
}
func (tgc *taskGC) tryLock() bool {
return atomic.CompareAndSwapInt64(&tgc.lock, 0, 1)
}
func (tgc *taskGC) unlock() {
atomic.StoreInt64(&tgc.lock, 0)
}