/
execution_maps.go
374 lines (330 loc) · 16.2 KB
/
execution_maps.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// 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 postgres
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/uber/cadence/common/persistence/sql/sqlplugin"
)
const (
deleteMapQueryTemplate = `DELETE FROM %v
WHERE
shard_id = $1 AND
domain_id = $2 AND
workflow_id = $3 AND
run_id = $4`
// %[2]v is the columns of the value struct (i.e. no primary key columns), comma separated
// %[3]v should be %[2]v with colons prepended.
// i.e. %[3]v = ",".join(":" + s for s in %[2]v)
// %[5]v should be %[2]v with "excluded." prepended.
// i.e. %[5]v = ",".join("excluded." + s for s in %[2]v)
// So that this query can be used with BindNamed
// %[4]v should be the name of the key associated with the map
// e.g. for ActivityInfo it is "schedule_id"
setKeyInMapQueryTemplate = `INSERT INTO %[1]v
(shard_id, domain_id, workflow_id, run_id, %[4]v, %[2]v)
VALUES
(:shard_id, :domain_id, :workflow_id, :run_id, :%[4]v, %[3]v)
ON CONFLICT (shard_id, domain_id, workflow_id, run_id, %[4]v) DO UPDATE
SET (shard_id, domain_id, workflow_id, run_id, %[4]v, %[2]v)
= (excluded.shard_id, excluded.domain_id, excluded.workflow_id, excluded.run_id, excluded.%[4]v, %[5]v)`
// %[2]v is the name of the key
deleteKeyInMapQueryTemplate = `DELETE FROM %[1]v
WHERE
shard_id = $1 AND
domain_id = $2 AND
workflow_id = $3 AND
run_id = $4 AND
%[2]v = $5`
// %[1]v is the name of the table
// %[2]v is the name of the key
// %[3]v is the value columns, separated by commas
getMapQueryTemplate = `SELECT %[2]v, %[3]v FROM %[1]v
WHERE
shard_id = $1 AND
domain_id = $2 AND
workflow_id = $3 AND
run_id = $4`
)
const (
deleteAllSignalsRequestedSetQuery = `DELETE FROM signals_requested_sets
WHERE
shard_id = $1 AND
domain_id = $2 AND
workflow_id = $3 AND
run_id = $4
`
createSignalsRequestedSetQuery = `INSERT INTO signals_requested_sets
(shard_id, domain_id, workflow_id, run_id, signal_id) VALUES
(:shard_id, :domain_id, :workflow_id, :run_id, :signal_id)
ON CONFLICT (shard_id, domain_id, workflow_id, run_id, signal_id) DO NOTHING`
deleteSignalsRequestedSetQuery = `DELETE FROM signals_requested_sets
WHERE
shard_id = $1 AND
domain_id = $2 AND
workflow_id = $3 AND
run_id = $4 AND
signal_id = $5`
getSignalsRequestedSetQuery = `SELECT signal_id FROM signals_requested_sets WHERE
shard_id = $1 AND
domain_id = $2 AND
workflow_id = $3 AND
run_id = $4`
)
func stringMap(a []string, f func(string) string) []string {
b := make([]string, len(a))
for i, v := range a {
b[i] = f(v)
}
return b
}
func makeDeleteMapQry(tableName string) string {
return fmt.Sprintf(deleteMapQueryTemplate, tableName)
}
func makeSetKeyInMapQry(tableName string, nonPrimaryKeyColumns []string, mapKeyName string) string {
return fmt.Sprintf(setKeyInMapQueryTemplate,
tableName,
strings.Join(nonPrimaryKeyColumns, ","),
strings.Join(stringMap(nonPrimaryKeyColumns, func(x string) string {
return ":" + x
}), ","),
mapKeyName,
strings.Join(stringMap(nonPrimaryKeyColumns, func(x string) string {
return "excluded." + x
}), ","))
}
func makeDeleteKeyInMapQry(tableName string, mapKeyName string) string {
return fmt.Sprintf(deleteKeyInMapQueryTemplate,
tableName,
mapKeyName)
}
func makeGetMapQryTemplate(tableName string, nonPrimaryKeyColumns []string, mapKeyName string) string {
return fmt.Sprintf(getMapQueryTemplate,
tableName,
mapKeyName,
strings.Join(nonPrimaryKeyColumns, ","))
}
var (
// Omit shard_id, run_id, domain_id, workflow_id, schedule_id since they're in the primary key
activityInfoColumns = []string{
"data",
"data_encoding",
"last_heartbeat_details",
"last_heartbeat_updated_time",
}
activityInfoTableName = "activity_info_maps"
activityInfoKey = "schedule_id"
deleteActivityInfoMapQry = makeDeleteMapQry(activityInfoTableName)
setKeyInActivityInfoMapQry = makeSetKeyInMapQry(activityInfoTableName, activityInfoColumns, activityInfoKey)
deleteKeyInActivityInfoMapQry = makeDeleteKeyInMapQry(activityInfoTableName, activityInfoKey)
getActivityInfoMapQry = makeGetMapQryTemplate(activityInfoTableName, activityInfoColumns, activityInfoKey)
)
// ReplaceIntoActivityInfoMaps replaces one or more rows in activity_info_maps table
func (pdb *db) ReplaceIntoActivityInfoMaps(ctx context.Context, rows []sqlplugin.ActivityInfoMapsRow) (sql.Result, error) {
for i := range rows {
rows[i].LastHeartbeatUpdatedTime = pdb.converter.ToPostgresDateTime(rows[i].LastHeartbeatUpdatedTime)
}
return pdb.conn.NamedExecContext(ctx, setKeyInActivityInfoMapQry, rows)
}
// SelectFromActivityInfoMaps reads one or more rows from activity_info_maps table
func (pdb *db) SelectFromActivityInfoMaps(ctx context.Context, filter *sqlplugin.ActivityInfoMapsFilter) ([]sqlplugin.ActivityInfoMapsRow, error) {
var rows []sqlplugin.ActivityInfoMapsRow
err := pdb.conn.SelectContext(ctx, &rows, getActivityInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
for i := 0; i < len(rows); i++ {
rows[i].ShardID = int64(filter.ShardID)
rows[i].DomainID = filter.DomainID
rows[i].WorkflowID = filter.WorkflowID
rows[i].RunID = filter.RunID
rows[i].LastHeartbeatUpdatedTime = pdb.converter.FromPostgresDateTime(rows[i].LastHeartbeatUpdatedTime)
}
return rows, err
}
// DeleteFromActivityInfoMaps deletes one or more rows from activity_info_maps table
func (pdb *db) DeleteFromActivityInfoMaps(ctx context.Context, filter *sqlplugin.ActivityInfoMapsFilter) (sql.Result, error) {
if filter.ScheduleID != nil {
return pdb.conn.ExecContext(ctx, deleteKeyInActivityInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID, *filter.ScheduleID)
}
return pdb.conn.ExecContext(ctx, deleteActivityInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
}
var (
timerInfoColumns = []string{
"data",
"data_encoding",
}
timerInfoTableName = "timer_info_maps"
timerInfoKey = "timer_id"
deleteTimerInfoMapSQLQuery = makeDeleteMapQry(timerInfoTableName)
setKeyInTimerInfoMapSQLQuery = makeSetKeyInMapQry(timerInfoTableName, timerInfoColumns, timerInfoKey)
deleteKeyInTimerInfoMapSQLQuery = makeDeleteKeyInMapQry(timerInfoTableName, timerInfoKey)
getTimerInfoMapSQLQuery = makeGetMapQryTemplate(timerInfoTableName, timerInfoColumns, timerInfoKey)
)
// ReplaceIntoTimerInfoMaps replaces one or more rows in timer_info_maps table
func (pdb *db) ReplaceIntoTimerInfoMaps(ctx context.Context, rows []sqlplugin.TimerInfoMapsRow) (sql.Result, error) {
return pdb.conn.NamedExecContext(ctx, setKeyInTimerInfoMapSQLQuery, rows)
}
// SelectFromTimerInfoMaps reads one or more rows from timer_info_maps table
func (pdb *db) SelectFromTimerInfoMaps(ctx context.Context, filter *sqlplugin.TimerInfoMapsFilter) ([]sqlplugin.TimerInfoMapsRow, error) {
var rows []sqlplugin.TimerInfoMapsRow
err := pdb.conn.SelectContext(ctx, &rows, getTimerInfoMapSQLQuery, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
for i := 0; i < len(rows); i++ {
rows[i].ShardID = int64(filter.ShardID)
rows[i].DomainID = filter.DomainID
rows[i].WorkflowID = filter.WorkflowID
rows[i].RunID = filter.RunID
}
return rows, err
}
// DeleteFromTimerInfoMaps deletes one or more rows from timer_info_maps table
func (pdb *db) DeleteFromTimerInfoMaps(ctx context.Context, filter *sqlplugin.TimerInfoMapsFilter) (sql.Result, error) {
if filter.TimerID != nil {
return pdb.conn.ExecContext(ctx, deleteKeyInTimerInfoMapSQLQuery, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID, *filter.TimerID)
}
return pdb.conn.ExecContext(ctx, deleteTimerInfoMapSQLQuery, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
}
var (
childExecutionInfoColumns = []string{
"data",
"data_encoding",
}
childExecutionInfoTableName = "child_execution_info_maps"
childExecutionInfoKey = "initiated_id"
deleteChildExecutionInfoMapQry = makeDeleteMapQry(childExecutionInfoTableName)
setKeyInChildExecutionInfoMapQry = makeSetKeyInMapQry(childExecutionInfoTableName, childExecutionInfoColumns, childExecutionInfoKey)
deleteKeyInChildExecutionInfoMapQry = makeDeleteKeyInMapQry(childExecutionInfoTableName, childExecutionInfoKey)
getChildExecutionInfoMapQry = makeGetMapQryTemplate(childExecutionInfoTableName, childExecutionInfoColumns, childExecutionInfoKey)
)
// ReplaceIntoChildExecutionInfoMaps replaces one or more rows in child_execution_info_maps table
func (pdb *db) ReplaceIntoChildExecutionInfoMaps(ctx context.Context, rows []sqlplugin.ChildExecutionInfoMapsRow) (sql.Result, error) {
return pdb.conn.NamedExecContext(ctx, setKeyInChildExecutionInfoMapQry, rows)
}
// SelectFromChildExecutionInfoMaps reads one or more rows from child_execution_info_maps table
func (pdb *db) SelectFromChildExecutionInfoMaps(ctx context.Context, filter *sqlplugin.ChildExecutionInfoMapsFilter) ([]sqlplugin.ChildExecutionInfoMapsRow, error) {
var rows []sqlplugin.ChildExecutionInfoMapsRow
err := pdb.conn.SelectContext(ctx, &rows, getChildExecutionInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
for i := 0; i < len(rows); i++ {
rows[i].ShardID = int64(filter.ShardID)
rows[i].DomainID = filter.DomainID
rows[i].WorkflowID = filter.WorkflowID
rows[i].RunID = filter.RunID
}
return rows, err
}
// DeleteFromChildExecutionInfoMaps deletes one or more rows from child_execution_info_maps table
func (pdb *db) DeleteFromChildExecutionInfoMaps(ctx context.Context, filter *sqlplugin.ChildExecutionInfoMapsFilter) (sql.Result, error) {
if filter.InitiatedID != nil {
return pdb.conn.ExecContext(ctx, deleteKeyInChildExecutionInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID, *filter.InitiatedID)
}
return pdb.conn.ExecContext(ctx, deleteChildExecutionInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
}
var (
requestCancelInfoColumns = []string{
"data",
"data_encoding",
}
requestCancelInfoTableName = "request_cancel_info_maps"
requestCancelInfoKey = "initiated_id"
deleteRequestCancelInfoMapQry = makeDeleteMapQry(requestCancelInfoTableName)
setKeyInRequestCancelInfoMapQry = makeSetKeyInMapQry(requestCancelInfoTableName, requestCancelInfoColumns, requestCancelInfoKey)
deleteKeyInRequestCancelInfoMapQry = makeDeleteKeyInMapQry(requestCancelInfoTableName, requestCancelInfoKey)
getRequestCancelInfoMapQry = makeGetMapQryTemplate(requestCancelInfoTableName, requestCancelInfoColumns, requestCancelInfoKey)
)
// ReplaceIntoRequestCancelInfoMaps replaces one or more rows in request_cancel_info_maps table
func (pdb *db) ReplaceIntoRequestCancelInfoMaps(ctx context.Context, rows []sqlplugin.RequestCancelInfoMapsRow) (sql.Result, error) {
return pdb.conn.NamedExecContext(ctx, setKeyInRequestCancelInfoMapQry, rows)
}
// SelectFromRequestCancelInfoMaps reads one or more rows from request_cancel_info_maps table
func (pdb *db) SelectFromRequestCancelInfoMaps(ctx context.Context, filter *sqlplugin.RequestCancelInfoMapsFilter) ([]sqlplugin.RequestCancelInfoMapsRow, error) {
var rows []sqlplugin.RequestCancelInfoMapsRow
err := pdb.conn.SelectContext(ctx, &rows, getRequestCancelInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
for i := 0; i < len(rows); i++ {
rows[i].ShardID = int64(filter.ShardID)
rows[i].DomainID = filter.DomainID
rows[i].WorkflowID = filter.WorkflowID
rows[i].RunID = filter.RunID
}
return rows, err
}
// DeleteFromRequestCancelInfoMaps deletes one or more rows from request_cancel_info_maps table
func (pdb *db) DeleteFromRequestCancelInfoMaps(ctx context.Context, filter *sqlplugin.RequestCancelInfoMapsFilter) (sql.Result, error) {
if filter.InitiatedID != nil {
return pdb.conn.ExecContext(ctx, deleteKeyInRequestCancelInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID, *filter.InitiatedID)
}
return pdb.conn.ExecContext(ctx, deleteRequestCancelInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
}
var (
signalInfoColumns = []string{
"data",
"data_encoding",
}
signalInfoTableName = "signal_info_maps"
signalInfoKey = "initiated_id"
deleteSignalInfoMapQry = makeDeleteMapQry(signalInfoTableName)
setKeyInSignalInfoMapQry = makeSetKeyInMapQry(signalInfoTableName, signalInfoColumns, signalInfoKey)
deleteKeyInSignalInfoMapQry = makeDeleteKeyInMapQry(signalInfoTableName, signalInfoKey)
getSignalInfoMapQry = makeGetMapQryTemplate(signalInfoTableName, signalInfoColumns, signalInfoKey)
)
// ReplaceIntoSignalInfoMaps replaces one or more rows in signal_info_maps table
func (pdb *db) ReplaceIntoSignalInfoMaps(ctx context.Context, rows []sqlplugin.SignalInfoMapsRow) (sql.Result, error) {
return pdb.conn.NamedExecContext(ctx, setKeyInSignalInfoMapQry, rows)
}
// SelectFromSignalInfoMaps reads one or more rows from signal_info_maps table
func (pdb *db) SelectFromSignalInfoMaps(ctx context.Context, filter *sqlplugin.SignalInfoMapsFilter) ([]sqlplugin.SignalInfoMapsRow, error) {
var rows []sqlplugin.SignalInfoMapsRow
err := pdb.conn.SelectContext(ctx, &rows, getSignalInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
for i := 0; i < len(rows); i++ {
rows[i].ShardID = int64(filter.ShardID)
rows[i].DomainID = filter.DomainID
rows[i].WorkflowID = filter.WorkflowID
rows[i].RunID = filter.RunID
}
return rows, err
}
// DeleteFromSignalInfoMaps deletes one or more rows from signal_info_maps table
func (pdb *db) DeleteFromSignalInfoMaps(ctx context.Context, filter *sqlplugin.SignalInfoMapsFilter) (sql.Result, error) {
if filter.InitiatedID != nil {
return pdb.conn.ExecContext(ctx, deleteKeyInSignalInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID, *filter.InitiatedID)
}
return pdb.conn.ExecContext(ctx, deleteSignalInfoMapQry, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
}
// InsertIntoSignalsRequestedSets inserts one or more rows into signals_requested_sets table
func (pdb *db) InsertIntoSignalsRequestedSets(ctx context.Context, rows []sqlplugin.SignalsRequestedSetsRow) (sql.Result, error) {
return pdb.conn.NamedExecContext(ctx, createSignalsRequestedSetQuery, rows)
}
// SelectFromSignalsRequestedSets reads one or more rows from signals_requested_sets table
func (pdb *db) SelectFromSignalsRequestedSets(ctx context.Context, filter *sqlplugin.SignalsRequestedSetsFilter) ([]sqlplugin.SignalsRequestedSetsRow, error) {
var rows []sqlplugin.SignalsRequestedSetsRow
err := pdb.conn.SelectContext(ctx, &rows, getSignalsRequestedSetQuery, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
for i := 0; i < len(rows); i++ {
rows[i].ShardID = int64(filter.ShardID)
rows[i].DomainID = filter.DomainID
rows[i].WorkflowID = filter.WorkflowID
rows[i].RunID = filter.RunID
}
return rows, err
}
// DeleteFromSignalsRequestedSets deletes one or more rows from signals_requested_sets table
func (pdb *db) DeleteFromSignalsRequestedSets(ctx context.Context, filter *sqlplugin.SignalsRequestedSetsFilter) (sql.Result, error) {
if filter.SignalID != nil {
return pdb.conn.ExecContext(ctx, deleteSignalsRequestedSetQuery, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID, *filter.SignalID)
}
return pdb.conn.ExecContext(ctx, deleteAllSignalsRequestedSetQuery, filter.ShardID, filter.DomainID, filter.WorkflowID, filter.RunID)
}