Skip to content

Commit abf5779

Browse files
authored
Reduce redundant history tree creation for workflow reset (#825)
* When performing workflow reset & terminating current workflow, one unnecessary create history tree API is called, this PR remove the redundant call to improve performance
1 parent 490d2fa commit abf5779

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

common/persistence/sql/sqlplugin/mysql/events.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ const (
4242
deleteHistoryNodesQuery = `DELETE FROM history_node WHERE shard_id = ? AND tree_id = ? AND branch_id = ? AND node_id >= ? `
4343

4444
// below are templates for history_tree table
45-
upsertHistoryTreeQuery = `INSERT INTO history_tree (` +
45+
addHistoryTreeQuery = `INSERT INTO history_tree (` +
4646
`shard_id, tree_id, branch_id, data, data_encoding) ` +
47-
`VALUES (:shard_id, :tree_id, :branch_id, :data, :data_encoding) ` +
48-
`ON DUPLICATE KEY UPDATE ` +
49-
`data=VALUES(data), data_encoding=VALUES(data_encoding)`
47+
`VALUES (:shard_id, :tree_id, :branch_id, :data, :data_encoding) `
5048

5149
getHistoryTreeQuery = `SELECT branch_id, data, data_encoding FROM history_tree WHERE shard_id = ? AND tree_id = ? `
5250

@@ -83,7 +81,7 @@ func (mdb *db) DeleteFromHistoryNode(filter sqlplugin.HistoryNodeDeleteFilter) (
8381

8482
// InsertIntoHistoryTree inserts a row into history_tree table
8583
func (mdb *db) InsertIntoHistoryTree(row *sqlplugin.HistoryTreeRow) (sql.Result, error) {
86-
return mdb.conn.NamedExec(upsertHistoryTreeQuery, row)
84+
return mdb.conn.NamedExec(addHistoryTreeQuery, row)
8785
}
8886

8987
// SelectFromHistoryTree reads one or more rows from history_tree table

common/persistence/sql/sqlplugin/postgresql/events.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ const (
4242
deleteHistoryNodesQuery = `DELETE FROM history_node WHERE shard_id = $1 AND tree_id = $2 AND branch_id = $3 AND node_id >= $4 `
4343

4444
// below are templates for history_tree table
45-
upsertHistoryTreeQuery = `INSERT INTO history_tree (` +
45+
addHistoryTreeQuery = `INSERT INTO history_tree (` +
4646
`shard_id, tree_id, branch_id, data, data_encoding) ` +
47-
`VALUES (:shard_id, :tree_id, :branch_id, :data, :data_encoding) ` +
48-
`ON CONFLICT (shard_id, tree_id, branch_id) DO UPDATE SET data = excluded.data, data_encoding = excluded.data_encoding`
47+
`VALUES (:shard_id, :tree_id, :branch_id, :data, :data_encoding) `
4948

5049
getHistoryTreeQuery = `SELECT branch_id, data, data_encoding FROM history_tree WHERE shard_id = $1 AND tree_id = $2 `
5150

@@ -82,7 +81,7 @@ func (pdb *db) DeleteFromHistoryNode(filter sqlplugin.HistoryNodeDeleteFilter) (
8281

8382
// InsertIntoHistoryTree inserts a row into history_tree table
8483
func (pdb *db) InsertIntoHistoryTree(row *sqlplugin.HistoryTreeRow) (sql.Result, error) {
85-
return pdb.conn.NamedExec(upsertHistoryTreeQuery, row)
84+
return pdb.conn.NamedExec(addHistoryTreeQuery, row)
8685
}
8786

8887
// SelectFromHistoryTree reads one or more rows from history_tree table

common/persistence/sql/sqlplugin/tests/history_tree_test.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,22 @@ func (s *historyTreeSuite) TestInsert_Success() {
9090
s.Equal(1, int(rowsAffected))
9191
}
9292

93-
// TODO: when issue https://github.com/temporalio/temporal/issues/818
94-
// is resolved, uncomment this test
95-
//func (s *historyTreeSuite) TestInsert_Fail_Duplicate() {
96-
// shardID := rand.Int31()
97-
// treeID := primitives.NewUUID()
98-
// branchID := primitives.NewUUID()
99-
//
100-
// node := s.newRandomTreeRow(shardID, treeID, branchID)
101-
// result, err := s.store.InsertIntoHistoryTree(&node)
102-
// s.NoError(err)
103-
// rowsAffected, err := result.RowsAffected()
104-
// s.NoError(err)
105-
// s.Equal(1, int(rowsAffected))
106-
//
107-
// node = s.newRandomTreeRow(shardID, treeID, branchID)
108-
// _, err = s.store.InsertIntoHistoryTree(&node)
109-
// s.Error(err) // TODO persistence layer should do proper error translation
110-
//}
93+
func (s *historyTreeSuite) TestInsert_Fail_Duplicate() {
94+
shardID := rand.Int31()
95+
treeID := primitives.NewUUID()
96+
branchID := primitives.NewUUID()
97+
98+
node := s.newRandomTreeRow(shardID, treeID, branchID)
99+
result, err := s.store.InsertIntoHistoryTree(&node)
100+
s.NoError(err)
101+
rowsAffected, err := result.RowsAffected()
102+
s.NoError(err)
103+
s.Equal(1, int(rowsAffected))
104+
105+
node = s.newRandomTreeRow(shardID, treeID, branchID)
106+
_, err = s.store.InsertIntoHistoryTree(&node)
107+
s.Error(err) // TODO persistence layer should do proper error translation
108+
}
111109

112110
func (s *historyTreeSuite) TestInsertSelect() {
113111
shardID := rand.Int31()

service/history/workflowExecutionContext.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -699,12 +699,22 @@ func (c *workflowExecutionContextImpl) updateWorkflowExecutionWithNew(
699699
}
700700
newWorkflowSizeSize := newContext.getHistorySize()
701701
startEvents := newWorkflowEventsSeq[0]
702-
eventsSize, err := c.persistFirstWorkflowEvents(startEvents)
703-
if err != nil {
704-
return err
702+
firstEventID := startEvents.Events[0].EventId
703+
if firstEventID == common.FirstEventID {
704+
eventsSize, err := c.persistFirstWorkflowEvents(startEvents)
705+
if err != nil {
706+
return err
707+
}
708+
newWorkflowSizeSize += eventsSize
709+
newContext.setHistorySize(newWorkflowSizeSize)
710+
} else {
711+
eventsSize, err := c.persistNonFirstWorkflowEvents(startEvents)
712+
if err != nil {
713+
return err
714+
}
715+
newWorkflowSizeSize += eventsSize
716+
newContext.setHistorySize(newWorkflowSizeSize)
705717
}
706-
newWorkflowSizeSize += eventsSize
707-
newContext.setHistorySize(newWorkflowSizeSize)
708718
newWorkflow.ExecutionStats = &persistenceblobs.ExecutionStats{
709719
HistorySize: newWorkflowSizeSize,
710720
}

0 commit comments

Comments
 (0)