Skip to content

Commit

Permalink
feat: alter clipboard thread to move all clipboard loading from main …
Browse files Browse the repository at this point in the history
…thread (#2867)
  • Loading branch information
dordsor21 authored Nov 6, 2024
1 parent e55e8fa commit eb0f07a
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,17 @@ public BukkitPermissionAttachmentManager getPermissionAttachmentManager() {
public BukkitPlayer wrapPlayer(Player player) {
//FAWE start - Use cache over returning a direct BukkitPlayer
BukkitPlayer wePlayer = getCachedPlayer(player);
if (wePlayer == null) {
synchronized (player) {
wePlayer = getCachedPlayer(player);
if (wePlayer == null) {
wePlayer = new BukkitPlayer(this, player);
player.setMetadata("WE", new FixedMetadataValue(this, wePlayer));
return wePlayer;
}
if (wePlayer != null) {
return wePlayer;
}
synchronized (player) {
BukkitPlayer bukkitPlayer = getCachedPlayer(player);
if (bukkitPlayer == null) {
bukkitPlayer = new BukkitPlayer(this, player);
player.setMetadata("WE", new FixedMetadataValue(this, bukkitPlayer));
}
return bukkitPlayer;
}
return wePlayer;
//FAWE end
}

Expand Down
63 changes: 57 additions & 6 deletions worldedit-core/src/main/java/com/fastasyncworldedit/core/Fawe.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import com.fastasyncworldedit.core.util.TextureUtil;
import com.fastasyncworldedit.core.util.WEManager;
import com.fastasyncworldedit.core.util.task.KeyQueuedExecutorService;
import com.fastasyncworldedit.core.util.task.UUIDKeyQueuedThreadFactory;
import com.github.luben.zstd.Zstd;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import net.jpountz.lz4.LZ4Factory;
Expand All @@ -37,6 +37,9 @@
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -92,7 +95,7 @@ public class Fawe {
* The platform specific implementation.
*/
private final IFawe implementation;
private final KeyQueuedExecutorService<UUID> clipboardExecutor;
private final KeyQueuedExecutorService<UUID> uuidKeyQueuedExecutorService;
private FaweVersion version;
private TextureUtil textures;
private QueueHandler queueHandler;
Expand Down Expand Up @@ -140,14 +143,13 @@ private Fawe(final IFawe implementation) {
}, 0);

TaskManager.taskManager().repeat(timer, 1);

clipboardExecutor = new KeyQueuedExecutorService<>(new ThreadPoolExecutor(
uuidKeyQueuedExecutorService = new KeyQueuedExecutorService<>(new ThreadPoolExecutor(
1,
Settings.settings().QUEUE.PARALLEL_THREADS,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new ThreadFactoryBuilder().setNameFormat("FAWE Clipboard - %d").build()
new UUIDKeyQueuedThreadFactory()
));
}

Expand Down Expand Up @@ -463,9 +465,58 @@ public Thread setMainThread() {
*
* @return Executor used for clipboard IO if clipboard on disk is enabled or null
* @since 2.6.2
* @deprecated Use any of {@link Fawe#submitUUIDKeyQueuedTask(UUID, Runnable)},
* {@link Fawe#submitUUIDKeyQueuedTask(UUID, Runnable, Object), {@link Fawe#submitUUIDKeyQueuedTask(UUID, Callable)}
* to ensure if a thread is already a UUID-queued thread, the task is immediately run
*/
@Deprecated(forRemoval = true, since = "TODO")
public KeyQueuedExecutorService<UUID> getClipboardExecutor() {
return this.clipboardExecutor;
return this.uuidKeyQueuedExecutorService;
}

/**
* Submit a task to the UUID key-queued executor
*
* @return Future representing the tank
* @since TODO
*/
public Future<?> submitUUIDKeyQueuedTask(UUID uuid, Runnable runnable) {
if (Thread.currentThread() instanceof UUIDKeyQueuedThreadFactory.UUIDKeyQueuedThread) {
runnable.run();
return CompletableFuture.completedFuture(null);
}
return this.uuidKeyQueuedExecutorService.submit(uuid, runnable);
}

/**
* Submit a task to the UUID key-queued executor
*
* @return Future representing the tank
* @since TODO
*/
public <T> Future<T> submitUUIDKeyQueuedTask(UUID uuid, Runnable runnable, T result) {
if (Thread.currentThread() instanceof UUIDKeyQueuedThreadFactory.UUIDKeyQueuedThread) {
runnable.run();
return CompletableFuture.completedFuture(result);
}
return this.uuidKeyQueuedExecutorService.submit(uuid, runnable, result);
}

/**
* Submit a task to the UUID key-queued executor
*
* @return Future representing the tank
* @since TODO
*/
public <T> Future<T> submitUUIDKeyQueuedTask(UUID uuid, Callable<T> callable) {
if (Thread.currentThread() instanceof UUIDKeyQueuedThreadFactory.UUIDKeyQueuedThread) {
try {
return CompletableFuture.completedFuture(callable.call());
} catch (Throwable t) {
return CompletableFuture.failedFuture(t);
}
}
return this.uuidKeyQueuedExecutorService.submit(uuid, callable);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.fastasyncworldedit.core.util.task;

import org.jetbrains.annotations.ApiStatus;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

@ApiStatus.Internal
public class UUIDKeyQueuedThreadFactory implements ThreadFactory {

private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;

public UUIDKeyQueuedThreadFactory() {
group = new ThreadGroup("UUIDKeyQueuedThreadGroup");
namePrefix = "FAWE UUID-key-queued - ";
}

public Thread newThread(@Nonnull Runnable r) {
Thread t = new UUIDKeyQueuedThread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}

public static final class UUIDKeyQueuedThread extends Thread {

public UUIDKeyQueuedThread(@Nullable ThreadGroup group, Runnable task, @Nonnull String name, long stackSize) {
super(group, task, name, stackSize);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ public void loadClipboardFromDisk(File file) throws FaweClipboardVersionMismatch
}
} catch (EmptyClipboardException ignored) {
}
DiskOptimizedClipboard doc = Fawe.instance().getClipboardExecutor().submit(
DiskOptimizedClipboard doc = Fawe.instance().submitUUIDKeyQueuedTask(
uuid,
() -> DiskOptimizedClipboard.loadFromFile(file)
).get();
Expand All @@ -954,7 +954,7 @@ public void deleteClipboardOnDisk() {
} else {
continue;
}
Fawe.instance().getClipboardExecutor().submit(uuid, () -> {
Fawe.instance().submitUUIDKeyQueuedTask(uuid, () -> {
doc.close(); // Ensure closed before deletion
doc.getFile().delete();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private void createCopy(
} else {
throw e;
}
Fawe.instance().getClipboardExecutor().submit(actor.getUniqueId(), () -> {
Fawe.instance().submitUUIDKeyQueuedTask(actor.getUniqueId(), () -> {
clipboard.close();
doc.getFile().delete();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ default void unregister() {
if (Settings.settings().CLIPBOARD.USE_DISK && Settings.settings().CLIPBOARD.DELETE_ON_LOGOUT) {
session.deleteClipboardOnDisk();
} else if (Settings.settings().CLIPBOARD.USE_DISK) {
Fawe.instance().getClipboardExecutor().submit(getUniqueId(), () -> session.setClipboard(null));
Fawe.instance().submitUUIDKeyQueuedTask(getUniqueId(), () -> session.setClipboard(null));
} else if (Settings.settings().CLIPBOARD.DELETE_ON_LOGOUT) {
session.setClipboard(null);
}
Expand All @@ -436,32 +436,8 @@ default void unregister() {
void sendTitle(Component title, Component sub);

/**
* Loads any history items from disk: - Should already be called if history on disk is enabled.
*/
default void loadClipboardFromDisk() {
File file = MainUtil.getFile(
Fawe.platform().getDirectory(),
Settings.settings().PATHS.CLIPBOARD + File.separator + getUniqueId() + ".bd"
);
try {
getSession().loadClipboardFromDisk(file);
} catch (FaweClipboardVersionMismatchException e) {
print(e.getComponent());
} catch (RuntimeException e) {
print(Caption.of("fawe.error.clipboard.invalid"));
e.printStackTrace();
print(Caption.of("fawe.error.stacktrace"));
print(Caption.of("fawe.error.clipboard.load.failure"));
print(Caption.of("fawe.error.clipboard.invalid.info", file.getName(), file.length()));
print(Caption.of("fawe.error.stacktrace"));
} catch (Exception e) {
print(Caption.of("fawe.error.clipboard.invalid"));
e.printStackTrace();
print(Caption.of("fawe.error.stacktrace"));
print(Caption.of("fawe.error.no-failure"));
print(Caption.of("fawe.error.clipboard.invalid.info", file.getName(), file.length()));
print(Caption.of("fawe.error.stacktrace"));
}
}
* Loads clipboard file from disk if it exists
*/
void loadClipboardFromDisk();
//FAWE end
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@

package com.sk89q.worldedit.extension.platform;

import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.configuration.Settings;
import com.fastasyncworldedit.core.internal.exception.FaweClipboardVersionMismatchException;
import com.fastasyncworldedit.core.internal.exception.FaweException;
import com.fastasyncworldedit.core.math.MutableBlockVector3;
import com.fastasyncworldedit.core.regions.FaweMaskManager;
import com.fastasyncworldedit.core.util.MainUtil;
import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.WEManager;
import com.fastasyncworldedit.core.util.task.AsyncNotifyKeyedQueue;
Expand Down Expand Up @@ -69,6 +73,8 @@
import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
Expand All @@ -82,6 +88,7 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {

//FAWE start
private final Map<String, Object> meta;
private final Semaphore clipboardLoading = new Semaphore(1);

// Queue for async tasks
private final AtomicInteger runningCount = new AtomicInteger();
Expand Down Expand Up @@ -524,22 +531,68 @@ public Region getLargestRegion() {

@Override
public void setSelection(Region region) {
RegionSelector selector;
if (region instanceof ConvexPolyhedralRegion) {
selector = new ConvexPolyhedralRegionSelector((ConvexPolyhedralRegion) region);
} else if (region instanceof CylinderRegion) {
selector = new CylinderRegionSelector((CylinderRegion) region);
} else if (region instanceof Polygonal2DRegion) {
selector = new Polygonal2DRegionSelector((Polygonal2DRegion) region);
} else {
selector = new CuboidRegionSelector(null, region.getMinimumPoint(),
region.getMaximumPoint()
);
}
RegionSelector selector = switch (region) {
case ConvexPolyhedralRegion blockVector3s -> new ConvexPolyhedralRegionSelector(blockVector3s);
case CylinderRegion blockVector3s -> new CylinderRegionSelector(blockVector3s);
case Polygonal2DRegion blockVector3s -> new Polygonal2DRegionSelector(blockVector3s);
default -> new CuboidRegionSelector(null, region.getMinimumPoint(), region.getMaximumPoint());
};
selector.setWorld(region.getWorld());

getSession().setRegionSelector(getWorld(), selector);
}

@Override
public void loadClipboardFromDisk() {
if (!clipboardLoading.tryAcquire()) {
if (!Fawe.isMainThread()) {
try {
clipboardLoading.acquire();
clipboardLoading.release();
} catch (InterruptedException e) {
LOGGER.error("Error waiting for clipboard-on-disk loading for player {}", getName(), e);
}
}
return;
}

File file = MainUtil.getFile(
Fawe.platform().getDirectory(),
Settings.settings().PATHS.CLIPBOARD + File.separator + getUniqueId() + ".bd"
);
try {
Future<?> fut = Fawe.instance().submitUUIDKeyQueuedTask(getUniqueId(), () -> {
try {
getSession().loadClipboardFromDisk(file);
} catch (FaweClipboardVersionMismatchException e) {
print(e.getComponent());
} catch (RuntimeException e) {
print(Caption.of("fawe.error.clipboard.invalid"));
LOGGER.error("Error loading clipboard from disk", e);
print(Caption.of("fawe.error.stacktrace"));
print(Caption.of("fawe.error.clipboard.load.failure"));
print(Caption.of("fawe.error.clipboard.invalid.info", file.getName(), file.length()));
print(Caption.of("fawe.error.stacktrace"));
} catch (Exception e) {
print(Caption.of("fawe.error.clipboard.invalid"));
LOGGER.error("Error loading clipboard from disk", e);
print(Caption.of("fawe.error.stacktrace"));
print(Caption.of("fawe.error.no-failure"));
print(Caption.of("fawe.error.clipboard.invalid.info", file.getName(), file.length()));
print(Caption.of("fawe.error.stacktrace"));
} finally {
clipboardLoading.release();
}
});
if (Fawe.isMainThread()) {
return;
}
fut.get();
} catch (Exception e) {
LOGGER.error("Error loading clipboard from disk", e);
print(Caption.of("fawe.error.clipboard.load.failure"));
}
}
//FAWE end

@Override
Expand Down Expand Up @@ -698,10 +751,9 @@ public void dispatchCUIEvent(CUIEvent event) {

@Override
public boolean equals(Object other) {
if (!(other instanceof Player)) {
if (!(other instanceof Player other2)) {
return false;
}
Player other2 = (Player) other;
return other2.getName().equals(getName());
}

Expand Down
2 changes: 1 addition & 1 deletion worldedit-core/src/main/resources/lang/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"fawe.error.parse.no-clipboard-source": "No clipboards found at given source: {0}",
"fawe.error.clipboard.invalid": "====== INVALID CLIPBOARD ======",
"fawe.error.clipboard.invalid.info": "File: {0} (len: {1})",
"fawe.error.clipboard.load.failure": "Could not load clipboard. Possible that the clipboard is still being written to from another server?!",
"fawe.error.clipboard.load.failure": "Unexpected failure loading clipboard from disk!",
"fawe.error.clipboard.on.disk.version.mismatch": "Clipboard version mismatch: expected {0} but got {1}. It is recommended you delete the clipboard folder and restart the server.\nYour clipboard folder is located at {2}.",
"fawe.error.limit.disallowed-block": "Your limit disallows use of block '{0}'",
"fawe.error.limit.disallowed-property": "Your limit disallows use of property '{0}'",
Expand Down

0 comments on commit eb0f07a

Please sign in to comment.