Switch the block queue to cache blocks locally, then upload, to reduce lag when snapshotting.

This commit is contained in:
zontreck 2024-04-25 04:10:53 -07:00
parent c19e971e92
commit c6e6ed9ffe
4 changed files with 68 additions and 2 deletions

View file

@ -53,7 +53,7 @@ mod_name=Zontreck's Library Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3
# The mod version. See https://semver.org/
mod_version=1201.13.042524.0327
mod_version=1201.13.042524.0409
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -24,6 +24,7 @@ public abstract class BlockRestoreQueue
private List<PrimitiveBlock> BLOCK_QUEUE = new ArrayList<>();
private final BlockRestoreRunner RUNNER;
private ScheduledFuture<?> RUNNING_TASK;
private ScheduledFuture<?> DATABASE_UPLOAD_RUNNER;
/**
* When in database mode, this flag will be checked by the Restore Runner so a database call is not made unnecessarily.
@ -75,13 +76,14 @@ public abstract class BlockRestoreQueue
*/
public void enqueueBlock(PrimitiveBlock block)
{
/*
if(usesDatabase())
{
databaseUpdate(block);
notifyDirtyQueue(true);
hasBlocks=true;
return;
}
}*/
BLOCK_QUEUE.add(block);
notifyDirtyQueue(true);
@ -94,6 +96,7 @@ public abstract class BlockRestoreQueue
public void databaseUpdate(PrimitiveBlock block)
{
hasBlocks=true;
PreparedStatement pstmt = null;
try {
pstmt = DatabaseWrapper.get().prepareStatement("INSERT INTO `blocks` (queueName, posX, posY, posZ, snapshotID, block) VALUES (?, ?, ?, ?, ?, ?);");
@ -256,6 +259,10 @@ public abstract class BlockRestoreQueue
public void schedule(long interval, TimeUnit unit)
{
RUNNING_TASK = LibZontreck.executor.scheduleAtFixedRate(RUNNER, 2000, interval, unit);
DATABASE_UPLOAD_RUNNER = LibZontreck.executor.scheduleAtFixedRate(new DatabaseUploadRunner(this), 2000, 250, TimeUnit.MILLISECONDS);
isCancelled=false;
}
/**
@ -263,9 +270,29 @@ public abstract class BlockRestoreQueue
*/
public void cancel()
{
isCancelled=true;
RUNNING_TASK.cancel(false);
}
public boolean isCancelled=false;
/**
* Remove a block from the local queue. This does not impact the database and is used internally
* @param block
*/
public void dequeue(PrimitiveBlock block)
{
BLOCK_QUEUE.remove(block);
}
/**
* Cancels the repeating upload to database task. This is automatically invoked when cancel has been invoked, and no more tasks are to be uploaded.
*/
public void cancelUploader()
{
DATABASE_UPLOAD_RUNNER.cancel(true);
}
@SubscribeEvent
public void onServerStopping(ServerStoppingEvent event)
{

View file

@ -0,0 +1,26 @@
package dev.zontreck.libzontreck.memory.world;
public class DatabaseUploadRunner implements Runnable {
private BlockRestoreQueue QUEUE;
public DatabaseUploadRunner(BlockRestoreQueue queue)
{
QUEUE = queue;
}
@Override
public void run() {
if(QUEUE.getQueuedBlocks() == 0)
{
if(QUEUE.isCancelled)
{
QUEUE.cancelUploader();
return;
}
} else {
PrimitiveBlock block = QUEUE.getQueue().get(0);
QUEUE.dequeue(block);
QUEUE.databaseUpdate(block);
}
}
}

View file

@ -29,4 +29,17 @@ public class TagUtils
if(tag.contains(entry)) return tag.getString(entry);
else return other;
}
/**
* Get either the entry, or supply a default value
* @param tag
* @param entry
* @param other
* @return
*/
public static boolean boolOr(CompoundTag tag, String entry, boolean other)
{
if(tag.contains(entry)) return tag.getBoolean(entry);
else return other;
}
}