Skip to content

Commit

Permalink
Heavily increase usage of ForkJoinPool and fix issues on low-core env…
Browse files Browse the repository at this point in the history
…ironments
  • Loading branch information
AlexProgrammerDE committed Sep 30, 2024
1 parent 54400be commit d190730
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,7 @@ private void start() {
break;
}

try {
connectSemaphore.acquire();
} catch (InterruptedException e) {
logger.error("Error while waiting for connection slot", e);
break;
}
TimeUtil.acquireYielding(connectSemaphore);

logger.debug("Scheduling bot {}", factory.minecraftAccount().lastKnownName());
scheduler.schedule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.soulfiremc.grpc.generated.*;
import com.soulfiremc.server.account.MCAuthService;
import com.soulfiremc.server.user.Permissions;
import com.soulfiremc.server.util.TimeUtil;
import com.soulfiremc.settings.account.MinecraftAccount;
import com.soulfiremc.settings.proxy.SFProxy;
import io.grpc.Status;
Expand Down Expand Up @@ -53,10 +54,8 @@ public void loginCredentials(CredentialsAuthRequest request, StreamObserver<Cred
.parallelStream()
.map(payload -> {
try {
semaphore.acquire();
TimeUtil.acquireYielding(semaphore);
return service.createDataAndLogin(payload, proxy).join().toProto();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
semaphore.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.soulfiremc.server.protocol.bot.model.ChunkKey;
import com.soulfiremc.server.protocol.bot.utils.SectionUtils;
import com.soulfiremc.server.util.NoopLock;
import com.soulfiremc.server.util.TimeUtil;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import org.cloudburstmc.math.vector.Vector3i;
Expand Down Expand Up @@ -64,7 +65,7 @@ public ChunkData getChunk(Vector3i block) {
}

private ChunkData getChunkFromSection(long sectionIndex) {
readLock.lock();
TimeUtil.lockYielding(readLock);
try {
return chunks.get(sectionIndex);
} finally {
Expand All @@ -73,7 +74,7 @@ private ChunkData getChunkFromSection(long sectionIndex) {
}

public boolean isChunkLoaded(int x, int z) {
readLock.lock();
TimeUtil.lockYielding(readLock);
try {
return chunks.containsKey(ChunkKey.calculateKey(x, z));
} finally {
Expand All @@ -87,7 +88,7 @@ public boolean isChunkLoaded(Vector3i block) {
}

public void removeChunk(int x, int z) {
writeLock.lock();
TimeUtil.lockYielding(writeLock);
try {
chunks.remove(ChunkKey.calculateKey(x, z));
} finally {
Expand All @@ -96,7 +97,7 @@ public void removeChunk(int x, int z) {
}

public ChunkData getOrCreateChunk(int x, int z) {
writeLock.lock();
TimeUtil.lockYielding(writeLock);
try {
return chunks.computeIfAbsent(
ChunkKey.calculateKey(x, z), (key) -> new ChunkData(levelHeightAccessor, false));
Expand Down
69 changes: 62 additions & 7 deletions server/src/main/java/com/soulfiremc/server/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
package com.soulfiremc.server.util;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.function.BooleanSupplier;

/**
Expand All @@ -31,19 +34,71 @@ private TimeUtil() {}

public static void waitTime(long time, TimeUnit unit) {
try {
unit.sleep(time);
ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker() {
private final long endTime = System.nanoTime() + unit.toNanos(time);

public boolean isReleasable() {
return System.nanoTime() >= endTime;
}

public boolean block() throws InterruptedException {
Thread.sleep(1);
return System.nanoTime() >= endTime;
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public static void waitCondition(BooleanSupplier condition) {
while (condition.getAsBoolean()) {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
try {
ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker() {
public boolean isReleasable() {
return !condition.getAsBoolean();
}

public boolean block() throws InterruptedException {
Thread.sleep(1);
return !condition.getAsBoolean();
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public static void acquireYielding(Semaphore semaphore) {
try {
ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker() {
public boolean isReleasable() {
return semaphore.tryAcquire();
}

public boolean block() throws InterruptedException {
semaphore.acquire();
return true;
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

public static void lockYielding(Lock lock) {
try {
ForkJoinPool.managedBlock(new ForkJoinPool.ManagedBlocker() {
public boolean isReleasable() {
return lock.tryLock();
}

public boolean block() {
lock.lock();
return true;
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

0 comments on commit d190730

Please sign in to comment.