From f96ae5f14651ff3b0a77f00de4d5011a1d16d345 Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Sat, 1 Feb 2025 13:37:43 -0800 Subject: [PATCH] sync stale and main highlight cache writes --- .../ChunkHighlightBaseCacheHandler.java | 2 +- .../ChunkHighlightCacheDimensionHandler.java | 95 ++++++++++++------- 2 files changed, 61 insertions(+), 36 deletions(-) diff --git a/common/src/main/java/xaeroplus/feature/render/highlights/ChunkHighlightBaseCacheHandler.java b/common/src/main/java/xaeroplus/feature/render/highlights/ChunkHighlightBaseCacheHandler.java index fabc2f56..4c9c7635 100644 --- a/common/src/main/java/xaeroplus/feature/render/highlights/ChunkHighlightBaseCacheHandler.java +++ b/common/src/main/java/xaeroplus/feature/render/highlights/ChunkHighlightBaseCacheHandler.java @@ -50,7 +50,7 @@ public boolean addHighlight(final int x, final int z, final long foundTime) { return true; } - private void addQueuedHighlight(final int x, final int z, final long foundTime) { + void addQueuedHighlight(final int x, final int z, final long foundTime) { final long chunkPos = chunkPosToLong(x, z); try { if (lock.writeLock().tryLock(1, TimeUnit.SECONDS)) { diff --git a/common/src/main/java/xaeroplus/feature/render/highlights/ChunkHighlightCacheDimensionHandler.java b/common/src/main/java/xaeroplus/feature/render/highlights/ChunkHighlightCacheDimensionHandler.java index dc0fde26..2c8622a4 100644 --- a/common/src/main/java/xaeroplus/feature/render/highlights/ChunkHighlightCacheDimensionHandler.java +++ b/common/src/main/java/xaeroplus/feature/render/highlights/ChunkHighlightCacheDimensionHandler.java @@ -233,71 +233,96 @@ private void writeStaleHighlightsToDatabase0() { } @Override - public boolean addHighlight(final int x, final int z) { - boolean b = super.addHighlight(x, z, System.currentTimeMillis()); - if (b) { - // todo: we should always add to stalechunks during the base cache write lock - try { + public boolean addHighlight(final int x, final int z, final long foundTime) { + final long chunkPos = chunkPosToLong(x, z); + try { + boolean b = false; + if (lock.writeLock().tryLock()) { if (staleChunksLock.writeLock().tryLock()) { + chunks.put(chunkPos, foundTime); staleChunks.add(chunkPosToLong(x, z)); + b = true; staleChunksLock.writeLock().unlock(); - } else { - executorService.execute(() -> addQueuedStaleHighlight(x, z)); } - } catch (final Exception e) { - XaeroPlus.LOGGER.error("Failed to add highlight to {} stale chunks: {}", database.databaseName, dimension.location(), e); + lock.writeLock().unlock(); } + if (!b) { + try { + executorService.execute(() -> addQueuedHighlight(x, z, foundTime)); + } catch (final Exception e) { + XaeroPlus.LOGGER.error("Failed to submit new queued highlight write: {}, {}", x, z, e); + } + } + } catch (final Exception e) { + XaeroPlus.LOGGER.error("Failed to add new highlight: {}, {}", x, z, e); } - return b; + return true; } - private void addQueuedStaleHighlight(final int x, final int z) { + @Override + void addQueuedHighlight(final int x, final int z, final long foundTime) { + final long chunkPos = chunkPosToLong(x, z); try { - if (staleChunksLock.writeLock().tryLock(1, TimeUnit.SECONDS)) { - staleChunks.add(chunkPosToLong(x, z)); - staleChunksLock.writeLock().unlock(); + if (lock.writeLock().tryLock(1, TimeUnit.SECONDS)) { + if (staleChunksLock.writeLock().tryLock(1, TimeUnit.SECONDS)) { + chunks.put(chunkPos, foundTime); + staleChunks.add(chunkPosToLong(x, z)); + staleChunksLock.writeLock().unlock(); + } else { + XaeroPlus.LOGGER.error("Failed to add new queued highlight: timed out stale lock {}, {}", x, z); + } + lock.writeLock().unlock(); } else { - XaeroPlus.LOGGER.error("Failed to add new queued stale highlight: timed out: {}, {}", x, z); + XaeroPlus.LOGGER.error("Failed to add new queued highlight: timed out cache lock: {}, {}", x, z); } } catch (InterruptedException e) { - XaeroPlus.LOGGER.debug("Thread interrupted while adding new queued stale highlight: {}, {}", x, z, e); + XaeroPlus.LOGGER.debug("Thread interrupted while adding new queued highlight: {}, {}", x, z, e); } catch (final Exception e) { - XaeroPlus.LOGGER.error("Failed to add new stale highlight: {}, {}", x, z, e); + XaeroPlus.LOGGER.error("Failed to add new highlight: {}, {}", x, z, e); } } @Override public void loadPreviousState(final Long2LongMap state) { - super.loadPreviousState(state); + if (state == null) return; try { if (staleChunksLock.writeLock().tryLock(1, TimeUnit.SECONDS)) { staleChunks.clear(); - if (lock.readLock().tryLock(1, TimeUnit.SECONDS)) { - for (var it = Long2LongMaps.fastIterator(chunks); it.hasNext(); ) { - staleChunks.add(it.next().getLongKey()); - } - lock.readLock().unlock(); + staleChunks.addAll(state.keySet()); + if (lock.writeLock().tryLock(1, TimeUnit.SECONDS)) { + chunks.putAll(state); + lock.writeLock().unlock(); } - staleChunksLock.writeLock().unlock(); } + } catch (final Exception e) { - XaeroPlus.LOGGER.error("Failed to load previous state for {} stale chunks: {}", database.databaseName, dimension.location(), e); + XaeroPlus.LOGGER.error("Error loading previous highlight cache state", e); } } @Override public boolean removeHighlight(final int x, final int z) { - boolean b = super.removeHighlight(x, z); - if (b) { - executorService.execute(() -> { - try { - database.removeHighlight(x, z, dimension); - } catch (final Exception e) { - XaeroPlus.LOGGER.error("Failed to remove highlight from {} disk cache dimension: {}", database.databaseName, dimension.location(), e); - } - }); + executorService.execute(() -> removeHighlight0(x, z)); + return true; + } + + private void removeHighlight0(final int x, final int z) { + final long chunkPos = chunkPosToLong(x, z); + try { + if (lock.writeLock().tryLock(1, TimeUnit.SECONDS)) { + chunks.remove(chunkPos); + lock.writeLock().unlock(); + } else { + XaeroPlus.LOGGER.error("Failed to remove queued highlight: timed out: {}, {}", x, z); + } + try { + database.removeHighlight(x, z, dimension); + } catch (final Exception e) { + XaeroPlus.LOGGER.error("Failed to remove highlight from {} disk cache dimension: {}", database.databaseName, dimension.location(), e); + } + } catch (final Exception e) { + XaeroPlus.LOGGER.error("Failed to remove queued highlight: {}, {}", x, z, e); } - return b; } @Override