feat: configure logging and add OpenTelemetry support (#49)
Build & Test (NowChessSystems) TeamCity build failed
Build & Test (NowChessSystems) TeamCity build failed
Reviewed-on: #49
This commit was merged in pull request #49.
This commit is contained in:
@@ -7,6 +7,7 @@ import de.nowchess.bot.ai.Evaluation
|
||||
import de.nowchess.bot.util.ZobristHash
|
||||
import de.nowchess.api.rules.RuleSet
|
||||
import de.nowchess.rules.sets.DefaultRules
|
||||
import io.micrometer.core.instrument.MeterRegistry
|
||||
import java.util.concurrent.atomic.{AtomicInteger, AtomicLong}
|
||||
|
||||
final class AlphaBetaSearch(
|
||||
@@ -14,6 +15,7 @@ final class AlphaBetaSearch(
|
||||
tt: TranspositionTable = TranspositionTable(),
|
||||
weights: Evaluation,
|
||||
numThreads: Int = Runtime.getRuntime.availableProcessors,
|
||||
meterRegistry: MeterRegistry = null,
|
||||
):
|
||||
|
||||
private val INF = Int.MaxValue / 2
|
||||
@@ -85,8 +87,8 @@ final class AlphaBetaSearch(
|
||||
val rootHash = ZobristHash.hash(context)
|
||||
|
||||
@scala.annotation.tailrec
|
||||
def loop(bestSoFar: Option[Move], prevScore: Int, depth: Int): Option[Move] =
|
||||
if isOutOfTime then bestSoFar
|
||||
def loop(bestSoFar: Option[Move], prevScore: Int, depth: Int, lastDepth: Int): (Option[Move], Int) =
|
||||
if isOutOfTime then (bestSoFar, lastDepth)
|
||||
else
|
||||
val (alpha, beta) =
|
||||
if depth == 1 then (-INF, INF) else (prevScore - ASPIRATION_DELTA, prevScore + ASPIRATION_DELTA)
|
||||
@@ -99,9 +101,16 @@ final class AlphaBetaSearch(
|
||||
rootHash,
|
||||
excludedRootMoves,
|
||||
)
|
||||
loop(move.orElse(bestSoFar), score, depth + 1)
|
||||
loop(move.orElse(bestSoFar), score, depth + 1, depth)
|
||||
|
||||
loop(None, 0, 1)
|
||||
val (result, depthReached) = loop(None, 0, 1, 0)
|
||||
recordSearchMetrics(depthReached)
|
||||
result
|
||||
|
||||
private def recordSearchMetrics(depthReached: Int): Unit =
|
||||
if meterRegistry != null then
|
||||
meterRegistry.summary("nowchess.bot.search.nodes").record(nodeCount.get().toDouble)
|
||||
meterRegistry.summary("nowchess.bot.search.depth").record(depthReached.toDouble)
|
||||
|
||||
private def isOutOfTime: Boolean =
|
||||
System.currentTimeMillis - timeStartMs.get >= timeLimitMs.get
|
||||
|
||||
+10
-5
@@ -6,6 +6,7 @@ import de.nowchess.bot.BotController
|
||||
import de.nowchess.bot.BotDifficulty
|
||||
import de.nowchess.bot.config.RedisConfig
|
||||
import de.nowchess.io.fen.FenParser
|
||||
import io.micrometer.core.instrument.MeterRegistry
|
||||
import io.quarkus.redis.datasource.RedisDataSource
|
||||
import io.quarkus.runtime.StartupEvent
|
||||
import jakarta.enterprise.context.ApplicationScoped
|
||||
@@ -18,10 +19,11 @@ import java.util.function.Consumer
|
||||
class OfficialBotService:
|
||||
|
||||
// scalafix:off DisableSyntax.var
|
||||
@Inject var redis: RedisDataSource = uninitialized
|
||||
@Inject var redisConfig: RedisConfig = uninitialized
|
||||
@Inject var objectMapper: ObjectMapper = uninitialized
|
||||
@Inject var botController: BotController = uninitialized
|
||||
@Inject var redis: RedisDataSource = uninitialized
|
||||
@Inject var redisConfig: RedisConfig = uninitialized
|
||||
@Inject var objectMapper: ObjectMapper = uninitialized
|
||||
@Inject var botController: BotController = uninitialized
|
||||
@Inject var meterRegistry: MeterRegistry = uninitialized
|
||||
// scalafix:on DisableSyntax.var
|
||||
|
||||
private val terminalStatuses =
|
||||
@@ -85,7 +87,10 @@ class OfficialBotService:
|
||||
val level = DifficultyMapper.fromElo(difficulty).getOrElse(BotDifficulty.Medium)
|
||||
botController.getBot(botName).orElse(botController.getBot(level.toString.toLowerCase)).foreach { bot =>
|
||||
FenParser.parseFen(fen).toOption.foreach { context =>
|
||||
bot(context).foreach { move =>
|
||||
val timer = meterRegistry.timer("nowchess.bot.move.duration", "bot", botName)
|
||||
val moveOpt = timer.recordCallable(() => bot(context))
|
||||
moveOpt.flatten.foreach { move =>
|
||||
meterRegistry.counter("nowchess.bot.moves.computed", "bot", botName).increment()
|
||||
val uci = toUci(move)
|
||||
val c2sTopic = s"${redisConfig.prefix}:game:$gameId:c2s"
|
||||
val moveMsg = s"""{"type":"MOVE","uci":"$uci","playerId":"$botAccountId"}"""
|
||||
|
||||
Reference in New Issue
Block a user