|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +// Some portions generated by Codex |
| 12 | +package org.eclipse.rdf4j.sail.lmdb; |
| 13 | + |
| 14 | +import static org.eclipse.rdf4j.sail.lmdb.LmdbUtil.E; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | +import static org.lwjgl.system.MemoryStack.stackPush; |
| 18 | +import static org.lwjgl.system.MemoryUtil.NULL; |
| 19 | +import static org.lwjgl.util.lmdb.LMDB.MDB_NOMETASYNC; |
| 20 | +import static org.lwjgl.util.lmdb.LMDB.MDB_NOSYNC; |
| 21 | +import static org.lwjgl.util.lmdb.LMDB.MDB_NOTLS; |
| 22 | +import static org.lwjgl.util.lmdb.LMDB.MDB_RDONLY; |
| 23 | +import static org.lwjgl.util.lmdb.LMDB.mdb_env_close; |
| 24 | +import static org.lwjgl.util.lmdb.LMDB.mdb_env_create; |
| 25 | +import static org.lwjgl.util.lmdb.LMDB.mdb_env_open; |
| 26 | +import static org.lwjgl.util.lmdb.LMDB.mdb_env_set_maxreaders; |
| 27 | +import static org.lwjgl.util.lmdb.LMDB.mdb_txn_abort; |
| 28 | +import static org.lwjgl.util.lmdb.LMDB.mdb_txn_begin; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.file.Path; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.junit.jupiter.api.io.TempDir; |
| 35 | +import org.lwjgl.PointerBuffer; |
| 36 | +import org.lwjgl.system.MemoryStack; |
| 37 | + |
| 38 | +public class TxnManagerTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void readersFullRetryDoesNotAbortTrackedInactiveTxn(@TempDir Path dataDir) throws Exception { |
| 42 | + long env = openEnv(dataDir, 2); |
| 43 | + long rawTxn1 = 0; |
| 44 | + TxnManager.Txn trackedTxn = null; |
| 45 | + TxnManager.Txn extraTxn = null; |
| 46 | + |
| 47 | + try { |
| 48 | + TxnManager txnManager = new TxnManager(env, TxnManager.Mode.RESET); |
| 49 | + trackedTxn = txnManager.createReadTxn(); |
| 50 | + long trackedHandle = trackedTxn.get(); |
| 51 | + |
| 52 | + txnManager.deactivate(); |
| 53 | + rawTxn1 = beginReadTxn(env); |
| 54 | + |
| 55 | + try { |
| 56 | + extraTxn = txnManager.createReadTxn(); |
| 57 | + } catch (IOException exception) { |
| 58 | + assertTrue(exception.getMessage().contains("MDB_READERS_FULL"), exception.getMessage()); |
| 59 | + } |
| 60 | + assertEquals(trackedHandle, trackedTxn.get(), |
| 61 | + "Reader recovery must not abort an inactive transaction that is still owned by a live caller"); |
| 62 | + } finally { |
| 63 | + if (extraTxn != null) { |
| 64 | + extraTxn.close(); |
| 65 | + } |
| 66 | + if (trackedTxn != null) { |
| 67 | + trackedTxn.close(); |
| 68 | + } |
| 69 | + if (rawTxn1 != 0) { |
| 70 | + mdb_txn_abort(rawTxn1); |
| 71 | + } |
| 72 | + mdb_env_close(env); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void resetDoesNotAbortTrackedInactiveTxn(@TempDir Path dataDir) throws Exception { |
| 78 | + long env = openEnv(dataDir, 2); |
| 79 | + TxnManager.Txn trackedTxn = null; |
| 80 | + |
| 81 | + try { |
| 82 | + TxnManager txnManager = new TxnManager(env, TxnManager.Mode.RESET); |
| 83 | + trackedTxn = txnManager.createReadTxn(); |
| 84 | + long trackedHandle = trackedTxn.get(); |
| 85 | + |
| 86 | + txnManager.deactivate(); |
| 87 | + txnManager.reset(); |
| 88 | + |
| 89 | + assertEquals(trackedHandle, trackedTxn.get(), |
| 90 | + "Reset must not abort an inactive transaction that is still owned by a live caller"); |
| 91 | + } finally { |
| 92 | + if (trackedTxn != null) { |
| 93 | + trackedTxn.close(); |
| 94 | + } |
| 95 | + mdb_env_close(env); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static long openEnv(Path dataDir, int maxReaders) throws IOException { |
| 100 | + try (MemoryStack stack = stackPush()) { |
| 101 | + PointerBuffer pp = stack.mallocPointer(1); |
| 102 | + E(mdb_env_create(pp)); |
| 103 | + long env = pp.get(0); |
| 104 | + E(mdb_env_set_maxreaders(env, maxReaders)); |
| 105 | + E(mdb_env_open(env, dataDir.toAbsolutePath().toString(), MDB_NOTLS | MDB_NOSYNC | MDB_NOMETASYNC, 0664)); |
| 106 | + return env; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static long beginReadTxn(long env) throws IOException { |
| 111 | + try (MemoryStack stack = stackPush()) { |
| 112 | + PointerBuffer pp = stack.mallocPointer(1); |
| 113 | + E(mdb_txn_begin(env, NULL, MDB_RDONLY, pp)); |
| 114 | + return pp.get(0); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments