/
tasklist.go
151 lines (136 loc) · 4.88 KB
/
tasklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2019 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 matching
import (
"bytes"
"fmt"
"strconv"
"strings"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/persistence"
)
type (
// taskListID is the key that uniquely identifies a task list
taskListID struct {
qualifiedTaskListName
domainID string
taskType int
}
// qualifiedTaskListName refers to the fully qualified task list name
qualifiedTaskListName struct {
name string // internal name of the tasks list
baseName string // original name of the task list as specified by user
partition int // partitionID of task list
}
)
// newTaskListName returns a fully qualified task list name.
// Fully qualified names contain additional metadata about task list
// derived from their given name. The additional metadata only makes sense
// when a task list has more than one partition. When there is more than
// one partition for a user specified task list, each of the
// individual partitions have an internal name of the form
//
// /__cadence_sys/[original-name]/[partitionID]
//
// The name of the root partition is always the same as the user specified name. Rest of
// the partitions follow the naming convention above. In addition, the task lists partitions
// logically form a N-ary tree where N is configurable dynamically. The tree formation is an
// optimization to allow for partitioned task lists to dispatch tasks with low latency when
// throughput is low - See https://github.com/uber/cadence/issues/2098
//
// Returns error if the given name is non-compliant with the required format
// for task list names
func newTaskListName(name string) (qualifiedTaskListName, error) {
tn := qualifiedTaskListName{
name: name,
baseName: name,
}
if err := tn.init(); err != nil {
return qualifiedTaskListName{}, err
}
return tn, nil
}
// IsRoot returns true if this task list is a root partition
func (tn *qualifiedTaskListName) IsRoot() bool {
return tn.partition == 0
}
// GetRoot returns the root name for a task list
func (tn *qualifiedTaskListName) GetRoot() string {
return tn.baseName
}
// Parent returns the name of the parent task list
// input:
// degree: Number of children at each level of the tree
// Returns empty string if this task list is the root
func (tn *qualifiedTaskListName) Parent(degree int) string {
if tn.IsRoot() || degree == 0 {
return ""
}
pid := (tn.partition+degree-1)/degree - 1
return tn.mkName(pid)
}
func (tn *qualifiedTaskListName) mkName(partition int) string {
if partition == 0 {
return tn.baseName
}
return fmt.Sprintf("%v%v/%v", common.ReservedTaskListPrefix, tn.baseName, partition)
}
func (tn *qualifiedTaskListName) init() error {
if !strings.HasPrefix(tn.name, common.ReservedTaskListPrefix) {
return nil
}
suffixOff := strings.LastIndex(tn.name, "/")
if suffixOff <= len(common.ReservedTaskListPrefix) {
return fmt.Errorf("invalid partitioned task list name %v", tn.name)
}
p, err := strconv.Atoi(tn.name[suffixOff+1:])
if err != nil || p <= 0 {
return fmt.Errorf("invalid partitioned task list name %v", tn.name)
}
tn.partition = p
tn.baseName = tn.name[len(common.ReservedTaskListPrefix):suffixOff]
return nil
}
// newTaskListID returns taskListID which uniquely identfies as task list
func newTaskListID(domainID, taskListName string, taskType int) (*taskListID, error) {
name, err := newTaskListName(taskListName)
if err != nil {
return nil, err
}
return &taskListID{
qualifiedTaskListName: name,
domainID: domainID,
taskType: taskType,
}, nil
}
func (tid *taskListID) String() string {
var b bytes.Buffer
b.WriteString("[")
b.WriteString("name=")
b.WriteString(tid.name)
b.WriteString("type=")
if tid.taskType == persistence.TaskListTypeActivity {
b.WriteString("activity")
} else {
b.WriteString("decision")
}
b.WriteString("]")
return b.String()
}