Fixing file info caching issue with Aurora for master (#5477)

Этот коммит содержится в:
Corey Hulen
2017-02-20 13:40:32 -05:00
коммит произвёл Christopher Speller
родитель dd4d8440ea
Коммит 274b9b6032
8 изменённых файлов: 39 добавлений и 17 удалений

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

@@ -421,13 +421,16 @@ func getFileInfosForPost(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
if infos, err := app.GetFileInfosForPost(postId); err != nil { if infos, err := app.GetFileInfosForPost(postId, false); err != nil {
c.Err = err c.Err = err
return return
} else if HandleEtag(model.GetEtagForFileInfos(infos), "Get File Infos For Post", w, r) { } else if HandleEtag(model.GetEtagForFileInfos(infos), "Get File Infos For Post", w, r) {
return return
} else { } else {
w.Header().Set("Cache-Control", "max-age=2592000, public") if len(infos) > 0 {
w.Header().Set("Cache-Control", "max-age=2592000, public")
}
w.Header().Set(model.HEADER_ETAG_SERVER, model.GetEtagForFileInfos(infos)) w.Header().Set(model.HEADER_ETAG_SERVER, model.GetEtagForFileInfos(infos))
w.Write([]byte(model.FileInfosToJson(infos))) w.Write([]byte(model.FileInfosToJson(infos)))
} }

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

@@ -136,7 +136,7 @@ func TestCreatePost(t *testing.T) {
} else if rpost9 := resp.Data.(*model.Post); len(rpost9.FileIds) != 3 { } else if rpost9 := resp.Data.(*model.Post); len(rpost9.FileIds) != 3 {
t.Fatal("post should have 3 files") t.Fatal("post should have 3 files")
} else { } else {
infos := store.Must(app.Srv.Store.FileInfo().GetForPost(rpost9.Id, true)).([]*model.FileInfo) infos := store.Must(app.Srv.Store.FileInfo().GetForPost(rpost9.Id, true, true)).([]*model.FileInfo)
if len(infos) != 3 { if len(infos) != 3 {
t.Fatal("should've attached all 3 files to post") t.Fatal("should've attached all 3 files to post")

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

@@ -319,7 +319,7 @@ func MigrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo {
return []*model.FileInfo{} return []*model.FileInfo{}
} else if newPost := result.Data.(*model.PostList).Posts[post.Id]; len(newPost.Filenames) != len(post.Filenames) { } else if newPost := result.Data.(*model.PostList).Posts[post.Id]; len(newPost.Filenames) != len(post.Filenames) {
// Another thread has already created FileInfos for this post, so just return those // Another thread has already created FileInfos for this post, so just return those
if result := <-Srv.Store.FileInfo().GetForPost(post.Id, false); result.Err != nil { if result := <-Srv.Store.FileInfo().GetForPost(post.Id, true, false); result.Err != nil {
l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.get_post_file_infos_again.app_error"), post.Id, result.Err) l4g.Error(utils.T("api.file.migrate_filenames_to_file_infos.get_post_file_infos_again.app_error"), post.Id, result.Err)
return []*model.FileInfo{} return []*model.FileInfo{}
} else { } else {

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

@@ -26,7 +26,11 @@ import (
func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User) ([]string, *model.AppError) { func SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User) ([]string, *model.AppError) {
pchan := Srv.Store.User().GetAllProfilesInChannel(channel.Id, true) pchan := Srv.Store.User().GetAllProfilesInChannel(channel.Id, true)
fchan := Srv.Store.FileInfo().GetForPost(post.Id, true) var fchan store.StoreChannel
if len(post.FileIds) != 0 {
fchan = Srv.Store.FileInfo().GetForPost(post.Id, true, true)
}
var profileMap map[string]*model.User var profileMap map[string]*model.User
if result := <-pchan; result.Err != nil { if result := <-pchan; result.Err != nil {
@@ -268,7 +272,7 @@ func SendNotifications(post *model.Post, team *model.Team, channel *model.Channe
message.Add("sender_name", senderUsername) message.Add("sender_name", senderUsername)
message.Add("team_id", team.Id) message.Add("team_id", team.Id)
if len(post.FileIds) != 0 { if len(post.FileIds) != 0 && fchan != nil {
message.Add("otherFile", "true") message.Add("otherFile", "true")
var infos []*model.FileInfo var infos []*model.FileInfo
@@ -410,7 +414,7 @@ func GetMessageForNotification(post *model.Post, translateFunc i18n.TranslateFun
// extract the filenames from their paths and determine what type of files are attached // extract the filenames from their paths and determine what type of files are attached
var infos []*model.FileInfo var infos []*model.FileInfo
if result := <-Srv.Store.FileInfo().GetForPost(post.Id, true); result.Err != nil { if result := <-Srv.Store.FileInfo().GetForPost(post.Id, true, true); result.Err != nil {
l4g.Warn(utils.T("api.post.get_message_for_notification.get_files.error"), post.Id, result.Err) l4g.Warn(utils.T("api.post.get_message_for_notification.get_files.error"), post.Id, result.Err)
} else { } else {
infos = result.Data.([]*model.FileInfo) infos = result.Data.([]*model.FileInfo)

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

@@ -463,9 +463,9 @@ func SearchPostsInTeam(terms string, userId string, teamId string, isOrSearch bo
return posts, nil return posts, nil
} }
func GetFileInfosForPost(postId string) ([]*model.FileInfo, *model.AppError) { func GetFileInfosForPost(postId string, readFromMaster bool) ([]*model.FileInfo, *model.AppError) {
pchan := Srv.Store.Post().GetSingle(postId) pchan := Srv.Store.Post().GetSingle(postId)
fchan := Srv.Store.FileInfo().GetForPost(postId, true) fchan := Srv.Store.FileInfo().GetForPost(postId, readFromMaster, true)
var infos []*model.FileInfo var infos []*model.FileInfo
if result := <-fchan; result.Err != nil { if result := <-fchan; result.Err != nil {

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

@@ -143,7 +143,7 @@ func (s SqlFileInfoStore) InvalidateFileInfosForPostCache(postId string) {
fileInfoCache.Remove(postId) fileInfoCache.Remove(postId)
} }
func (fs SqlFileInfoStore) GetForPost(postId string, allowFromCache bool) StoreChannel { func (fs SqlFileInfoStore) GetForPost(postId string, readFromMaster bool, allowFromCache bool) StoreChannel {
storeChannel := make(StoreChannel, 1) storeChannel := make(StoreChannel, 1)
go func() { go func() {
@@ -174,7 +174,13 @@ func (fs SqlFileInfoStore) GetForPost(postId string, allowFromCache bool) StoreC
var infos []*model.FileInfo var infos []*model.FileInfo
if _, err := fs.GetReplica().Select(&infos, dbmap := fs.GetReplica()
if readFromMaster {
dbmap = fs.GetMaster()
}
if _, err := dbmap.Select(&infos,
`SELECT `SELECT
* *
FROM FROM
@@ -187,7 +193,10 @@ func (fs SqlFileInfoStore) GetForPost(postId string, allowFromCache bool) StoreC
result.Err = model.NewLocAppError("SqlFileInfoStore.GetForPost", result.Err = model.NewLocAppError("SqlFileInfoStore.GetForPost",
"store.sql_file_info.get_for_post.app_error", nil, "post_id="+postId+", "+err.Error()) "store.sql_file_info.get_for_post.app_error", nil, "post_id="+postId+", "+err.Error())
} else { } else {
fileInfoCache.AddWithExpiresInSecs(postId, infos, FILE_INFO_CACHE_SEC) if len(infos) > 0 {
fileInfoCache.AddWithExpiresInSecs(postId, infos, FILE_INFO_CACHE_SEC)
}
result.Data = infos result.Data = infos
} }

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

@@ -114,13 +114,19 @@ func TestFileInfoGetForPost(t *testing.T) {
infos[i] = Must(store.FileInfo().Save(info)).(*model.FileInfo) infos[i] = Must(store.FileInfo().Save(info)).(*model.FileInfo)
} }
if result := <-store.FileInfo().GetForPost(postId, false); result.Err != nil { if result := <-store.FileInfo().GetForPost(postId, true, false); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 {
t.Fatal("should've returned exactly 2 file infos") t.Fatal("should've returned exactly 2 file infos")
} }
if result := <-store.FileInfo().GetForPost(postId, true); result.Err != nil { if result := <-store.FileInfo().GetForPost(postId, false, false); result.Err != nil {
t.Fatal(result.Err)
} else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 {
t.Fatal("should've returned exactly 2 file infos")
}
if result := <-store.FileInfo().GetForPost(postId, true, true); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 { } else if returned := result.Data.([]*model.FileInfo); len(returned) != 2 {
t.Fatal("should've returned exactly 2 file infos") t.Fatal("should've returned exactly 2 file infos")
@@ -163,7 +169,7 @@ func TestFileInfoAttachToPost(t *testing.T) {
info2 = Must(store.FileInfo().Get(info2.Id)).(*model.FileInfo) info2 = Must(store.FileInfo().Get(info2.Id)).(*model.FileInfo)
} }
if result := <-store.FileInfo().GetForPost(postId, false); result.Err != nil { if result := <-store.FileInfo().GetForPost(postId, true, false); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if infos := result.Data.([]*model.FileInfo); len(infos) != 2 { } else if infos := result.Data.([]*model.FileInfo); len(infos) != 2 {
t.Fatal("should've returned exactly 2 file infos") t.Fatal("should've returned exactly 2 file infos")
@@ -208,7 +214,7 @@ func TestFileInfoDeleteForPost(t *testing.T) {
t.Fatal(result.Err) t.Fatal(result.Err)
} }
if infos := Must(store.FileInfo().GetForPost(postId, false)).([]*model.FileInfo); len(infos) != 0 { if infos := Must(store.FileInfo().GetForPost(postId, true, false)).([]*model.FileInfo); len(infos) != 0 {
t.Fatal("shouldn't have returned any file infos") t.Fatal("shouldn't have returned any file infos")
} }
} }

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

@@ -326,7 +326,7 @@ type FileInfoStore interface {
Save(info *model.FileInfo) StoreChannel Save(info *model.FileInfo) StoreChannel
Get(id string) StoreChannel Get(id string) StoreChannel
GetByPath(path string) StoreChannel GetByPath(path string) StoreChannel
GetForPost(postId string, allowFromCache bool) StoreChannel GetForPost(postId string, readFromMaster bool, allowFromCache bool) StoreChannel
InvalidateFileInfosForPostCache(postId string) InvalidateFileInfosForPostCache(postId string)
AttachToPost(fileId string, postId string) StoreChannel AttachToPost(fileId string, postId string) StoreChannel
DeleteForPost(postId string) StoreChannel DeleteForPost(postId string) StoreChannel