Skip to content

Commit

Permalink
feat(watcher): add config for runtime watcher
Browse files Browse the repository at this point in the history
The new config will be read under runtimeWatcher YAML key.
For now, only disruption watcher properties are read such
as intervalSeconds and thresholdPercentage
  • Loading branch information
hspedro committed Mar 5, 2024
1 parent f49af9e commit 35496e6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
9 changes: 9 additions & 0 deletions cmd/runtimewatcher/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/topfreegames/maestro/internal/core/services/events"
"github.com/topfreegames/maestro/internal/core/services/workers"
"github.com/topfreegames/maestro/internal/core/worker"
workerconfigs "github.com/topfreegames/maestro/internal/core/worker/config"
"github.com/topfreegames/maestro/internal/core/worker/runtimewatcher"
"github.com/topfreegames/maestro/internal/service"
)
Expand All @@ -42,9 +43,17 @@ func provideRuntimeWatcherBuilder() *worker.WorkerBuilder {
}
}

func provideRuntimeWatcherConfig(c config.Config) *workerconfigs.RuntimeWatcherConfig {
return &workerconfigs.RuntimeWatcherConfig{
DisruptionWatcherIntervalSeconds: c.GetDuration("runtimeWatcher.disruption.intervalSeconds"),
DisruptionWatcherThresholdPercentage: c.GetFloat64("runtimeWatcher.disruption.thresholdPercentage"),
}
}

var WorkerOptionsSet = wire.NewSet(
service.NewRuntimeKubernetes,
RoomManagerSet,
provideRuntimeWatcherConfig,
wire.Struct(new(worker.WorkerOptions), "RoomManager", "Runtime"))

var RoomManagerSet = wire.NewSet(
Expand Down
11 changes: 10 additions & 1 deletion cmd/runtimewatcher/wire_gen.go

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

4 changes: 4 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ workers:
reporter:
metrics:
intervalMillis: 10000
runtimeWatcher:
disruption:
intervalSeconds: 5
thresholdPercentage: 5

services:
roomManager:
Expand Down
30 changes: 30 additions & 0 deletions internal/core/worker/config/runtime_watcher_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// MIT License
//
// Copyright (c) 2021 TFG Co
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package config

import "time"

type RuntimeWatcherConfig struct {
DisruptionWatcherIntervalSeconds time.Duration
DisruptionWatcherThresholdPercentage float64
}
13 changes: 10 additions & 3 deletions internal/core/worker/runtimewatcher/runtime_watcher_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/topfreegames/maestro/internal/core/logs"
"github.com/topfreegames/maestro/internal/core/worker/config"

"github.com/topfreegames/maestro/internal/core/entities"
"github.com/topfreegames/maestro/internal/core/entities/game_room"
Expand All @@ -53,6 +54,7 @@ type runtimeWatcherWorker struct {
logger *zap.Logger
ctx context.Context
cancelFunc context.CancelFunc
config *config.RuntimeWatcherConfig
workerWaitGroup *sync.WaitGroup
}

Expand All @@ -63,6 +65,7 @@ func NewRuntimeWatcherWorker(scheduler *entities.Scheduler, opts *worker.WorkerO
runtime: opts.Runtime,
logger: zap.L().With(zap.String(logs.LogFieldServiceName, WorkerName), zap.String(logs.LogFieldSchedulerName, scheduler.Name)),
workerWaitGroup: &sync.WaitGroup{},
config: opts.RuntimeWatcherConfig,
}
}

Expand Down Expand Up @@ -105,7 +108,12 @@ func (w *runtimeWatcherWorker) mitigateDisruptions() error {
)
return err
}
err = w.runtime.MitigateDisruption(w.ctx, w.scheduler, occupiedRoomsAmount, float64(5))
err = w.runtime.MitigateDisruption(
w.ctx,
w.scheduler,
occupiedRoomsAmount,
w.config.DisruptionWatcherThresholdPercentage,
)
if err != nil {
w.logger.Error(
"failed to mitigate disruption",
Expand All @@ -128,8 +136,7 @@ func (w *runtimeWatcherWorker) spawnDisruptionWatcher() {

go func() {
defer w.workerWaitGroup.Done()
// TODO: Replace number with config like w.config.MetricsReporterIntervalMillis
ticker := time.NewTicker(time.Second * 5)
ticker := time.NewTicker(time.Second * w.config.DisruptionWatcherIntervalSeconds)
defer ticker.Stop()

for {
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 @@ -55,6 +55,7 @@ type WorkerOptions struct {
RoomStorage ports.RoomStorage
InstanceStorage ports.GameRoomInstanceStorage
MetricsReporterConfig *config.MetricsReporterConfig
RuntimeWatcherConfig *config.RuntimeWatcherConfig
}

// Configuration holds all worker configuration parameters.
Expand Down

0 comments on commit 35496e6

Please sign in to comment.