Skip to content

Commit

Permalink
feat: allow maestro to deaul with multiple matches per game server
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldooli committed Oct 29, 2024
1 parent 9453a17 commit cc9b97a
Show file tree
Hide file tree
Showing 39 changed files with 1,196 additions and 465 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ deps/down: ## Delete containers dependencies.
.PHONY: maestro/start
maestro/start: build-linux-x86_64 ## Start Maestro with all of its dependencies.
@echo "Starting maestro..."
@cd ./e2e/framework/maestro; docker-compose up --build -d
@cd ./e2e/framework/maestro; docker compose up --build -d
@MAESTRO_MIGRATION_PATH="file://internal/service/migrations" go run main.go migrate;
@cd ./e2e/framework/maestro; docker-compose up --build -d worker runtime-watcher #Worker and watcher do not work before migration, so we start them after it.
@cd ./e2e/framework/maestro; docker compose up --build -d worker runtime-watcher #Worker and watcher do not work before migration, so we start them after it.
@echo "Maestro is up and running!"

.PHONY: maestro/down
maestro/down: ## Delete Maestro and all of its dependencies.
@echo "Deleting maestro..."
@cd ./e2e/framework/maestro; docker-compose down
@cd ./e2e/framework/maestro; docker compose down
@echo "Maestro was deleted with success!"
10 changes: 5 additions & 5 deletions cmd/roomsapi/wire_gen.go

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

2 changes: 1 addition & 1 deletion cmd/runtimewatcher/wire_gen.go

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

2 changes: 1 addition & 1 deletion cmd/worker/wire_gen.go

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

1 change: 1 addition & 0 deletions internal/adapters/events/forwarder_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (f *ForwarderClient) createGRPCConnection(address string) (*grpc.ClientConn

tracer := opentracing.GlobalTracer()
dialOption := grpc.WithInsecure() //nolint:staticcheck // I want to use deprecated method.
//nolint:staticcheck
conn, err := grpc.Dial(
address,
dialOption,
Expand Down
2 changes: 1 addition & 1 deletion internal/adapters/runtime/kubernetes/game_room_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (k *kubernetes) WatchGameRoomInstances(ctx context.Context, scheduler *enti
}),
).Core().V1().Pods().Informer()

podsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
_, _ = podsInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: watcher.addFunc,
UpdateFunc: watcher.updateFunc,
DeleteFunc: watcher.deleteFunc,
Expand Down
4 changes: 4 additions & 0 deletions internal/adapters/storage/postgres/scheduler/db_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ghodss/yaml"
"github.com/go-pg/pg/v10"
"github.com/topfreegames/maestro/internal/core/entities"
"github.com/topfreegames/maestro/internal/core/entities/allocation"
"github.com/topfreegames/maestro/internal/core/entities/autoscaling"
"github.com/topfreegames/maestro/internal/core/entities/forwarder"
"github.com/topfreegames/maestro/internal/core/entities/game_room"
Expand Down Expand Up @@ -62,6 +63,7 @@ type schedulerInfo struct {
Annotations map[string]string
Labels map[string]string
LastDownscaleAt time.Time
MatchAllocation allocation.MatchAllocation
}

func NewDBScheduler(scheduler *entities.Scheduler) *Scheduler {
Expand All @@ -78,6 +80,7 @@ func NewDBScheduler(scheduler *entities.Scheduler) *Scheduler {
Annotations: scheduler.Annotations,
Labels: scheduler.Labels,
LastDownscaleAt: scheduler.LastDownscaleAt,
MatchAllocation: scheduler.MatchAllocation,
}
yamlBytes, _ := yaml.Marshal(info)
return &Scheduler{
Expand Down Expand Up @@ -117,5 +120,6 @@ func (s *Scheduler) ToScheduler() (*entities.Scheduler, error) {
RoomsReplicas: info.RoomsReplicas,
Forwarders: info.Forwarders,
Autoscaling: info.Autoscaling,
MatchAllocation: info.MatchAllocation,
}, nil
}
39 changes: 35 additions & 4 deletions internal/adapters/storage/postgres/scheduler/db_scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ import (
"testing"
"time"

"github.com/topfreegames/maestro/internal/core/entities/autoscaling"

"github.com/topfreegames/maestro/internal/core/entities/forwarder"

"github.com/stretchr/testify/require"
"github.com/topfreegames/maestro/internal/core/entities"
"github.com/topfreegames/maestro/internal/core/entities/allocation"
"github.com/topfreegames/maestro/internal/core/entities/autoscaling"
"github.com/topfreegames/maestro/internal/core/entities/forwarder"
"github.com/topfreegames/maestro/internal/core/entities/game_room"
"github.com/topfreegames/maestro/internal/core/entities/port"
)
Expand Down Expand Up @@ -66,6 +65,10 @@ func TestScheduler_ToScheduler(t *testing.T) {
Affinity: "affinity",
},
RoomsReplicas: 0,
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 1,
MinFreeSlots: 1,
},
},
{
Name: "scheduler-2",
Expand All @@ -83,6 +86,10 @@ func TestScheduler_ToScheduler(t *testing.T) {
End: 60000,
},
RoomsReplicas: 1,
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 2,
MinFreeSlots: 2,
},
},
{
Name: "scheduler-3",
Expand Down Expand Up @@ -128,6 +135,10 @@ func TestScheduler_ToScheduler(t *testing.T) {
TerminationGracePeriod: 60,
},
RoomsReplicas: 2,
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 3,
MinFreeSlots: 3,
},
},
{
Name: "scheduler-3",
Expand Down Expand Up @@ -177,6 +188,10 @@ func TestScheduler_ToScheduler(t *testing.T) {
End: 60000,
},
RoomsReplicas: 3,
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 4,
MinFreeSlots: 4,
},
},
{
Name: "scheduler-5",
Expand Down Expand Up @@ -205,6 +220,10 @@ func TestScheduler_ToScheduler(t *testing.T) {
},
Annotations: map[string]string{"imageregistry": "https://hub.docker.com/"},
Labels: map[string]string{"scheduler": "scheduler-name"},
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 5,
MinFreeSlots: 5,
},
},
{
Name: "scheduler-6",
Expand Down Expand Up @@ -233,6 +252,10 @@ func TestScheduler_ToScheduler(t *testing.T) {
},
},
Annotations: map[string]string{"imageregistry": "https://hub.docker.com/"},
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 6,
MinFreeSlots: 6,
},
},
{
Name: "scheduler-7",
Expand Down Expand Up @@ -262,6 +285,10 @@ func TestScheduler_ToScheduler(t *testing.T) {
},
},
Annotations: map[string]string{"imageregistry": "https://hub.docker.com/"},
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 7,
MinFreeSlots: 7,
},
},
{
Name: "scheduler-8",
Expand Down Expand Up @@ -290,6 +317,10 @@ func TestScheduler_ToScheduler(t *testing.T) {
},
},
Annotations: map[string]string{"imageregistry": "https://hub.docker.com/"},
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 8,
MinFreeSlots: 8,
},
},
}

Expand Down
28 changes: 25 additions & 3 deletions internal/adapters/storage/redis/room/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (r redisStateStorage) GetRoom(ctx context.Context, scheduler, roomID string

room.Status = game_room.GameRoomStatus(statusCmd.Val())
room.LastPingAt = time.Unix(int64(pingCmd.Val()), 0)
room.OccupiedSlots = int(occupancyCmd.Val())
room.RunningMatches = int(occupancyCmd.Val())
err = json.NewDecoder(strings.NewReader(roomHashCmd.Val()[metadataKey])).Decode(&room.Metadata)
if err != nil {
return nil, errors.NewErrEncoding("error unmarshalling room %s json", roomID).WithError(err)
Expand Down Expand Up @@ -130,7 +130,7 @@ func (r *redisStateStorage) CreateRoom(ctx context.Context, room *game_room.Game

occupancyCmd := p.ZAddNX(ctx, getRoomOccupancyRedisKey(room.SchedulerID), &redis.Z{
Member: room.ID,
Score: float64(room.OccupiedSlots),
Score: float64(room.RunningMatches),
})

statusCmd := p.ZAddNX(ctx, getRoomStatusSetRedisKey(room.SchedulerID), &redis.Z{
Expand Down Expand Up @@ -182,7 +182,7 @@ func (r *redisStateStorage) UpdateRoom(ctx context.Context, room *game_room.Game

p.ZAddXXCh(ctx, getRoomOccupancyRedisKey(room.SchedulerID), &redis.Z{
Member: room.ID,
Score: float64(room.OccupiedSlots),
Score: float64(room.RunningMatches),
})

metrics.RunWithMetrics(roomStorageMetricLabel, func() error {
Expand Down Expand Up @@ -327,6 +327,28 @@ func (r *redisStateStorage) UpdateRoomStatus(ctx context.Context, scheduler, roo
return nil
}

func (r *redisStateStorage) GetRunningMatchesCount(ctx context.Context, scheduler string) (count int, err error) {
client := r.client
var rooms []redis.Z
metrics.RunWithMetrics(roomStorageMetricLabel, func() error {
rooms, err = client.ZRangeByScoreWithScores(ctx, getRoomOccupancyRedisKey(scheduler), &redis.ZRangeBy{
Min: "-inf",
Max: "+inf",
}).Result()
return err
})
if err != nil {
return 0, errors.NewErrUnexpected("error getting running matches scores from redis").WithError(err)
}

sum := 0
for _, room := range rooms {
sum += int(room.Score)
}

return sum, nil
}

func (r *redisStateStorage) WatchRoomStatus(ctx context.Context, room *game_room.GameRoom) (ports.RoomStorageStatusWatcher, error) {
var sub *redis.PubSub
metrics.RunWithMetrics(roomStorageMetricLabel, func() error {
Expand Down
Loading

0 comments on commit cc9b97a

Please sign in to comment.