Skip to content

Commit 27f5d50

Browse files
committed
feat: port join_hub to ipc
1 parent 333b4ec commit 27f5d50

11 files changed

Lines changed: 195 additions & 3 deletions

File tree

‎bin/api-server/src/main/java/net/hollowcube/apiserver/session/SessionServiceImpl.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package net.hollowcube.apiserver.session;
22

33
import net.hollowcube.apiserver.db.ApiDatabase;
4+
import net.hollowcube.ipc.session.GameServer;
45
import net.hollowcube.ipc.session.SessionService;
6+
import org.jetbrains.annotations.Nullable;
57

68
/// Player sessions, served out of the `player_sessions` table the Go api-server writes.
79
public final class SessionServiceImpl implements SessionService {
@@ -17,4 +19,10 @@ public int onlinePlayers() {
1719
// Every row, hidden players and all, the way the player count graphs count them.
1820
return (int) db.sessions.countPlayerSessions();
1921
}
22+
23+
@Override
24+
public @Nullable GameServer findHub(@Nullable String exclude) {
25+
var hub = db.servers.findHub(exclude);
26+
return hub == null ? null : new GameServer(hub.id(), hub.clusterIp(), hub.protocolVersion());
27+
}
2028
}

‎bin/api-server/src/test/java/net/hollowcube/apiserver/session/SessionServiceImplTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.sun.net.httpserver.HttpServer;
44
import net.hollowcube.apiserver.db.ApiDatabase;
5+
import net.hollowcube.ipc.session.GameServer;
56
import net.hollowcube.ipc.session.SessionClient;
67
import net.hollowcube.ipc.session.SessionServer;
78
import net.hollowcube.sqlgen.testing.TestDb;
@@ -15,6 +16,7 @@
1516
import java.net.http.HttpClient;
1617

1718
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
1820

1921
/// Sessions end to end, the same way as the head database: a real Postgres under the service, the
2022
/// generated server over it, and the generated client talking to that over a real socket.
@@ -61,4 +63,29 @@ insert into player_sessions (player_id, proxy_id, skin_texture, skin_signature,
6163

6264
assertEquals(3, sessions.onlinePlayers());
6365
}
66+
67+
@Test
68+
void findHub_returnsAReadyHubWithItsProtocol() {
69+
TEST_DB.seed(
70+
"""
71+
insert into server_states (id, role, status, cluster_ip, protocol_version) values
72+
('hub-starting', 'hub', 0, '10.0.0.1', 0),
73+
('map-a', 'map', 1, '10.0.0.2', 777),
74+
('hub-a', 'hub', 1, '10.0.0.3', 777)"""
75+
);
76+
77+
assertEquals(new GameServer("hub-a", "10.0.0.3", 777), sessions.findHub(null));
78+
}
79+
80+
@Test
81+
void findHub_skipsTheExcludedHub() {
82+
TEST_DB.seed(
83+
"""
84+
insert into server_states (id, role, status, cluster_ip) values
85+
('hub-a', 'hub', 1, '10.0.0.3')"""
86+
);
87+
88+
assertNull(sessions.findHub("hub-a"));
89+
assertEquals(new GameServer("hub-a", "10.0.0.3", 0), sessions.findHub("hub-b"));
90+
}
6491
}

‎modules/api/src/generated/java/net/hollowcube/apiserver/db/ApiDatabase.java‎

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎modules/api/src/generated/java/net/hollowcube/apiserver/db/ServerStates.java‎

Lines changed: 6 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎modules/api/src/generated/java/net/hollowcube/apiserver/db/ServersQueries.java‎

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎modules/api/src/generated/java/net/hollowcube/apiserver/db/ServersQueriesImpl.java‎

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Describe-time mirror of Go's 000006, 000007 and 000012, in the order they ran. Nothing to apply.
2+
alter table server_states add column if not exists status_v2 varchar(10) default 'starting';
3+
alter table server_states alter column status_v2 set not null;
4+
alter table server_states add column if not exists status_since timestamptz not null default now();
5+
-- 0 until the tracker has asked the server, and for a build too old to answer.
6+
alter table server_states add column if not exists protocol_version int not null default 0;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- name: findHub :one
2+
-- Ready as of the Go tracker's last pod sync, which runs every 5s.
3+
select id, cluster_ip, protocol_version
4+
from server_states
5+
where role = 'hub'
6+
and status = 1
7+
and ($exclude::text is null or id != $exclude)
8+
limit 1;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package net.hollowcube.ipc.session;
2+
3+
/// A server a player can be sent to, as the proxy connects to it.
4+
///
5+
/// @param id the pod name
6+
/// @param protocolVersion what the server speaks, or 0 when the tracker has not learned it yet
7+
public record GameServer(String id, String clusterIp, int protocolVersion) {
8+
}

‎modules/ipc/src/main/java/net/hollowcube/ipc/session/SessionService.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.hollowcube.ipc.session;
22

33
import net.hollowcube.ipc.util.Ipc;
4+
import org.jetbrains.annotations.Nullable;
45

56
/// Player sessions: the one row per player online anywhere on the network. The rest of the Go
67
/// `/v3/internal/session` surface lands here as it is ported.
@@ -11,4 +12,8 @@ public interface SessionService {
1112
/// shows: a single proxy only knows its own.
1213
int onlinePlayers();
1314

15+
/// A ready hub other than `exclude`, or null when there is none.
16+
@Nullable
17+
GameServer findHub(@Nullable String exclude);
18+
1419
}

0 commit comments

Comments
 (0)