3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-12-26 19:02:36 +01:00

feat: alter clipboard thread to move all clipboard loading from main thread (#2867)

Dieser Commit ist enthalten in:
Jordan 2024-11-06 17:16:03 +00:00 committet von GitHub
Ursprung e55e8fabe8
Commit eb0f07af81
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
8 geänderte Dateien mit 181 neuen und 61 gelöschten Zeilen

Datei anzeigen

@ -564,17 +564,17 @@ public class WorldEditPlugin extends JavaPlugin {
public BukkitPlayer wrapPlayer(Player player) { public BukkitPlayer wrapPlayer(Player player) {
//FAWE start - Use cache over returning a direct BukkitPlayer //FAWE start - Use cache over returning a direct BukkitPlayer
BukkitPlayer wePlayer = getCachedPlayer(player); BukkitPlayer wePlayer = getCachedPlayer(player);
if (wePlayer == null) { if (wePlayer != null) {
synchronized (player) { return wePlayer;
wePlayer = getCachedPlayer(player); }
if (wePlayer == null) { synchronized (player) {
wePlayer = new BukkitPlayer(this, player); BukkitPlayer bukkitPlayer = getCachedPlayer(player);
player.setMetadata("WE", new FixedMetadataValue(this, wePlayer)); if (bukkitPlayer == null) {
return wePlayer; bukkitPlayer = new BukkitPlayer(this, player);
} player.setMetadata("WE", new FixedMetadataValue(this, bukkitPlayer));
} }
return bukkitPlayer;
} }
return wePlayer;
//FAWE end //FAWE end
} }

Datei anzeigen

@ -14,8 +14,8 @@ import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.TextureUtil; import com.fastasyncworldedit.core.util.TextureUtil;
import com.fastasyncworldedit.core.util.WEManager; import com.fastasyncworldedit.core.util.WEManager;
import com.fastasyncworldedit.core.util.task.KeyQueuedExecutorService; import com.fastasyncworldedit.core.util.task.KeyQueuedExecutorService;
import com.fastasyncworldedit.core.util.task.UUIDKeyQueuedThreadFactory;
import com.github.luben.zstd.Zstd; import com.github.luben.zstd.Zstd;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.internal.util.LogManagerCompat; import com.sk89q.worldedit.internal.util.LogManagerCompat;
import net.jpountz.lz4.LZ4Factory; import net.jpountz.lz4.LZ4Factory;
@ -37,6 +37,9 @@ import java.lang.management.MemoryUsage;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; 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.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -92,7 +95,7 @@ public class Fawe {
* The platform specific implementation. * The platform specific implementation.
*/ */
private final IFawe implementation; private final IFawe implementation;
private final KeyQueuedExecutorService<UUID> clipboardExecutor; private final KeyQueuedExecutorService<UUID> uuidKeyQueuedExecutorService;
private FaweVersion version; private FaweVersion version;
private TextureUtil textures; private TextureUtil textures;
private QueueHandler queueHandler; private QueueHandler queueHandler;
@ -140,14 +143,13 @@ public class Fawe {
}, 0); }, 0);
TaskManager.taskManager().repeat(timer, 1); TaskManager.taskManager().repeat(timer, 1);
uuidKeyQueuedExecutorService = new KeyQueuedExecutorService<>(new ThreadPoolExecutor(
clipboardExecutor = new KeyQueuedExecutorService<>(new ThreadPoolExecutor(
1, 1,
Settings.settings().QUEUE.PARALLEL_THREADS, Settings.settings().QUEUE.PARALLEL_THREADS,
0L, 0L,
TimeUnit.MILLISECONDS, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(), new LinkedBlockingQueue<>(),
new ThreadFactoryBuilder().setNameFormat("FAWE Clipboard - %d").build() new UUIDKeyQueuedThreadFactory()
)); ));
} }
@ -463,9 +465,58 @@ public class Fawe {
* *
* @return Executor used for clipboard IO if clipboard on disk is enabled or null * @return Executor used for clipboard IO if clipboard on disk is enabled or null
* @since 2.6.2 * @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() { 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);
} }
} }

Datei anzeigen

@ -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);
}
}
}

Datei anzeigen

@ -930,7 +930,7 @@ public class LocalSession implements TextureHolder {
} }
} catch (EmptyClipboardException ignored) { } catch (EmptyClipboardException ignored) {
} }
DiskOptimizedClipboard doc = Fawe.instance().getClipboardExecutor().submit( DiskOptimizedClipboard doc = Fawe.instance().submitUUIDKeyQueuedTask(
uuid, uuid,
() -> DiskOptimizedClipboard.loadFromFile(file) () -> DiskOptimizedClipboard.loadFromFile(file)
).get(); ).get();
@ -954,7 +954,7 @@ public class LocalSession implements TextureHolder {
} else { } else {
continue; continue;
} }
Fawe.instance().getClipboardExecutor().submit(uuid, () -> { Fawe.instance().submitUUIDKeyQueuedTask(uuid, () -> {
doc.close(); // Ensure closed before deletion doc.close(); // Ensure closed before deletion
doc.getFile().delete(); doc.getFile().delete();
}); });

Datei anzeigen

@ -325,7 +325,7 @@ public class ClipboardCommands {
} else { } else {
throw e; throw e;
} }
Fawe.instance().getClipboardExecutor().submit(actor.getUniqueId(), () -> { Fawe.instance().submitUUIDKeyQueuedTask(actor.getUniqueId(), () -> {
clipboard.close(); clipboard.close();
doc.getFile().delete(); doc.getFile().delete();
}); });

Datei anzeigen

@ -423,7 +423,7 @@ public interface Player extends Entity, Actor {
if (Settings.settings().CLIPBOARD.USE_DISK && Settings.settings().CLIPBOARD.DELETE_ON_LOGOUT) { if (Settings.settings().CLIPBOARD.USE_DISK && Settings.settings().CLIPBOARD.DELETE_ON_LOGOUT) {
session.deleteClipboardOnDisk(); session.deleteClipboardOnDisk();
} else if (Settings.settings().CLIPBOARD.USE_DISK) { } 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) { } else if (Settings.settings().CLIPBOARD.DELETE_ON_LOGOUT) {
session.setClipboard(null); session.setClipboard(null);
} }
@ -436,32 +436,8 @@ public interface Player extends Entity, Actor {
void sendTitle(Component title, Component sub); void sendTitle(Component title, Component sub);
/** /**
* Loads any history items from disk: - Should already be called if history on disk is enabled. * Loads clipboard file from disk if it exists
*/ */
default void loadClipboardFromDisk() { 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"));
}
}
//FAWE end //FAWE end
} }

Datei anzeigen

@ -19,10 +19,14 @@
package com.sk89q.worldedit.extension.platform; package com.sk89q.worldedit.extension.platform;
import com.fastasyncworldedit.core.Fawe;
import com.fastasyncworldedit.core.configuration.Caption; 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.internal.exception.FaweException;
import com.fastasyncworldedit.core.math.MutableBlockVector3; import com.fastasyncworldedit.core.math.MutableBlockVector3;
import com.fastasyncworldedit.core.regions.FaweMaskManager; import com.fastasyncworldedit.core.regions.FaweMaskManager;
import com.fastasyncworldedit.core.util.MainUtil;
import com.fastasyncworldedit.core.util.TaskManager; import com.fastasyncworldedit.core.util.TaskManager;
import com.fastasyncworldedit.core.util.WEManager; import com.fastasyncworldedit.core.util.WEManager;
import com.fastasyncworldedit.core.util.task.AsyncNotifyKeyedQueue; import com.fastasyncworldedit.core.util.task.AsyncNotifyKeyedQueue;
@ -69,6 +73,8 @@ import javax.annotation.Nullable;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
@ -82,6 +88,7 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
//FAWE start //FAWE start
private final Map<String, Object> meta; private final Map<String, Object> meta;
private final Semaphore clipboardLoading = new Semaphore(1);
// Queue for async tasks // Queue for async tasks
private final AtomicInteger runningCount = new AtomicInteger(); private final AtomicInteger runningCount = new AtomicInteger();
@ -524,22 +531,68 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
@Override @Override
public void setSelection(Region region) { public void setSelection(Region region) {
RegionSelector selector; RegionSelector selector = switch (region) {
if (region instanceof ConvexPolyhedralRegion) { case ConvexPolyhedralRegion blockVector3s -> new ConvexPolyhedralRegionSelector(blockVector3s);
selector = new ConvexPolyhedralRegionSelector((ConvexPolyhedralRegion) region); case CylinderRegion blockVector3s -> new CylinderRegionSelector(blockVector3s);
} else if (region instanceof CylinderRegion) { case Polygonal2DRegion blockVector3s -> new Polygonal2DRegionSelector(blockVector3s);
selector = new CylinderRegionSelector((CylinderRegion) region); default -> new CuboidRegionSelector(null, region.getMinimumPoint(), region.getMaximumPoint());
} else if (region instanceof Polygonal2DRegion) { };
selector = new Polygonal2DRegionSelector((Polygonal2DRegion) region);
} else {
selector = new CuboidRegionSelector(null, region.getMinimumPoint(),
region.getMaximumPoint()
);
}
selector.setWorld(region.getWorld()); selector.setWorld(region.getWorld());
getSession().setRegionSelector(getWorld(), selector); 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 //FAWE end
@Override @Override
@ -698,10 +751,9 @@ public abstract class AbstractPlayerActor implements Actor, Player, Cloneable {
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (!(other instanceof Player)) { if (!(other instanceof Player other2)) {
return false; return false;
} }
Player other2 = (Player) other;
return other2.getName().equals(getName()); return other2.getName().equals(getName());
} }

Datei anzeigen

@ -132,7 +132,7 @@
"fawe.error.parse.no-clipboard-source": "No clipboards found at given source: {0}", "fawe.error.parse.no-clipboard-source": "No clipboards found at given source: {0}",
"fawe.error.clipboard.invalid": "====== INVALID CLIPBOARD ======", "fawe.error.clipboard.invalid": "====== INVALID CLIPBOARD ======",
"fawe.error.clipboard.invalid.info": "File: {0} (len: {1})", "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.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-block": "Your limit disallows use of block '{0}'",
"fawe.error.limit.disallowed-property": "Your limit disallows use of property '{0}'", "fawe.error.limit.disallowed-property": "Your limit disallows use of property '{0}'",