Skip to content

Commit e5faa2c

Browse files
committed
GH-5059 improve error handling and logging
1 parent 935f515 commit e5faa2c

3 files changed

Lines changed: 50 additions & 8 deletions

File tree

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbSailStore.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,12 +572,14 @@ public void approve(Resource subj, IRI pred, Value obj, Resource ctx) throws Sai
572572

573573
@Override
574574
public void approveAll(Set<Statement> approved, Set<Resource> approvedContexts) {
575+
Statement last = null;
575576

576577
sinkStoreAccessLock.lock();
577578
try {
578579
startTransaction(true);
579580

580581
for (Statement statement : approved) {
582+
last = statement;
581583
Resource subj = statement.getSubject();
582584
IRI pred = statement.getPredicate();
583585
Value obj = statement.getObject();
@@ -604,13 +606,20 @@ public void approveAll(Set<Statement> approved, Set<Resource> approvedContexts)
604606
}
605607

606608
}
607-
} catch (IOException e) {
609+
} catch (IOException | RuntimeException e) {
608610
rollback();
611+
if (multiThreadingActive) {
612+
logger.error("Encountered an unexpected problem while trying to add a statement.", e);
613+
} else {
614+
logger.error(
615+
"Encountered an unexpected problem while trying to add a statement. Last statement that was attempted to be added: [ {} ]",
616+
last, e);
617+
}
618+
619+
if (e instanceof RuntimeException) {
620+
throw (RuntimeException) e;
621+
}
609622
throw new SailException(e);
610-
} catch (RuntimeException e) {
611-
rollback();
612-
logger.error("Encountered an unexpected problem while trying to add a statement", e);
613-
throw e;
614623
} finally {
615624
sinkStoreAccessLock.unlock();
616625
}

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/LmdbUtil.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static org.lwjgl.system.MemoryStack.stackPush;
1818
import static org.lwjgl.system.MemoryUtil.NULL;
19+
import static org.lwjgl.util.lmdb.LMDB.MDB_DBS_FULL;
1920
import static org.lwjgl.util.lmdb.LMDB.MDB_KEYEXIST;
2021
import static org.lwjgl.util.lmdb.LMDB.MDB_NOTFOUND;
2122
import static org.lwjgl.util.lmdb.LMDB.MDB_RDONLY;
@@ -39,12 +40,16 @@
3940
import org.lwjgl.system.Pointer;
4041
import org.lwjgl.util.lmdb.MDBCmpFuncI;
4142
import org.lwjgl.util.lmdb.MDBVal;
43+
import org.slf4j.Logger;
44+
import org.slf4j.LoggerFactory;
4245

4346
/**
4447
* Utility class for working with LMDB.
4548
*/
4649
final class LmdbUtil {
4750

51+
private static final Logger logger = LoggerFactory.getLogger(LmdbUtil.class);
52+
4853
/**
4954
* Minimum free space in an LMDB db before automatically resizing the map.
5055
*/
@@ -61,7 +66,9 @@ private LmdbUtil() {
6166

6267
static int E(int rc) throws IOException {
6368
if (rc != MDB_SUCCESS && rc != MDB_NOTFOUND && rc != MDB_KEYEXIST) {
64-
throw new IOException(mdb_strerror(rc));
69+
IOException ioException = new IOException(mdb_strerror(rc));
70+
logger.info("Possible LMDB error: {}", mdb_strerror(rc), ioException);
71+
throw ioException;
6572
}
6673
return rc;
6774
}

core/sail/lmdb/src/main/java/org/eclipse/rdf4j/sail/lmdb/PersistentSet.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import static org.eclipse.rdf4j.sail.lmdb.LmdbUtil.E;
1414
import static org.lwjgl.system.MemoryUtil.NULL;
15+
import static org.lwjgl.util.lmdb.LMDB.MDB_MAP_FULL;
1516
import static org.lwjgl.util.lmdb.LMDB.MDB_NEXT;
1617
import static org.lwjgl.util.lmdb.LMDB.MDB_NOOVERWRITE;
1718
import static org.lwjgl.util.lmdb.LMDB.MDB_SET;
@@ -24,6 +25,7 @@
2425
import static org.lwjgl.util.lmdb.LMDB.mdb_del;
2526
import static org.lwjgl.util.lmdb.LMDB.mdb_drop;
2627
import static org.lwjgl.util.lmdb.LMDB.mdb_put;
28+
import static org.lwjgl.util.lmdb.LMDB.mdb_strerror;
2729
import static org.lwjgl.util.lmdb.LMDB.mdb_txn_abort;
2830
import static org.lwjgl.util.lmdb.LMDB.mdb_txn_begin;
2931

@@ -43,12 +45,16 @@
4345
import org.lwjgl.PointerBuffer;
4446
import org.lwjgl.system.MemoryStack;
4547
import org.lwjgl.util.lmdb.MDBVal;
48+
import org.slf4j.Logger;
49+
import org.slf4j.LoggerFactory;
4650

4751
/**
4852
* A LMDB-based persistent set.
4953
*/
5054
class PersistentSet<T extends Serializable> extends AbstractSet<T> {
5155

56+
private static final Logger logger = LoggerFactory.getLogger(PersistentSet.class);
57+
5258
private PersistentSetFactory<T> factory;
5359
private final int dbi;
5460
private int size;
@@ -126,15 +132,35 @@ private synchronized boolean update(Object element, boolean add) throws IOExcept
126132
keyVal.mv_data(keyBuf);
127133

128134
if (add) {
129-
if (mdb_put(factory.writeTxn, dbi, keyVal, dataVal, MDB_NOOVERWRITE) == MDB_SUCCESS) {
135+
int rc = mdb_put(factory.writeTxn, dbi, keyVal, dataVal, MDB_NOOVERWRITE);
136+
if (rc == MDB_SUCCESS) {
130137
size++;
131138
return true;
139+
} else if (rc == MDB_MAP_FULL) {
140+
factory.ensureResize();
141+
if (mdb_put(factory.writeTxn, dbi, keyVal, dataVal, MDB_NOOVERWRITE) == MDB_SUCCESS) {
142+
size++;
143+
return true;
144+
}
145+
return false;
146+
} else {
147+
logger.debug("Failed to add element due to error {}: {}", mdb_strerror(rc), element);
132148
}
133149
} else {
134150
// delete element
135-
if (mdb_del(factory.writeTxn, dbi, keyVal, dataVal) == MDB_SUCCESS) {
151+
int rc = mdb_del(factory.writeTxn, dbi, keyVal, dataVal);
152+
if (rc == MDB_SUCCESS) {
136153
size--;
137154
return true;
155+
} else if (rc == MDB_MAP_FULL) {
156+
factory.ensureResize();
157+
if (mdb_del(factory.writeTxn, dbi, keyVal, dataVal) == MDB_SUCCESS) {
158+
size--;
159+
return true;
160+
}
161+
return false;
162+
} else {
163+
logger.debug("Failed to remove element due to error {}: {}", mdb_strerror(rc), element);
138164
}
139165
}
140166
return false;

0 commit comments

Comments
 (0)