Improve threading by making use of green threads with Java 21

This is an experiment. The code was checked to keep blocking
of platform threads to a minimum (See [1]). This will keep
overhead to of using threads small while only I/O will
allocate more threads.

The project now uses Multi-Release Jars, so it will detect
correct version during runtime while keeping backwards
compatibility. However, your IDE might be set manually to
Java 21. A multi-project [2] architecture might be necessary,
but not tested if it really fixes it.

[1] https://openjdk.org/jeps/444
[2] https://maven.apache.org/plugins/maven-compiler-plugin/multirelease.html
This commit is contained in:
games647
2024-04-30 15:57:02 +02:00
parent 896e250fa2
commit b9e94d3020
6 changed files with 158 additions and 16 deletions

View File

@ -60,11 +60,11 @@ Possible values: `Premium`, `Cracked`, `Unknown`
## Requirements
* Java 17+ (Recommended)
* Java 8 supported, but Java 21 recommended for improved threading
* Server software in offlinemode:
* Spigot (or a fork e.g. Paper) 1.8.8+
* Protocol plugin:
* [ProtocolLib 5.1+](https://www.spigotmc.org/resources/protocollib.1997/) or
* [ProtocolLib 5.2+](https://www.spigotmc.org/resources/protocollib.1997/) or
* [ProtocolSupport](https://www.spigotmc.org/resources/protocolsupport.7201/)
* Latest BungeeCord (or a fork e.g. Waterfall) or Velocity
* An auth plugin.

View File

@ -98,7 +98,6 @@
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>**/module-info.class</exclude>
</excludes>
</filter>

View File

@ -77,9 +77,67 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>jdk21</id>
<activation>
<jdk>[21,)</jdk>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>jdk9</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>21</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java21</compileSourceRoot>
</compileSourceRoots>
<multiReleaseOutput>true</multiReleaseOutput>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
<dependencies>
<!-- Libraries that we shade into the project -->
@ -158,7 +216,7 @@
<dependency>
<groupId>com.github.games647</groupId>
<artifactId>craftapi</artifactId>
<version>0.6.2</version>
<version>0.7</version>
</dependency>
<!-- APIs we can use because they are available in all platforms (Spigot, Bungee, Velocity) -->

View File

@ -40,20 +40,16 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
public class AsyncScheduler {
private static final int MAX_CAPACITY = 1024;
//todo: single thread for delaying and scheduling tasks
private final Logger logger;
// 30 threads are still too many - the optimal solution is to separate into processing and blocking threads
// where processing threads could only be max number of cores while blocking threads could be minimized using
// non-blocking I/O and a single event executor
private final Executor processingPool;
private final AtomicInteger currentlyRunning = new AtomicInteger();
public AsyncScheduler(Logger logger, Executor processingPool) {
this.logger = logger;
logger.info("Using legacy scheduler");
this.processingPool = processingPool;
}

View File

@ -0,0 +1,75 @@
/*
* SPDX-License-Identifier: MIT
*
* The MIT License (MIT)
*
* Copyright (c) 2015-2023 games647 and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.games647.fastlogin.core;
import org.slf4j.Logger;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This limits the number of threads that are used at maximum. Thread creation can be very heavy for the CPU and
* context switching between threads too. However, we need many threads for blocking HTTP and database calls.
* Nevertheless, this number can be further limited, because the number of actually working database threads
* is limited by the size of our database pool. The goal is to separate concerns into processing and blocking only
* threads.
*/
public class AsyncScheduler {
private final Logger logger;
private final Executor asyncPool;
private final AtomicInteger currentlyRunning = new AtomicInteger();
public AsyncScheduler(Logger logger, Executor processingPool) {
this.logger = logger;
logger.info("Using optimized green threads with Java 21");
this.asyncPool = Executors.newVirtualThreadPerTaskExecutor();
}
public CompletableFuture<Void> runAsync(Runnable task) {
return CompletableFuture
.runAsync(() -> process(task), asyncPool)
.exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
private void process(Runnable task) {
currentlyRunning.incrementAndGet();
try {
task.run();
} finally {
currentlyRunning.getAndDecrement();
}
}
}

26
pom.xml
View File

@ -48,12 +48,9 @@
<!-- Set default for non-git clones -->
<git.commit.id>Unknown</git.commit.id>
<!-- Verify Java 8 compatibility while compiling with a newer toolchain
(i.e. check for unavailable methods) -->
<java.version>8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.release>${java.version}</maven.compiler.release>
<lombook.version>1.18.30</lombook.version>
<floodgate.version>development-2.2.2-SNAPSHOT</floodgate.version>
<geyser.version>2.1.0-SNAPSHOT</geyser.version>
@ -161,6 +158,23 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<configuration>
<archive>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
@ -188,7 +202,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<version>${lombook.version}</version>
<scope>provided</scope>
</dependency>