-
Notifications
You must be signed in to change notification settings - Fork 770
/
history_store.go
502 lines (459 loc) · 13.7 KB
/
history_store.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// 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 sql
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"math"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/api/serviceerror"
p "go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/sql/sqlplugin"
"go.temporal.io/server/common/primitives"
)
const (
// NOTE: transaction ID is *= -1 in DB
MinTxnID int64 = math.MaxInt64
MaxTxnID int64 = math.MinInt64 + 1 // int overflow
)
// AppendHistoryNodes add(or override) a node to a history branch
func (m *sqlExecutionStore) AppendHistoryNodes(
ctx context.Context,
request *p.InternalAppendHistoryNodesRequest,
) error {
branchInfo := request.BranchInfo
node := request.Node
treeIDBytes, err := primitives.ParseUUID(branchInfo.GetTreeId())
if err != nil {
return err
}
branchIDBytes, err := primitives.ParseUUID(branchInfo.GetBranchId())
if err != nil {
return err
}
nodeRow := &sqlplugin.HistoryNodeRow{
TreeID: treeIDBytes,
BranchID: branchIDBytes,
NodeID: node.NodeID,
PrevTxnID: node.PrevTransactionID,
TxnID: node.TransactionID,
Data: node.Events.Data,
DataEncoding: node.Events.EncodingType.String(),
ShardID: request.ShardID,
}
if !request.IsNewBranch {
_, err = m.Db.InsertIntoHistoryNode(ctx, nodeRow)
switch err {
case nil:
return nil
case context.DeadlineExceeded, context.Canceled:
return &p.AppendHistoryTimeoutError{
Msg: err.Error(),
}
default:
if m.Db.IsDupEntryError(err) {
return &p.ConditionFailedError{Msg: fmt.Sprintf("AppendHistoryNodes: row already exist: %v", err)}
}
return serviceerror.NewUnavailable(fmt.Sprintf("AppendHistoryNodes: %v", err))
}
}
treeInfoBlob := request.TreeInfo
treeRow := &sqlplugin.HistoryTreeRow{
ShardID: request.ShardID,
TreeID: treeIDBytes,
BranchID: branchIDBytes,
Data: treeInfoBlob.Data,
DataEncoding: treeInfoBlob.EncodingType.String(),
}
return m.txExecute(ctx, "AppendHistoryNodes", func(tx sqlplugin.Tx) error {
result, err := tx.InsertIntoHistoryNode(ctx, nodeRow)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if !(rowsAffected == 1 || rowsAffected == 2) {
return fmt.Errorf("expected 1 or 2 row to be affected for node table, got %v", rowsAffected)
}
result, err = tx.InsertIntoHistoryTree(ctx, treeRow)
switch err {
case nil:
rowsAffected, err = result.RowsAffected()
if err != nil {
return err
}
if !(rowsAffected == 1 || rowsAffected == 2) {
return fmt.Errorf("expected 1 or 2 rows to be affected for tree table as we allow upserts, got %v", rowsAffected)
}
return nil
case context.DeadlineExceeded, context.Canceled:
return &p.AppendHistoryTimeoutError{
Msg: err.Error(),
}
default:
return serviceerror.NewUnavailable(fmt.Sprintf("AppendHistoryNodes: %v", err))
}
})
}
func (m *sqlExecutionStore) DeleteHistoryNodes(
ctx context.Context,
request *p.InternalDeleteHistoryNodesRequest,
) error {
branchInfo := request.BranchInfo
nodeID := request.NodeID
txnID := request.TransactionID
shardID := request.ShardID
if nodeID < p.GetBeginNodeID(branchInfo) {
return &p.InvalidPersistenceRequestError{
Msg: "cannot append to ancestors' nodes",
}
}
treeIDBytes, err := primitives.ParseUUID(branchInfo.GetTreeId())
if err != nil {
return err
}
branchIDBytes, err := primitives.ParseUUID(branchInfo.GetBranchId())
if err != nil {
return err
}
nodeRow := &sqlplugin.HistoryNodeRow{
TreeID: treeIDBytes,
BranchID: branchIDBytes,
NodeID: nodeID,
TxnID: txnID,
ShardID: shardID,
}
_, err = m.Db.DeleteFromHistoryNode(ctx, nodeRow)
if err != nil {
return serviceerror.NewUnavailable(fmt.Sprintf("DeleteHistoryNodes: %v", err))
}
return nil
}
type historyNodePaginationToken struct {
LastNodeID int64
LastTxnID int64
}
// ReadHistoryBranch returns history node data for a branch
func (m *sqlExecutionStore) ReadHistoryBranch(
ctx context.Context,
request *p.InternalReadHistoryBranchRequest,
) (*p.InternalReadHistoryBranchResponse, error) {
branch, err := m.GetHistoryBranchUtil().ParseHistoryBranchInfo(request.BranchToken)
if err != nil {
return nil, err
}
branchIDBytes, err := primitives.ParseUUID(request.BranchID)
if err != nil {
return nil, err
}
treeIDBytes, err := primitives.ParseUUID(branch.TreeId)
if err != nil {
return nil, err
}
var token *historyNodePaginationToken
if len(request.NextPageToken) == 0 {
if request.ReverseOrder {
token = &historyNodePaginationToken{LastNodeID: request.MaxNodeID, LastTxnID: MaxTxnID}
} else {
token = &historyNodePaginationToken{LastNodeID: request.MinNodeID, LastTxnID: MinTxnID}
}
} else {
token, err = deserializePageTokenJson[historyNodePaginationToken](request.NextPageToken)
if err != nil {
return nil, err
}
}
minNodeId, maxNodeId := request.MinNodeID, request.MaxNodeID
minTxnId, maxTxnId := MinTxnID, MaxTxnID
if request.ReverseOrder {
maxNodeId = token.LastNodeID
maxTxnId = token.LastTxnID
} else {
minNodeId = token.LastNodeID
minTxnId = token.LastTxnID
}
rows, err := m.Db.RangeSelectFromHistoryNode(ctx, sqlplugin.HistoryNodeSelectFilter{
ShardID: request.ShardID,
TreeID: treeIDBytes,
BranchID: branchIDBytes,
MinNodeID: minNodeId,
MinTxnID: minTxnId,
MaxNodeID: maxNodeId,
MaxTxnID: maxTxnId,
PageSize: request.PageSize,
MetadataOnly: request.MetadataOnly,
ReverseOrder: request.ReverseOrder,
})
switch err {
case nil:
// noop
case sql.ErrNoRows:
// noop
default:
return nil, err
}
nodes := make([]p.InternalHistoryNode, 0, len(rows))
for _, row := range rows {
nodes = append(nodes, p.InternalHistoryNode{
NodeID: row.NodeID,
PrevTransactionID: row.PrevTxnID,
TransactionID: row.TxnID,
Events: p.NewDataBlob(row.Data, row.DataEncoding),
})
}
var pagingToken []byte
if len(rows) < request.PageSize {
pagingToken = nil
} else {
lastRow := rows[len(rows)-1]
pagingToken, err = serializePageTokenJson(&historyNodePaginationToken{
LastNodeID: lastRow.NodeID,
LastTxnID: lastRow.TxnID,
})
if err != nil {
return nil, err
}
}
return &p.InternalReadHistoryBranchResponse{
Nodes: nodes,
NextPageToken: pagingToken,
}, nil
}
// ForkHistoryBranch forks a new branch from an existing branch
// Note that application must provide a void forking nodeID, it must be a valid nodeID in that branch.
// A valid forking nodeID can be an ancestor from the existing branch.
// For example, we have branch B1 with three nodes(1[1,2], 3[3,4,5] and 6[6,7,8]. 1, 3 and 6 are nodeIDs (first eventID of the batch).
// So B1 looks like this:
//
// 1[1,2]
// /
// 3[3,4,5]
// /
// 6[6,7,8]
//
// Assuming we have branch B2 which contains one ancestor B1 stopping at 6 (exclusive). So B2 inherit nodeID 1 and 3 from B1, and have its own nodeID 6 and 8.
// Branch B2 looks like this:
//
// 1[1,2]
// /
// 3[3,4,5]
// \
// 6[6,7]
// \
// 8[8]
//
// Now we want to fork a new branch B3 from B2.
// The only valid forking nodeIDs are 3,6 or 8.
// 1 is not valid because we can't fork from first node.
// 2/4/5 is NOT valid either because they are inside a batch.
//
// Case #1: If we fork from nodeID 6, then B3 will have an ancestor B1 which stops at 6(exclusive).
// As we append a batch of events[6,7,8,9] to B3, it will look like :
//
// 1[1,2]
// /
// 3[3,4,5]
// \
// 6[6,7,8,9]
//
// Case #2: If we fork from node 8, then B3 will have two ancestors: B1 stops at 6(exclusive) and ancestor B2 stops at 8(exclusive)
// As we append a batch of events[8,9] to B3, it will look like:
//
// 1[1,2]
// /
// 3[3,4,5]
// /
// 6[6,7]
// \
// 8[8,9]
func (m *sqlExecutionStore) ForkHistoryBranch(
ctx context.Context,
request *p.InternalForkHistoryBranchRequest,
) error {
forkB := request.ForkBranchInfo
treeInfoBlob := request.TreeInfo
newBranchIdBytes, err := primitives.ParseUUID(request.NewBranchID)
if err != nil {
return err
}
treeIDBytes, err := primitives.ParseUUID(forkB.GetTreeId())
if err != nil {
return err
}
row := &sqlplugin.HistoryTreeRow{
ShardID: request.ShardID,
TreeID: treeIDBytes,
BranchID: newBranchIdBytes,
Data: treeInfoBlob.Data,
DataEncoding: treeInfoBlob.EncodingType.String(),
}
result, err := m.Db.InsertIntoHistoryTree(ctx, row)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if !(rowsAffected == 1 || rowsAffected == 2) {
return fmt.Errorf("expected 1 or 2 row to be affected for tree table, got %v", rowsAffected)
}
return nil
}
// DeleteHistoryBranch removes a branch
func (m *sqlExecutionStore) DeleteHistoryBranch(
ctx context.Context,
request *p.InternalDeleteHistoryBranchRequest,
) error {
branchIDBytes, err := primitives.ParseUUID(request.BranchInfo.BranchId)
if err != nil {
return err
}
treeIDBytes, err := primitives.ParseUUID(request.BranchInfo.TreeId)
if err != nil {
return err
}
return m.txExecute(ctx, "DeleteHistoryBranch", func(tx sqlplugin.Tx) error {
_, err = tx.DeleteFromHistoryTree(ctx, sqlplugin.HistoryTreeDeleteFilter{
TreeID: treeIDBytes,
BranchID: branchIDBytes,
ShardID: request.ShardID,
})
if err != nil {
return err
}
// delete each branch range
for _, br := range request.BranchRanges {
branchIDBytes, err := primitives.ParseUUID(br.BranchId)
if err != nil {
return err
}
deleteFilter := sqlplugin.HistoryNodeDeleteFilter{
ShardID: request.ShardID,
TreeID: treeIDBytes,
BranchID: branchIDBytes,
MinNodeID: br.BeginNodeId,
}
_, err = tx.RangeDeleteFromHistoryNode(ctx, deleteFilter)
if err != nil {
return err
}
}
return nil
})
}
// getAllHistoryTreeBranchesPaginationToken represents the primary key of the latest row in the history_tree table that
// we returned.
type getAllHistoryTreeBranchesPaginationToken struct {
ShardID int32
TreeID primitives.UUID
BranchID primitives.UUID
}
func (m *sqlExecutionStore) GetAllHistoryTreeBranches(
ctx context.Context,
request *p.GetAllHistoryTreeBranchesRequest,
) (*p.InternalGetAllHistoryTreeBranchesResponse, error) {
pageSize := request.PageSize
if pageSize <= 0 {
return nil, fmt.Errorf("PageSize must be greater than 0, but was %d", pageSize)
}
page := sqlplugin.HistoryTreeBranchPage{
Limit: pageSize,
}
if len(request.NextPageToken) != 0 {
var token getAllHistoryTreeBranchesPaginationToken
if err := json.Unmarshal(request.NextPageToken, &token); err != nil {
return nil, err
}
page.ShardID = token.ShardID
page.TreeID = token.TreeID
page.BranchID = token.BranchID
}
rows, err := m.Db.PaginateBranchesFromHistoryTree(ctx, page)
if err != nil {
return nil, err
}
branches := make([]p.InternalHistoryBranchDetail, 0, pageSize)
for _, row := range rows {
branch := p.InternalHistoryBranchDetail{
TreeID: row.TreeID.String(),
BranchID: row.BranchID.String(),
Data: row.Data,
Encoding: row.DataEncoding,
}
branches = append(branches, branch)
}
response := &p.InternalGetAllHistoryTreeBranchesResponse{
Branches: branches,
}
if len(branches) < pageSize {
// no next page token because there are no more results
return response, nil
}
// if we filled the page with rows, then set the next page token
lastRow := rows[len(rows)-1]
token := getAllHistoryTreeBranchesPaginationToken{
ShardID: lastRow.ShardID,
TreeID: lastRow.TreeID,
BranchID: lastRow.BranchID,
}
tokenBytes, err := json.Marshal(token)
if err != nil {
return nil, err
}
response.NextPageToken = tokenBytes
return response, nil
}
// GetHistoryTreeContainingBranch returns all branch information of a tree
func (m *sqlExecutionStore) GetHistoryTreeContainingBranch(
ctx context.Context,
request *p.InternalGetHistoryTreeContainingBranchRequest,
) (*p.InternalGetHistoryTreeContainingBranchResponse, error) {
branch, err := m.GetHistoryBranchUtil().ParseHistoryBranchInfo(request.BranchToken)
if err != nil {
return nil, err
}
treeID, err := primitives.ParseUUID(branch.TreeId)
if err != nil {
return nil, err
}
rows, err := m.Db.SelectFromHistoryTree(ctx, sqlplugin.HistoryTreeSelectFilter{
TreeID: treeID,
ShardID: request.ShardID,
})
if err == sql.ErrNoRows || (err == nil && len(rows) == 0) {
return &p.InternalGetHistoryTreeContainingBranchResponse{}, nil
}
treeInfos := make([]*commonpb.DataBlob, 0, len(rows))
for _, row := range rows {
treeInfos = append(treeInfos, p.NewDataBlob(row.Data, row.DataEncoding))
}
return &p.InternalGetHistoryTreeContainingBranchResponse{
TreeInfos: treeInfos,
}, nil
}