Skip to content

Commit

Permalink
Define type and name for task categories (#2522)
Browse files Browse the repository at this point in the history
  • Loading branch information
yycptt committed Feb 19, 2022
1 parent faa1dc1 commit 2e69c93
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 14 deletions.
6 changes: 3 additions & 3 deletions service/history/shard/context_impl.go
Expand Up @@ -1058,8 +1058,8 @@ func (s *ContextImpl) allocateTaskIDsLocked(
transferMaxReadLevel *int64,
) error {
currentCluster := s.GetClusterMetadata().GetCurrentClusterName()
for _, tasks := range newTasks {
for _, task := range tasks {
for category, tasksByCategory := range newTasks {
for _, task := range tasksByCategory {
// set taskID
id, err := s.generateTransferTaskIDLocked()
if err != nil {
Expand All @@ -1070,7 +1070,7 @@ func (s *ContextImpl) allocateTaskIDsLocked(
*transferMaxReadLevel = id

// if scheduled task, check if fire time is in the past
if !task.GetKey().FireTime.IsZero() {
if category.Type() == tasks.CategoryTypeScheduled {
ts := task.GetVisibilityTime()
if task.GetVersion() != common.EmptyVersion {
// cannot use version to determine the corresponding cluster for timer task
Expand Down
141 changes: 141 additions & 0 deletions service/history/tasks/category.go
@@ -0,0 +1,141 @@
// 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 tasks

import (
"fmt"
"sync"
)

type (
Category struct {
id int32
cType CategoryType
name string
}

CategoryType int
)

const (
CategoryIDUnspecified int32 = iota
CategoryIDTransfer
CategoryIDTimer
CategoryIDVisibility
CategoryIDReplication
)

const (
CategoryTypeUnspecified CategoryType = iota
CategoryTypeImmediate
CategoryTypeScheduled
)

const (
CategoryNameTransfer = "transfer"
CategoryNameTimer = "timer"
CategoryNameVisibility = "visibility"
CategoryNameReplication = "replication"
)

var (
CategoryTransfer = Category{
id: CategoryIDTransfer,
cType: CategoryTypeImmediate,
name: CategoryNameTransfer,
}

CategoryTimer = Category{
id: CategoryIDTimer,
cType: CategoryTypeScheduled,
name: CategoryNameTimer,
}

CategoryVisibility = Category{
id: CategoryIDVisibility,
cType: CategoryTypeImmediate,
name: CategoryNameVisibility,
}

CategoryReplication = Category{
id: CategoryIDReplication,
cType: CategoryTypeImmediate,
name: CategoryNameReplication,
}
)

var (
categories = struct {
sync.Mutex
list []Category
}{
list: []Category{
CategoryTransfer,
CategoryTimer,
CategoryVisibility,
CategoryReplication,
},
}
)

func NewCategory(
id int32,
categoryType CategoryType,
name string,
) Category {
categories.Lock()
defer categories.Unlock()

for _, existingCategory := range categories.list {
if existingCategory.ID() == id {
panic(fmt.Sprintf("category id: %v has already been used", id))
}

if existingCategory.Name() == name {
panic(fmt.Sprintf("categeory name: %s has already been used", name))
}
}

newCategory := Category{
id: id,
cType: categoryType,
name: name,
}
categories.list = append(categories.list, newCategory)

return newCategory
}

func (c *Category) ID() int32 {
return c.id
}

func (c *Category) Name() string {
return c.name
}

func (c *Category) Type() CategoryType {
return c.cType
}
10 changes: 0 additions & 10 deletions service/history/tasks/task.go
Expand Up @@ -38,8 +38,6 @@ type (

Keys []Key

Category int32

// Task is the generic task interface
Task interface {
GetKey() Key
Expand All @@ -57,14 +55,6 @@ type (
}
)

const (
CategoryUnspecified Category = iota
CategoryTransfer
CategoryTimer
CategoryVisibility
CategoryReplication
)

func (left Key) CompareTo(right Key) int {
if left.FireTime.Before(right.FireTime) {
return -1
Expand Down
2 changes: 1 addition & 1 deletion service/history/workflow/mutable_state_util.go
Expand Up @@ -73,7 +73,7 @@ func setTaskInfo(

for _, task := range tasksByCategory {
task.SetVersion(version)
if task.GetKey().FireTime.IsZero() {
if category.Type() == tasks.CategoryTypeImmediate {
task.SetVisibilityTime(timestamp)
}
}
Expand Down

0 comments on commit 2e69c93

Please sign in to comment.