|
| 1 | +package dev.imprex.orebfuscator.cache; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Queue; |
| 8 | +import java.util.concurrent.CompletableFuture; |
| 9 | +import java.util.concurrent.locks.Condition; |
| 10 | +import java.util.concurrent.locks.Lock; |
| 11 | +import java.util.concurrent.locks.ReentrantLock; |
| 12 | +import dev.imprex.orebfuscator.interop.OrebfuscatorCore; |
| 13 | +import dev.imprex.orebfuscator.logging.OfcLogger; |
| 14 | +import dev.imprex.orebfuscator.util.ChunkCacheKey; |
| 15 | +import org.jspecify.annotations.NullMarked; |
| 16 | +import org.jspecify.annotations.Nullable; |
| 17 | + |
| 18 | +/** |
| 19 | + * This class works similar to a bounded buffer for cache read and write requests but also functions as the only |
| 20 | + * consumer of said buffer. All requests can get reorder similar to modern memory access reordering in CPUs. If for |
| 21 | + * example a write request is already in the buffer and a new read request for the same position is created then the |
| 22 | + * read request doesn't get put in the buffer and gets completed with the content of the write request. |
| 23 | + * |
| 24 | + * @see <a href="https://en.wikipedia.org/wiki/Producer–consumer_problem">Bound buffer</a> |
| 25 | + * @see <a href="https://en.wikipedia.org/wiki/Memory_ordering">Memory ordering</a> |
| 26 | + */ |
| 27 | +// TODO: add statistice for read/write times and ratio of read/write as well as average throughput for read/write |
| 28 | +@NullMarked |
| 29 | +public class AsyncChunkSerializer implements Runnable { |
| 30 | + |
| 31 | + private final Lock lock = new ReentrantLock(true); |
| 32 | + private final Condition notFull = lock.newCondition(); |
| 33 | + private final Condition notEmpty = lock.newCondition(); |
| 34 | + |
| 35 | + private final Map<ChunkCacheKey, Runnable> tasks = new HashMap<>(); |
| 36 | + private final Queue<ChunkCacheKey> positions = new LinkedList<>(); |
| 37 | + |
| 38 | + private final int maxTaskQueueSize; |
| 39 | + private final ChunkSerializer serializer; |
| 40 | + |
| 41 | + private final Thread thread; |
| 42 | + private volatile boolean running = true; |
| 43 | + |
| 44 | + public AsyncChunkSerializer(OrebfuscatorCore orebfuscator, AbstractRegionFileCache<?> regionFileCache) { |
| 45 | + this.maxTaskQueueSize = orebfuscator.config().cache().maximumTaskQueueSize(); |
| 46 | + this.serializer = new ChunkSerializer(regionFileCache); |
| 47 | + |
| 48 | + this.thread = new Thread(OrebfuscatorCore.THREAD_GROUP, this, "ofc-chunk-serializer"); |
| 49 | + this.thread.setDaemon(true); |
| 50 | + this.thread.start(); |
| 51 | + |
| 52 | + orebfuscator.statistics().cache.setDiskCacheQueueLength(this.tasks::size); |
| 53 | + } |
| 54 | + |
| 55 | + public CompletableFuture<@Nullable ChunkCacheEntry> read(ChunkCacheKey key) { |
| 56 | + this.lock.lock(); |
| 57 | + try { |
| 58 | + Runnable task = this.tasks.get(key); |
| 59 | + if (task instanceof WriteTask) { |
| 60 | + return CompletableFuture.completedFuture(((WriteTask) task).chunk); |
| 61 | + } else if (task instanceof ReadTask) { |
| 62 | + return ((ReadTask) task).future; |
| 63 | + } else { |
| 64 | + CompletableFuture<ChunkCacheEntry> future = new CompletableFuture<>(); |
| 65 | + this.queueTask(key, new ReadTask(key, future)); |
| 66 | + return future; |
| 67 | + } |
| 68 | + } finally { |
| 69 | + this.lock.unlock(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void write(ChunkCacheKey key, ChunkCacheEntry chunk) { |
| 74 | + this.lock.lock(); |
| 75 | + try { |
| 76 | + Runnable prevTask = this.queueTask(key, new WriteTask(key, chunk)); |
| 77 | + if (prevTask instanceof ReadTask) { |
| 78 | + ((ReadTask) prevTask).future.complete(chunk); |
| 79 | + } |
| 80 | + } finally { |
| 81 | + this.lock.unlock(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Nullable |
| 86 | + private Runnable queueTask(ChunkCacheKey key, Runnable nextTask) { |
| 87 | + while (this.positions.size() >= this.maxTaskQueueSize) { |
| 88 | + this.notFull.awaitUninterruptibly(); |
| 89 | + } |
| 90 | + |
| 91 | + if (!this.running) { |
| 92 | + throw new IllegalStateException("AsyncChunkSerializer already closed"); |
| 93 | + } |
| 94 | + |
| 95 | + Runnable prevTask = this.tasks.put(key, nextTask); |
| 96 | + if (prevTask == null) { |
| 97 | + this.positions.offer(key); |
| 98 | + } |
| 99 | + |
| 100 | + this.notEmpty.signal(); |
| 101 | + return prevTask; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void run() { |
| 106 | + while (this.running) { |
| 107 | + this.lock.lock(); |
| 108 | + try { |
| 109 | + while (this.positions.isEmpty()) { |
| 110 | + this.notEmpty.await(); |
| 111 | + } |
| 112 | + |
| 113 | + this.tasks.remove(this.positions.poll()).run(); |
| 114 | + |
| 115 | + this.notFull.signal(); |
| 116 | + } catch (InterruptedException e) { |
| 117 | + break; |
| 118 | + } finally { |
| 119 | + this.lock.unlock(); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public void close() { |
| 125 | + this.lock.lock(); |
| 126 | + try { |
| 127 | + this.running = false; |
| 128 | + this.thread.interrupt(); |
| 129 | + |
| 130 | + while (!this.positions.isEmpty()) { |
| 131 | + Runnable task = this.tasks.remove(this.positions.poll()); |
| 132 | + if (task instanceof WriteTask) { |
| 133 | + task.run(); |
| 134 | + } |
| 135 | + } |
| 136 | + } finally { |
| 137 | + this.lock.unlock(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private class WriteTask implements Runnable { |
| 142 | + |
| 143 | + private final ChunkCacheKey key; |
| 144 | + private final ChunkCacheEntry chunk; |
| 145 | + |
| 146 | + public WriteTask(ChunkCacheKey key, ChunkCacheEntry chunk) { |
| 147 | + this.key = key; |
| 148 | + this.chunk = chunk; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void run() { |
| 153 | + try { |
| 154 | + serializer.write(key, chunk); |
| 155 | + } catch (IOException e) { |
| 156 | + OfcLogger.error(e); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private class ReadTask implements Runnable { |
| 162 | + |
| 163 | + private final ChunkCacheKey key; |
| 164 | + private final CompletableFuture<@Nullable ChunkCacheEntry> future; |
| 165 | + |
| 166 | + public ReadTask(ChunkCacheKey key, CompletableFuture<@Nullable ChunkCacheEntry> future) { |
| 167 | + this.key = key; |
| 168 | + this.future = future; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void run() { |
| 173 | + try { |
| 174 | + future.complete(serializer.read(key)); |
| 175 | + } catch (Exception e) { |
| 176 | + future.completeExceptionally(e); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments