Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(native): create native image #63

Closed
DarkAtra opened this issue Mar 14, 2023 · 5 comments · Fixed by #65
Closed

feat(native): create native image #63

DarkAtra opened this issue Mar 14, 2023 · 5 comments · Fixed by #65
Assignees
Labels
enhancement New feature or request

Comments

@DarkAtra
Copy link
Owner

DarkAtra commented Mar 14, 2023

The issue
One of the biggest drawbacks of this bot is its memory consumption. With the recent upgrade to Spring Boot 3 we could try to build a native image using GraalVM.

Proposed solution
Configure the spring-boot-maven-plugin so that it builds a native image via GraalVM. This would result in near instant startup times and lower memory consumption for the bot. It would also eliminate the need for a JVM.

Notes
I've already attempted to build a native image when Spring Boot 3 was released and it didn't work out of the box. The main issue back then was that kotlin coroutines didn't play nicely with native image, see: oracle/graal#5606. Also, the database is using jackson for serialization, e.g. runtime hints are required, see: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/aot/hint/annotation/RegisterReflectionForBinding.html

@DarkAtra DarkAtra added the enhancement New feature or request label Mar 14, 2023
@DarkAtra DarkAtra changed the title feat: create native image feat(native): create native image Mar 14, 2023
@DarkAtra DarkAtra self-assigned this Mar 18, 2023
@DarkAtra
Copy link
Owner Author

DarkAtra commented Mar 20, 2023

added runtime hints:

The native image compiles and runs fine. Tested:

  • /add-server
  • /update-server
  • /remove-server
  • /get-server-details
    with a single server. Startup time is near instant and the memory footprint reduced to ~50Mi-100Mi.

Note: The current next branch is using a snapshot build of Kord (see: kordlib/kord#787).

Pre Releases:

@DarkAtra DarkAtra pinned this issue Mar 20, 2023
@DarkAtra
Copy link
Owner Author

There seems to be an issue with the native build. I'm currently trying to find the root cause. What i can tell right now is that it has something to do with the async-gamequery-lib and/or netty.

Stacktrace:

2023-03-21T00:57:52.763Z ERROR 1 --- [pool-2-thread-1] d.d.v.d.ServerStatusMonitorService       : Exception while fetching the status of 0c7a7e95-ff7a-11ec-8875-199d13cc1b6e
java.util.concurrent.CompletionException: java.lang.IllegalStateException: channel not registered to an event loop
	at com.ibasco.agql.protocols.valve.source.query.SourceQueryMessenger$RequestContext.completion(SourceQueryMessenger.java:368) ~[na:na]
	at com.ibasco.agql.protocols.valve.source.query.SourceQueryMessenger$RequestContext.access$1000(SourceQueryMessenger.java:279) ~[na:na]
	at com.ibasco.agql.protocols.valve.source.query.SourceQueryMessenger.lambda$send$1(SourceQueryMessenger.java:240) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:934) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:911) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.spi.FailsafeFuture.completeResult(FailsafeFuture.java:99) ~[na:na]
	at dev.failsafe.AsyncExecutionImpl.complete(AsyncExecutionImpl.java:153) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.internal.RetryPolicyExecutor.lambda$null$5(RetryPolicyExecutor.java:155) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2325) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.internal.RetryPolicyExecutor.lambda$handleAsync$6(RetryPolicyExecutor.java:150) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.Functions.lambda$null$2(Functions.java:152) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162) ~[de.darkatra.vrising.discord.BotKt:na]
	at com.ibasco.agql.core.transport.pool.SimpleNettyChannelPool.lambda$notifyOnComplete$2(SimpleNettyChannelPool.java:253) ~[de.darkatra.vrising.discord.BotKt:1.1.0]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.spi.FailsafeFuture.completeResult(FailsafeFuture.java:99) ~[na:na]
	at dev.failsafe.AsyncExecutionImpl.complete(AsyncExecutionImpl.java:153) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.internal.RetryPolicyExecutor.lambda$null$5(RetryPolicyExecutor.java:155) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2325) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.internal.RetryPolicyExecutor.lambda$handleAsync$6(RetryPolicyExecutor.java:150) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.spi.PolicyExecutor.lambda$null$2(PolicyExecutor.java:109) ~[de.darkatra.vrising.discord.BotKt:3.3.0]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.Functions.lambda$null$5(Functions.java:202) ~[na:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2325) ~[de.darkatra.vrising.discord.BotKt:na]
	at dev.failsafe.Functions.lambda$null$6(Functions.java:198) ~[na:na]
	at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:96) ~[de.darkatra.vrising.discord.BotKt:4.1.89.Final]
	at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:153) ~[de.darkatra.vrising.discord.BotKt:4.1.89.Final]
	at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174) ~[de.darkatra.vrising.discord.BotKt:4.1.89.Final]
	at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167) ~[de.darkatra.vrising.discord.BotKt:4.1.89.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470) ~[de.darkatra.vrising.discord.BotKt:4.1.89.Final]
	at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:403) ~[na:na]
	at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[na:na]
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[na:na]
	at [email protected]/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[de.darkatra.vrising.discord.BotKt:na]
	at [email protected]/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[na:na]
	at [email protected]/java.lang.Thread.run(Thread.java:833) ~[de.darkatra.vrising.discord.BotKt:na]
	at com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775) ~[de.darkatra.vrising.discord.BotKt:na]
	at com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:203) ~[na:na]
Caused by: java.lang.IllegalStateException: channel not registered to an event loop
	at io.netty.channel.AbstractChannel.eventLoop(AbstractChannel.java:164) ~[de.darkatra.vrising.discord.BotKt:4.1.89.Final]
	at com.ibasco.agql.core.util.Netty.toCompletable(Netty.java:101) ~[na:na]
	at com.ibasco.agql.core.transport.UdpNettyChannelFactory.newChannel(UdpNettyChannelFactory.java:75) ~[na:na]
	at com.ibasco.agql.core.transport.AbstractNettyChannelFactory.create(AbstractNettyChannelFactory.java:234) ~[de.darkatra.vrising.discord.BotKt:1.1.0]
	at com.ibasco.agql.core.transport.NettyChannelFactoryDecorator.create(NettyChannelFactoryDecorator.java:55) ~[de.darkatra.vrising.discord.BotKt:1.1.0]
	at com.ibasco.agql.core.transport.FailsafeChannelFactory.access$101(FailsafeChannelFactory.java:52) ~[na:na]
	at com.ibasco.agql.core.transport.FailsafeChannelFactory$ChannelSupplier.get(FailsafeChannelFactory.java:138) ~[na:na]
	at com.ibasco.agql.core.transport.FailsafeChannelFactory$ChannelSupplier.get(FailsafeChannelFactory.java:127) ~[na:na]
	at dev.failsafe.Functions.lambda$getPromiseOfStage$3(Functions.java:136) ~[na:na]
	... 15 common frames omitted

It seems to be specific to linux builds as both Windows and MacOS executables are working just fine.

The error can also be reproduced in WSL by compiling via (Note, that GraalVM needs to be installed in WSL):

mvn -Pnative clean native:compile

and then running the resulting exectuable with at least one server in the database.

@DarkAtra
Copy link
Owner Author

The above issue can be avoided on linux by passing -Dagql.nativeTransport=false as an argument to the docker image or the executable.

Example for docker-compose:

services:
  v-rising-discord-bot:
    image: ghcr.io/darkatra/v-rising-discord-bot:2.0.6-next.3-native
    command: -Dagql.nativeTransport=false
    mem_reservation: 256M
    mem_limit: 512M
    volumes:
      - /opt/v-rising-discord-bot:/data/v-rising-discord-bot
    environment:
      - BOT_DISCORD_BOT_TOKEN=<your-discord-bot-token>
      - BOT_DATABASE_PATH=/data/v-rising-discord-bot/bot.db
      - BOT_DATABASE_PASSWORD=<the-database-password>
    restart: unless-stopped

@DarkAtra
Copy link
Owner Author

progress is now tracked in the PR: #65

@DarkAtra DarkAtra unpinned this issue Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant