mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
<!-- Describe what has changed in this PR --> **What changed?** Added support for specifying a separate activity worker for system-level worker service components. If not set, activities will use the same task queue name as workflow tasks. <!-- Tell your future self why have you made these changes --> **Why?** To give more fine-grained control over priorities and rate limits for different types of background processes. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** Existing tests. No functional changes in this PR <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** None <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?** No --------- Co-authored-by: David Reiss <[email protected]>
222 lines
8.1 KiB
Go
222 lines
8.1 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 reclaimresources
|
|
|
|
import (
|
|
stderrors "errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"go.temporal.io/sdk/temporal"
|
|
"go.temporal.io/sdk/workflow"
|
|
"go.temporal.io/server/common/primitives"
|
|
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/service/worker/deletenamespace/deleteexecutions"
|
|
"go.temporal.io/server/service/worker/deletenamespace/errors"
|
|
)
|
|
|
|
const (
|
|
WorkflowName = "temporal-sys-reclaim-namespace-resources-workflow"
|
|
|
|
namespaceCacheRefreshDelay = 11 * time.Second
|
|
)
|
|
|
|
type (
|
|
ReclaimResourcesParams struct {
|
|
deleteexecutions.DeleteExecutionsParams
|
|
|
|
// NamespaceDeleteDelay indicates the duration for how long ReclaimResourcesWorkflow
|
|
// will sleep between workflow execution and namespace deletion.
|
|
// Default is 0, means, workflow won't sleep.
|
|
NamespaceDeleteDelay time.Duration
|
|
}
|
|
|
|
ReclaimResourcesResult struct {
|
|
DeleteSuccessCount int
|
|
DeleteErrorCount int
|
|
NamespaceDeleted bool
|
|
}
|
|
)
|
|
|
|
var (
|
|
retryPolicy = &temporal.RetryPolicy{
|
|
InitialInterval: 1 * time.Second,
|
|
MaximumInterval: 10 * time.Second,
|
|
}
|
|
|
|
localActivityOptions = workflow.LocalActivityOptions{
|
|
RetryPolicy: retryPolicy,
|
|
StartToCloseTimeout: 30 * time.Second,
|
|
ScheduleToCloseTimeout: 5 * time.Minute,
|
|
}
|
|
|
|
deleteExecutionsWorkflowOptions = workflow.ChildWorkflowOptions{
|
|
RetryPolicy: retryPolicy,
|
|
WorkflowRunTimeout: 60 * time.Minute,
|
|
}
|
|
|
|
ensureNoExecutionsActivityRetryPolicy = &temporal.RetryPolicy{
|
|
InitialInterval: 1 * time.Second,
|
|
MaximumInterval: 2 * time.Minute,
|
|
BackoffCoefficient: 2,
|
|
}
|
|
|
|
ensureNoExecutionsStdVisibilityOptionsActivity = workflow.ActivityOptions{
|
|
RetryPolicy: ensureNoExecutionsActivityRetryPolicy,
|
|
StartToCloseTimeout: 30 * time.Second,
|
|
ScheduleToCloseTimeout: 30 * time.Minute, // ~20 attempts
|
|
}
|
|
|
|
ensureNoExecutionsAdvVisibilityActivityOptions = workflow.ActivityOptions{
|
|
RetryPolicy: ensureNoExecutionsActivityRetryPolicy,
|
|
StartToCloseTimeout: 30 * time.Second,
|
|
ScheduleToCloseTimeout: 10 * time.Hour, // Sanity check, advanced visibility can control the progress of activity.
|
|
}
|
|
)
|
|
|
|
func validateParams(params *ReclaimResourcesParams) error {
|
|
if params.NamespaceID.IsEmpty() {
|
|
return temporal.NewNonRetryableApplicationError("namespace ID is required", "", nil)
|
|
}
|
|
|
|
if params.Namespace.IsEmpty() {
|
|
return temporal.NewNonRetryableApplicationError("namespace is required", "", nil)
|
|
}
|
|
|
|
params.Config.ApplyDefaults()
|
|
|
|
return nil
|
|
}
|
|
|
|
func ReclaimResourcesWorkflow(ctx workflow.Context, params ReclaimResourcesParams) (ReclaimResourcesResult, error) {
|
|
logger := workflow.GetLogger(ctx)
|
|
logger.Info("Workflow started.", tag.WorkflowType(WorkflowName))
|
|
|
|
if err := validateParams(¶ms); err != nil {
|
|
return ReclaimResourcesResult{}, err
|
|
}
|
|
|
|
ctx = workflow.WithTaskQueue(ctx, primitives.DeleteNamespaceActivityTQ)
|
|
|
|
var la *LocalActivities
|
|
|
|
// Step 0. This workflow is started right after namespace is marked as DELETED and renamed.
|
|
// Wait for namespace cache refresh to make sure no new executions are created.
|
|
err := workflow.Sleep(ctx, namespaceCacheRefreshDelay)
|
|
if err != nil {
|
|
return ReclaimResourcesResult{}, err
|
|
}
|
|
|
|
// Step 1. Delete workflow executions.
|
|
result, err := deleteWorkflowExecutions(ctx, params)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
// Step 2. Sleep before deleting namespace from database.
|
|
err = workflow.Sleep(ctx, params.NamespaceDeleteDelay)
|
|
if err != nil {
|
|
return result, fmt.Errorf("%w: %v", errors.ErrUnableToSleep, err)
|
|
}
|
|
|
|
// Step 3. Delete namespace from database.
|
|
ctx5 := workflow.WithLocalActivityOptions(ctx, localActivityOptions)
|
|
err = workflow.ExecuteLocalActivity(ctx5, la.DeleteNamespaceActivity, params.NamespaceID, params.Namespace).Get(ctx, nil)
|
|
if err != nil {
|
|
return result, fmt.Errorf("%w: DeleteNamespaceActivity: %v", errors.ErrUnableToExecuteActivity, err)
|
|
}
|
|
|
|
result.NamespaceDeleted = true
|
|
logger.Info("Workflow finished successfully.", tag.WorkflowType(WorkflowName))
|
|
return result, nil
|
|
}
|
|
|
|
func deleteWorkflowExecutions(ctx workflow.Context, params ReclaimResourcesParams) (ReclaimResourcesResult, error) {
|
|
var a *Activities
|
|
var la *LocalActivities
|
|
|
|
logger := workflow.GetLogger(ctx)
|
|
var result ReclaimResourcesResult
|
|
|
|
ctx1 := workflow.WithLocalActivityOptions(ctx, localActivityOptions)
|
|
var isAdvancedVisibility bool
|
|
err := workflow.ExecuteLocalActivity(ctx1, la.IsAdvancedVisibilityActivity, params.Namespace).Get(ctx, &isAdvancedVisibility)
|
|
if err != nil {
|
|
return result, fmt.Errorf("%w: IsAdvancedVisibilityActivity: %v", errors.ErrUnableToExecuteActivity, err)
|
|
}
|
|
|
|
if isAdvancedVisibility {
|
|
ctx4 := workflow.WithLocalActivityOptions(ctx, localActivityOptions)
|
|
var executionsCount int64
|
|
err = workflow.ExecuteLocalActivity(ctx4, la.CountExecutionsAdvVisibilityActivity, params.NamespaceID, params.Namespace).Get(ctx, &executionsCount)
|
|
if err != nil {
|
|
return result, fmt.Errorf("%w: CountExecutionsAdvVisibilityActivity: %v", errors.ErrUnableToExecuteActivity, err)
|
|
}
|
|
if executionsCount == 0 {
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
ctx2 := workflow.WithChildOptions(ctx, deleteExecutionsWorkflowOptions)
|
|
ctx2 = workflow.WithWorkflowID(ctx2, fmt.Sprintf("%s/%s", deleteexecutions.WorkflowName, params.Namespace))
|
|
var der deleteexecutions.DeleteExecutionsResult
|
|
err = workflow.ExecuteChildWorkflow(ctx2, deleteexecutions.DeleteExecutionsWorkflow, params.DeleteExecutionsParams).Get(ctx, &der)
|
|
if err != nil {
|
|
logger.Error("Unable to execute child workflow.", tag.WorkflowType(deleteexecutions.WorkflowName), tag.Error(err))
|
|
return result, fmt.Errorf("%w: %s: %v", errors.ErrUnableToExecuteChildWorkflow, deleteexecutions.WorkflowName, err)
|
|
}
|
|
result.DeleteSuccessCount = der.SuccessCount
|
|
result.DeleteErrorCount = der.ErrorCount
|
|
|
|
if isAdvancedVisibility {
|
|
ctx3 := workflow.WithActivityOptions(ctx, ensureNoExecutionsAdvVisibilityActivityOptions)
|
|
err = workflow.ExecuteActivity(ctx3, a.EnsureNoExecutionsAdvVisibilityActivity, params.NamespaceID, params.Namespace, der.ErrorCount).Get(ctx, nil)
|
|
} else {
|
|
ctx3 := workflow.WithActivityOptions(ctx, ensureNoExecutionsStdVisibilityOptionsActivity)
|
|
err = workflow.ExecuteActivity(ctx3, a.EnsureNoExecutionsStdVisibilityActivity, params.NamespaceID, params.Namespace).Get(ctx, nil)
|
|
}
|
|
if err != nil {
|
|
var appErr *temporal.ApplicationError
|
|
if stderrors.As(err, &appErr) {
|
|
switch appErr.Type() {
|
|
case errors.ExecutionsStillExistErrType, errors.NoProgressErrType, errors.NotDeletedExecutionsStillExistErrType:
|
|
var notDeletedCount int
|
|
var counterTag tag.ZapTag
|
|
if appErr.HasDetails() {
|
|
_ = appErr.Details(¬DeletedCount)
|
|
counterTag = tag.Counter(notDeletedCount)
|
|
}
|
|
logger.Info("Unable to delete workflow executions.", tag.WorkflowNamespace(params.Namespace.String()), counterTag)
|
|
// appErr is not retryable. Convert it to retryable for the server to retry.
|
|
return result, temporal.NewApplicationError(appErr.Message(), appErr.Type(), notDeletedCount)
|
|
}
|
|
}
|
|
return result, fmt.Errorf("%w: EnsureNoExecutionsActivity: %v", errors.ErrUnableToExecuteActivity, err)
|
|
}
|
|
|
|
return result, nil
|
|
}
|