Skip to content

Commit e802dca

Browse files
authored
Merge pull request #309 from topfreegames/chore/improve-watcher-event-processing
chore(runtime watcher, rooms adapters): troubleshooting improvements
2 parents 3983ba3 + 26376d1 commit e802dca

File tree

9 files changed

+53
-26
lines changed

9 files changed

+53
-26
lines changed

config/rooms-api.local.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
internalApi:
2-
port: 8081
2+
port: 8091
33
gracefulShutdownTimeout: 10s
44
metrics:
55
enabled: true
66
healthcheck:
77
enabled: true
88
roomsApi:
9-
port: 8080
9+
port: 8090
1010
gracefulShutdownTimeout: 10s
1111
adapters:
1212
portAllocator:

config/runtime-watcher.local.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
internalApi:
2-
port: 8081
2+
port: 8083
33
gracefulShutdownTimeout: 10s
44
metrics:
55
enabled: true

config/worker.local.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
internalApi:
2-
port: 8081
2+
port: 8082
33
gracefulShutdownTimeout: 10s
44
metrics:
55
enabled: true

internal/adapters/instance_storage/redis/redis.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func (r redisInstanceStorage) GetInstance(ctx context.Context, scheduler string,
6060
}
6161

6262
func (r redisInstanceStorage) UpsertInstance(ctx context.Context, instance *game_room.Instance) error {
63+
if instance == nil {
64+
return errors.NewErrUnexpected("Cannot upsert nil instance")
65+
}
6366
instanceJson, err := json.Marshal(instance)
6467
if err != nil {
6568
return errors.NewErrUnexpected("error marshalling room %s json", instance.ID).WithError(err)

internal/adapters/instance_storage/redis/redis_test.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,38 @@ func assertInstanceRedis(t *testing.T, client *redis.Client, expectedInstance *g
6262
func TestRedisInstanceStorage_UpsertInstance(t *testing.T) {
6363
client := test.GetRedisConnection(t, redisAddress)
6464
storage := NewRedisInstanceStorage(client, 0)
65-
instance := &game_room.Instance{
66-
ID: "1",
67-
SchedulerID: "game",
68-
Status: game_room.InstanceStatus{
69-
Type: game_room.InstancePending,
70-
},
71-
}
65+
t.Run("should succeed", func(t *testing.T) {
66+
instance := &game_room.Instance{
67+
ID: "1",
68+
SchedulerID: "game",
69+
Status: game_room.InstanceStatus{
70+
Type: game_room.InstancePending,
71+
},
72+
}
73+
74+
require.NoError(t, storage.UpsertInstance(context.Background(), instance))
75+
assertInstanceRedis(t, client, instance)
7276

73-
require.NoError(t, storage.UpsertInstance(context.Background(), instance))
74-
assertInstanceRedis(t, client, instance)
75-
76-
instance.Status.Type = game_room.InstanceReady
77-
instance.Address = &game_room.Address{
78-
Host: "host",
79-
Ports: []game_room.Port{
80-
{
81-
Name: "game",
82-
Port: 7000,
83-
Protocol: "udp",
77+
instance.Status.Type = game_room.InstanceReady
78+
instance.Address = &game_room.Address{
79+
Host: "host",
80+
Ports: []game_room.Port{
81+
{
82+
Name: "game",
83+
Port: 7000,
84+
Protocol: "udp",
85+
},
8486
},
85-
},
86-
}
87+
}
8788

88-
require.NoError(t, storage.UpsertInstance(context.Background(), instance))
89-
assertInstanceRedis(t, client, instance)
89+
require.NoError(t, storage.UpsertInstance(context.Background(), instance))
90+
assertInstanceRedis(t, client, instance)
91+
})
92+
93+
t.Run("should fail - instance is nil", func(t *testing.T) {
94+
err := storage.UpsertInstance(context.Background(), nil)
95+
require.Error(t, err)
96+
})
9097
}
9198

9299
func TestRedisInstanceStorage_GetInstance(t *testing.T) {

internal/core/entities/game_room/game_room.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ var validStatusTransitions = map[GameRoomStatus]map[GameRoomStatus]struct{}{
145145
GameStatusTerminating: struct{}{},
146146
GameStatusReady: struct{}{},
147147
GameStatusError: struct{}{},
148+
GameStatusPending: struct{}{},
148149
},
149150
GameStatusOccupied: {
150151
GameStatusReady: struct{}{},
@@ -183,6 +184,7 @@ var roomStatusComposition = []struct {
183184

184185
// Unready
185186
{GameRoomPingStatusUnknown, InstanceReady, GameStatusUnready},
187+
{GameRoomPingStatusUnknown, InstancePending, GameStatusUnready},
186188

187189
// Terminating
188190
{GameRoomPingStatusUnknown, InstanceTerminating, GameStatusTerminating},

internal/core/services/room_manager/room_manager.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ func (m *RoomManager) UpdateRoom(ctx context.Context, gameRoom *game_room.GameRo
172172
}
173173

174174
func (m *RoomManager) UpdateRoomInstance(ctx context.Context, gameRoomInstance *game_room.Instance) error {
175+
if gameRoomInstance == nil {
176+
return fmt.Errorf("cannot update room instance since it is nil")
177+
}
178+
m.Logger.Sugar().Infof("Updating room instance. ID: %v", gameRoomInstance.ID)
175179
err := m.InstanceStorage.UpsertInstance(ctx, gameRoomInstance)
176180
if err != nil {
177181
return fmt.Errorf("failed when updating the game room instance on storage: %w", err)
@@ -182,10 +186,12 @@ func (m *RoomManager) UpdateRoomInstance(ctx context.Context, gameRoomInstance *
182186
return fmt.Errorf("failed to update game room status: %w", err)
183187
}
184188

189+
m.Logger.Info("Updating room success")
185190
return nil
186191
}
187192

188193
func (m *RoomManager) CleanRoomState(ctx context.Context, schedulerName, roomId string) error {
194+
m.Logger.Sugar().Infof("Cleaning room \"%v\", scheduler \"%v\"", roomId, schedulerName)
189195
err := m.RoomStorage.DeleteRoom(ctx, schedulerName, roomId)
190196
if err != nil && !errors.Is(porterrors.ErrNotFound, err) {
191197
return fmt.Errorf("failed to delete room state: %w", err)
@@ -196,6 +202,7 @@ func (m *RoomManager) CleanRoomState(ctx context.Context, schedulerName, roomId
196202
return fmt.Errorf("failed to delete room state: %w", err)
197203
}
198204

205+
m.Logger.Info("cleaning room success")
199206
return nil
200207
}
201208

internal/core/services/room_manager/room_manager_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,11 @@ func TestRoomManager_UpdateRoomInstance(t *testing.T) {
557557
err := roomManager.UpdateRoomInstance(context.Background(), newGameRoomInstance)
558558
require.Error(t, err)
559559
})
560+
561+
t.Run("should fail - room instance is nil => returns error", func(t *testing.T) {
562+
err := roomManager.UpdateRoomInstance(context.Background(), nil)
563+
require.Error(t, err)
564+
})
560565
}
561566

562567
func TestRoomManager_CleanRoomState(t *testing.T) {

internal/core/workers/runtime_watcher_worker/runtime_watcher_worker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,16 @@ func (w *runtimeWatcherWorker) IsRunning() bool {
9393
}
9494

9595
func (w *runtimeWatcherWorker) processEvent(ctx context.Context, event game_room.InstanceEvent) error {
96+
w.logger.Sugar().Infof("processing event: %++v", event)
9697
switch event.Type {
9798
case game_room.InstanceEventTypeAdded, game_room.InstanceEventTypeUpdated:
99+
w.logger.Info("processing event. Updating rooms instance")
98100
err := w.roomManager.UpdateRoomInstance(ctx, event.Instance)
99101
if err != nil {
100102
return fmt.Errorf("failed to update room instance %s: %w", event.Instance.ID, err)
101103
}
102104
case game_room.InstanceEventTypeDeleted:
105+
w.logger.Info("processing event. Cleaning Room state")
103106
err := w.roomManager.CleanRoomState(ctx, event.Instance.SchedulerID, event.Instance.ID)
104107
if err != nil {
105108
return fmt.Errorf("failed to clean room %s state: %w", event.Instance.ID, err)

0 commit comments

Comments
 (0)