MM-58038: Use context to call master for DeletePost (#27098)

Calling app.DeletePost immediately after creating a post
is susceptible to replica lag because we were calling the
replica to check for the post.

We fix this by passing a context to always query master.

https://mattermost.atlassian.net/browse/MM-58038

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2024-05-24 19:35:48 +05:30
коммит произвёл GitHub
родитель 09c0eb7e7a
Коммит 6f3327ce0f
34 изменённых файлов: 94 добавлений и 93 удалений

Просмотреть файл

@@ -329,8 +329,9 @@ func (scs *Service) upsertSyncPost(post *model.Post, channel *model.Channel, rc
var appErr *model.AppError
post.RemoteId = model.NewString(rc.RemoteId)
rctx := request.EmptyContext(scs.server.Log())
rpost, err := scs.server.GetStore().Post().GetSingle(post.Id, true)
rpost, err := scs.server.GetStore().Post().GetSingle(rctx, post.Id, true)
if err != nil {
if _, ok := err.(errNotFound); !ok {
return nil, fmt.Errorf("error checking sync post: %w", err)
@@ -339,7 +340,7 @@ func (scs *Service) upsertSyncPost(post *model.Post, channel *model.Channel, rc
if rpost == nil {
// post doesn't exist; create new one
rpost, appErr = scs.app.CreatePost(request.EmptyContext(scs.server.Log()), post, channel, true, true)
rpost, appErr = scs.app.CreatePost(rctx, post, channel, true, true)
if appErr == nil {
scs.server.Log().Log(mlog.LvlSharedChannelServiceDebug, "Created sync post",
mlog.String("post_id", post.Id),
@@ -348,7 +349,7 @@ func (scs *Service) upsertSyncPost(post *model.Post, channel *model.Channel, rc
}
} else if post.DeleteAt > 0 {
// delete post
rpost, appErr = scs.app.DeletePost(request.EmptyContext(scs.server.Log()), post.Id, post.UserId)
rpost, appErr = scs.app.DeletePost(rctx, post.Id, post.UserId)
if appErr == nil {
scs.server.Log().Log(mlog.LvlSharedChannelServiceDebug, "Deleted sync post",
mlog.String("post_id", post.Id),

Просмотреть файл

@@ -322,7 +322,7 @@ func (scs *Service) handlePostError(postId string, task syncTask, rc *model.Remo
}
// this post failed as part of a group of posts. Retry as an individual post.
post, err := scs.server.GetStore().Post().GetSingle(postId, true)
post, err := scs.server.GetStore().Post().GetSingle(request.EmptyContext(scs.server.Log()), postId, true)
if err != nil {
scs.server.Log().Log(mlog.LvlSharedChannelServiceError, "error fetching post for sync retry",
mlog.String("remote", rc.DisplayName),

Просмотреть файл

@@ -270,7 +270,7 @@ func (scs *Service) fetchPostsForSync(sd *syncData) error {
return fmt.Errorf("could not fetch new posts for sync: %w", err)
}
count := len(posts)
sd.posts = appendPosts(sd.posts, posts, scs.server.GetStore().Post(), cursor.LastPostCreateAt)
sd.posts = appendPosts(sd.posts, posts, scs.server.GetStore().Post(), cursor.LastPostCreateAt, scs.server.Log())
cache := postsSliceToMap(posts)
@@ -284,7 +284,7 @@ func (scs *Service) fetchPostsForSync(sd *syncData) error {
}
posts = reducePostsSliceInCache(posts, cache)
count += len(posts)
sd.posts = appendPosts(sd.posts, posts, scs.server.GetStore().Post(), cursor.LastPostUpdateAt)
sd.posts = appendPosts(sd.posts, posts, scs.server.GetStore().Post(), cursor.LastPostUpdateAt, scs.server.Log())
}
sd.resultNextCursor = nextCursor
@@ -293,7 +293,7 @@ func (scs *Service) fetchPostsForSync(sd *syncData) error {
return nil
}
func appendPosts(dest []*model.Post, posts []*model.Post, postStore store.PostStore, timestamp int64) []*model.Post {
func appendPosts(dest []*model.Post, posts []*model.Post, postStore store.PostStore, timestamp int64, logger mlog.LoggerIFace) []*model.Post {
// Append the posts individually, checking for root posts that might appear later in the list.
// This is due to the UpdateAt collision handling algorithm where the order of posts is not based
// on UpdateAt or CreateAt when the posts have the same UpdateAt value. Here we are guarding
@@ -302,7 +302,7 @@ func appendPosts(dest []*model.Post, posts []*model.Post, postStore store.PostSt
// happens during load testing or bulk imports.
for _, p := range posts {
if p.RootId != "" {
root, err := postStore.GetSingle(p.RootId, true)
root, err := postStore.GetSingle(request.EmptyContext(logger), p.RootId, true)
if err == nil {
if (root.CreateAt >= timestamp || root.UpdateAt >= timestamp) && !containsPost(dest, root) {
dest = append(dest, root)