Skip to content

Commit

Permalink
fix: backward compatibility with exiting schedulers
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldooli committed Nov 28, 2024
1 parent b1a9a9e commit 33bf082
Show file tree
Hide file tree
Showing 17 changed files with 119 additions and 40 deletions.
8 changes: 7 additions & 1 deletion internal/adapters/storage/postgres/scheduler/db_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type schedulerInfo struct {
Annotations map[string]string
Labels map[string]string
LastDownscaleAt time.Time
MatchAllocation allocation.MatchAllocation
MatchAllocation *allocation.MatchAllocation
}

func NewDBScheduler(scheduler *entities.Scheduler) *Scheduler {
Expand Down Expand Up @@ -99,6 +99,12 @@ func (s *Scheduler) ToScheduler() (*entities.Scheduler, error) {
if err != nil {
return nil, err
}

// Backward compatibility with schedulers that don't have MatchAllocation
if info.MatchAllocation == nil {
info.MatchAllocation = &allocation.MatchAllocation{MaxMatches: 1}
}

return &entities.Scheduler{
Name: s.Name,
Game: s.Game,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestScheduler_ToScheduler(t *testing.T) {
Affinity: "affinity",
},
RoomsReplicas: 0,
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 1,
},
},
Expand All @@ -85,7 +85,7 @@ func TestScheduler_ToScheduler(t *testing.T) {
End: 60000,
},
RoomsReplicas: 1,
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 2,
},
},
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestScheduler_ToScheduler(t *testing.T) {
TerminationGracePeriod: 60,
},
RoomsReplicas: 2,
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 3,
},
},
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestScheduler_ToScheduler(t *testing.T) {
End: 60000,
},
RoomsReplicas: 3,
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 4,
},
},
Expand Down Expand Up @@ -216,7 +216,7 @@ 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{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 5,
},
},
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestScheduler_ToScheduler(t *testing.T) {
},
},
Annotations: map[string]string{"imageregistry": "https://hub.docker.com/"},
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 6,
},
},
Expand Down Expand Up @@ -279,7 +279,7 @@ func TestScheduler_ToScheduler(t *testing.T) {
},
},
Annotations: map[string]string{"imageregistry": "https://hub.docker.com/"},
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 7,
},
},
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestScheduler_ToScheduler(t *testing.T) {
},
},
Annotations: map[string]string{"imageregistry": "https://hub.docker.com/"},
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 8,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package scheduler
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"

Check failure on line 31 in internal/adapters/storage/postgres/scheduler/scheduler_storage_pg_test.go

View workflow job for this annotation

GitHub Actions / Run linters

File is not `goimports`-ed (goimports)
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -91,6 +92,18 @@ func TestSchedulerStorage_GetScheduler(t *testing.T) {
assertSchedulers(t, []*entities.Scheduler{expectedScheduler}, []*entities.Scheduler{actualScheduler})
})

t.Run("scheduler exists and was created before multiple matches", func(t *testing.T) {
db := getPostgresDB(t)
storage := NewSchedulerStorage(db.Options())

err := storage.CreateScheduler(context.Background(), expectedScheduler)
require.NoError(t, err)

actualScheduler, err := storage.GetScheduler(context.Background(), "scheduler")
require.NoError(t, err)
assert.NotNil(t, actualScheduler.MatchAllocation)
})

t.Run("scheduler does not exists", func(t *testing.T) {
db := getPostgresDB(t)
storage := NewSchedulerStorage(db.Options())
Expand Down
27 changes: 18 additions & 9 deletions internal/api/handlers/requestadapters/schedulers.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func FromApiPatchSchedulerRequestToChangeMap(request *api.PatchSchedulerRequest)
patchMap[patch.LabelLabels] = request.GetLabels()
}

if request.MatchAllocation != nil {
patchMap[patch.LabelMatchAllocation] = fromApiMatchAllocation(request.GetMatchAllocation())
}

return patchMap
}

Expand Down Expand Up @@ -126,7 +130,7 @@ func FromApiCreateSchedulerRequestToEntity(request *api.CreateSchedulerRequest)
fromApiForwarders(request.GetForwarders()),
request.GetAnnotations(),
request.GetLabels(),
*fromApiMatchAllocation(request.GetMatchAllocation()),
fromApiMatchAllocation(request.GetMatchAllocation()),
)
}

Expand All @@ -140,7 +144,7 @@ func FromEntitySchedulerToListResponse(entity *entities.Scheduler) *api.Schedule
CreatedAt: timestamppb.New(entity.CreatedAt),
MaxSurge: entity.MaxSurge,
RoomsReplicas: int32(entity.RoomsReplicas),
MatchAllocation: fromEntityMatchAllocationToResponse(&entity.MatchAllocation),
MatchAllocation: fromEntityMatchAllocationToResponse(entity.MatchAllocation),
}
}

Expand All @@ -165,7 +169,7 @@ func FromApiNewSchedulerVersionRequestToEntity(request *api.NewSchedulerVersionR
fromApiForwarders(request.GetForwarders()),
request.GetAnnotations(),
request.GetLabels(),
*fromApiMatchAllocation(request.GetMatchAllocation()),
fromApiMatchAllocation(request.GetMatchAllocation()),
)
}

Expand All @@ -189,7 +193,7 @@ func FromEntitySchedulerToResponse(entity *entities.Scheduler) (*api.Scheduler,
Forwarders: forwarders,
Annotations: entity.Annotations,
Labels: entity.Labels,
MatchAllocation: fromEntityMatchAllocationToResponse(&entity.MatchAllocation),
MatchAllocation: fromEntityMatchAllocationToResponse(entity.MatchAllocation),
}, nil
}

Expand Down Expand Up @@ -337,7 +341,11 @@ func fromEntityForwardOptions(entity *forwarder.ForwardOptions) (*api.ForwarderO
}

func fromEntityMatchAllocationToResponse(entity *allocation.MatchAllocation) *api.MatchAllocation {
return &api.MatchAllocation{MaxMatches: int32(entity.MaxMatches)}
if entity != nil {
return &api.MatchAllocation{MaxMatches: int32(entity.MaxMatches)}
}

return nil
}

func fromApiSpec(apiSpec *api.Spec) *game_room.Spec {
Expand Down Expand Up @@ -503,12 +511,13 @@ func fromApiForwarders(apiForwarders []*api.Forwarder) []*forwarder.Forwarder {
}

func fromApiMatchAllocation(matchAllocation *api.MatchAllocation) *allocation.MatchAllocation {
maxMatches := int(matchAllocation.GetMaxMatches())
if maxMatches == 0 {
maxMatches = 1
if matchAllocation != nil {
return &allocation.MatchAllocation{
MaxMatches: int(matchAllocation.GetMaxMatches()),
}
}

return &allocation.MatchAllocation{MaxMatches: maxMatches}
return nil
}

func getPortRange(portRange *port.PortRange) *api.PortRange {
Expand Down
31 changes: 26 additions & 5 deletions internal/api/handlers/requestadapters/schedulers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ func TestFromApiCreateSchedulerRequestToEntity(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: &api.MatchAllocation{
MaxMatches: 2,
},
},
},
Output: Output{
Expand Down Expand Up @@ -674,8 +677,8 @@ func TestFromApiCreateSchedulerRequestToEntity(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 1,
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 2,
},
},
},
Expand Down Expand Up @@ -764,6 +767,9 @@ func TestFromApiCreateSchedulerRequestToEntity(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: &api.MatchAllocation{
MaxMatches: 0,
},
},
},
Output: Output{
Expand Down Expand Up @@ -851,8 +857,8 @@ func TestFromApiCreateSchedulerRequestToEntity(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: allocation.MatchAllocation{
MaxMatches: 1,
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 0,
},
},
},
Expand Down Expand Up @@ -980,6 +986,9 @@ func TestFromEntitySchedulerToListResponse(t *testing.T) {
},
},
},
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 0,
},
},
},
Output: Output{
Expand Down Expand Up @@ -1084,6 +1093,9 @@ func TestFromEntitySchedulerToListResponse(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 0,
},
},
},
Output: Output{
Expand Down Expand Up @@ -1239,6 +1251,9 @@ func TestFromApiNewSchedulerVersionRequestToEntity(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: &api.MatchAllocation{
MaxMatches: 1,
},
},
},
Output: Output{
Expand Down Expand Up @@ -1340,7 +1355,7 @@ func TestFromApiNewSchedulerVersionRequestToEntity(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 1,
},
},
Expand Down Expand Up @@ -1477,6 +1492,9 @@ func TestFromEntitySchedulerToResponse(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 0,
},
},
},
Output: Output{
Expand Down Expand Up @@ -1661,6 +1679,9 @@ func TestFromEntitySchedulerToResponse(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 0,
},
},
},
Output: Output{
Expand Down
28 changes: 25 additions & 3 deletions internal/api/handlers/schedulers_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func TestGetScheduler(t *testing.T) {
},
Annotations: map[string]string{},
Labels: map[string]string{},
MatchAllocation: allocation.MatchAllocation{MaxMatches: 1},
MatchAllocation: &allocation.MatchAllocation{MaxMatches: 1},
}

schedulerCache.EXPECT().GetScheduler(gomock.Any(), gomock.Any()).Return(scheduler, nil)
Expand Down Expand Up @@ -542,7 +542,7 @@ func TestCreateScheduler(t *testing.T) {
},
Annotations: map[string]string{"imageregistry": "https://docker.hub.com/"},
Labels: map[string]string{"scheduler": "scheduler-name"},
MatchAllocation: allocation.MatchAllocation{MaxMatches: 1},
MatchAllocation: &allocation.MatchAllocation{MaxMatches: 1},
}

schedulerStorage.EXPECT().CreateScheduler(gomock.Any(), gomock.Any()).Do(
Expand All @@ -562,6 +562,7 @@ func TestCreateScheduler(t *testing.T) {
}
assert.Equal(t, scheduler.Annotations, arg.Annotations)
assert.Equal(t, scheduler.Labels, arg.Labels)
assert.Equal(t, scheduler.MatchAllocation, arg.MatchAllocation)
},
).Return(nil)
operationManager.EXPECT().CreateOperation(gomock.Any(), scheduler.Name, gomock.Any()).Return(&operation.Operation{ID: "id-1"}, nil)
Expand Down Expand Up @@ -1077,6 +1078,27 @@ func TestPatchScheduler(t *testing.T) {
Status: http.StatusInternalServerError,
},
},
{
Title: "When the scheduler was created before the MatchAllocation return 200",
Input: Input{
Request: &api.PatchSchedulerRequest{
RoomsReplicas: &roomsReplicas,
},
},
Mocks: Mocks{
RequestFile: "scheduler-patch.json",
GetSchedulerReturn: newValidScheduler(),
GetSchedulerError: nil,
CreateOperationReturn: &operation.Operation{
ID: "some-id",
},
CreateOperationError: nil,
},
Output: Output{
Response: nil,
Status: http.StatusOK,
},
},
}

err := validations.RegisterValidations()
Expand Down Expand Up @@ -1274,7 +1296,7 @@ func newValidScheduler() *entities.Scheduler {
},
},
},
MatchAllocation: allocation.MatchAllocation{
MatchAllocation: &allocation.MatchAllocation{
MaxMatches: 1,
},
PortRange: &port.PortRange{
Expand Down
4 changes: 0 additions & 4 deletions internal/core/entities/allocation/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@ type MatchAllocation struct {
// MaxMatches defined the maximum number of matches that a room can host.
MaxMatches int `validate:"required,min=1,max=30"`
}

func NewDefaultMatchAllocation() *MatchAllocation {
return &MatchAllocation{MaxMatches: 1}
}
4 changes: 2 additions & 2 deletions internal/core/entities/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Scheduler struct {
Forwarders []*forwarder.Forwarder `validate:"dive"`
Annotations map[string]string
Labels map[string]string
MatchAllocation allocation.MatchAllocation
MatchAllocation *allocation.MatchAllocation `validate:"required"`
}

// NewScheduler instantiate a new scheduler struct.
Expand All @@ -90,7 +90,7 @@ func NewScheduler(
forwarders []*forwarder.Forwarder,
annotations map[string]string,
labels map[string]string,
matchAllocation allocation.MatchAllocation,
matchAllocation *allocation.MatchAllocation,
) (*Scheduler, error) {
scheduler := &Scheduler{
Name: name,
Expand Down
Loading

0 comments on commit 33bf082

Please sign in to comment.