-
Notifications
You must be signed in to change notification settings - Fork 776
/
task.go
280 lines (251 loc) · 7.96 KB
/
task.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// 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 postgresql
import (
"context"
"database/sql"
"fmt"
"go.temporal.io/api/serviceerror"
"go.temporal.io/server/common/persistence/sql/sqlplugin"
)
const (
taskQueueCreatePart = `INTO task_queues(range_hash, task_queue_id, range_id, data, data_encoding) ` +
`VALUES (:range_hash, :task_queue_id, :range_id, :data, :data_encoding)`
// (default range ID: initialRangeID == 1)
createTaskQueueQry = `INSERT ` + taskQueueCreatePart
updateTaskQueueQry = `UPDATE task_queues SET
range_id = :range_id,
data = :data,
data_encoding = :data_encoding
WHERE
range_hash = :range_hash AND
task_queue_id = :task_queue_id
`
listTaskQueueRowSelect = `SELECT range_hash, task_queue_id, range_id, data, data_encoding from task_queues `
listTaskQueueWithHashRangeQry = listTaskQueueRowSelect +
`WHERE range_hash >= $1 AND range_hash <= $2 AND task_queue_id > $3 ORDER BY task_queue_id ASC LIMIT $4`
listTaskQueueQry = listTaskQueueRowSelect +
`WHERE range_hash = $1 AND task_queue_id > $2 ORDER BY task_queue_id ASC LIMIT $3`
getTaskQueueQry = listTaskQueueRowSelect +
`WHERE range_hash = $1 AND task_queue_id=$2`
deleteTaskQueueQry = `DELETE FROM task_queues WHERE range_hash=$1 AND task_queue_id=$2 AND range_id=$3`
lockTaskQueueQry = `SELECT range_id FROM task_queues ` +
`WHERE range_hash=$1 AND task_queue_id=$2 FOR UPDATE`
// *** Task_Queues Table Above ***
// *** Tasks Below ***
getTaskMinMaxQry = `SELECT task_id, data, data_encoding ` +
`FROM tasks ` +
`WHERE range_hash = $1 AND task_queue_id=$2 AND task_id >= $3 AND task_id < $4 ` +
`ORDER BY task_id LIMIT $5`
getTaskMinQry = `SELECT task_id, data, data_encoding ` +
`FROM tasks ` +
`WHERE range_hash = $1 AND task_queue_id = $2 AND task_id >= $3 ORDER BY task_id LIMIT $4`
createTaskQry = `INSERT INTO ` +
`tasks(range_hash, task_queue_id, task_id, data, data_encoding) ` +
`VALUES(:range_hash, :task_queue_id, :task_id, :data, :data_encoding)`
deleteTaskQry = `DELETE FROM tasks ` +
`WHERE range_hash = $1 AND task_queue_id = $2 AND task_id = $3`
rangeDeleteTaskQry = `DELETE FROM tasks ` +
`WHERE range_hash = $1 AND task_queue_id = $2 AND task_id IN (SELECT task_id FROM
tasks WHERE range_hash = $1 AND task_queue_id = $2 AND task_id < $3 ` +
`ORDER BY task_queue_id,task_id LIMIT $4 )`
)
// InsertIntoTasks inserts one or more rows into tasks table
func (pdb *db) InsertIntoTasks(
ctx context.Context,
rows []sqlplugin.TasksRow,
) (sql.Result, error) {
return pdb.conn.NamedExecContext(ctx,
createTaskQry,
rows,
)
}
// SelectFromTasks reads one or more rows from tasks table
func (pdb *db) SelectFromTasks(
ctx context.Context,
filter sqlplugin.TasksFilter,
) ([]sqlplugin.TasksRow, error) {
var err error
var rows []sqlplugin.TasksRow
switch {
case filter.ExclusiveMaxTaskID != nil:
err = pdb.conn.SelectContext(ctx,
&rows,
getTaskMinMaxQry,
filter.RangeHash,
filter.TaskQueueID,
*filter.InclusiveMinTaskID,
*filter.ExclusiveMaxTaskID,
*filter.PageSize,
)
default:
err = pdb.conn.SelectContext(ctx,
&rows,
getTaskMinQry,
filter.RangeHash,
filter.TaskQueueID,
*filter.InclusiveMinTaskID,
*filter.PageSize,
)
}
return rows, err
}
// DeleteFromTasks deletes one or more rows from tasks table
func (pdb *db) DeleteFromTasks(
ctx context.Context,
filter sqlplugin.TasksFilter,
) (sql.Result, error) {
if filter.ExclusiveMaxTaskID != nil {
if filter.Limit == nil || *filter.Limit == 0 {
return nil, fmt.Errorf("missing limit parameter")
}
return pdb.conn.ExecContext(ctx,
rangeDeleteTaskQry,
filter.RangeHash,
filter.TaskQueueID,
*filter.ExclusiveMaxTaskID,
*filter.Limit,
)
}
return pdb.conn.ExecContext(ctx,
deleteTaskQry,
filter.RangeHash,
filter.TaskQueueID,
*filter.TaskID,
)
}
// InsertIntoTaskQueues inserts one or more rows into task_queues table
func (pdb *db) InsertIntoTaskQueues(
ctx context.Context,
row *sqlplugin.TaskQueuesRow,
) (sql.Result, error) {
return pdb.conn.NamedExecContext(ctx,
createTaskQueueQry,
row,
)
}
// UpdateTaskQueues updates a row in task_queues table
func (pdb *db) UpdateTaskQueues(
ctx context.Context,
row *sqlplugin.TaskQueuesRow,
) (sql.Result, error) {
return pdb.conn.NamedExecContext(ctx,
updateTaskQueueQry,
row,
)
}
// SelectFromTaskQueues reads one or more rows from task_queues table
func (pdb *db) SelectFromTaskQueues(
ctx context.Context,
filter sqlplugin.TaskQueuesFilter,
) ([]sqlplugin.TaskQueuesRow, error) {
switch {
case filter.TaskQueueID != nil:
if filter.RangeHashLessThanEqualTo != 0 || filter.RangeHashGreaterThanEqualTo != 0 {
return nil, serviceerror.NewInternal("shardID range not supported for specific selection")
}
return pdb.selectFromTaskQueues(ctx, filter)
case filter.RangeHashLessThanEqualTo != 0 && filter.PageSize != nil:
if filter.RangeHashLessThanEqualTo < filter.RangeHashGreaterThanEqualTo {
return nil, serviceerror.NewInternal("range of hashes bound is invalid")
}
return pdb.rangeSelectFromTaskQueues(ctx, filter)
case filter.TaskQueueIDGreaterThan != nil && filter.PageSize != nil:
return pdb.rangeSelectFromTaskQueues(ctx, filter)
default:
return nil, serviceerror.NewInternal("invalid set of query filter params")
}
}
func (pdb *db) selectFromTaskQueues(
ctx context.Context,
filter sqlplugin.TaskQueuesFilter,
) ([]sqlplugin.TaskQueuesRow, error) {
var err error
var row sqlplugin.TaskQueuesRow
err = pdb.conn.GetContext(ctx,
&row,
getTaskQueueQry,
filter.RangeHash,
filter.TaskQueueID,
)
if err != nil {
return nil, err
}
return []sqlplugin.TaskQueuesRow{row}, err
}
func (pdb *db) rangeSelectFromTaskQueues(
ctx context.Context,
filter sqlplugin.TaskQueuesFilter,
) ([]sqlplugin.TaskQueuesRow, error) {
var err error
var rows []sqlplugin.TaskQueuesRow
if filter.RangeHashLessThanEqualTo > 0 {
err = pdb.conn.SelectContext(ctx,
&rows,
listTaskQueueWithHashRangeQry,
filter.RangeHashGreaterThanEqualTo,
filter.RangeHashLessThanEqualTo,
filter.TaskQueueIDGreaterThan,
*filter.PageSize,
)
} else {
err = pdb.conn.SelectContext(ctx,
&rows,
listTaskQueueQry,
filter.RangeHash,
filter.TaskQueueIDGreaterThan,
*filter.PageSize,
)
}
if err != nil {
return nil, err
}
return rows, nil
}
// DeleteFromTaskQueues deletes a row from task_queues table
func (pdb *db) DeleteFromTaskQueues(
ctx context.Context,
filter sqlplugin.TaskQueuesFilter,
) (sql.Result, error) {
return pdb.conn.ExecContext(ctx,
deleteTaskQueueQry,
filter.RangeHash,
filter.TaskQueueID,
*filter.RangeID,
)
}
// LockTaskQueues locks a row in task_queues table
func (pdb *db) LockTaskQueues(
ctx context.Context,
filter sqlplugin.TaskQueuesFilter,
) (int64, error) {
var rangeID int64
err := pdb.conn.GetContext(ctx,
&rangeID,
lockTaskQueueQry,
filter.RangeHash,
filter.TaskQueueID,
)
return rangeID, err
}