Skip to content

Commit 4426261

Browse files
committed
More stuff
1 parent f7a065e commit 4426261

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

vanilla/src/main/java/gg/beemo/vanilla/Config.java

+3
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ public class Config {
1212

1313
public static int GRPC_PORT = 1337;
1414

15+
public static int TEA_SHARD_COUNT = 128;
16+
public static int TEA_CLUSTER_COUNT = 8;
17+
1518
}

vanilla/src/main/java/gg/beemo/vanilla/rpc/GrpcClusteringService.kt

+57-22
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ package gg.beemo.vanilla.rpc
22

33
import com.google.protobuf.Empty
44
import gg.beemo.latte.logging.Log
5-
import gg.beemo.latte.proto.ClusterConfigRequest
6-
import gg.beemo.latte.proto.ClusterConfigResponse
7-
import gg.beemo.latte.proto.clusterConfigResponse
85
import gg.beemo.latte.proto.ClusteringGrpcKt
96
import gg.beemo.latte.proto.GetClusterConfigRequest
107
import gg.beemo.latte.proto.GetClusterConfigResponse
118
import gg.beemo.latte.proto.GuildState
129
import gg.beemo.latte.proto.LookupGuildClusterRequest
1310
import gg.beemo.latte.proto.LookupGuildClusterResponse
1411
import gg.beemo.latte.proto.ShardIdentifier
15-
import gg.beemo.latte.proto.shardIdentifier
1612
import gg.beemo.latte.proto.UpdateGuildStateRequest
1713
import gg.beemo.latte.proto.getClusterConfigResponse
1814
import gg.beemo.latte.proto.lookupGuildClusterResponse
15+
import gg.beemo.latte.proto.shardIdentifier
16+
import gg.beemo.vanilla.Config
17+
import io.grpc.Status
1918
import kotlinx.coroutines.flow.Flow
2019
import java.util.HashMap
20+
import kotlin.math.min
2121

2222
data class ClusterConfig(
2323
val clusterId: String,
@@ -31,27 +31,32 @@ data class GuildStatus(
3131
)
3232

3333
class GrpcClusteringService : ClusteringGrpcKt.ClusteringCoroutineImplBase() {
34-
3534
private val log by Log
3635

3736
private val clusters = HashMap<String, ClusterConfig>()
3837
private val guilds = HashMap<Long, GuildStatus>()
3938

4039
override suspend fun getClusterConfig(request: GetClusterConfigRequest): GetClusterConfigResponse {
4140
log.info("Received cluster config request from cluster ID '${request.clusterId}'")
42-
this.clusters[request.clusterId] = ClusterConfig(
43-
clusterId = request.clusterId,
44-
grpcEndpoint = request.grpcEndpoint,
45-
)
46-
// TODO Return correct shard mapping
47-
return getClusterConfigResponse {
48-
this.shards += listOf(
49-
shardIdentifier {
50-
this.clusterId = "lol"
51-
this.shardId = 0
52-
this.shardCount = 1
53-
},
41+
this.clusters[request.clusterId] =
42+
ClusterConfig(
43+
clusterId = request.clusterId,
44+
grpcEndpoint = request.grpcEndpoint,
5445
)
46+
47+
val clusterIndex = 0 // TODO map cluster id to index
48+
val shardRange = getClusterShardRange(clusterIndex, Config.TEA_SHARD_COUNT, Config.TEA_CLUSTER_COUNT)
49+
val shards =
50+
shardRange.map { shardId ->
51+
shardIdentifier {
52+
this.clusterId = request.clusterId
53+
this.shardId = shardId
54+
this.shardCount = Config.TEA_SHARD_COUNT
55+
}
56+
}
57+
58+
return getClusterConfigResponse {
59+
this.shards += shards
5560
}
5661
}
5762

@@ -60,7 +65,11 @@ class GrpcClusteringService : ClusteringGrpcKt.ClusteringCoroutineImplBase() {
6065
val shard = update.shard
6166
log.debug(
6267
"Guild {} in Cluster {} Shard {}/{} has changed state to {}",
63-
update.guildId, shard.clusterId, shard.shardId, shard.clusterId, update.state,
68+
update.guildId,
69+
shard.clusterId,
70+
shard.shardId,
71+
shard.clusterId,
72+
update.state,
6473
)
6574
if (!clusters.containsKey(shard.clusterId)) {
6675
log.warn("Unknown cluster {} in guild update for {}", shard.clusterId, update.guildId)
@@ -75,14 +84,40 @@ class GrpcClusteringService : ClusteringGrpcKt.ClusteringCoroutineImplBase() {
7584
}
7685

7786
override suspend fun lookupGuildCluster(request: LookupGuildClusterRequest): LookupGuildClusterResponse {
78-
val guild = guilds[request.guildId]
79-
requireNotNull(guild) // TODO How to properly return errors in gRPC?
80-
val cluster = clusters[guild.shard.clusterId]
81-
requireNotNull(cluster) // TODO Same as above
87+
val guild = guilds[request.guildId] ?: throw Status.NOT_FOUND.withDescription("Guild not found").asRuntimeException()
88+
val cluster = clusters[guild.shard.clusterId] ?: throw Status.NOT_FOUND.withDescription("Cluster not found").asRuntimeException()
8289
return lookupGuildClusterResponse {
8390
this.clusterId = cluster.clusterId
8491
this.grpcEndpoint = cluster.grpcEndpoint
8592
}
8693
}
8794

95+
private fun getClusterShardRange(
96+
cluster: Int,
97+
totalShards: Int,
98+
totalClusters: Int,
99+
): IntRange {
100+
val numShardsForNormalCluster = totalShards / totalClusters
101+
val extraShards = totalShards % totalClusters
102+
103+
// If the shard cluster is within the first 0 to (extraShards - 1) shard clusters,
104+
// we will allocate one of the extra shards to it.
105+
val numCommandedShards =
106+
if (extraShards > 0 && cluster < extraShards) {
107+
numShardsForNormalCluster + 1
108+
} else {
109+
numShardsForNormalCluster
110+
}
111+
112+
val firstShardNumber =
113+
if (extraShards > 0) {
114+
cluster * numShardsForNormalCluster + min(cluster, extraShards - 1)
115+
} else {
116+
cluster * numShardsForNormalCluster
117+
}
118+
119+
val lastShardNumber = firstShardNumber + numCommandedShards - 1
120+
121+
return firstShardNumber..lastShardNumber
122+
}
88123
}

0 commit comments

Comments
 (0)