Files
tasks/common/timer/validation.go
Stephan Behnke 2d0e892384 Drop scheduled tasks if duration too long (#6124)
## What changed?
<!-- Describe what has changed in this PR -->
- Drop scheduled tasks if it's scheduled to run far in the future (
duration > 99 year )
- What if we want to recover those timers?
  - Do a namespace migration, that will re-create all the timers.

## Why?
<!-- Tell your future self why have you made these changes -->
- There's no point creating those timers. They just consume storage
spaces. Persistence implementations also don't have a good way for
handling them.
- #4775 capped all timer
settings to MaxAllowTimer for new workflows. But if workflows are
created before that check is added, we may still create timers for those
large durations. E.g. the new logic added for creating workflow
execution timeout timer based on the persisted workflow execution
expiration time.
- NOTE: all capped timers will be dropped as well.

## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
- Added unit test

## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->

## 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/`? -->

## Is hotfix candidate?
<!-- Is this PR a hotfix candidate or does it require a notification to
be sent to the broader community? (Yes/No) -->
- Yes
2024-06-13 18:01:44 -07:00

68 lines
2.4 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 timer
import (
"errors"
"time"
"google.golang.org/protobuf/types/known/durationpb"
"go.temporal.io/server/common/primitives/timestamp"
)
const (
// MaxAllowedTimer is the maximum allowed timer duration in the system
// exported for integration tests
MaxAllowedTimer = 100 * 365 * 24 * time.Hour
)
var (
errNegativeDuration = errors.New("negative timer duration")
// Pre-extracted for ease of use later
maxSeconds = MaxAllowedTimer.Nanoseconds() / 1e9
maxNanos = int32(MaxAllowedTimer.Nanoseconds() - maxSeconds*1e9)
)
// TODO: remove this logic, rely on scheduled task dropping logic in mutableState for long duration timers
func ValidateAndCapTimer(delay *durationpb.Duration) error {
duration := timestamp.DurationValue(delay)
if duration < 0 {
return errNegativeDuration
}
// unix nano (max int64) is 2262-04-11T23:47:16.854775807Z
// allowing 100 years timer is safe until 2162
//
// NOTE: we choose to cap the timer instead of returning error so that
// existing workflows implementation using higher than allowed timer
// can continue to run.
if duration > MaxAllowedTimer {
delay.Nanos = maxNanos
delay.Seconds = maxSeconds
}
return nil
}