Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix panic #16776

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cmd/mo-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ import (
"time"
_ "time/tzdata"

"go.uber.org/automaxprocs/maxprocs"

"github.com/google/uuid"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"

"github.com/matrixorigin/matrixone/pkg/clusterservice"
Expand All @@ -44,6 +43,7 @@ import (
"github.com/matrixorigin/matrixone/pkg/common/system"
"github.com/matrixorigin/matrixone/pkg/defines"
"github.com/matrixorigin/matrixone/pkg/fileservice"
"github.com/matrixorigin/matrixone/pkg/frontend"
"github.com/matrixorigin/matrixone/pkg/gossip"
"github.com/matrixorigin/matrixone/pkg/logservice"
"github.com/matrixorigin/matrixone/pkg/logutil"
Expand Down Expand Up @@ -487,7 +487,10 @@ func initTraceMetric(ctx context.Context, st metadata.ServiceType, cfg *Config,
}
if !SV.DisableMetric || SV.EnableMetricToProm {
stopper.RunNamedTask("metric", func(ctx context.Context) {
if act := mometric.InitMetric(ctx, nil, &SV, UUID, nodeRole, mometric.WithWriterFactory(writerFactory)); !act {
if act := mometric.InitMetric(ctx, nil, &SV, UUID, nodeRole,
mometric.WithWriterFactory(writerFactory),
mometric.WithFrontendServerStarted(frontend.MoServerIsStarted),
); !act {
return
}
<-ctx.Done()
Expand Down
12 changes: 12 additions & 0 deletions pkg/frontend/resp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package frontend

import (
"math"
"sync"

"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/container/batch"
Expand Down Expand Up @@ -162,6 +163,7 @@ const (
type NullResp struct {
username string
database string
sync.Mutex
}

func (resper *NullResp) MysqlRrWr() MysqlRrWr {
Expand All @@ -170,6 +172,11 @@ func (resper *NullResp) MysqlRrWr() MysqlRrWr {
}

func (resper *NullResp) GetStr(id PropertyID) string {
if resper == nil {
return ""
}
resper.Lock()
defer resper.Unlock()
switch id {
case DBNAME:
return resper.database
Expand All @@ -182,6 +189,11 @@ func (resper *NullResp) GetStr(id PropertyID) string {
}
}
func (resper *NullResp) SetStr(id PropertyID, val string) {
if resper == nil {
return
}
resper.Lock()
defer resper.Unlock()
switch id {
case DBNAME:
resper.database = val
Expand Down
19 changes: 17 additions & 2 deletions pkg/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"time"

"github.com/fagongzi/goetty/v2"
"go.uber.org/zap"

"github.com/matrixorigin/matrixone/pkg/config"
"github.com/matrixorigin/matrixone/pkg/defines"
"github.com/matrixorigin/matrixone/pkg/logutil"
"github.com/matrixorigin/matrixone/pkg/queryservice"
"go.uber.org/zap"
)

// RelationName counter for the new connection
Expand Down Expand Up @@ -66,7 +67,12 @@ func (mo *MOServer) GetRoutineManager() *RoutineManager {

func (mo *MOServer) Start() error {
logutil.Infof("Server Listening on : %s ", mo.addr)
return mo.app.Start()
err := mo.app.Start()
if err != nil {
return err
}
setMoServerStarted(true)
return nil
}

func (mo *MOServer) Stop() error {
Expand All @@ -80,6 +86,7 @@ func nextConnectionID() uint32 {
var globalRtMgr atomic.Value
var globalPu atomic.Value
var globalAicm atomic.Value
var moServerStarted atomic.Bool

func setGlobalRtMgr(rtMgr *RoutineManager) {
globalRtMgr.Store(rtMgr)
Expand Down Expand Up @@ -108,6 +115,14 @@ func getGlobalAic() *defines.AutoIncrCacheManager {
return nil
}

func MoServerIsStarted() bool {
return moServerStarted.Load()
}

func setMoServerStarted(b bool) {
moServerStarted.Store(b)
}

func NewMOServer(
ctx context.Context,
addr string,
Expand Down
27 changes: 17 additions & 10 deletions pkg/util/metric/mometric/cron_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,23 @@ import (
"sync/atomic"
"time"

"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/defines"
"go.uber.org/zap"

"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/common/log"
"github.com/matrixorigin/matrixone/pkg/util/export/table"
"github.com/matrixorigin/matrixone/pkg/util/metric"

"github.com/matrixorigin/matrixone/pkg/common/runtime"
"github.com/matrixorigin/matrixone/pkg/defines"
"github.com/matrixorigin/matrixone/pkg/pb/task"
"github.com/matrixorigin/matrixone/pkg/taskservice"
"github.com/matrixorigin/matrixone/pkg/util/export/table"
ie "github.com/matrixorigin/matrixone/pkg/util/internalExecutor"
"github.com/matrixorigin/matrixone/pkg/util/metric"
"github.com/matrixorigin/matrixone/pkg/util/trace"

"go.uber.org/zap"
)

const (
LoggerName = "MetricTask"
LoggerNameMetricStorage = "MetricStorage"
LoggerNameMetricStorage = "MetricStorageUsage"

StorageUsageCronTask = "StorageUsage"
StorageUsageTaskCronExpr = ExprEvery05Min
Expand Down Expand Up @@ -92,11 +90,13 @@ const (
var (
gUpdateStorageUsageInterval atomic.Int64
gCheckNewInterval atomic.Int64
frontendServerStarted func() bool
)

func init() {
gUpdateStorageUsageInterval.Store(int64(time.Minute))
gCheckNewInterval.Store(int64(time.Minute))
frontendServerStarted = func() bool { return true }
}

func SetUpdateStorageUsageInterval(interval time.Duration) {
Expand All @@ -113,18 +113,25 @@ func cleanStorageUsageMetric(logger *log.MOLogger, actor string) {
logger.Info("clean storage usage metric", zap.String("actor", actor))
}

func checkServerStarted(logger *log.MOLogger) bool {
return frontendServerStarted()
}

func CalculateStorageUsage(ctx context.Context, sqlExecutor func() ie.InternalExecutor) (err error) {
ctx, span := trace.Start(ctx, "MetricStorageUsage")
defer span.End()
ctx = defines.AttachAccount(ctx, catalog.System_Account, catalog.System_User, catalog.System_Role)
ctx, cancel := context.WithCancel(ctx)
defer cancel() // quit CheckNewAccountSize goroutine
logger := runtime.ProcessLevelRuntime().Logger().WithContext(ctx).Named(LoggerNameMetricStorage)
logger.Info("started")
if !checkServerStarted(logger) {
logger.Info("mo server is not started yet, wait next schedule.")
return nil
}
defer func() {
logger.Info("finished", zap.Error(err))
cleanStorageUsageMetric(logger, "CalculateStorageUsage")
// quit CheckNewAccountSize goroutine
cancel()
}()

// start background task to check new account
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/metric/mometric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func InitMetric(ctx context.Context, ieFactory func() ie.InternalExecutor, SV *c
}

enable = true
frontendServerStarted = initOpts.frontendServerStarted
SetUpdateStorageUsageInterval(initOpts.updateInterval)
SetStorageUsageCheckNewInterval(initOpts.checkNewInterval)
logutil.Debugf("metric with ExportInterval: %v", initOpts.exportInterval)
Expand Down Expand Up @@ -374,6 +375,8 @@ type InitOptions struct {
checkNewInterval time.Duration
// internalGatherInterval, handle metric.SubSystemMO gather interval
internalGatherInterval time.Duration
// frontendServerStarted, function return bool, represent server started or not
frontendServerStarted func() bool
}

type InitOption func(*InitOptions)
Expand Down Expand Up @@ -419,6 +422,12 @@ func WithInternalGatherInterval(interval time.Duration) InitOption {
})
}

func WithFrontendServerStarted(f func() bool) InitOption {
return InitOption(func(options *InitOptions) {
options.frontendServerStarted = f
})
}

var (
metricNameColumn = table.StringDefaultColumn(`metric_name`, `sys`, `metric name, like: sql_statement_total, server_connections, process_cpu_percent, sys_memory_used, ...`)
metricCollectTimeColumn = table.DatetimeColumn(`collecttime`, `metric data collect time`)
Expand Down
Loading