MM-56906: Remove redundant calls on team switch (#30771)
On page load, we load ALL channels and channel members from all teams. But then, on team_switch, we would again load channels and channel members from that team. This was redundant and mainly kept because previously the websocket events were considered unreliable. Now with reliable websockets, and client-side pings, we can detect broken connections faster and recover without loss. Additionally, the getAllChannelMembers call would page through all responses on the client side. This was inefficient and incur extra latency. To optimize for this, we introduce server-side streaming of the full response if page is set to -1. This optimizes the intial response as well. https://mattermost.atlassian.net/browse/MM-56906 ```release-note Optimize team switch operation by removing calls to get channels and channel members. ``` Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
883711c72d
Коммит
4803892492
@@ -2071,6 +2071,27 @@ func (s *RetryLayerChannelStore) GetMembersForUser(teamID string, userID string)
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerChannelStore) GetMembersForUserWithCursorPagination(userId string, perPage int, fromChanneID string) (model.ChannelMembersWithTeamData, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.ChannelStore.GetMembersForUserWithCursorPagination(userId, perPage, fromChanneID)
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return result, err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return result, err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerChannelStore) GetMembersForUserWithPagination(userID string, page int, perPage int) (model.ChannelMembersWithTeamData, error) {
|
||||
|
||||
tries := 0
|
||||
|
||||
@@ -3077,6 +3077,20 @@ func (s SqlChannelStore) GetMembersForUserWithPagination(userId string, page, pe
|
||||
return dbMembers.ToModel(), nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetMembersForUserWithCursorPagination(userId string, perPage int, fromChannelID string) (model.ChannelMembersWithTeamData, error) {
|
||||
dbMembers := channelMemberWithTeamWithSchemeRolesList{}
|
||||
err := s.GetReplica().Select(&dbMembers, channelMembersWithSchemeSelectQuery+"WHERE ChannelMembers.UserId = ? AND ChannelId > ? ORDER BY ChannelId ASC Limit ?", userId, fromChannelID, perPage)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to find ChannelMembers data with and userId=%s", userId)
|
||||
}
|
||||
|
||||
if len(dbMembers) == 0 {
|
||||
return nil, store.NewErrNotFound("ChannelMembers", "userId="+userId)
|
||||
}
|
||||
|
||||
return dbMembers.ToModel(), nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetTeamMembersForChannel(channelID string) ([]string, error) {
|
||||
teamMemberIDs := []string{}
|
||||
if err := s.GetReplica().Select(&teamMemberIDs, `SELECT tm.UserId
|
||||
|
||||
@@ -266,6 +266,7 @@ type ChannelStore interface {
|
||||
GetMembersForUser(teamID string, userID string) (model.ChannelMembers, error)
|
||||
GetTeamMembersForChannel(channelID string) ([]string, error)
|
||||
GetMembersForUserWithPagination(userID string, page, perPage int) (model.ChannelMembersWithTeamData, error)
|
||||
GetMembersForUserWithCursorPagination(userId string, perPage int, fromChanneID string) (model.ChannelMembersWithTeamData, error)
|
||||
Autocomplete(rctx request.CTX, userID, term string, includeDeleted, isGuest bool) (model.ChannelListWithTeamData, error)
|
||||
AutocompleteInTeam(rctx request.CTX, teamID, userID, term string, includeDeleted, isGuest bool) (model.ChannelList, error)
|
||||
AutocompleteInTeamForSearch(teamID string, userID string, term string, includeDeleted bool) (model.ChannelList, error)
|
||||
|
||||
@@ -107,6 +107,7 @@ func TestChannelStore(t *testing.T, rctx request.CTX, ss store.Store, s SqlStore
|
||||
t.Run("GetChannelCounts", func(t *testing.T) { testChannelStoreGetChannelCounts(t, rctx, ss) })
|
||||
t.Run("GetMembersForUser", func(t *testing.T) { testChannelStoreGetMembersForUser(t, rctx, ss) })
|
||||
t.Run("GetMembersForUserWithPagination", func(t *testing.T) { testChannelStoreGetMembersForUserWithPagination(t, rctx, ss) })
|
||||
t.Run("GetMembersForUserWithCursorPagination", func(t *testing.T) { testChannelStoreGetMembersForUserWithCursorPagination(t, rctx, ss) })
|
||||
t.Run("CountPostsAfter", func(t *testing.T) { testCountPostsAfter(t, rctx, ss) })
|
||||
t.Run("CountUrgentPostsAfter", func(t *testing.T) { testCountUrgentPostsAfter(t, rctx, ss) })
|
||||
t.Run("UpdateLastViewedAt", func(t *testing.T) { testChannelStoreUpdateLastViewedAt(t, rctx, ss) })
|
||||
@@ -4719,6 +4720,93 @@ func testChannelStoreGetMembersForUserWithPagination(t *testing.T, rctx request.
|
||||
assert.Len(t, members, 1)
|
||||
}
|
||||
|
||||
func testChannelStoreGetMembersForUserWithCursorPagination(t *testing.T, rctx request.CTX, ss store.Store) {
|
||||
t1 := model.Team{
|
||||
DisplayName: "team1",
|
||||
Name: NewTestID(),
|
||||
Email: MakeEmail(),
|
||||
Type: model.TeamOpen,
|
||||
}
|
||||
_, err := ss.Team().Save(&t1)
|
||||
require.NoError(t, err)
|
||||
|
||||
userID := NewTestID()
|
||||
|
||||
var channelIDs []string
|
||||
for i := 0; i < 20; i++ {
|
||||
ch := &model.Channel{
|
||||
TeamId: t1.Id,
|
||||
DisplayName: "Channel1",
|
||||
Name: NewTestID(),
|
||||
Type: model.ChannelTypeOpen,
|
||||
}
|
||||
ch, err = ss.Channel().Save(rctx, ch, -1)
|
||||
require.NoError(t, err)
|
||||
channelIDs = append(channelIDs, ch.Id)
|
||||
|
||||
m1 := model.ChannelMember{}
|
||||
m1.ChannelId = ch.Id
|
||||
m1.UserId = userID
|
||||
m1.NotifyProps = model.GetDefaultChannelNotifyProps()
|
||||
_, err = ss.Channel().SaveMember(rctx, &m1)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
members, err := ss.Channel().GetMembersForUserWithCursorPagination(userID, 200, "")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, members, 20)
|
||||
|
||||
pageSize := 6
|
||||
channelID := ""
|
||||
numPages := 0
|
||||
var gotChannelIDs []string
|
||||
for {
|
||||
members, err := ss.Channel().GetMembersForUserWithCursorPagination(userID, pageSize, channelID)
|
||||
require.NoError(t, err)
|
||||
numPages++
|
||||
for _, m := range members {
|
||||
gotChannelIDs = append(gotChannelIDs, m.ChannelId)
|
||||
}
|
||||
if len(members) < pageSize {
|
||||
// Total 20. PageSize=6. Therefore 6*3=18 + 2 (last page)
|
||||
assert.Len(t, members, 2)
|
||||
break
|
||||
}
|
||||
if len(members) == pageSize {
|
||||
channelID = members[len(members)-1].ChannelId
|
||||
continue
|
||||
}
|
||||
require.Fail(t, "len(members) is > pageSize")
|
||||
}
|
||||
assert.Equal(t, numPages, 4)
|
||||
assert.ElementsMatch(t, channelIDs, gotChannelIDs)
|
||||
|
||||
pageSize = 5
|
||||
channelID = ""
|
||||
numPages = 0
|
||||
gotChannelIDs = []string{}
|
||||
for {
|
||||
members, err := ss.Channel().GetMembersForUserWithCursorPagination(userID, pageSize, channelID)
|
||||
numPages++
|
||||
if numPages < 5 {
|
||||
channelID = members[len(members)-1].ChannelId
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
// For the last page, it will have no rows.
|
||||
var nfErr *store.ErrNotFound
|
||||
require.True(t, errors.As(err, &nfErr))
|
||||
require.Nil(t, members)
|
||||
break
|
||||
}
|
||||
for _, m := range members {
|
||||
gotChannelIDs = append(gotChannelIDs, m.ChannelId)
|
||||
}
|
||||
assert.Len(t, members, pageSize)
|
||||
}
|
||||
assert.Equal(t, numPages, 5)
|
||||
assert.ElementsMatch(t, channelIDs, gotChannelIDs)
|
||||
}
|
||||
|
||||
func testCountPostsAfter(t *testing.T, rctx request.CTX, ss store.Store) {
|
||||
t.Run("should count all posts with or without the given user ID", func(t *testing.T) {
|
||||
userID1 := model.NewId()
|
||||
|
||||
@@ -1700,6 +1700,36 @@ func (_m *ChannelStore) GetMembersForUser(teamID string, userID string) (model.C
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetMembersForUserWithCursorPagination provides a mock function with given fields: userId, perPage, fromChanneID
|
||||
func (_m *ChannelStore) GetMembersForUserWithCursorPagination(userId string, perPage int, fromChanneID string) (model.ChannelMembersWithTeamData, error) {
|
||||
ret := _m.Called(userId, perPage, fromChanneID)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for GetMembersForUserWithCursorPagination")
|
||||
}
|
||||
|
||||
var r0 model.ChannelMembersWithTeamData
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(string, int, string) (model.ChannelMembersWithTeamData, error)); ok {
|
||||
return rf(userId, perPage, fromChanneID)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(string, int, string) model.ChannelMembersWithTeamData); ok {
|
||||
r0 = rf(userId, perPage, fromChanneID)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(model.ChannelMembersWithTeamData)
|
||||
}
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(string, int, string) error); ok {
|
||||
r1 = rf(userId, perPage, fromChanneID)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetMembersForUserWithPagination provides a mock function with given fields: userID, page, perPage
|
||||
func (_m *ChannelStore) GetMembersForUserWithPagination(userID string, page int, perPage int) (model.ChannelMembersWithTeamData, error) {
|
||||
ret := _m.Called(userID, page, perPage)
|
||||
|
||||
@@ -1719,6 +1719,22 @@ func (s *TimerLayerChannelStore) GetMembersForUser(teamID string, userID string)
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerChannelStore) GetMembersForUserWithCursorPagination(userId string, perPage int, fromChanneID string) (model.ChannelMembersWithTeamData, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.ChannelStore.GetMembersForUserWithCursorPagination(userId, perPage, fromChanneID)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("ChannelStore.GetMembersForUserWithCursorPagination", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerChannelStore) GetMembersForUserWithPagination(userID string, page int, perPage int) (model.ChannelMembersWithTeamData, error) {
|
||||
start := time.Now()
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user