Skip to content

Commit

Permalink
[MM-57991] Add log message about deleted rows from Data Retention job (
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzei authored Oct 5, 2024
1 parent 5a73852 commit 88a8053
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 33 deletions.
6 changes: 3 additions & 3 deletions server/channels/store/opentracinglayer/opentracinglayer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions server/channels/store/retrylayer/retrylayer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions server/channels/store/sqlstore/reaction_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ func (s SqlReactionStore) PermanentDeleteByUser(userId string) error {
return nil
}

func (s *SqlReactionStore) DeleteOrphanedRowsByIds(r *model.RetentionIdsForDeletion) error {
func (s *SqlReactionStore) DeleteOrphanedRowsByIds(r *model.RetentionIdsForDeletion) (int64, error) {
txn, err := s.GetMasterX().Beginx()
if err != nil {
return err
return 0, err
}
defer finalizeTransactionX(txn, &err)

Expand All @@ -343,18 +343,24 @@ func (s *SqlReactionStore) DeleteOrphanedRowsByIds(r *model.RetentionIdsForDelet
sq.Eq{"PostId": r.Ids},
)

_, err = txn.ExecBuilder(query)
sqlResult, err := txn.ExecBuilder(query)
if err != nil {
return errors.Wrapf(err, "failed to delete orphaned reactions with RetentionIdsForDeletion Id=%s", r.Id)
return 0, errors.Wrapf(err, "failed to delete orphaned reactions with RetentionIdsForDeletion Id=%s", r.Id)
}
err = deleteFromRetentionIdsTx(txn, r.Id)
if err != nil {
return err
return 0, err
}
if err = txn.Commit(); err != nil {
return err
return 0, err
}
return nil

rowsAffected, err := sqlResult.RowsAffected()
if err != nil {
return 0, errors.Wrap(err, "unable to retrieve rows affected")
}

return rowsAffected, nil
}

func (s *SqlReactionStore) PermanentDeleteBatch(endTime int64, limit int64) (int64, error) {
Expand Down
6 changes: 4 additions & 2 deletions server/channels/store/sqlstore/retention_policy_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,9 +1152,11 @@ func getDeleteQueriesForMySQL(r RetentionPolicyBatchDeletionInfo, query string)
return fmt.Sprintf("DELETE %s FROM %s INNER JOIN (%s) AS A ON %s", r.Table, r.Table, query, joinClause)
}

func deleteFromRetentionIdsTx(txn *sqlxTxWrapper, id string) (err error) {
if _, err := txn.Exec("DELETE FROM RetentionIdsForDeletion WHERE Id = ?", id); err != nil {
func deleteFromRetentionIdsTx(txn *sqlxTxWrapper, id string) error {
_, err := txn.Exec("DELETE FROM RetentionIdsForDeletion WHERE Id = ?", id)
if err != nil {
return errors.Wrap(err, "Failed to delete from RetentionIdsForDeletion")
}

return nil
}
2 changes: 1 addition & 1 deletion server/channels/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ type ReactionStore interface {
DeleteAllWithEmojiName(emojiName string) error
BulkGetForPosts(postIds []string) ([]*model.Reaction, error)
GetSingle(userID, postID, remoteID, emojiName string) (*model.Reaction, error)
DeleteOrphanedRowsByIds(r *model.RetentionIdsForDeletion) error
DeleteOrphanedRowsByIds(r *model.RetentionIdsForDeletion) (int64, error)
PermanentDeleteBatch(endTime int64, limit int64) (int64, error)
PermanentDeleteByUser(userID string) error
}
Expand Down
20 changes: 15 additions & 5 deletions server/channels/store/storetest/mocks/ReactionStore.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions server/channels/store/storetest/post_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4015,8 +4015,9 @@ func testPostStorePermanentDeleteBatch(t *testing.T, rctx request.CTX, ss store.
require.Equal(t, 1, len(rows))
require.Equal(t, 2, len(rows[0].Ids))
// Clean up retention ids table
err = ss.Reaction().DeleteOrphanedRowsByIds(rows[0])
deleted, err = ss.Reaction().DeleteOrphanedRowsByIds(rows[0])
require.NoError(t, err)
require.Equal(t, int64(0), deleted)

t.Run("with pagination", func(t *testing.T) {
for i := 0; i < 3; i++ {
Expand All @@ -4040,8 +4041,9 @@ func testPostStorePermanentDeleteBatch(t *testing.T, rctx request.CTX, ss store.
require.Equal(t, 2, len(rows[0].Ids))

// Clean up retention ids table
err = ss.Reaction().DeleteOrphanedRowsByIds(rows[0])
deleted, err = ss.Reaction().DeleteOrphanedRowsByIds(rows[0])
require.NoError(t, err)
require.Equal(t, int64(0), deleted)

deleted, _, err = ss.Post().PermanentDeleteBatchForRetentionPolicies(0, 2, 2, cursor)
require.NoError(t, err)
Expand All @@ -4053,8 +4055,9 @@ func testPostStorePermanentDeleteBatch(t *testing.T, rctx request.CTX, ss store.
require.Equal(t, 1, len(rows[0].Ids))

// Clean up retention ids table
err = ss.Reaction().DeleteOrphanedRowsByIds(rows[0])
deleted, err = ss.Reaction().DeleteOrphanedRowsByIds(rows[0])
require.NoError(t, err)
require.Equal(t, int64(0), deleted)
})

t.Run("with data retention policies", func(t *testing.T) {
Expand Down Expand Up @@ -4127,8 +4130,9 @@ func testPostStorePermanentDeleteBatch(t *testing.T, rctx request.CTX, ss store.
rows, err = ss.RetentionPolicy().GetIdsForDeletionByTableName("Posts", 1000)
require.NoError(t, err)
for _, row := range rows {
err = ss.Reaction().DeleteOrphanedRowsByIds(row)
deleted, err = ss.Reaction().DeleteOrphanedRowsByIds(row)
require.NoError(t, err)
require.Equal(t, int64(0), deleted)
}
})

Expand Down Expand Up @@ -4203,8 +4207,9 @@ func testPostStorePermanentDeleteBatch(t *testing.T, rctx request.CTX, ss store.

// Clean up retention ids table
for _, row := range rows {
err = ss.Reaction().DeleteOrphanedRowsByIds(row)
deleted, err = ss.Reaction().DeleteOrphanedRowsByIds(row)
require.NoError(t, err)
require.Equal(t, int64(0), deleted)
}
})
}
Expand Down
3 changes: 2 additions & 1 deletion server/channels/store/storetest/preference_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,9 @@ func testPreferenceDeleteOrphanedRows(t *testing.T, rctx request.CTX, ss store.S
require.Equal(t, 1, len(rows))

// Clean up retention ids table
err = ss.Reaction().DeleteOrphanedRowsByIds(rows[0])
deleted, err := ss.Reaction().DeleteOrphanedRowsByIds(rows[0])
require.NoError(t, err)
require.Equal(t, int64(0), deleted)

_, nErr = ss.Preference().DeleteOrphanedRows(limit)
assert.NoError(t, nErr)
Expand Down
4 changes: 3 additions & 1 deletion server/channels/store/storetest/reaction_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,10 @@ func testReactionStorePermanentDeleteBatch(t *testing.T, rctx request.CTX, ss st
require.Contains(t, rows[0].Ids, olderPost.Id)

for _, row := range rows {
err = ss.Reaction().DeleteOrphanedRowsByIds(row)
var deleted int64
deleted, err = ss.Reaction().DeleteOrphanedRowsByIds(row)
require.NoError(t, err)
require.Equal(t, int64(2), deleted)
}

rows, err = ss.RetentionPolicy().GetIdsForDeletionByTableName("Posts", 1000)
Expand Down
6 changes: 3 additions & 3 deletions server/channels/store/timerlayer/timerlayer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 88a8053

Please sign in to comment.