Move cluster, webhub and store out of Server (#20899)

Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-10-06 11:04:21 +03:00
коммит произвёл GitHub
родитель 203df2f537
Коммит 5e69c6b02f
222 изменённых файлов: 9093 добавлений и 7710 удалений

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

@@ -8,11 +8,8 @@ import (
"fmt"
"io"
"net/http"
"os"
"runtime/debug"
"time"
"github.com/mattermost/mattermost-server/v6/config"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/services/cache"
"github.com/mattermost/mattermost-server/v6/shared/i18n"
@@ -28,8 +25,8 @@ func (s *Server) GetLogs(page, perPage int) ([]string, *model.AppError) {
var lines []string
license := s.License()
if license != nil && *license.Features.Cluster && s.Cluster != nil && *s.platform.Config().ClusterSettings.Enable {
if info := s.Cluster.GetMyClusterInfo(); info != nil {
if license != nil && *license.Features.Cluster && s.platform.Cluster() != nil && *s.platform.Config().ClusterSettings.Enable {
if info := s.platform.Cluster().GetMyClusterInfo(); info != nil {
lines = append(lines, "-----------------------------------------------------------------------------------------------------------")
lines = append(lines, "-----------------------------------------------------------------------------------------------------------")
lines = append(lines, info.Hostname)
@@ -47,8 +44,8 @@ func (s *Server) GetLogs(page, perPage int) ([]string, *model.AppError) {
lines = append(lines, melines...)
if s.Cluster != nil && *s.platform.Config().ClusterSettings.Enable {
clines, err := s.Cluster.GetLogs(page, perPage)
if s.platform.Cluster() != nil && *s.platform.Config().ClusterSettings.Enable {
clines, err := s.platform.Cluster().GetLogs(page, perPage)
if err != nil {
return nil, err
}
@@ -64,75 +61,7 @@ func (a *App) GetLogs(page, perPage int) ([]string, *model.AppError) {
}
func (s *Server) GetLogsSkipSend(page, perPage int) ([]string, *model.AppError) {
var lines []string
if *s.platform.Config().LogSettings.EnableFile {
s.Log().Flush()
logFile := config.GetLogFileLocation(*s.platform.Config().LogSettings.FileLocation)
file, err := os.Open(logFile)
if err != nil {
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
defer file.Close()
var newLine = []byte{'\n'}
var lineCount int
const searchPos = -1
b := make([]byte, 1)
var endOffset int64 = 0
// if the file exists and it's last byte is '\n' - skip it
var stat os.FileInfo
if stat, err = os.Stat(logFile); err == nil {
if _, err = file.ReadAt(b, stat.Size()-1); err == nil && b[0] == newLine[0] {
endOffset = -1
}
}
lineEndPos, err := file.Seek(endOffset, io.SeekEnd)
if err != nil {
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
for {
pos, err := file.Seek(searchPos, io.SeekCurrent)
if err != nil {
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
_, err = file.ReadAt(b, pos)
if err != nil {
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
if b[0] == newLine[0] || pos == 0 {
lineCount++
if lineCount > page*perPage {
line := make([]byte, lineEndPos-pos)
_, err := file.ReadAt(line, pos)
if err != nil {
return nil, model.NewAppError("getLogs", "api.admin.file_read_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
lines = append(lines, string(line))
}
if pos == 0 {
break
}
lineEndPos = pos
}
if len(lines) == perPage {
break
}
}
for i, j := 0, len(lines)-1; i < j; i, j = i+1, j-1 {
lines[i], lines[j] = lines[j], lines[i]
}
} else {
lines = append(lines, "")
}
return lines, nil
return s.platform.GetLogsSkipSend(page, perPage)
}
func (a *App) GetLogsSkipSend(page, perPage int) ([]string, *model.AppError) {
@@ -150,35 +79,12 @@ func (a *App) GetClusterStatus() []*model.ClusterInfo {
}
func (s *Server) InvalidateAllCaches() *model.AppError {
debug.FreeOSMemory()
s.InvalidateAllCachesSkipSend()
if s.Cluster != nil {
msg := &model.ClusterMessage{
Event: model.ClusterEventInvalidateAllCaches,
SendType: model.ClusterSendReliable,
WaitForAllToSend: true,
}
s.Cluster.SendClusterMessage(msg)
}
return nil
return s.platform.InvalidateAllCaches()
}
func (s *Server) InvalidateAllCachesSkipSend() {
mlog.Info("Purging all caches")
s.userService.ClearAllUsersSessionCacheLocal()
s.statusCache.Purge()
s.Store.Team().ClearCaches()
s.Store.Channel().ClearCaches()
s.Store.User().ClearCaches()
s.Store.Post().ClearCaches()
s.Store.FileInfo().ClearCaches()
s.Store.Webhook().ClearCaches()
linkCache.Purge()
s.LoadLicense()
s.platform.InvalidateAllCachesSkipSend()
}
func (a *App) RecycleDatabaseConnection() {
@@ -187,7 +93,7 @@ func (a *App) RecycleDatabaseConnection() {
// This works by setting 10 seconds as the max conn lifetime for all DB connections.
// This allows in gradually closing connections as they expire. In future, we can think
// of exposing this as a param from the REST api.
a.Srv().Store.RecycleDBConnections(10 * time.Second)
a.Srv().Store().RecycleDBConnections(10 * time.Second)
mlog.Info("Finished recycling database connections.")
}
@@ -237,16 +143,6 @@ func (a *App) TestEmail(userID string, cfg *model.Config) *model.AppError {
return nil
}
// serverBusyStateChanged is called when a CLUSTER_EVENT_BUSY_STATE_CHANGED is received.
func (s *Server) serverBusyStateChanged(sbs *model.ServerBusyState) {
s.Busy.ClusterEventChanged(sbs)
if sbs.Busy {
mlog.Warn("server busy state activated via cluster event - non-critical services disabled", mlog.Int64("expires_sec", sbs.Expires))
} else {
mlog.Info("server busy state cleared via cluster event - non-critical services enabled")
}
}
func (a *App) GetLatestVersion(latestVersionUrl string) (*model.GithubReleaseInfo, *model.AppError) {
var cachedLatestVersion *model.GithubReleaseInfo
if cacheErr := latestVersionCache.Get("latest_version_cache", &cachedLatestVersion); cacheErr == nil {