zoom zoom faster - make 3 different Kafka consumers for player/tile/packet updates
This commit is contained in:
parent
215ef66fbd
commit
ba03f761e0
3 changed files with 60 additions and 22 deletions
|
|
@ -7,29 +7,38 @@ import dev.adamko.kafkatorio.processor.topology.saveMapTiles
|
||||||
import dev.adamko.kafkatorio.processor.topology.splitFactorioServerPacketStream
|
import dev.adamko.kafkatorio.processor.topology.splitFactorioServerPacketStream
|
||||||
import dev.adamko.kafkatorio.processor.topology.tileProtoColourDictionary
|
import dev.adamko.kafkatorio.processor.topology.tileProtoColourDictionary
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
import kotlinx.coroutines.CoroutineName
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.job
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import org.apache.kafka.streams.KafkaStreams
|
import org.apache.kafka.streams.KafkaStreams
|
||||||
import org.apache.kafka.streams.StreamsBuilder
|
import org.apache.kafka.streams.StreamsBuilder
|
||||||
import org.apache.kafka.streams.TopologyDescription
|
import org.apache.kafka.streams.StreamsConfig
|
||||||
|
import org.apache.kafka.streams.Topology
|
||||||
|
|
||||||
class KafkatorioTopology(
|
class KafkatorioTopology(
|
||||||
private val websocketServer: WebsocketServer,
|
private val websocketServer: WebsocketServer,
|
||||||
private val builder: StreamsBuilder = StreamsBuilder(),
|
) : CoroutineScope {
|
||||||
) {
|
|
||||||
|
|
||||||
|
override val coroutineContext: CoroutineContext =
|
||||||
|
Dispatchers.Default + SupervisorJob() + CoroutineName("KafkatorioTopology")
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val sourceTopic = "factorio-server-log"
|
const val sourceTopic = "factorio-server-log"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun start() {
|
||||||
|
saveTiles()
|
||||||
|
splitPackets()
|
||||||
|
playerUpdates()
|
||||||
|
}
|
||||||
|
|
||||||
fun build() {
|
private fun saveTiles() {
|
||||||
|
val builder = StreamsBuilder()
|
||||||
val packets = factorioServerPacketStream(builder)
|
val packets = factorioServerPacketStream(builder)
|
||||||
|
|
||||||
splitFactorioServerPacketStream(packets)
|
|
||||||
|
|
||||||
playerUpdatesToWsServer(websocketServer, builder)
|
|
||||||
|
|
||||||
val tileProtoColourDict = tileProtoColourDictionary(packets)
|
val tileProtoColourDict = tileProtoColourDictionary(packets)
|
||||||
|
|
||||||
val tilePrototypesTable = groupTilesIntoChunksWithColours(
|
val tilePrototypesTable = groupTilesIntoChunksWithColours(
|
||||||
|
|
@ -40,17 +49,46 @@ class KafkatorioTopology(
|
||||||
|
|
||||||
val topology = builder.build()
|
val topology = builder.build()
|
||||||
|
|
||||||
val streams = KafkaStreams(topology, appProps.kafkaConfig)
|
launchTopology("saveTiles", topology)
|
||||||
|
}
|
||||||
|
|
||||||
// streams.cleanUp()
|
private fun splitPackets() {
|
||||||
|
|
||||||
|
val builder = StreamsBuilder()
|
||||||
|
val packets = factorioServerPacketStream(builder)
|
||||||
|
splitFactorioServerPacketStream(packets)
|
||||||
|
|
||||||
|
val topology = builder.build()
|
||||||
|
|
||||||
|
launchTopology("splitPackets", topology)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun playerUpdates() {
|
||||||
|
val builder = StreamsBuilder()
|
||||||
|
playerUpdatesToWsServer(websocketServer, builder)
|
||||||
|
|
||||||
|
val topology = builder.build()
|
||||||
|
|
||||||
|
launchTopology("playerUpdates", topology)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun launchTopology(id: String, topology: Topology) {
|
||||||
|
|
||||||
|
val props = appProps.kafkaConfig
|
||||||
|
|
||||||
|
val appId = props.compute(StreamsConfig.APPLICATION_ID_CONFIG) { _, v -> "$v.$id" } as? String
|
||||||
|
props.setProperty(StreamsConfig.APPLICATION_ID_CONFIG, appId)
|
||||||
|
|
||||||
|
|
||||||
|
val streams = KafkaStreams(topology, props)
|
||||||
streams.setUncaughtExceptionHandler(StreamsExceptionHandler())
|
streams.setUncaughtExceptionHandler(StreamsExceptionHandler())
|
||||||
Runtime.getRuntime().addShutdownHook(Thread { streams.close(Duration.ofSeconds(1)) })
|
|
||||||
|
|
||||||
val description: TopologyDescription = topology.describe()
|
coroutineContext.job.invokeOnCompletion {
|
||||||
println(description)
|
println("closing $id KafkaStreams")
|
||||||
|
streams.close(Duration.ofSeconds(1))
|
||||||
streams.start()
|
}
|
||||||
|
|
||||||
|
launch { streams.start() }
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,10 +45,10 @@ class ApplicationProperties(
|
||||||
val kafkaConfig: Properties
|
val kafkaConfig: Properties
|
||||||
get() = EnvironmentKey
|
get() = EnvironmentKey
|
||||||
.nonEmptyString()
|
.nonEmptyString()
|
||||||
.map {
|
.map { srcString ->
|
||||||
it.reader().use { sr ->
|
srcString.reader().use { stringReader ->
|
||||||
Properties().also { p ->
|
Properties().also { properties ->
|
||||||
p.load(sr)
|
properties.load(stringReader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ fun main(): Unit = runBlocking {
|
||||||
val tileServer = WebMapTileServer()
|
val tileServer = WebMapTileServer()
|
||||||
|
|
||||||
val topology = KafkatorioTopology(wsServer)
|
val topology = KafkatorioTopology(wsServer)
|
||||||
topology.build()
|
topology.start()
|
||||||
|
|
||||||
val webServer = PolyHandler(
|
val webServer = PolyHandler(
|
||||||
tileServer.build(),
|
tileServer.build(),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue