Replace db count query in user signup process (#18072)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
d3973557fc
Коммит
bd65e8daf9
@@ -10356,6 +10356,24 @@ func (s *OpenTracingLayerUserStore) InvalidateProfilesInChannelCacheByUser(userI
|
||||
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerUserStore) IsEmpty() (bool, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "UserStore.IsEmpty")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.UserStore.IsEmpty()
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerUserStore) PermanentDelete(userID string) error {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "UserStore.PermanentDelete")
|
||||
|
||||
@@ -11236,6 +11236,26 @@ func (s *RetryLayerUserStore) InvalidateProfilesInChannelCacheByUser(userID stri
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerUserStore) IsEmpty() (bool, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.UserStore.IsEmpty()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerUserStore) PermanentDelete(userID string) error {
|
||||
|
||||
tries := 0
|
||||
|
||||
@@ -2002,3 +2002,13 @@ func (us SqlUserStore) GetKnownUsers(userId string) ([]string, error) {
|
||||
|
||||
return userIds, nil
|
||||
}
|
||||
|
||||
// IsEmpty returns whether or not the Users table is empty.
|
||||
func (us SqlUserStore) IsEmpty() (bool, error) {
|
||||
var hasRows bool
|
||||
err := us.GetReplica().SelectOne(&hasRows, `SELECT EXISTS (SELECT 1 FROM Users)`)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "failed to check if table is empty")
|
||||
}
|
||||
return !hasRows, nil
|
||||
}
|
||||
|
||||
@@ -424,6 +424,7 @@ type UserStore interface {
|
||||
DeactivateGuests() ([]string, error)
|
||||
AutocompleteUsersInChannel(teamID, channelID, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, error)
|
||||
GetKnownUsers(userID string) ([]string, error)
|
||||
IsEmpty() (bool, error)
|
||||
}
|
||||
|
||||
type BotStore interface {
|
||||
|
||||
@@ -1038,6 +1038,27 @@ func (_m *UserStore) InvalidateProfilesInChannelCacheByUser(userID string) {
|
||||
_m.Called(userID)
|
||||
}
|
||||
|
||||
// IsEmpty provides a mock function with given fields:
|
||||
func (_m *UserStore) IsEmpty() (bool, error) {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 bool
|
||||
if rf, ok := ret.Get(0).(func() bool); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
r0 = ret.Get(0).(bool)
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func() error); ok {
|
||||
r1 = rf()
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// PermanentDelete provides a mock function with given fields: userID
|
||||
func (_m *UserStore) PermanentDelete(userID string) error {
|
||||
ret := _m.Called(userID)
|
||||
|
||||
@@ -36,6 +36,7 @@ func TestUserStore(t *testing.T, ss store.Store, s SqlStore) {
|
||||
require.NoError(t, err, "failed cleaning up test user %s", u.Username)
|
||||
}
|
||||
|
||||
t.Run("IsEmpty", func(t *testing.T) { testIsEmpty(t, ss) })
|
||||
t.Run("Count", func(t *testing.T) { testCount(t, ss) })
|
||||
t.Run("AnalyticsActiveCount", func(t *testing.T) { testUserStoreAnalyticsActiveCount(t, ss, s) })
|
||||
t.Run("AnalyticsActiveCountForPeriod", func(t *testing.T) { testUserStoreAnalyticsActiveCountForPeriod(t, ss, s) })
|
||||
@@ -5711,3 +5712,28 @@ func testGetKnownUsers(t *testing.T, ss store.Store) {
|
||||
assert.ElementsMatch(t, userIds, []string{u2.Id, u3.Id})
|
||||
})
|
||||
}
|
||||
|
||||
func testIsEmpty(t *testing.T, ss store.Store) {
|
||||
ok, err := ss.User().IsEmpty()
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
|
||||
u := model.User{
|
||||
Email: MakeEmail(),
|
||||
Username: model.NewId(),
|
||||
}
|
||||
|
||||
_, err = ss.User().Save(&u)
|
||||
require.NoError(t, err)
|
||||
|
||||
ok, err = ss.User().IsEmpty()
|
||||
require.NoError(t, err)
|
||||
require.False(t, ok)
|
||||
|
||||
err = ss.User().PermanentDelete(u.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
ok, err = ss.User().IsEmpty()
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
}
|
||||
|
||||
@@ -9345,6 +9345,22 @@ func (s *TimerLayerUserStore) InvalidateProfilesInChannelCacheByUser(userID stri
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TimerLayerUserStore) IsEmpty() (bool, error) {
|
||||
start := timemodule.Now()
|
||||
|
||||
result, err := s.UserStore.IsEmpty()
|
||||
|
||||
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("UserStore.IsEmpty", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerUserStore) PermanentDelete(userID string) error {
|
||||
start := timemodule.Now()
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user