Skip to content

Commit

Permalink
fix: sync scheduler before sending metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldooli committed Jan 22, 2025
1 parent 9c7f8c8 commit d52508e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/metricsreporter/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func provideMetricsReporterConfig(c config.Config) *workerconfigs.MetricsReporte
var WorkerOptionsSet = wire.NewSet(
service.NewRoomStorageRedis,
service.NewGameRoomInstanceStorageRedis,
service.NewSchedulerCacheRedis,
provideMetricsReporterConfig,
wire.Struct(new(worker.WorkerOptions), "RoomStorage", "InstanceStorage", "MetricsReporterConfig"))
wire.Struct(new(worker.WorkerOptions), "RoomStorage", "InstanceStorage", "SchedulerCache", "MetricsReporterConfig"))

func initializeMetricsReporter(c config.Config) (*workers.WorkersManager, error) {
wire.Build(
Expand Down
7 changes: 6 additions & 1 deletion cmd/metricsreporter/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/core/worker/metricsreporter/metrics_reporter_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const WorkerName = "metrics_reporter"
// MetricsReporterWorker is the service responsible producing periodic metrics.
type MetricsReporterWorker struct {
scheduler *entities.Scheduler
schedulerCache ports.SchedulerCache
config *config.MetricsReporterConfig
roomStorage ports.RoomStorage
instanceStorage ports.GameRoomInstanceStorage
Expand All @@ -56,6 +57,7 @@ type MetricsReporterWorker struct {
func NewMetricsReporterWorker(scheduler *entities.Scheduler, opts *worker.WorkerOptions) worker.Worker {
return &MetricsReporterWorker{
scheduler: scheduler,
schedulerCache: opts.SchedulerCache,
config: opts.MetricsReporterConfig,
roomStorage: opts.RoomStorage,
instanceStorage: opts.InstanceStorage,
Expand All @@ -77,6 +79,7 @@ func (w *MetricsReporterWorker) Start(ctx context.Context) error {
w.Stop(w.workerContext)
return nil
case <-ticker.C:
w.syncScheduler(w.workerContext)
w.reportInstanceMetrics()
w.reportGameRoomMetrics()
w.reportSchedulerMetrics()
Expand All @@ -96,6 +99,18 @@ func (w *MetricsReporterWorker) IsRunning() bool {
return w.workerContext != nil && w.workerContext.Err() == nil
}

func (w *MetricsReporterWorker) syncScheduler(ctx context.Context) {
scheduler, err := w.schedulerCache.GetScheduler(ctx, w.scheduler.Name)
if err != nil {
w.logger.Error("Error loading scheduler", zap.Error(err))
return
}

if w.scheduler.Spec.Version != scheduler.Spec.Version {
w.scheduler = scheduler
}
}

func (w *MetricsReporterWorker) reportInstanceMetrics() {
w.logger.Info("Reporting instance metrics")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestMetricsReporterWorker_StartProduceMetrics(t *testing.T) {
mockCtl := gomock.NewController(t)
roomStorage := mock.NewMockRoomStorage(mockCtl)
instanceStorage := mock.NewMockGameRoomInstanceStorage(mockCtl)
schedulerCache := mock.NewMockSchedulerCache(mockCtl)
ctx, cancelFunc := context.WithCancel(context.Background())
scheduler := &entities.Scheduler{Name: "random-scheduler"}
instances := newInstancesList(40)
Expand All @@ -58,6 +59,7 @@ func TestMetricsReporterWorker_StartProduceMetrics(t *testing.T) {
RoomStorage: roomStorage,
InstanceStorage: instanceStorage,
MetricsReporterConfig: &config.MetricsReporterConfig{MetricsReporterIntervalMillis: 500},
SchedulerCache: schedulerCache,
}

worker := NewMetricsReporterWorker(scheduler, workerOpts)
Expand All @@ -79,6 +81,7 @@ func TestMetricsReporterWorker_StartProduceMetrics(t *testing.T) {
roomStorage.EXPECT().GetRunningMatchesCount(gomock.Any(), scheduler.Name).
Return(88, nil).MinTimes(3)
instanceStorage.EXPECT().GetAllInstances(gomock.Any(), scheduler.Name).Return(instances, nil).MinTimes(3)
schedulerCache.EXPECT().GetScheduler(gomock.Any(), scheduler.Name).Return(scheduler, nil).MinTimes(3)

go func() {
err := worker.Start(ctx)
Expand Down Expand Up @@ -116,6 +119,7 @@ func TestMetricsReporterWorker_StartDoNotProduceMetrics(t *testing.T) {
mockCtl := gomock.NewController(t)
roomStorage := mock.NewMockRoomStorage(mockCtl)
instanceStorage := mock.NewMockGameRoomInstanceStorage(mockCtl)
schedulerCache := mock.NewMockSchedulerCache(mockCtl)
ctx, cancelFunc := context.WithCancel(context.Background())

scheduler := &entities.Scheduler{Name: "random-scheduler-2"}
Expand All @@ -124,6 +128,7 @@ func TestMetricsReporterWorker_StartDoNotProduceMetrics(t *testing.T) {
RoomStorage: roomStorage,
InstanceStorage: instanceStorage,
MetricsReporterConfig: &config.MetricsReporterConfig{MetricsReporterIntervalMillis: 500},
SchedulerCache: schedulerCache,
}
worker := NewMetricsReporterWorker(scheduler, workerOpts)

Expand All @@ -145,6 +150,8 @@ func TestMetricsReporterWorker_StartDoNotProduceMetrics(t *testing.T) {
Return(0, errors.New("some_error")).MinTimes(3)
instanceStorage.EXPECT().GetAllInstances(gomock.Any(), scheduler.Name).
Return([]*game_room.Instance{}, errors.New("some_error")).MinTimes(3)
schedulerCache.EXPECT().GetScheduler(gomock.Any(), scheduler.Name).
Return(scheduler, errors.New("some_error")).MinTimes(3)

go func() {
err := worker.Start(ctx)
Expand Down
1 change: 1 addition & 0 deletions internal/core/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type WorkerOptions struct {
InstanceStorage ports.GameRoomInstanceStorage
MetricsReporterConfig *config.MetricsReporterConfig
RuntimeWatcherConfig *config.RuntimeWatcherConfig
SchedulerCache ports.SchedulerCache
}

// Configuration holds all worker configuration parameters.
Expand Down

0 comments on commit d52508e

Please sign in to comment.