From 3a325845c7874a14359dd3f42e546936dfb32fbe Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Mon, 22 Jan 2024 12:58:33 +0100 Subject: [PATCH] (mq) Add better error handling in fsm and mq java.lang.Error:s were not handled properly, leading to mismatch in the bookkeeping of the FSMs. These are now caught, acted on, and re-thrown. MqSynchronousInbox also no longer assumes all exceptions are InterruptedException. --- .../marginalia/actor/ActorStateMachine.java | 5 ++ .../mq/inbox/MqSynchronousInbox.java | 5 +- third-party/uppend/build.gradle | 17 ----- third-party/uppend/readme.md | 6 -- .../com/upserve/uppend/blobs/NativeIO.java | 72 ------------------- 5 files changed, 9 insertions(+), 96 deletions(-) delete mode 100644 third-party/uppend/build.gradle delete mode 100644 third-party/uppend/readme.md delete mode 100644 third-party/uppend/src/main/java/com/upserve/uppend/blobs/NativeIO.java diff --git a/code/libraries/message-queue/src/main/java/nu/marginalia/actor/ActorStateMachine.java b/code/libraries/message-queue/src/main/java/nu/marginalia/actor/ActorStateMachine.java index 8bb2a068..27215f50 100644 --- a/code/libraries/message-queue/src/main/java/nu/marginalia/actor/ActorStateMachine.java +++ b/code/libraries/message-queue/src/main/java/nu/marginalia/actor/ActorStateMachine.java @@ -288,6 +288,11 @@ public class ActorStateMachine { logger.error("Error in state machine transition", e); setErrorState(); } + catch (Error e) { + setErrorState(); + logger.error("Error in state machine transition", e); + throw e; + } } private void setErrorState() { diff --git a/code/libraries/message-queue/src/main/java/nu/marginalia/mq/inbox/MqSynchronousInbox.java b/code/libraries/message-queue/src/main/java/nu/marginalia/mq/inbox/MqSynchronousInbox.java index 99a81c1e..4074b642 100644 --- a/code/libraries/message-queue/src/main/java/nu/marginalia/mq/inbox/MqSynchronousInbox.java +++ b/code/libraries/message-queue/src/main/java/nu/marginalia/mq/inbox/MqSynchronousInbox.java @@ -133,9 +133,12 @@ public class MqSynchronousInbox implements MqInboxIf { currentTask = executorService.submit(() -> handleMessage(msg)); currentTask.get(); } - catch (Exception ex) { + catch (InterruptedException ex) { logger.error("Inbox task was aborted"); } + catch (Exception ex) { + logger.error("Exception handling inbox task", ex); + } finally { currentTask = null; } diff --git a/third-party/uppend/build.gradle b/third-party/uppend/build.gradle deleted file mode 100644 index 7d069b18..00000000 --- a/third-party/uppend/build.gradle +++ /dev/null @@ -1,17 +0,0 @@ -plugins { - id 'java' -} - -java { - toolchain { - languageVersion.set(JavaLanguageVersion.of(21)) - } -} - -dependencies { - implementation libs.ffi -} - -test { - useJUnitPlatform() -} diff --git a/third-party/uppend/readme.md b/third-party/uppend/readme.md deleted file mode 100644 index 92eb20f7..00000000 --- a/third-party/uppend/readme.md +++ /dev/null @@ -1,6 +0,0 @@ -# Uppend - -[Uppend](https://github.com/upserve/uppend) - MIT - -It's "an append-only, key-multivalue store". Cool project, but we're unceremoniously pillaging just a small piece of -code they did for calling [memadvise()](https://man7.org/linux/man-pages/man2/madvise.2.html) on off-heap byte buffers. diff --git a/third-party/uppend/src/main/java/com/upserve/uppend/blobs/NativeIO.java b/third-party/uppend/src/main/java/com/upserve/uppend/blobs/NativeIO.java deleted file mode 100644 index abe47a73..00000000 --- a/third-party/uppend/src/main/java/com/upserve/uppend/blobs/NativeIO.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.upserve.uppend.blobs; - - -import com.kenai.jffi.MemoryIO; -import jnr.ffi.LibraryLoader; -import jnr.ffi.types.size_t; - -import java.io.IOException; -import java.nio.MappedByteBuffer; - -// https://github.com/upserve/uppend/blob/70967c6f24d7f1a3bbc18799f485d981da93f53b/src/main/java/com/upserve/uppend/blobs/NativeIO.java -// MIT License - -public class NativeIO { - - private static final NativeC nativeC = LibraryLoader.create(NativeC.class).load("c"); - public static final int pageSize = nativeC.getpagesize(); // 4096 on most Linux - - public enum Advice { - // These seem to be fairly stable https://github.com/torvalds/linux - // TODO add to https://github.com/jnr/jnr-constants - Normal(0), Random(1), Sequential(2), WillNeed(3), DontNeed(4); - private final int value; - Advice(int val) { - this.value = val; - } - } - - public interface NativeC { - int madvise(@size_t long address, @size_t long size, int advice); - int getpagesize(); - } - - static long alignedAddress(long address) { - return address & (- pageSize); - } - - static long alignedSize(long address, int capacity) { - long end = address + capacity; - end = (end + pageSize - 1) & (-pageSize); - return end - alignedAddress(address); - } - - public static void madvise(MappedByteBuffer buffer, Advice advice) throws IOException { - - final long address = MemoryIO.getInstance().getDirectBufferAddress(buffer); - final int capacity = buffer.capacity(); - - long alignedAddress = alignedAddress(address); - long alignedSize = alignedSize(alignedAddress, capacity); - - int val = nativeC.madvise(alignedAddress, alignedSize, advice.value); - - if (val != 0) { - throw new IOException(String.format("System call madvise failed with code: %d", val)); - } - } - - public static void madviseRange(MappedByteBuffer buffer, Advice advice, long offset, int length) throws IOException { - - final long address = MemoryIO.getInstance().getDirectBufferAddress(buffer); - - long alignedAddress = alignedAddress(address+offset); - long alignedSize = alignedSize(alignedAddress, length); - - int val = nativeC.madvise(alignedAddress, alignedSize, advice.value); - - if (val != 0) { - throw new IOException(String.format("System call madvise failed with code: %d", val)); - } - } -} \ No newline at end of file